Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: merge from dev #477

Merged
merged 11 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .viperlightignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ source/portal/src/utils/const.ts
source/lambda/online/lambda_main/test/main_local_test_retail.py
source/lambda/online/lambda_main/test/main_local_test_common.py
source/lambda/online/functions/retail_tools/lambda_product_information_search/product_information_search.py
source/lambda/online/common_logic/common_utils/prompt_utils.py
source/lambda/job/test/prepare_data.py
README.md
README_zh-cn.md
Expand Down
2 changes: 2 additions & 0 deletions source/lambda/online/common_logic/common_utils/constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ class LLMModelType(ConstantBase):
LLAMA3_2_90B_INSTRUCT = "us.meta.llama3-2-90b-instruct-v1:0"
MISTRAL_LARGE_2407 = "mistral.mistral-large-2407-v1:0"
COHERE_COMMAND_R_PLUS = "cohere.command-r-plus-v1:0"
# NOVA_PRO = "amazon.nova-pro-v1:0"
NOVA_PRO = "us.amazon.nova-pro-v1:0"


class EmbeddingModelType(ConstantBase):
Expand Down
57 changes: 45 additions & 12 deletions source/lambda/online/common_logic/common_utils/prompt_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
LLMModelType.CLAUDE_3_5_SONNET_V2,
LLMModelType.LLAMA3_1_70B_INSTRUCT,
LLMModelType.MISTRAL_LARGE_2407,
LLMModelType.COHERE_COMMAND_R_PLUS
LLMModelType.COHERE_COMMAND_R_PLUS,
LLMModelType.NOVA_PRO,
]

