fixes for anthropic function calling
This commit is contained in:
@@ -67,7 +67,8 @@ async def main():
|
|||||||
|
|
||||||
llm = AnthropicLLMService(
|
llm = AnthropicLLMService(
|
||||||
api_key=os.getenv("ANTHROPIC_API_KEY"),
|
api_key=os.getenv("ANTHROPIC_API_KEY"),
|
||||||
model="claude-3-5-sonnet-20240620",
|
# model="claude-3-5-sonnet-20240620",
|
||||||
|
model="claude-3-5-sonnet-latest",
|
||||||
enable_prompt_caching_beta=True,
|
enable_prompt_caching_beta=True,
|
||||||
)
|
)
|
||||||
llm.register_function("get_weather", get_weather)
|
llm.register_function("get_weather", get_weather)
|
||||||
|
|||||||
@@ -671,6 +671,7 @@ class AnthropicAssistantContextAggregator(LLMAssistantContextAggregator):
|
|||||||
):
|
):
|
||||||
self._function_call_in_progress = None
|
self._function_call_in_progress = None
|
||||||
self._function_call_result = frame
|
self._function_call_result = frame
|
||||||
|
await self._push_aggregation()
|
||||||
else:
|
else:
|
||||||
logger.warning(
|
logger.warning(
|
||||||
"FunctionCallResultFrame tool_call_id != InProgressFrame tool_call_id"
|
"FunctionCallResultFrame tool_call_id != InProgressFrame tool_call_id"
|
||||||
@@ -679,9 +680,12 @@ class AnthropicAssistantContextAggregator(LLMAssistantContextAggregator):
|
|||||||
self._function_call_result = None
|
self._function_call_result = None
|
||||||
elif isinstance(frame, AnthropicImageMessageFrame):
|
elif isinstance(frame, AnthropicImageMessageFrame):
|
||||||
self._pending_image_frame_message = frame
|
self._pending_image_frame_message = frame
|
||||||
|
await self._push_aggregation()
|
||||||
|
|
||||||
async def _push_aggregation(self):
|
async def _push_aggregation(self):
|
||||||
if not self._aggregation:
|
if not (
|
||||||
|
self._aggregation or self._function_call_result or self._pending_image_frame_message
|
||||||
|
):
|
||||||
return
|
return
|
||||||
|
|
||||||
run_llm = False
|
run_llm = False
|
||||||
@@ -694,20 +698,18 @@ class AnthropicAssistantContextAggregator(LLMAssistantContextAggregator):
|
|||||||
frame = self._function_call_result
|
frame = self._function_call_result
|
||||||
self._function_call_result = None
|
self._function_call_result = None
|
||||||
if frame.result:
|
if frame.result:
|
||||||
self._context.add_message(
|
assistant_message = {"role": "assistant", "content": []}
|
||||||
|
if aggregation:
|
||||||
|
assistant_message["content"].append({"type": "text", "text": aggregation})
|
||||||
|
assistant_message["content"].append(
|
||||||
{
|
{
|
||||||
"role": "assistant",
|
"type": "tool_use",
|
||||||
"content": [
|
"id": frame.tool_call_id,
|
||||||
{"type": "text", "text": aggregation},
|
"name": frame.function_name,
|
||||||
{
|
"input": frame.arguments,
|
||||||
"type": "tool_use",
|
|
||||||
"id": frame.tool_call_id,
|
|
||||||
"name": frame.function_name,
|
|
||||||
"input": frame.arguments,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
self._context.add_message(assistant_message)
|
||||||
self._context.add_message(
|
self._context.add_message(
|
||||||
{
|
{
|
||||||
"role": "user",
|
"role": "user",
|
||||||
@@ -721,7 +723,7 @@ class AnthropicAssistantContextAggregator(LLMAssistantContextAggregator):
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
run_llm = True
|
run_llm = True
|
||||||
else:
|
elif aggregation:
|
||||||
self._context.add_message({"role": "assistant", "content": aggregation})
|
self._context.add_message({"role": "assistant", "content": aggregation})
|
||||||
|
|
||||||
if self._pending_image_frame_message:
|
if self._pending_image_frame_message:
|
||||||
|
|||||||
Reference in New Issue
Block a user