patterns-ai-core / langchainrb

Build LLM-powered applications in Ruby
https://rubydoc.info/gems/langchainrb
MIT License
1.42k stars 195 forks source link

Add `assistant.tool_choice` parameter to force the Assistant to use a certain Tool function #738

Closed andreibondarev closed 2 months ago

andreibondarev commented 2 months ago

Currently the Assistant optionally picks a tool whenever it "thinks" it's appropriate to do so (tool_choice = "auto"). There are instances when you want the Assistant to use a specific tool 100% of the time (tool_choice = { "tool_name" }). This ticket was inspired by the following discussion: https://github.com/patterns-ai-core/langchainrb/discussions/733

Proposed changes:

If you take any of the built-in tools, you can see a list of that Tool's functions:

Langchain::Tool::NewsRetriever.function_schemas.to_openai_format.map { |h| h.dig(:function, :name) }
=> ["langchain_tool_news_retriever__get_everything", "langchain_tool_news_retriever__get_top_headlines", "langchain_tool_news_retriever__get_sources"]

This should be the value we're setting the tool_choice to:

assistant.tool_choice = "langchain_tool_news_retriever__get_sources"