0
0
LangChainframework~10 mins

OpenAI functions agent in LangChain - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - OpenAI functions agent
User Input
Agent Receives Input
Agent Checks for Function Call
Call Function
Function Executes
Return Function Output
Agent Incorporates Output
Respond to User
The agent receives user input, decides if a function call is needed, executes it if yes, then responds with the function output or text.
Execution Sample
LangChain
from langchain.agents import create_openai_functions_agent, AgentExecutor

agent = create_openai_functions_agent(llm, tools)
agent_executor = AgentExecutor(agent=agent, tools=tools)
response = agent_executor.invoke({'input': 'What is the weather in Paris?'})
This code creates an OpenAI functions agent with executor and sends a user question to get a response, possibly calling a function.
Execution Table
StepActionInputAgent DecisionFunction CalledFunction OutputAgent Response
1Receive user inputWhat is the weather in Paris?Check if function needed
2Decide to call functionWhat is the weather in Paris?Yes, call weather functionget_weather(location='Paris')
3Execute functionlocation='Paris'Sunny, 25°C
4Incorporate function outputSunny, 25°CThe weather in Paris is Sunny, 25°C.
5Return response to userThe weather in Paris is Sunny, 25°C.
💡 Agent returns final response after function call and output incorporation.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
user_inputWhat is the weather in Paris?What is the weather in Paris?What is the weather in Paris?What is the weather in Paris?
agent_decisionCheck if function neededCall functionFunction output receivedResponse ready
function_calledget_weather(location='Paris')get_weather(location='Paris')get_weather(location='Paris')
function_outputSunny, 25°CSunny, 25°C
agent_responseThe weather in Paris is Sunny, 25°C.
Key Moments - 3 Insights
How does the agent know when to call a function instead of just replying?
The agent checks the input and matches it to available functions. If a function fits the request, it chooses to call it (see execution_table step 2).
What happens if the function output is empty or missing?
The agent waits for the function output before responding. If missing, it may fallback to a text response or error handling (not shown here).
Can the agent call multiple functions in one interaction?
Yes, but this example shows a single function call. The agent can chain calls by repeating the decision and execution steps.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what function does the agent call at step 2?
Asend_email(address='Paris')
Bget_news(topic='Paris')
Cget_weather(location='Paris')
DNo function called
💡 Hint
Check the 'Function Called' column at step 2 in the execution_table.
At which step does the agent receive the function output?
AStep 3
BStep 1
CStep 4
DStep 5
💡 Hint
Look at the 'Function Output' column in the execution_table.
If the user input was 'Tell me a joke', what would likely change in the execution_table?
AAgent calls a joke function instead of weather
BAgent skips function call and replies directly
CAgent calls get_weather function anyway
DAgent crashes
💡 Hint
Think about when the agent decides to call a function or just reply (see agent decision in execution_table step 2).
Concept Snapshot
OpenAI functions agent:
- Receives user input
- Checks if a function call fits the request
- Calls the function if needed
- Gets function output
- Responds using output or text
- Enables dynamic, real-time info in conversations
Full Transcript
The OpenAI functions agent works by first receiving what the user says. It then decides if it needs to call a special function to answer. If yes, it calls that function with the right details. The function runs and sends back an answer. The agent uses this answer to reply to the user. If no function is needed, the agent just replies with text. This way, the agent can give smart answers using live data or tools.