gosia
gosia

Reputation: 59

Force LangChain agent to use a tool

Is there a way to force a LangChain agent to always use a tool?

(my specific use case: I need the agent to look for information in my database as opposed to generally accessible information and my tool searches through my database)

Upvotes: 2

Views: 6630

Answers (2)

wormhit
wormhit

Reputation: 3827

Most models support tool_choice="any"

  • OpenAI
  • MistralAI
  • FireworksAI
  • Groq

always_call_tool_llm = llm.bind_tools([add, multiply], tool_choice="any")

ref

UPDATE

Solution mentioned above is quite brute force. I figured that it pays off to improve tool description to make calls more consistent.

Good extra description that so far did not failed me is:

"Use tool when phrases like 'Whats new', 'Tell me the news' are used. "

You can extend it with more example phrases and so far it did not failed me. Tested with groq, mistral and openai.

Upvotes: 2

ZKS
ZKS

Reputation: 2816

When running an agent query, you can explicitly mention about the custom tool.

agent(
         "Using Custom Tool please calculate result of 5"
     )

Also, in your _run() function, you add custom log for tracking when custom tool is been called.

Upvotes: 5

Related Questions