@@ -148,23 +148,15 @@ public ChatMessage chat(
148148 }
149149
150150 private ChatCompletionCreateParams buildRequest (
151- List <ChatMessage > messages , List <Tool > tools , Map <String , Object > rawArguments )
152- throws JsonProcessingException {
153-
151+ List <ChatMessage > messages , List <Tool > tools , Map <String , Object > rawArguments ) {
154152 Map <String , Object > arguments =
155153 rawArguments != null ? new HashMap <>(rawArguments ) : new HashMap <>();
156154
157- boolean strictMode = Boolean .TRUE .equals (arguments .get ("strict" ));
158-
159- String modelName = (String ) arguments .get ("model" );
155+ boolean strictMode = Boolean .TRUE .equals (arguments .remove ("strict" ));
156+ String modelName = (String ) arguments .remove ("model" );
160157 if (modelName == null || modelName .isBlank ()) {
161158 modelName = this .defaultModel ;
162159 }
163- if (modelName == null || modelName .isBlank ()) {
164- throw new IllegalArgumentException (
165- "OpenAI chat model requires a model name. "
166- + "Provide it via setup parameters or connection defaults." );
167- }
168160
169161 ChatCompletionCreateParams .Builder builder =
170162 ChatCompletionCreateParams .builder ()
@@ -180,19 +172,12 @@ private ChatCompletionCreateParams buildRequest(
180172
181173 @ SuppressWarnings ("unchecked" )
182174 Map <String , Object > additionalKwargs =
183- (Map <String , Object >) arguments .get ("additional_kwargs" );
175+ (Map <String , Object >) arguments .remove ("additional_kwargs" );
184176 if (additionalKwargs != null ) {
185177 additionalKwargs .forEach (
186178 (key , value ) -> builder .putAdditionalBodyProperty (key , toJsonValue (value )));
187179 }
188180
189- arguments .forEach (
190- (key , value ) -> {
191- if (!"additional_kwargs" .equals (key )) {
192- builder .putAdditionalBodyProperty (key , toJsonValue (value ));
193- }
194- });
195-
196181 return builder .build ();
197182 }
198183
@@ -274,18 +259,12 @@ private ChatCompletionMessageParam convertToOpenAIMessage(ChatMessage message) {
274259 case TOOL :
275260 ChatCompletionToolMessageParam .Builder toolBuilder =
276261 ChatCompletionToolMessageParam .builder ().content (content );
277- Object toolCallId =
278- Optional .ofNullable (message .getExtraArgs ().get ("tool_call_id" ))
279- .orElse (message .getExtraArgs ().get ("externalId" ));
262+ Object toolCallId = message .getExtraArgs ().get ("externalId" );
280263 if (toolCallId == null ) {
281- List <Map <String , Object >> assistantToolCalls = message .getToolCalls ();
282- if (assistantToolCalls != null && !assistantToolCalls .isEmpty ()) {
283- toolCallId = assistantToolCalls .get (0 ).get ("id" );
284- }
285- }
286- if (toolCallId != null ) {
287- toolBuilder .toolCallId (toolCallId .toString ());
264+ throw new IllegalArgumentException (
265+ "Tool message must have an externalId in extraArgs." );
288266 }
267+ toolBuilder .toolCallId (toolCallId .toString ());
289268 return ChatCompletionMessageParam .ofTool (toolBuilder .build ());
290269 default :
291270 throw new IllegalArgumentException ("Unsupported role: " + role );
@@ -373,7 +352,7 @@ private List<Map<String, Object>> convertResponseToolCalls(
373352 functionMap .put ("name" , function .name ());
374353 functionMap .put ("arguments" , parseArguments (function .arguments ()));
375354 callMap .put ("function" , functionMap );
376- callMap .put ("original_id" , functionToolCall . id () );
355+ callMap .put ("original_id" , toolCallId );
377356 result .add (callMap );
378357 }
379358 return result ;
0 commit comments