mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-07 18:35:22 +03:00
4.6 KiB
4.6 KiB
In [1]:
from langchain.llms import OpenAI
from langchain.callbacks import get_openai_callbackIn [2]:
llm = OpenAI(model_name="text-davinci-002", n=2, best_of=2)In [4]:
with get_openai_callback() as cb:
result = llm("Tell me a joke")
print(cb.total_tokens)42
In [6]:
with get_openai_callback() as cb:
result = llm("Tell me a joke")
result2 = llm("Tell me a joke")
print(cb.total_tokens)83
In [7]:
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.llms import OpenAI
llm = OpenAI(temperature=0)
tools = load_tools(["serpapi", "llm-math"], llm=llm)
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)In [8]:
with get_openai_callback() as cb:
response = agent.run("Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?")
print(cb.total_tokens)[1m> Entering new AgentExecutor chain...[0m [32;1m[1;3m I need to find out who Olivia Wilde's boyfriend is and then calculate his age raised to the 0.23 power. Action: Search Action Input: "Olivia Wilde boyfriend"[0m Observation: [36;1m[1;3mJason Sudeikis[0m Thought:[32;1m[1;3m I need to find out Jason Sudeikis' age Action: Search Action Input: "Jason Sudeikis age"[0m Observation: [36;1m[1;3m47 years[0m Thought:[32;1m[1;3m I need to calculate 47 raised to the 0.23 power Action: Calculator Action Input: 47^0.23[0m Observation: [33;1m[1;3mAnswer: 2.4242784855673896 [0m Thought:[32;1m[1;3m I now know the final answer Final Answer: Jason Sudeikis, Olivia Wilde's boyfriend, is 47 years old and his age raised to the 0.23 power is 2.4242784855673896.[0m [1m> Finished chain.[0m 1465
In [ ]: