Skip to content

Commit 4bd940a

Browse files
Updated function calling
1 parent bc67d2a commit 4bd940a

File tree

5 files changed

+30
-16
lines changed

5 files changed

+30
-16
lines changed

examples/function_calling_agent.json

+12-8
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
"llama_generation_settings": {
33
"temperature": 0.65,
44
"top_k": 40,
5-
"top_p": 0.5,
5+
"top_p": 0.95,
66
"min_p": 0.05,
77
"n_predict": -1,
88
"n_keep": 0,
99
"stream": true,
1010
"stop_sequences": [],
11-
"tfs_z": 0.975,
11+
"tfs_z": 1.0,
1212
"typical_p": 1.0,
1313
"repeat_penalty": 1.1,
1414
"repeat_last_n": 64,
@@ -22,25 +22,29 @@
2222
"seed": -1,
2323
"ignore_eos": false
2424
},
25-
"system_prompt": "You are an advanced AI assistant. You are interacting with the user and with your environment by calling functions. You call functions by writing JSON objects, which represent specific function calls.\nBelow is a list of your available function calls:\n\nFunction: send_message_to_user\n Description: Send a message to the user.\n Parameters:\n message (str):\n Description: The message send to the user.\n\nFunction: Calculator\n Description: Perform a math operation on two numbers.\n Parameters:\n number_one (any):\n Description: First number.\n operation (math-operation):\n Description: Math operation to perform.\n number_two (any):\n Description: Second number.\n\nFunction: write_to_file\n Description: Write file to the user filesystem.\n Parameters:\n chain_of_thought (str):\n Description: Your chain of thought while writing the file.\n file_path (str):\n Description: The file path includes the filename and file ending.\n file_content (str):\n Description: The actual content to write.\n\nFunction: read_file\n Description: Read file from the user filesystem.\n Parameters:\n file_path (str):\n Description: The file path includes the filename and file ending.\n\nFunction: get_current_datetime\n Description: Get the current date and time in the given format.\n Parameters:\n output_format (str or none-type):\n Description: format string for the date and time. Defaults to '%Y-%m-%d %H:%M:%S'\n\nFunction: get_current_weather\n Parameters:\n location (str)\n unit (unit or none-type)\n\n",
25+
"system_prompt": "You are an advanced AI assistant. You are interacting with the user and with your environment by calling functions. You call functions by writing JSON objects, which represent specific function calls.\nBelow is a list of your available function calls:\n\nFunction: send_message_to_user\n Description: Send a message to the user.\n Parameters:\n message (str):\n Description: The message send to the user.\n\nFunction: Calculator\n Description: Perform a math operation on two numbers.\n Parameters:\n number_one (int or float):\n Description: First number.\n operation (math-operation):\n Description: Math operation to perform.\n number_two (int or float):\n Description: Second number.\n\nFunction: get_current_datetime\n Description: Get the current date and time in the given format.\n Parameters:\n output_format (str or none-type):\n Description: formatting string for the date and time, defaults to '%Y-%m-%d %H:%M:%S'\n\nFunction: get_current_weather\n Parameters:\n location (str)\n unit (unit or none-type)\n\n",
2626
"k_last_messages_from_chat_history": 0,
2727
"debug_output": true,
2828
"messages": [
2929
{
3030
"role": "user",
31-
"content": "Solve the following calculations: 42 * 42, 24 * 24, 5 * 5"
31+
"content": "Format the answer clearly: Get the date and time, get the current weather in celsius in London and solve the following calculation: 42 * 42"
3232
},
3333
{
3434
"role": "assistant",
35-
"content": "[\n {\n \"function\": \"Calculator\",\n \"params\": {\n \"number_one\": 42,\n \"operation\": \"multiply\",\n \"number_two\": 42\n }\n },\n {\n \"function\": \"Calculator\",\n \"params\": {\n \"number_one\": 24,\n \"operation\": \"multiply\",\n \"number_two\": 24\n }\n },\n {\n \"function\": \"Calculator\",\n \"params\": {\n \"number_one\": 5,\n \"operation\": \"multiply\",\n \"number_two\": 5\n }\n }\n]"
35+
"content": "[\n {\n \"function\": \"get_current_datetime\",\n \"params\": {\n \"output_format\": \"%Y-%m-%d %H:%M:%S\"\n }\n },\n {\n \"function\": \"get_current_weather\",\n \"params\": {\n \"location\": \"London\",\n \"unit\": \"celsius\"\n }\n },\n {\n \"function\": \"Calculator\",\n \"params\": {\n \"number_one\": 42,\n \"operation\": \"multiply\",\n \"number_two\": 42\n }\n }\n]"
3636
},
3737
{
3838
"role": "function",
39-
"content": "FUNCTION_CALLING_RESULTS:\n1764\n\n576\n\n25"
39+
"content": "Results from calling functions:\n2024-01-16 10:11:46\n\n{\"location\": \"London\", \"temperature\": \"42\", \"unit\": \"celsius\"}\n\n1764"
4040
},
4141
{
4242
"role": "assistant",
43-
"content": "[\n {\n \"function\": \"send_message_to_user\",\n \"params\": {\n \"message\": \"1764\"\n }\n },\n {\n \"function\": \"send_message_to_user\",\n \"params\": {\n \"message\": \"576\"\n }\n },\n {\n \"function\": \"send_message_to_user\",\n \"params\": {\n \"message\": \"25\"\n }\n }\n]"
43+
"content": "[\n {\n \"function\": \"send_message_to_user\",\n \"params\": {\n \"message\": \"The date and time is 2024-01-16 10:11:46.\"\n }\n },\n {\n \"function\": \"send_message_to_user\",\n \"params\": {\n \"message\": \"The current weather in London is 42 degrees Celsius.\"\n }\n },\n {\n \"function\": \"send_message_to_user\",\n \"params\": {\n \"message\": \"The result of the calculation 42 * 42 is 1764.\"\n }\n }\n]"
44+
},
45+
{
46+
"role": "function",
47+
"content": "Results from calling functions:\nNone\n\nNone\n\nNone"
4448
}
4549
],
4650
"custom_messages_formatter": {
@@ -57,7 +61,7 @@
5761
],
5862
"FUNCTION_PROMPT_START": "<|im_start|>function\n",
5963
"FUNCTION_PROMPT_END": "<|im_end|>\n",
60-
"USE_USER_ROLE_FUNCTION_CALL_RESULT": true,
64+
"USE_USER_ROLE_FUNCTION_CALL_RESULT": false,
6165
"STRIP_PROMPT": true
6266
}
6367
}

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "llama-cpp-agent"
7-
version = "0.0.15"
7+
version = "0.0.16"
88
description = "A framework for building LLM based AI agents with llama-cpp-python."
99

