0
0
LangChainframework~30 mins

OpenAI functions agent in LangChain - Mini Project: Build & Apply

Choose your learning style9 modes available
Build a Simple OpenAI Functions Agent with LangChain
📖 Scenario: You want to create a small assistant that can answer questions by calling a function you define. This is useful when you want your AI to do specific tasks like math or fetching data.
🎯 Goal: Build a LangChain OpenAI Functions agent that uses a custom function to answer questions about simple math operations.
📋 What You'll Learn
Create a function that adds two numbers
Set up a LangChain OpenAI Functions agent
Configure the agent to use the custom addition function
Run the agent to answer a math question using the function
💡 Why This Matters
🌍 Real World
OpenAI Functions agents let you connect AI chat models to your own code, so the AI can perform tasks like calculations, database queries, or API calls automatically.
💼 Career
Understanding how to build OpenAI Functions agents is useful for AI developers, chatbot creators, and software engineers who want to integrate AI with custom business logic.
Progress0 / 4 steps
1
Create the addition function
Write a Python function called add_numbers that takes two parameters a and b and returns their sum.
LangChain
Need a hint?

Think of a function that takes two inputs and returns their sum using the return keyword.

2
Set up the OpenAI Functions agent
Import OpenAI and create_openai_functions_agent from langchain. Then create an OpenAI instance called llm with temperature=0.
LangChain
Need a hint?

Use from langchain.chat_models import OpenAI and from langchain.agents import create_openai_functions_agent. Then create llm = OpenAI(temperature=0).

3
Configure the agent with the addition function
Create a list called functions with one dictionary describing the add_numbers function. The dictionary must have keys: name with value 'add_numbers', description with value 'Add two numbers', and parameters describing two required integer properties a and b. Then create the agent by calling create_openai_functions_agent(llm, functions) and assign it to agent.
LangChain
Need a hint?

Define functions as a list with one dictionary describing the function name, description, and parameters. Then create agent using create_openai_functions_agent.

4
Run the agent to answer a math question
Call agent.run with the string 'What is the sum of 5 and 7?' and assign the result to answer.
LangChain
Need a hint?

Use answer = agent.run("What is the sum of 5 and 7?") to get the agent's response.