Skip to content

Commit

Permalink
Updates:
Browse files Browse the repository at this point in the history
- Improve tools detection
- Focus on chat history
  • Loading branch information
unclecode committed Mar 18, 2024
1 parent b3177fe commit 0766116
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
7 changes: 4 additions & 3 deletions app/libs/tools_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ class ToolExtractionHandler(Handler):
async def handle(self, context: Context):
body = context.body
if context.is_tool_call:

# Prepare the messages and tools for the tool extraction
messages = [
f"{m['role'].title()}: {m['content']}"
f"<{m['role'].lower()}>\n{m['content']}\n</{m['role'].lower()}>"
for m in context.messages
if m["role"] != "system"
]
Expand Down Expand Up @@ -80,11 +79,13 @@ async def handle(self, context: Context):
)
suffix = SUFFIX if not forced_mode else get_forced_tool_suffix(tool_choice)

messages_flatten = '\n'.join(messages)

new_messages = [
{"role": "system", "content": system_message},
{
"role": "system",
"content": f"Conversation History:\n{''.join(messages)}\n\nTools: \n{tools_json}\n\n{suffix}",
"content": f"# Conversation History:\n{messages_flatten}\n\n# Available Tools: \n{tools_json}\n\n{suffix}",
},
]

Expand Down
12 changes: 9 additions & 3 deletions app/prompts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
SYSTEM_MESSAGE = """A history of conversations between an AI assistant and the user, plus the last user's message, is given to you.
SYSTEM_MESSAGE = """You are a functiona-call proxy for an advanced LLM. Your jobe is to identify the required tools for answering the user queries, if any. You will received the result of those tools, and then based ont hem you generate final response for user. Some of these tools are like "send_email", "run python code" and etc. Remember you are not in charge to execute these tools as you ar an AI model, you just detect them, then the middle system, executes, and returns you with response, then you use it to generate final response.
A history of conversations between an AI assistant and the user, plus the last user's message, is given to you.
In addition, you have access to a list of available tools. Each tool is a function that requires a set of parameters and, in response, returns information that the AI assistant needs to provide a proper answer.
Expand All @@ -11,6 +13,7 @@
Notes:
- If you can synthesis the answer without using any tools, then return an empty list for "tool_calls".
- You need tools if there is clear direction between the user's last message and the tools description.
- If you can't devise a value for a parameter directly from the user's message, only return null and NEVER TRY TO GUESS THE VALUE.
You should think step by step, provide your reasoning for your response, then add the JSON response at the end following the below schema:
Expand All @@ -31,9 +34,12 @@
]
}
** If no tools are required, then return an empty list for "tool_calls". **
IMPORTANT NOTES:
** If no tools are required, then return an empty list for "tool_calls". **
**Wrap the JSON response between ```json and ```, and rememebr "tool_calls" is a list.**.
** If you can not extract or conclude a value for a parameter directly from the from the user messagem, only return null and NEVER TRY TO GUESS THE VALUE.**
** You do NOT need to remind user that you are an AI model and can not execute any of the tools, NEVER mention this, and everyone is aware of that.
MESSAGE SUFFIX:
- "SYSTEM MESSGAE": Whenever a message starts with 'SYSTEM MESSAGE', that is a guide and help information for you to generate your next response. Do not consider them a message from the user, and do not reply to them at all. Just use the information and continue your conversation with the user.
Expand Down Expand Up @@ -70,7 +76,7 @@

CLEAN_UP_MESSAGE = "When I tried to extract the content between ```json and ``` and parse the content to valid JSON object, I faced with the abovr error. Remember, you are supposed to wrap the schema between ```json and ```, and do this only one time. First find out what went wrong, that I couldn't extract the JSON between ```json and ```, and also faced error when trying to parse it, then regenerate the your last message and fix the issue."

SUFFIX = """Think step by step and justify your response. Make sure to not miss in case to answer user query we need multiple tools, in that case detect all that we need, then generate a JSON response wrapped between "```json" and "```". Remember to USE THIS JSON WRAPPER ONLY ONE TIME."""
SUFFIX = """# Task:\nThink step by step and justify your response. Make sure to not miss in case to answer user query we need multiple tools, in that case detect all that we need, then generate a JSON response wrapped between "```json" and "```". Remember to USE THIS JSON WRAPPER ONLY ONE TIME. For some of arguments you may need their values to be extracted from previous messages in the conversation and not only the last message."""

FORCE_CALL_SUFFIX = """For this task, you HAVE to choose the tool (function) {tool_name}, and ignore other rools. Therefore think step by step and justify your response, then closely examine the user's last message and the history of the conversation, then extract the necessary parameter values for the given tool based on the provided JSON schema. Remember that you must use the specified tool to generate the response. Finally generate a JSON response wrapped between "```json" and "```". Remember to USE THIS JSON WRAPPER ONLY ONE TIME."""

Expand Down

0 comments on commit 0766116

Please sign in to comment.