Skip to content

Commit 0766116

Browse files
committed
Updates:
- Improve tools detection - Focus on chat history
1 parent b3177fe commit 0766116

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

app/libs/tools_handler.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,9 @@ class ToolExtractionHandler(Handler):
4242
async def handle(self, context: Context):
4343
body = context.body
4444
if context.is_tool_call:
45-
4645
# Prepare the messages and tools for the tool extraction
4746
messages = [
48-
f"{m['role'].title()}: {m['content']}"
47+
f"<{m['role'].lower()}>\n{m['content']}\n</{m['role'].lower()}>"
4948
for m in context.messages
5049
if m["role"] != "system"
5150
]
@@ -80,11 +79,13 @@ async def handle(self, context: Context):
8079
)
8180
suffix = SUFFIX if not forced_mode else get_forced_tool_suffix(tool_choice)
8281

82+
messages_flatten = '\n'.join(messages)
83+
8384
new_messages = [
8485
{"role": "system", "content": system_message},
8586
{
8687
"role": "system",
87-
"content": f"Conversation History:\n{''.join(messages)}\n\nTools: \n{tools_json}\n\n{suffix}",
88+
"content": f"# Conversation History:\n{messages_flatten}\n\n# Available Tools: \n{tools_json}\n\n{suffix}",
8889
},
8990
]
9091

app/prompts.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
SYSTEM_MESSAGE = """A history of conversations between an AI assistant and the user, plus the last user's message, is given to you.
1+
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.
2+
3+
A history of conversations between an AI assistant and the user, plus the last user's message, is given to you.
24
35
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.
46
@@ -11,6 +13,7 @@
1113
Notes:
1214
- If you can synthesis the answer without using any tools, then return an empty list for "tool_calls".
1315
- You need tools if there is clear direction between the user's last message and the tools description.
16+
- 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.
1417
1518
You should think step by step, provide your reasoning for your response, then add the JSON response at the end following the below schema:
1619
@@ -31,9 +34,12 @@
3134
]
3235
}
3336
34-
** If no tools are required, then return an empty list for "tool_calls". **
3537
38+
IMPORTANT NOTES:
39+
** If no tools are required, then return an empty list for "tool_calls". **
3640
**Wrap the JSON response between ```json and ```, and rememebr "tool_calls" is a list.**.
41+
** 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.**
42+
** 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.
3743
3844
MESSAGE SUFFIX:
3945
- "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.
@@ -70,7 +76,7 @@
7076

7177
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."
7278

73-
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."""
79+
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."""
7480

7581
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."""
7682

0 commit comments

Comments
 (0)