2

I'm trying to create a custom agent using langgraph and OpenAI in my Python project, but I'm encountering an error when using the create_react_agent function. Here's the code snippet that is causing the issue:

`import os
from dotenv import load_dotenv
from langchain_community.utilities import GoogleSerperAPIWrapper
from langchain_openai import OpenAI
from langgraph.prebuilt import create_react_agent

load_dotenv()

openai_api_key = os.getenv("OPENAI_API_KEY")
serper_api_key = os.getenv("SERPER_API_KEY")

llm = OpenAI(temperature=0, openai_api_key=openai_api_key, model="gpt-4o-mini")
search = GoogleSerperAPIWrapper()

tools = [
    Tool(
        name="Intermediate Answer",
        func=search.run,
        description="useful for when you need to ask with search",
    )
]

graph = create_react_agent(llm, tools=tools)

inputs = {"messages": [("user", "what is the hometown of the reigning men's U.S. Open champion?")]}
response = graph.stream(inputs, stream_mode="values")
`

However, when running the code, I encounter the following error:

 AttributeError: 'OpenAI' object has no attribute 'bind_tools'

This issue arises after the following line:

graph = create_react_agent(llm, tools=tools)

I suspect it might be related to the way OpenAI is interacting with langgraph or the create_react_agent function. I also receive a deprecation warning when using from langchain.agents import AgentType, Tool, initialize_agent.

What I've tried: Using the OpenAI model directly as shown in the code. Verifying that the API keys for both OPENAI_API_KEY and SERPER_API_KEY are correctly set in the .env file. Checking the documentation for the correct usage of langgraph. Any help would be appreciated in resolving this issue. How can I fix the 'bind_tools' error and successfully create a React agent using langgraph?

2 Answers 2

2

If you want to use tools with your model, you need to use ChatOpenAI instead of OpenAI. Update your code as follows:

from langchain_openai import ChatOpenAI

llm = ChatOpenAI(temperature=0, api_key=openai_api_key, model="gpt-4o-mini")
Sign up to request clarification or add additional context in comments.

Comments

0

Typescript Version

import { ChatOpenAI } from "@langchain/openai";
    
const model = new ChatOpenAI({apiKey: process.env.OPEN_AI_API_KEY, model: "gpt-4o-mini"});
      
# The TS equivalent is bindTools and not bind_tools
model.bindTools(tools);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.