@@ -163,8 +163,10 @@ async def generate_text(
163
163
api_key : Optional [str ] = None ,
164
164
base_url : Optional [str ] = None ,
165
165
model : Optional [str ] = None ,
166
- ) -> str : # pragma: no cover
167
- """Generate text from OpenAI models as a single string (no images)."""
166
+ functions : Optional [List [Dict [str , Any ]]] = None ,
167
+ function_call : Optional [Union [str , Dict [str , Any ]]] = None ,
168
+ ) -> Any : # pragma: no cover
169
+ """Generate text or function call from OpenAI models."""
168
170
messages = []
169
171
if system_prompt :
170
172
messages .append ({"role" : "system" , "content" : system_prompt })
@@ -174,6 +176,10 @@ async def generate_text(
174
176
"messages" : messages ,
175
177
"model" : model or self .text_model ,
176
178
}
179
+ if functions :
180
+ request_params ["functions" ] = functions
181
+ if function_call :
182
+ request_params ["function_call" ] = function_call
177
183
178
184
if api_key and base_url :
179
185
client = AsyncOpenAI (api_key = api_key , base_url = base_url )
@@ -185,17 +191,13 @@ async def generate_text(
185
191
186
192
try :
187
193
response = await client .chat .completions .create (** request_params )
188
- if response .choices and response .choices [0 ].message .content :
189
- return response .choices [0 ].message .content
190
- else :
191
- logger .warning ("Received non-streaming response with no content." )
192
- return ""
193
- except OpenAIError as e : # Catch specific OpenAI errors
194
+ return response
195
+ except OpenAIError as e :
194
196
logger .error (f"OpenAI API error during text generation: { e } " )
195
- return f"I apologize, but I encountered an API error: { e } "
197
+ return None
196
198
except Exception as e :
197
199
logger .exception (f"Error in generate_text: { e } " )
198
- return f"I apologize, but I encountered an unexpected error: { e } "
200
+ return None
199
201
200
202
def _calculate_gpt41_image_cost (self , width : int , height : int , model : str ) -> int :
201
203
"""Calculates the token cost for an image with GPT-4.1 models."""
0 commit comments