Logging and tracing
You can pass the verbose
flag when creating an agent to enable logging of all events to the console. For example:
You can also enable tracing by setting the LANGCHAIN_TRACING environment variable to true
.
import { initializeAgentExecutorWithOptions } from "langchain/agents";
import { OpenAI } from "langchain/llms/openai";
import { SerpAPI } from "langchain/tools";
import { Calculator } from "langchain/tools/calculator";
const model = new OpenAI({ temperature: 0 });
const tools = [
new SerpAPI(process.env.SERPAPI_API_KEY, {
location: "Austin,Texas,United States",
hl: "en",
gl: "us",
}),
new Calculator(),
];
const executor = await initializeAgentExecutorWithOptions(tools, model, {
agentType: "zero-shot-react-description",
verbose: true,
});
const input = `Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?`;
const result = await executor.invoke({ input });
console.log(result);
/*
{ output: '2.2800773226742175' }
*/
API Reference:
- initializeAgentExecutorWithOptions from
langchain/agents
- OpenAI from
langchain/llms/openai
- SerpAPI from
langchain/tools
- Calculator from
langchain/tools/calculator
[chain/start] [1:chain:agent_executor] Entering Chain run with input: {
"input": "Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?"
}
[chain/start] [1:chain:agent_executor > 2:chain:llm_chain] Entering Chain run with input: {
"input": "Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?",
"agent_scratchpad": "",
"stop": [
"\nObservation: "
]
}
[llm/start] [1:chain:agent_executor > 2:chain:llm_chain > 3:llm:openai] Entering LLM run with input: {
"prompts": [
"Answer the following questions as best you can. You have access to the following tools:\n\nsearch: a search engine. useful for when you need to answer questions about current events. input should be a search query.\ncalculator: Useful for getting the result of a math expression. The input to this tool should be a valid mathematical expression that could be executed by a simple calculator.\n\nUse the following format in your response:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction: the action to take, should be one of [search,calculator]\nAction Input: the input to the action\nObservation: the result of the action\n... (this Thought/Action/Action Input/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question\n\nBegin!\n\nQuestion: Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?\nThought:"
]
}
[llm/end] [1:chain:agent_executor > 2:chain:llm_chain > 3:llm:openai] [3.52s] Exiting LLM run with output: {
"generations": [
[
{
"text": " I need to find out who Olivia Wilde's boyfriend is and then calculate his age raised to the 0.23 power.\nAction: search\nAction Input: \"Olivia Wilde boyfriend\"",
"generationInfo": {
"finishReason": "stop",
"logprobs": null
}
}
]
],
"llmOutput": {
"tokenUsage": {
"completionTokens": 39,
"promptTokens": 220,
"totalTokens": 259
}
}
}
[chain/end] [1:chain:agent_executor > 2:chain:llm_chain] [3.53s] Exiting Chain run with output: {
"text": " I need to find out who Olivia Wilde's boyfriend is and then calculate his age raised to the 0.23 power.\nAction: search\nAction Input: \"Olivia Wilde boyfriend\""
}
[agent/action] [1:chain:agent_executor] Agent selected action: {
"tool": "search",
"toolInput": "Olivia Wilde boyfriend",
"log": " I need to find out who Olivia Wilde's boyfriend is and then calculate his age raised to the 0.23 power.\nAction: search\nAction Input: \"Olivia Wilde boyfriend\""
}
[tool/start] [1:chain:agent_executor > 4:tool:search] Entering Tool run with input: "Olivia Wilde boyfriend"
[tool/end] [1:chain:agent_executor > 4:tool:search] [845ms] Exiting Tool run with output: "In January 2021, Wilde began dating singer Harry Styles after meeting during the filming of Don't Worry Darling. Their relationship ended in November 2022."
[chain/start] [1:chain:agent_executor > 5:chain:llm_chain] Entering Chain run with input: {
"input": "Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?",
"agent_scratchpad": " I need to find out who Olivia Wilde's boyfriend is and then calculate his age raised to the 0.23 power.\nAction: search\nAction Input: \"Olivia Wilde boyfriend\"\nObservation: In January 2021, Wilde began dating singer Harry Styles after meeting during the filming of Don't Worry Darling. Their relationship ended in November 2022.\nThought:",
"stop": [
"\nObservation: "
]
}
[llm/start] [1:chain:agent_executor > 5:chain:llm_chain > 6:llm:openai] Entering LLM run with input: {
"prompts": [
"Answer the following questions as best you can. You have access to the following tools:\n\nsearch: a search engine. useful for when you need to answer questions about current events. input should be a search query.\ncalculator: Useful for getting the result of a math expression. The input to this tool should be a valid mathematical expression that could be executed by a simple calculator.\n\nUse the following format in your response:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction: the action to take, should be one of [search,calculator]\nAction Input: the input to the action\nObservation: the result of the action\n... (this Thought/Action/Action Input/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question\n\nBegin!\n\nQuestion: Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?\nThought: I need to find out who Olivia Wilde's boyfriend is and then calculate his age raised to the 0.23 power.\nAction: search\nAction Input: \"Olivia Wilde boyfriend\"\nObservation: In January 2021, Wilde began dating singer Harry Styles after meeting during the filming of Don't Worry Darling. Their relationship ended in November 2022.\nThought:"
]
}
[llm/end] [1:chain:agent_executor > 5:chain:llm_chain > 6:llm:openai] [3.65s] Exiting LLM run with output: {
"generations": [
[
{
"text": " I need to find out Harry Styles' age.\nAction: search\nAction Input: \"Harry Styles age\"",
"generationInfo": {
"finishReason": "stop",
"logprobs": null
}
}
]
],
"llmOutput": {
"tokenUsage": {
"completionTokens": 23,
"promptTokens": 296,
"totalTokens": 319
}
}
}
[chain/end] [1:chain:agent_executor > 5:chain:llm_chain] [3.65s] Exiting Chain run with output: {
"text": " I need to find out Harry Styles' age.\nAction: search\nAction Input: \"Harry Styles age\""
}
[agent/action] [1:chain:agent_executor] Agent selected action: {
"tool": "search",
"toolInput": "Harry Styles age",
"log": " I need to find out Harry Styles' age.\nAction: search\nAction Input: \"Harry Styles age\""
}
[tool/start] [1:chain:agent_executor > 7:tool:search] Entering Tool run with input: "Harry Styles age"
[tool/end] [1:chain:agent_executor > 7:tool:search] [632ms] Exiting Tool run with output: "29 years"
[chain/start] [1:chain:agent_executor > 8:chain:llm_chain] Entering Chain run with input: {
"input": "Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?",
"agent_scratchpad": " I need to find out who Olivia Wilde's boyfriend is and then calculate his age raised to the 0.23 power.\nAction: search\nAction Input: \"Olivia Wilde boyfriend\"\nObservation: In January 2021, Wilde began dating singer Harry Styles after meeting during the filming of Don't Worry Darling. Their relationship ended in November 2022.\nThought: I need to find out Harry Styles' age.\nAction: search\nAction Input: \"Harry Styles age\"\nObservation: 29 years\nThought:",
"stop": [
"\nObservation: "
]
}
[llm/start] [1:chain:agent_executor > 8:chain:llm_chain > 9:llm:openai] Entering LLM run with input: {
"prompts": [
"Answer the following questions as best you can. You have access to the following tools:\n\nsearch: a search engine. useful for when you need to answer questions about current events. input should be a search query.\ncalculator: Useful for getting the result of a math expression. The input to this tool should be a valid mathematical expression that could be executed by a simple calculator.\n\nUse the following format in your response:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction: the action to take, should be one of [search,calculator]\nAction Input: the input to the action\nObservation: the result of the action\n... (this Thought/Action/Action Input/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question\n\nBegin!\n\nQuestion: Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?\nThought: I need to find out who Olivia Wilde's boyfriend is and then calculate his age raised to the 0.23 power.\nAction: search\nAction Input: \"Olivia Wilde boyfriend\"\nObservation: In January 2021, Wilde began dating singer Harry Styles after meeting during the filming of Don't Worry Darling. Their relationship ended in November 2022.\nThought: I need to find out Harry Styles' age.\nAction: search\nAction Input: \"Harry Styles age\"\nObservation: 29 years\nThought:"
]
}
[llm/end] [1:chain:agent_executor > 8:chain:llm_chain > 9:llm:openai] [2.72s] Exiting LLM run with output: {
"generations": [
[
{
"text": " I need to calculate 29 raised to the 0.23 power.\nAction: calculator\nAction Input: 29^0.23",
"generationInfo": {
"finishReason": "stop",
"logprobs": null
}
}
]
],
"llmOutput": {
"tokenUsage": {
"completionTokens": 26,
"promptTokens": 329,
"totalTokens": 355
}
}
}
[chain/end] [1:chain:agent_executor > 8:chain:llm_chain] [2.72s] Exiting Chain run with output: {
"text": " I need to calculate 29 raised to the 0.23 power.\nAction: calculator\nAction Input: 29^0.23"
}
[agent/action] [1:chain:agent_executor] Agent selected action: {
"tool": "calculator",
"toolInput": "29^0.23",
"log": " I need to calculate 29 raised to the 0.23 power.\nAction: calculator\nAction Input: 29^0.23"
}
[tool/start] [1:chain:agent_executor > 10:tool:calculator] Entering Tool run with input: "29^0.23"
[tool/end] [1:chain:agent_executor > 10:tool:calculator] [3ms] Exiting Tool run with output: "2.169459462491557"
[chain/start] [1:chain:agent_executor > 11:chain:llm_chain] Entering Chain run with input: {
"input": "Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?",
"agent_scratchpad": " I need to find out who Olivia Wilde's boyfriend is and then calculate his age raised to the 0.23 power.\nAction: search\nAction Input: \"Olivia Wilde boyfriend\"\nObservation: In January 2021, Wilde began dating singer Harry Styles after meeting during the filming of Don't Worry Darling. Their relationship ended in November 2022.\nThought: I need to find out Harry Styles' age.\nAction: search\nAction Input: \"Harry Styles age\"\nObservation: 29 years\nThought: I need to calculate 29 raised to the 0.23 power.\nAction: calculator\nAction Input: 29^0.23\nObservation: 2.169459462491557\nThought:",
"stop": [
"\nObservation: "
]
}
[llm/start] [1:chain:agent_executor > 11:chain:llm_chain > 12:llm:openai] Entering LLM run with input: {
"prompts": [
"Answer the following questions as best you can. You have access to the following tools:\n\nsearch: a search engine. useful for when you need to answer questions about current events. input should be a search query.\ncalculator: Useful for getting the result of a math expression. The input to this tool should be a valid mathematical expression that could be executed by a simple calculator.\n\nUse the following format in your response:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction: the action to take, should be one of [search,calculator]\nAction Input: the input to the action\nObservation: the result of the action\n... (this Thought/Action/Action Input/Observation can repeat N times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question\n\nBegin!\n\nQuestion: Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?\nThought: I need to find out who Olivia Wilde's boyfriend is and then calculate his age raised to the 0.23 power.\nAction: search\nAction Input: \"Olivia Wilde boyfriend\"\nObservation: In January 2021, Wilde began dating singer Harry Styles after meeting during the filming of Don't Worry Darling. Their relationship ended in November 2022.\nThought: I need to find out Harry Styles' age.\nAction: search\nAction Input: \"Harry Styles age\"\nObservation: 29 years\nThought: I need to calculate 29 raised to the 0.23 power.\nAction: calculator\nAction Input: 29^0.23\nObservation: 2.169459462491557\nThought:"
]
}
[llm/end] [1:chain:agent_executor > 11:chain:llm_chain > 12:llm:openai] [3.51s] Exiting LLM run with output: {
"generations": [
[
{
"text": " I now know the final answer.\nFinal Answer: Harry Styles is Olivia Wilde's boyfriend and his current age raised to the 0.23 power is 2.169459462491557.",
"generationInfo": {
"finishReason": "stop",
"logprobs": null
}
}
]
],
"llmOutput": {
"tokenUsage": {
"completionTokens": 39,
"promptTokens": 371,
"totalTokens": 410
}
}
}
[chain/end] [1:chain:agent_executor > 11:chain:llm_chain] [3.51s] Exiting Chain run with output: {
"text": " I now know the final answer.\nFinal Answer: Harry Styles is Olivia Wilde's boyfriend and his current age raised to the 0.23 power is 2.169459462491557."
}
[chain/end] [1:chain:agent_executor] [14.90s] Exiting Chain run with output: {
"output": "Harry Styles is Olivia Wilde's boyfriend and his current age raised to the 0.23 power is 2.169459462491557."
}