mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-05 17:35:28 +03:00
5.7 KiB
5.7 KiB
In [2]:
from langchain.llms import OpenAI
from langchain.callbacks import get_openai_callbackIn [3]:
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(f"Total Tokens: {cb.total_tokens}")
print(f"Prompt Tokens: {cb.prompt_tokens}")
print(f"Completion Tokens: {cb.completion_tokens}")
print(f"Successful Requests: {cb.successful_requests}")
print(f"Total Cost (USD): ${cb.total_cost}")Total Tokens: 39 Prompt Tokens: 4 Completion Tokens: 35 Successful Requests: 1 Total Cost (USD): $0.0007800000000000001
In [5]:
with get_openai_callback() as cb:
result = llm("Tell me a joke")
result2 = llm("Tell me a joke")
print(cb.total_tokens)91
In [6]:
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.llms import OpenAI
llm = OpenAI(temperature=0)
tools = load_tools(["serpapi", "llm-math"], llm=llm)
agent = initialize_agent(tools, llm, agent=AgentType.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(f"Total Tokens: {cb.total_tokens}")
print(f"Prompt Tokens: {cb.prompt_tokens}")
print(f"Completion Tokens: {cb.completion_tokens}")
print(f"Total Cost (USD): ${cb.total_cost}")[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;3mSudeikis and Wilde's relationship ended in November 2020. Wilde was publicly served with court documents regarding child custody while she was presenting Don't Worry Darling at CinemaCon 2022. In January 2021, Wilde began dating singer Harry Styles after meeting during the filming of Don't Worry Darling.[0m Thought:[32;1m[1;3m I need to find out Harry Styles' age. Action: Search Action Input: "Harry Styles age"[0m Observation: [36;1m[1;3m29 years[0m Thought:[32;1m[1;3m I need to calculate 29 raised to the 0.23 power. Action: Calculator Action Input: 29^0.23[0m Observation: [33;1m[1;3mAnswer: 2.169459462491557 [0m Thought:[32;1m[1;3m I now know the final answer. Final Answer: Harry Styles, Olivia Wilde's boyfriend, is 29 years old and his age raised to the 0.23 power is 2.169459462491557.[0m [1m> Finished chain.[0m Total Tokens: 1506 Prompt Tokens: 1350 Completion Tokens: 156 Total Cost (USD): $0.03012
In [7]: