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?