An OpenAI functions agent helps your program talk to OpenAI's AI models and use special functions easily. It makes your app smarter by letting it ask AI for help and get answers or actions back.
0
0
OpenAI functions agent in LangChain
Introduction
You want your app to answer questions using AI.
You need to call specific functions based on AI responses.
You want to automate tasks by combining AI with your code.
You want to build a chatbot that can do things beyond just chatting.
You want to handle user requests with AI and custom logic.
Syntax
LangChain
from langchain.agents import create_openai_functions_agent from langchain.chat_models import ChatOpenAI llm = ChatOpenAI(temperature=0) agent = create_openai_functions_agent(llm, functions)
llm is the AI model you use to chat with OpenAI.
functions is a list of Python functions you want the agent to use.
Examples
This example creates an agent with one function called
my_function.LangChain
from langchain.agents import create_openai_functions_agent from langchain.chat_models import ChatOpenAI llm = ChatOpenAI(temperature=0) functions = [my_function] agent = create_openai_functions_agent(llm, functions)
Here, the agent uses two functions and a bit more creative AI with temperature 0.5.
LangChain
from langchain.agents import create_openai_functions_agent from langchain.chat_models import ChatOpenAI llm = ChatOpenAI(temperature=0.5) functions = [func1, func2] agent = create_openai_functions_agent(llm, functions)
Sample Program
This program creates an agent that can greet people by name. It asks the agent to greet Alice and prints the reply.
LangChain
from langchain.agents import create_openai_functions_agent from langchain.chat_models import ChatOpenAI # Define a simple function the agent can call def greet(name: str) -> str: return f"Hello, {name}!" # List of functions the agent can use functions = [greet] # Create the AI chat model llm = ChatOpenAI(temperature=0) # Create the agent with the model and functions agent = create_openai_functions_agent(llm, functions) # Ask the agent to greet someone response = agent.invoke({"input": "Greet Alice"}) print(response["output"])
OutputSuccess
Important Notes
Make sure your functions have clear input and output types for the agent to use them well.
Keep the AI temperature low (like 0) for predictable results when calling functions.
Test your functions separately before adding them to the agent.
Summary
An OpenAI functions agent connects AI chat with your own functions.
Use it to make apps that understand and act on user requests smartly.
It needs an AI model and a list of functions to work.