Skip to content

Commit 8f74e5b

Browse files
committed
Use tool calling instead of function calling
1 parent abcfdd1 commit 8f74e5b

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

interpreter/core/llm/llm.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
from ...terminal_interface.utils.display_markdown_message import (
1616
display_markdown_message,
1717
)
18-
from .run_function_calling_llm import run_function_calling_llm
19-
20-
# from .run_tool_calling_llm import run_tool_calling_llm
2118
from .run_text_llm import run_text_llm
19+
20+
# from .run_function_calling_llm import run_function_calling_llm
21+
from .run_tool_calling_llm import run_tool_calling_llm
2222
from .utils.convert_to_openai_messages import convert_to_openai_messages
2323

2424

@@ -287,8 +287,8 @@ def run(self, messages):
287287
time.sleep(5)
288288

289289
if self.supports_functions:
290-
yield from run_function_calling_llm(self, params)
291-
# yield from run_tool_calling_llm(self, params)
290+
# yield from run_function_calling_llm(self, params)
291+
yield from run_tool_calling_llm(self, params)
292292
else:
293293
yield from run_text_llm(self, params)
294294

interpreter/core/llm/run_tool_calling_llm.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,23 @@ def run_tool_calling_llm(llm, request_params):
3636
]
3737
request_params["tools"] = [tool_schema]
3838

39+
last_tool_id = 0
40+
for message in request_params["messages"]:
41+
if "function_call" in message:
42+
function = message.pop("function_call")
43+
message["tool_calls"] = [
44+
{
45+
"id": "toolu_" + str(last_tool_id),
46+
"type": "function",
47+
"function": function,
48+
}
49+
]
50+
if message["role"] == "function":
51+
message["role"] = "tool"
52+
message["tool_call_id"] = "toolu_" + str(last_tool_id)
53+
54+
last_tool_id += 1
55+
3956
# Add OpenAI's recommended function message
4057
# request_params["messages"][0][
4158
# "content"
@@ -55,12 +72,16 @@ def run_tool_calling_llm(llm, request_params):
5572
delta = chunk["choices"][0]["delta"]
5673

5774
# Convert tool call into function call, which we have great parsing logic for below
58-
if "tool_calls" in delta:
59-
if (
60-
len(delta["tool_calls"]) > 0
61-
and "function_call" in delta["tool_calls"][0]
62-
):
63-
delta["function_call"] = delta["tool_calls"][0]["function_call"]
75+
if "tool_calls" in delta and delta["tool_calls"]:
76+
# import pdb; pdb.set_trace()
77+
if len(delta["tool_calls"]) > 0 and delta["tool_calls"][0].function:
78+
delta = {
79+
# "id": delta["tool_calls"][0],
80+
"function_call": {
81+
"name": delta["tool_calls"][0].function.name,
82+
"arguments": delta["tool_calls"][0].function.arguments,
83+
}
84+
}
6485

6586
# Accumulate deltas
6687
accumulated_deltas = merge_deltas(accumulated_deltas, delta)

0 commit comments

Comments
 (0)