Skip to content
Open
Changes from 1 commit
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
17 changes: 14 additions & 3 deletions sgpt/handlers/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,21 @@ def make_messages(self, prompt: str) -> List[Dict[str, str]]:
def handle_function_call(
self,
messages: List[dict[str, Any]],
id: str,
name: str,
arguments: str,
) -> Generator[str, None, None]:
messages.append(
{
"role": "assistant",
"content": "",
"function_call": {"name": name, "arguments": arguments},
"tool_calls": [
{
"id": id,
"type": "function",
"function": {"name": name, "arguments": arguments},
}
],
}
)

Expand All @@ -79,7 +86,9 @@ def handle_function_call(
result = get_function(name)(**dict_args)
if cfg.get("SHOW_FUNCTIONS_OUTPUT") == "true":
yield f"```text\n{result}\n```\n"
messages.append({"role": "function", "content": result, "name": name})
messages.append(
{"role": "tool", "content": result, "tool_call_id": id, "name": name}
)

@cache
def get_completion(
Expand Down Expand Up @@ -121,12 +130,14 @@ def get_completion(
)
if tool_calls:
for tool_call in tool_calls:
if tool_call.id:
id = tool_call.id
if tool_call.function.name:
name = tool_call.function.name
if tool_call.function.arguments:
arguments += tool_call.function.arguments
if chunk.choices[0].finish_reason == "tool_calls":
yield from self.handle_function_call(messages, name, arguments)
yield from self.handle_function_call(messages, id, name, arguments)
yield from self.get_completion(
model=model,
temperature=temperature,
Expand Down