Spaces:
Sleeping
Sleeping
| 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 | |
| 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?")) | |