EXPORT_SCENES = [
Expand Down Expand Up @@ -126,18 +127,45 @@ def prompt_template_render(self, prompt_template: dict):

#### rag template #######

CLAUDE_RAG_SYSTEM_PROMPT = """You are a customer service agent, and answering user's query. You ALWAYS follow these response rules when writing your response:
CLAUDE_RAG_SYSTEM_PROMPT = """
You are a customer service agent responding to user queries. ALWAYS adhere to these response rules:

<response_rules>
- 如果<docs> </docs>里面的内容包含markdown格式的图片,如 ![image](https://www.demo.com/demo.png),请保留这个markdown格式的图片,并将他原封不动的输出到回答内容的最后,注意:不要修改这个markdown格式的图片.
- NERVER say "根据搜索结果/大家好/谢谢/根据这个文档...".
- 回答简单明了
- 如果问题与<docs> </docs>里面的内容不相关,直接回答 "根据内部知识库,找不到相关内容。"
1. Image Handling:
- If <docs></docs> contains markdown-formatted images, append them unaltered to the end of your response.
- Only process markdown-formatted images within <docs></docs>.
- IMPORTANT: If no markdown-formatted images are present in <docs></docs>, do not add any image references to your response.

2. Language and Tone:
- Never use phrases like "According to search results," "Hello everyone," "Thank you," or "According to this document..."
- Provide concise and clear answers.
- Maintain a professional and helpful tone throughout the response.

3. Relevance:
- If the query is unrelated to the content in <docs></docs>, respond with: "根据内部知识库,找不到相关内容。"

4. Language Adaptation:
- Respond in the same language as the user's query.
- If the query is in a language other than English, adapt your response accordingly.

5. Confidentiality:
- Do not disclose any information not present in the provided documents.
- If asked about topics outside your knowledge base, politely state that you don't have that information.

6. Formatting:
- Use appropriate formatting (bold, italics, bullet points) to enhance readability when necessary.

7. Completeness:
- Ensure your response addresses all aspects of the user's query.
- If multiple relevant documents are provided, synthesize the information coherently.

</response_rules>

Here are some documents for you to reference for your query.
Reference the following documents to answer the query:
<docs>
{context}
</docs>"""
</docs>
"""

register_prompt_templates(
model_ids=[
Expand All @@ -154,6 +182,7 @@ def prompt_template_render(self, prompt_template: dict):
LLMModelType.LLAMA3_2_90B_INSTRUCT,
LLMModelType.MISTRAL_LARGE_2407,
LLMModelType.COHERE_COMMAND_R_PLUS,
LLMModelType.NOVA_PRO,
],
task_type=LLMTaskType.RAG,
prompt_template=CLAUDE_RAG_SYSTEM_PROMPT,
Expand Down Expand Up @@ -274,7 +303,7 @@ def prompt_template_render(self, prompt_template: dict):
LLMModelType.LLAMA3_2_90B_INSTRUCT,
LLMModelType.MISTRAL_LARGE_2407,
LLMModelType.COHERE_COMMAND_R_PLUS,

LLMModelType.NOVA_PRO,
],
task_type=LLMTaskType.CONVERSATION_SUMMARY_TYPE,
prompt_template=CQR_SYSTEM_PROMPT,
Expand All @@ -299,6 +328,7 @@ def prompt_template_render(self, prompt_template: dict):
LLMModelType.LLAMA3_2_90B_INSTRUCT,
LLMModelType.MISTRAL_LARGE_2407,
LLMModelType.COHERE_COMMAND_R_PLUS,
LLMModelType.NOVA_PRO,
],
task_type=LLMTaskType.CONVERSATION_SUMMARY_TYPE,
prompt_template=CQR_USER_PROMPT_TEMPLATE,
Expand All @@ -324,6 +354,7 @@ def prompt_template_render(self, prompt_template: dict):
LLMModelType.LLAMA3_2_90B_INSTRUCT,
LLMModelType.MISTRAL_LARGE_2407,
LLMModelType.COHERE_COMMAND_R_PLUS,
LLMModelType.NOVA_PRO,
],
task_type=LLMTaskType.CONVERSATION_SUMMARY_TYPE,
prompt_template=json.dumps(CQR_FEW_SHOTS, ensure_ascii=False, indent=2),
Expand Down Expand Up @@ -382,15 +413,15 @@ def prompt_template_render(self, prompt_template: dict):
AGENT_SYSTEM_PROMPT = """\
You are a helpful and honest AI assistant. Today is {date},{weekday}.
Here are some guidelines for you:
<guidlines>
<guidelines>
- Here are steps for you to decide to use which tool:
1. Determine whether the current context is sufficient to answer the user's question.
2. If the current context is sufficient to answer the user's question, call the `give_final_response` tool.
3. If the current context is not sufficient to answer the user's question, you can consider calling one of the provided tools.
4. If any of required parameters of the tool you want to call do not appears in context, call the `give_rhetorical_question` tool to ask the user for more information.
- Always output with the same language as the content from user. If the content is English, use English to output. If the content is Chinese, use Chinese to output.
- Always call one tool at a time.
</guidlines>
</guidelines>
Here's some context for reference:
<context>
{context}
Expand Down Expand Up @@ -441,6 +472,7 @@ def prompt_template_render(self, prompt_template: dict):
LLMModelType.CLAUDE_3_5_SONNET,
LLMModelType.CLAUDE_3_5_SONNET_V2,
LLMModelType.CLAUDE_3_5_HAIKU,
LLMModelType.NOVA_PRO,
# LLMModelType.LLAMA3_1_70B_INSTRUCT,
# LLMModelType.LLAMA3_2_90B_INSTRUCT,
# LLMModelType.MISTRAL_LARGE_2407,
Expand All @@ -458,7 +490,7 @@ def prompt_template_render(self, prompt_template: dict):
Here's some context for reference:
{context}

## Guidlines
## Guidelines
Here are some guidelines for you:
- Here are strategies for you to decide to use which tool:
1. Determine whether the current context is sufficient to answer the user's question.
Expand Down Expand Up @@ -498,6 +530,7 @@ def prompt_template_render(self, prompt_template: dict):
LLMModelType.LLAMA3_2_90B_INSTRUCT,
LLMModelType.MISTRAL_LARGE_2407,
LLMModelType.COHERE_COMMAND_R_PLUS,
LLMModelType.NOVA_PRO,
],
task_type=LLMTaskType.TOOL_CALLING_API,
prompt_template=TOOL_FEWSHOT_PROMPT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ def stream_response(event_body: dict, response: dict):
ws_connection_id = event_body["ws_connection_id"]
custom_message_id = event_body["custom_message_id"]
answer = response["answer"]
figure = response.get("ddb_additional_kwargs", {}).get("figure")
if isinstance(answer, str):
answer = iter([answer])

Expand Down Expand Up @@ -133,10 +132,13 @@ def stream_response(event_body: dict, response: dict):
"message_type": StreamMessageType.CONTEXT,
"message_id": f"ai_{message_id}",
"custom_message_id": custom_message_id,
"ddb_additional_kwargs": {},
**response["extra_response"]
}
if figure and len(figure) > 1:
context_msg["figure"] = figure

figure = response.get("extra_response").get("ref_figures", [])
if figure and figure[0]:
context_msg["ddb_additional_kwargs"]["figure"] = figure[0][:2]
send_to_ws_client(
message=context_msg,
ws_connection_id=ws_connection_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def _import_chat_chain():
Baichuan2Chat13B4BitsChatChain,
Claude3HaikuChatChain,
Claude3SonnetChatChain,
NovaProChatChain
)

def _import_conversation_summary_chain():
Expand All @@ -48,7 +49,8 @@ def _import_conversation_summary_chain():
Claude21ConversationSummaryChain,
Claude3HaikuConversationSummaryChain,
Claude3SonnetConversationSummaryChain,
Internlm2Chat20BConversationSummaryChain
Internlm2Chat20BConversationSummaryChain,
NovaProConversationSummaryChain
)

def _import_intention_chain():
Expand All @@ -71,7 +73,8 @@ def _import_rag_chain():
ClaudeInstanceRAGLLMChain,
Claude3HaikuRAGLLMChain,
Claude3SonnetRAGLLMChain,
Baichuan2Chat13B4BitsKnowledgeQaChain
Baichuan2Chat13B4BitsKnowledgeQaChain,
NovaProRAGLLMChain
)


Expand Down Expand Up @@ -117,7 +120,8 @@ def _import_hyde_chain():
Claude3SonnetHydeChain,
ClaudeInstanceHydeChain,
Internlm2Chat20BHydeChain,
Internlm2Chat7BHydeChain
Internlm2Chat7BHydeChain,
NovaProHydeChain
)

def _import_query_rewrite_chain():
Expand All @@ -128,7 +132,8 @@ def _import_query_rewrite_chain():
Claude3HaikuQueryRewriteChain,
Claude3SonnetQueryRewriteChain,
Internlm2Chat20BQueryRewriteChain,
Internlm2Chat7BQueryRewriteChain
Internlm2Chat7BQueryRewriteChain,
NovaProQueryRewriteChain
)


Expand All @@ -138,7 +143,8 @@ def _import_tool_calling_chain_claude_xml():
Claude3HaikuToolCallingChain,
Claude2ToolCallingChain,
Claude3SonnetToolCallingChain,
ClaudeInstanceToolCallingChain
ClaudeInstanceToolCallingChain,
NovaProToolCallingChain
)

def _import_retail_conversation_summary_chain():
Expand Down Expand Up @@ -169,7 +175,8 @@ def _import_tool_calling_chain_api():
Claude3SonnetToolCallingChain,
Llama31Instruct70BToolCallingChain,
CohereCommandRPlusToolCallingChain,
MistraLlarge2407ToolCallingChain
MistraLlarge2407ToolCallingChain,
NovaProToolCallingChain,
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,7 @@ class ChatGPT4ChatChain(ChatGPT35ChatChain):

class ChatGPT4oChatChain(ChatGPT35ChatChain):
model_id = LLMModelType.CHATGPT_4O


class NovaProChatChain(Claude2ChatChain):
model_id = LLMModelType.NOVA_PRO
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,7 @@ class Qwen2Instruct7BConversationSummaryChain(Claude2ConversationSummaryChain):

class GLM4Chat9BConversationSummaryChain(Claude2ConversationSummaryChain):
model_id = LLMModelType.GLM_4_9B_CHAT


class NovaProConversationSummaryChain(Claude2ConversationSummaryChain):
model_id = LLMModelType.NOVA_PRO
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,7 @@ def create_prompt(cls, x):
class Internlm2Chat20BHydeChain(Internlm2Chat7BHydeChain):
model_id = LLMModelType.INTERNLM2_CHAT_20B
intent_type = HYDE_TYPE


class NovaProHydeChain(Claude2HydeChain):
model_id = LLMModelType.NOVA_PRO
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class Claude3SonnetQueryRewriteChain(Claude2QueryRewriteChain):


class Claude35SonnetQueryRewriteChain(Claude2QueryRewriteChain):
mdoel_id = LLMModelType.CLAUDE_3_5_SONNET
model_id = LLMModelType.CLAUDE_3_5_SONNET


class Claude35SonnetV2QueryRewriteChain(Claude2QueryRewriteChain):
Expand Down Expand Up @@ -151,3 +151,8 @@ def create_chain(cls, model_kwargs=None, **kwargs):
class Internlm2Chat20BQueryRewriteChain(Internlm2Chat7BQueryRewriteChain):
model_id = LLMModelType.INTERNLM2_CHAT_20B
intent_type = QUERY_REWRITE_TYPE


class NovaProQueryRewriteChain(Claude2QueryRewriteChain):
model_id = LLMModelType.NOVA_PRO

Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,8 @@ def add_system_prompt(x):
)
llm_chain = chat_history_chain | llm_chain
return llm_chain


class NovaProRAGLLMChain(Claude2RagLLMChain):
model_id = LLMModelType.NOVA_PRO

Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,7 @@ class MistraLlarge2407ToolCallingChain(Claude2ToolCallingChain):

class CohereCommandRPlusToolCallingChain(Claude2ToolCallingChain):
model_id = LLMModelType.COHERE_COMMAND_R_PLUS


class NovaProToolCallingChain(Claude2ToolCallingChain):
model_id = LLMModelType.NOVA_PRO
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,8 @@ class Claude3HaikuToolCallingChain(Claude2ToolCallingChain):

class Claude35SonnetToolCallingChain(Claude2ToolCallingChain):
model_id = "anthropic.claude-3-5-sonnet-20240620-v1:0"


class NovaProToolCallingChain(Claude2ToolCallingChain):
model_id = LLMModelType.NOVA_PRO

Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ def _import_bedrock_models():
Claude35SonnetV2,
MistralLarge2407,
Llama3d1Instruct70B,
CohereCommandRPlus
CohereCommandRPlus,
NovaPro
)

def _import_openai_models():
Expand Down Expand Up @@ -95,6 +96,7 @@ def _load_module(model_id):
LLMModelType.COHERE_COMMAND_R_PLUS:_import_bedrock_models,
LLMModelType.CLAUDE_3_5_SONNET_V2:_import_bedrock_models,
LLMModelType.CLAUDE_3_5_HAIKU:_import_bedrock_models,
LLMModelType.NOVA_PRO:_import_bedrock_models,
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ def create_model(cls, model_kwargs=None, **kwargs):
or None
)
br_aws_access_key_id = os.environ.get("BEDROCK_AWS_ACCESS_KEY_ID", "")
br_aws_secret_access_key = os.environ.get("BEDROCK_AWS_SECRET_ACCESS_KEY", "")
br_aws_secret_access_key = os.environ.get(
"BEDROCK_AWS_SECRET_ACCESS_KEY", "")

if br_aws_access_key_id != "" and br_aws_secret_access_key != "":
logger.info(f"Bedrock Using AWS AKSK from environment variables. Key ID: {br_aws_access_key_id}")
logger.info(
f"Bedrock Using AWS AKSK from environment variables. Key ID: {br_aws_access_key_id}")

client = boto3.client("bedrock-runtime", region_name=region_name,
client = boto3.client("bedrock-runtime", region_name=region_name,
aws_access_key_id=br_aws_access_key_id, aws_secret_access_key=br_aws_secret_access_key)

llm = ChatBedrockConverse(
Expand Down Expand Up @@ -119,3 +121,9 @@ class CohereCommandRPlus(Claude2):
model_id = LLMModelType.COHERE_COMMAND_R_PLUS
enable_auto_tool_choice = False
enable_prefill = False


class NovaPro(Claude2):
model_id = LLMModelType.NOVA_PRO
enable_auto_tool_choice = False
enable_prefill = False
Loading
Loading