1+ import json
12import os
23from typing import List , Optional
34
1011from letta .otel .tracing import trace_method
1112from letta .schemas .llm_config import LLMConfig
1213from letta .schemas .message import Message as PydanticMessage
14+ from letta .schemas .openai .chat_completion_request import ChatMessage , cast_message_to_subtype
1315from letta .schemas .openai .chat_completion_response import ChatCompletionResponse
1416from letta .settings import model_settings
1517
@@ -36,15 +38,19 @@ def build_request_data(
3638 data = super ().build_request_data (messages , llm_config , tools , force_tool_call )
3739
3840 def add_functions_to_system_message (system_message : ChatMessage ):
39- system_message .content += f"<available functions> { '' .join (json .dumps (f ) for f in functions )} </available functions>"
41+ system_message .content += f"<available functions> { '' .join (json .dumps (f ) for f in tools )} </available functions>"
4042 system_message .content += 'Select best function to call simply respond with a single json block with the fields "name" and "arguments". Use double quotes around the arguments.'
4143
44+ openai_message_list = [cast_message_to_subtype (m .to_openai_dict (put_inner_thoughts_in_kwargs = False )) for m in messages ]
45+
4246 if llm_config .model == "deepseek-reasoner" : # R1 currently doesn't support function calling natively
4347 add_functions_to_system_message (
44- data [ "messages" ] [0 ]
48+ openai_message_list [0 ]
4549 ) # Inject additional instructions to the system prompt with the available functions
4650
47- data ["messages" ] = map_messages_to_deepseek_format (data ["messages" ])
51+ openai_message_list = map_messages_to_deepseek_format (openai_message_list )
52+
53+ data ["messages" ] = [m .dict () for m in openai_message_list ]
4854
4955 return data
5056
@@ -94,4 +100,6 @@ def convert_response_to_chat_completion(
94100 Handles potential extraction of inner thoughts if they were added via kwargs.
95101 """
96102 response = ChatCompletionResponse (** response_data )
103+ if response .choices [0 ].message .tool_calls :
104+ return super ().convert_response_to_chat_completion (response_data , input_messages , llm_config )
97105 return convert_deepseek_response_to_chatcompletion (response )
0 commit comments