From 26d03f26c99c15ad813458641669249dde6ba2e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleix=20Conchillo=20Flaqu=C3=A9?= Date: Sun, 18 Aug 2024 20:48:43 -0700 Subject: [PATCH] services(openai, anthropic): a None result should not run inference --- src/pipecat/services/anthropic.py | 43 ++++++++++++++++--------------- src/pipecat/services/openai.py | 39 ++++++++++++++-------------- 2 files changed, 42 insertions(+), 40 deletions(-) diff --git a/src/pipecat/services/anthropic.py b/src/pipecat/services/anthropic.py index f8c21f4e5..899b9c174 100644 --- a/src/pipecat/services/anthropic.py +++ b/src/pipecat/services/anthropic.py @@ -490,32 +490,33 @@ class AnthropicAssistantContextAggregator(LLMAssistantContextAggregator): if self._function_call_result: frame = self._function_call_result self._function_call_result = None - self._context.add_message({ - "role": "assistant", - "content": [ - { - "type": "text", - "text": aggregation - }, - { - "type": "tool_use", - "id": frame.tool_call_id, - "name": frame.function_name, - "input": frame.arguments - } - ] - }) - self._context.add_message({ - "role": "user", - "content": [ + if frame.result: + self._context.add_message({ + "role": "assistant", + "content": [ + { + "type": "text", + "text": aggregation + }, + { + "type": "tool_use", + "id": frame.tool_call_id, + "name": frame.function_name, + "input": frame.arguments + } + ] + }) + self._context.add_message({ + "role": "user", + "content": [ { "type": "tool_result", "tool_use_id": frame.tool_call_id, "content": json.dumps(frame.result) } - ] - }) - run_llm = True + ] + }) + run_llm = True else: self._context.add_message({"role": "assistant", "content": aggregation}) diff --git a/src/pipecat/services/openai.py b/src/pipecat/services/openai.py index 0cad2b8bb..2d0a24589 100644 --- a/src/pipecat/services/openai.py +++ b/src/pipecat/services/openai.py @@ -399,26 +399,27 @@ class OpenAIAssistantContextAggregator(LLMAssistantContextAggregator): try: if self._function_call_result: frame = self._function_call_result - self._context.add_message({ - "role": "assistant", - "tool_calls": [ - { - "id": frame.tool_call_id, - "function": { - "name": frame.function_name, - "arguments": json.dumps(frame.arguments) - }, - "type": "function" - } - ] - }) - self._context.add_message({ - "role": "tool", - "content": json.dumps(frame.result), - "tool_call_id": frame.tool_call_id - }) self._function_call_result = None - run_llm = True + if frame.result: + self._context.add_message({ + "role": "assistant", + "tool_calls": [ + { + "id": frame.tool_call_id, + "function": { + "name": frame.function_name, + "arguments": json.dumps(frame.arguments) + }, + "type": "function" + } + ] + }) + self._context.add_message({ + "role": "tool", + "content": json.dumps(frame.result), + "tool_call_id": frame.tool_call_id + }) + run_llm = True else: self._context.add_message({"role": "assistant", "content": aggregation})