1010
readme = "ReadMe.md"

src/llama_cpp_agent/function_calling_agent.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,21 @@ def generate_response(self, message: str):
247247
"""
248248
count = 0
249249
msg = copy(message)
250-
while msg and not ("None" in '\n'.join([str(m) for m in msg])):
250+
while msg:
251251
if count > 0:
252252

253-
msg = "FUNCTION_CALLING_RESULTS:\n" + '\n\n'.join([str(m) for m in msg])
254-
msg = self.llama_cpp_agent.get_chat_response(msg, role="function", system_prompt=self.system_prompt,
253+
msg = "Results from calling functions:\n" + '\n\n'.join([str(m) for m in msg])
254+
self.llama_cpp_agent.add_message(role="function", message=msg)
255+
lines = msg.splitlines()
256+
257+
found_none = False
258+
for line in lines:
259+
if line.startswith("None"):
260+
found_none = True
261+
break
262+
if found_none:
263+
break
264+
msg = self.llama_cpp_agent.get_chat_response(system_prompt=self.system_prompt,
255265
function_tool_registry=self.tool_registry,
256266
streaming_callback=self.streaming_callback,
257267
k_last_messages=self.k_last_messages_from_chat_history,

src/llama_cpp_agent/llm_agent.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def get_function_tool_registry(function_tool_list: List[LlamaCppFunctionTool], a
8686
function_tool_registry.finalize()
8787
return function_tool_registry
8888

89-
def add_message(self, message: str, role: Literal["system"] | Literal["user"] | Literal["assistant"] = "user"):
89+
def add_message(self, message: str, role: Literal["system"] | Literal["user"] | Literal["assistant"] | Literal["function"] = "user"):
9090
"""
9191
Adds a message to the chat history.
9292

src/llama_cpp_agent/messages_formatter.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,14 @@ def format_messages(self, messages: List[Dict[str, str]]) -> Tuple[str, str]:
170170
last_role = "assistant"
171171
elif message["role"] == "function":
172172
if isinstance(message["content"], list):
173-
message["content"] = "FUNCTION_CALLING_RESULTS:\n" + '\n\n'.join([str(m) for m in message["content"]])
173+
message["content"] = "Results from calling functions:\n" + '\n'.join([str(m) for m in message["content"]])
174174
if self.USE_USER_ROLE_FUNCTION_CALL_RESULT:
175175
formatted_messages += self.USER_PROMPT_START + message["content"] + self.USER_PROMPT_END
176176
last_role = "user"
177177
else:
178178
formatted_messages += self.FUNCTION_PROMPT_START + message["content"] + self.FUNCTION_PROMPT_END
179179
last_role = "function"
180-
if last_role == "system" or last_role == "user":
180+
if last_role == "system" or last_role == "user" or last_role == "function":
181181
if self.STRIP_PROMPT:
182182
return formatted_messages + self.ASSISTANT_PROMPT_START.strip(), "assistant"
183183
else:
@@ -240,7 +240,7 @@ def as_dict(self) -> dict:
240240
ASSISTANT_PROMPT_END_MIXTRAL, True, DEFAULT_MIXTRAL_STOP_SEQUENCES)
241241
chatml_formatter = MessagesFormatter("", SYS_PROMPT_START_CHATML, SYS_PROMPT_END_CHATML, USER_PROMPT_START_CHATML,
242242
USER_PROMPT_END_CHATML, ASSISTANT_PROMPT_START_CHATML,
243-
ASSISTANT_PROMPT_END_CHATML, False, DEFAULT_CHATML_STOP_SEQUENCES, True, FUNCTION_PROMPT_START_CHATML, FUNCTION_PROMPT_END_CHATML)
243+
ASSISTANT_PROMPT_END_CHATML, False, DEFAULT_CHATML_STOP_SEQUENCES, False, FUNCTION_PROMPT_START_CHATML, FUNCTION_PROMPT_END_CHATML)
244244
vicuna_formatter = MessagesFormatter("", SYS_PROMPT_START_VICUNA, SYS_PROMPT_END_VICUNA, USER_PROMPT_START_VICUNA,
245245
USER_PROMPT_END_VICUNA, ASSISTANT_PROMPT_START_VICUNA,
246246
ASSISTANT_PROMPT_END_VICUNA, False, DEFAULT_VICUNA_STOP_SEQUENCES)

0 commit comments

Comments
 (0)