mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-10 20:05:14 +03:00
5.7 KiB
5.7 KiB
In [1]:
import os
os.environ["LANGCHAIN_HANDLER"] = "langchain"
## Uncomment this if using hosted setup.
# os.environ["LANGCHAIN_ENDPOINT"] = "https://langchain-api-gateway-57eoxz8z.uc.gateway.dev"
## Uncomment this if you want traces to be recorded to "my_session" instead of default.
# os.environ["LANGCHAIN_SESSION"] = "my_session"
## Better to set this environment variable in the terminal
## Uncomment this if using hosted version. Replace "my_api_key" with your actual API Key.
# os.environ["LANGCHAIN_API_KEY"] = "my_api_key"
import langchain
from langchain.agents import Tool, initialize_agent, load_tools
from langchain.agents import AgentType
from langchain.chat_models import ChatOpenAI
from langchain.llms import OpenAIIn [2]:
# Agent run with tracing. Ensure that OPENAI_API_KEY is set appropriately to run this example.
llm = OpenAI(temperature=0)
tools = load_tools(["llm-math"], llm=llm)In [3]:
agent = initialize_agent(
tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True
)
agent.run("What is 2 raised to .123243 power?")Out [3]:
[1m> Entering new AgentExecutor chain...[0m [32;1m[1;3m I need to use a calculator to solve this. Action: Calculator Action Input: 2^.123243[0m Observation: [36;1m[1;3mAnswer: 1.0891804557407723 [0m Thought:[32;1m[1;3m I now know the final answer. Final Answer: 1.0891804557407723[0m [1m> Finished chain.[0m
'1.0891804557407723'
In [4]:
# Agent run with tracing using a chat model
agent = initialize_agent(
tools, ChatOpenAI(temperature=0), agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True
)
agent.run("What is 2 raised to .123243 power?")Out [4]:
[1m> Entering new AgentExecutor chain...[0m
[32;1m[1;3mQuestion: What is 2 raised to .123243 power?
Thought: I need a calculator to solve this problem.
Action:
```
{
"action": "calculator",
"action_input": "2^0.123243"
}
```
[0m
Observation: calculator is not a valid tool, try another one.
[32;1m[1;3mI made a mistake, I need to use the correct tool for this question.
Action:
```
{
"action": "calculator",
"action_input": "2^0.123243"
}
```
[0m
Observation: calculator is not a valid tool, try another one.
[32;1m[1;3mI made a mistake, the tool name is actually "calc" instead of "calculator".
Action:
```
{
"action": "calc",
"action_input": "2^0.123243"
}
```
[0m
Observation: calc is not a valid tool, try another one.
[32;1m[1;3mI made another mistake, the tool name is actually "Calculator" instead of "calc".
Action:
```
{
"action": "Calculator",
"action_input": "2^0.123243"
}
```
[0m
Observation: [36;1m[1;3mAnswer: 1.0891804557407723
[0m
Thought:[32;1m[1;3mThe final answer is 1.0891804557407723.
Final Answer: 1.0891804557407723[0m
[1m> Finished chain.[0m
'1.0891804557407723'
In [ ]: