In Langchain, is it possible to force Open AI to always call a custom function?

I have defined a function to read user’s name from database. How to force Chat-GPT to always call that function.

As for now, sometimes called, others not.

Open AI alllows tp specify function_call={"name": "function_name"}, but not sure if Langchain supports that.

@brainlid

You can tell the model to call the function, or if that’s the ONLY thing the model should do, then you can force it to call the function as the only thing it CAN do. Both ChatOpenAi and ChatAnthropic support a “tool_choice” where you can force the model.

However, I suspect that’s not what you want. Instead, don’t even have the model call the function. (It’s extra work, token usage, and delay.) If it’s in a Phoenix application, you know who the user is already, so use a PromptTemplate and load the user’s name into the system or user message yourself.

1 Like

Yes, that what I did eventually (but without PromptTemplate). After the system message (on chat start) I added a user message saying something like:

Hello, my name is my name and my age is my age (in case name and age are already in DB)

OR

Hello, please ask me for my name and age and store them in DB (in case those values are null in DB).

1 Like