File size: 881 Bytes
1eb76aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from langchain_openai import ChatOpenAI
from langchain.agents import initialize_agent, Tool
from langchain.agents import AgentType
from langchain.utilities import SerpAPIWrapper
from langchain.tools import tool

# 1. Define the LLM (the brain)
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)

# 2. Define tools the agent can use
@tool
def calculator(expression: str) -> str:
    """Useful for solving math problems."""
    try:
        result = eval(expression)
        return str(result)
    except Exception:
        return "Error in calculation."

tools = [calculator]

# 3. Initialize the Agent
agent = initialize_agent(
    tools,              # the toolbox
    llm,                # the brain
    agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True
)

# 4. Try it out
print(agent.run("What is 12 * 7 + 5?"))
print(agent.run("Who is the president of France?"))