Created
May 6, 2025 13:09
-
-
Save malitov/cf3f238f627f71c8ea0853a700c066ab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// IMPORTANT - Add your API keys here. Be careful not to publish them. | |
process.env.OPENAI_API_KEY = "sk-..."; | |
process.env.TAVILY_API_KEY = "tvly-..."; | |
import { TavilySearchResults } from "@langchain/community/tools/tavily_search"; | |
import { ChatOpenAI } from "@langchain/openai"; | |
import { MemorySaver } from "@langchain/langgraph"; | |
import { HumanMessage } from "@langchain/core/messages"; | |
import { createReactAgent } from "@langchain/langgraph/prebuilt"; | |
// Define the tools for the agent to use | |
const agentTools = [new TavilySearchResults({ maxResults: 3 })]; | |
const agentModel = new ChatOpenAI({ temperature: 0 }); | |
// Initialize memory to persist state between graph runs | |
const agentCheckpointer = new MemorySaver(); | |
const agent = createReactAgent({ | |
llm: agentModel, | |
tools: agentTools, | |
checkpointSaver: agentCheckpointer, | |
}); | |
// Now it's time to use! | |
const agentFinalState = await agent.invoke( | |
{ messages: [new HumanMessage("what is the current weather in sf")] }, | |
{ configurable: { thread_id: "42" } }, | |
); | |
console.log( | |
agentFinalState.messages[agentFinalState.messages.length - 1].content, | |
); | |
const agentNextState = await agent.invoke( | |
{ messages: [new HumanMessage("what about ny")] }, | |
{ configurable: { thread_id: "42" } }, | |
); | |
console.log( | |
agentNextState.messages[agentNextState.messages.length - 1].content, | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment