Skip to content

Commit

Permalink
- Updated the SmartChatModelRequestAdapter to conditionally include t…
Browse files Browse the repository at this point in the history
…ool_choice in the request body only when tools are present and tool_choice is not 'none'.

- Modified the SmartChatModelGeminiRequestAdapter to apply similar logic for tool_config, ensuring it is set only when tools are available.
- Adjusted the SmartThread class to remove tool_choice when the last message is a tool, enhancing request clarity.
- Improved streaming logic in SmartThread to account for cases where tool_choice is either undefined or explicitly set to 'none'.
  • Loading branch information
Brian Joseph Petro committed Dec 26, 2024
1 parent fd7e307 commit dcb7f0f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
5 changes: 4 additions & 1 deletion smart-chat-model/adapters/_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,8 +436,11 @@ export class SmartChatModelRequestAdapter {
temperature: this.temperature,
stream: streaming,
...(this.tools && { tools: this._transform_tools_to_openai() }),
...(this._req.tool_choice && { tool_choice: this._req.tool_choice }),
};
if((body.tools?.length > 0) && this.tool_choice !== 'none'){
// no tool choice if no tools
body.tool_choice = this.tool_choice;
}
// special handling for o1 models
if(this.model.startsWith('o1-')){
body.messages = body.messages.filter(m => m.role !== 'system'); // remove system messages (not supported by o1 models)
Expand Down
2 changes: 1 addition & 1 deletion smart-chat-model/adapters/google.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export class SmartChatModelGeminiRequestAdapter extends SmartChatModelRequestAda
]
};
if(this.tools) gemini_body.tools = this._transform_tools_to_gemini();
if(this._req.tool_choice) gemini_body.tool_config = this._transform_tool_choice_to_gemini();
if(gemini_body.tools && (this.tool_choice !== 'none')) gemini_body.tool_config = this._transform_tool_choice_to_gemini();

return {
url: streaming ? this.adapter.endpoint_streaming : this.adapter.endpoint,
Expand Down
5 changes: 3 additions & 2 deletions smart-chats/smart_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ export class SmartThread extends SmartSource {
request.tools = null;
}
if(this.last_message_is_tool){
request.tool_choice = 'none';
// request.tool_choice = 'none';
delete request.tool_choice;
}

// Set default AI model parameters
Expand Down Expand Up @@ -268,7 +269,7 @@ export class SmartThread extends SmartSource {
const request = await this.to_request();

// Use streaming if available and no immediate tool calls are requested
const should_stream = this.chat_model.can_stream && !request.tool_choice;
const should_stream = this.chat_model.can_stream && (!request.tool_choice || request.tool_choice === 'none');
if (should_stream) {
await this.chat_model.stream(request, {
chunk: this.chunk_handler.bind(this),
Expand Down

0 comments on commit dcb7f0f

Please sign in to comment.