mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-09-06 17:57:49 +02:00
25 KiB
25 KiB
In [ ]:
import { Client } from "@langchain/langgraph-sdk";
// If you deployed using Langsmith use this option
// Find this url on your Langsmith deployment page
const example_deployed_url = "https://ht-unhealthy-buffalo25-39d00f953458585aa9f7b5a4fa-g3ps4aazkq-uc.a.run.app";
// If you deployed locally using langgraph up -c langgraph.json use this option
// This is the default URL, and you can just call get_client() to use it
const example_local_url = "http://localhost:8123";
const client = new Client({apiUrl:"whatever-your-url-is"});In [ ]:
// List all assistants
const assistants = await client.assistants.search({
metadata: null,
offset: 0,
limit: 10,
});
// We auto-create an assistant for each graph you register in config.
const agent = assistants[0];In [ ]:
console.log(agent);In [ ]:
{
assistant_id: 'fe096781-5601-53d2-b2f6-0d3403f7e9ca',
graph_id: 'agent',
created_at: '2024-06-11T20:12:45.862108+00:00',
updated_at: '2024-06-11T20:12:45.862108+00:00',
config: {},
metadata: { created_by: 'system' }
}In [ ]:
const base_assistant = assistants[0];In [ ]:
const config_graph = await client.assistants.create({graphId: base_assistant['graph_id'],config: {'configurable':{'model':'openai'}}});
console.log(config_graph);In [ ]:
{
assistant_id: '414a1ddd-4453-4500-ab0b-7bc4f5bb41b1',
graph_id: 'agent',
created_at: '2024-06-21T23:42:16.388341+00:00',
updated_at: '2024-06-21T23:42:16.388341+00:00',
config: { configurable: { model: 'openai' } },
metadata: {}
}In [ ]:
// Start a new thread
const thread = await client.threads.create();In [ ]:
console.log(thread);In [ ]:
{
thread_id: '12a112dc-d175-42ce-8b06-85697733cccc',
created_at: '2024-06-20T22:01:18.715497+00:00',
updated_at: '2024-06-20T22:01:18.715497+00:00',
metadata: {}
}In [ ]:
from typing import Annotated, TypedDict
from langchain_core.messages import AnyMessage
from langgraph.graph import add_messages
def update_user_info(old_info, new_info):
if "name" not in new_info or new_info["age"] == -1:
return old_info
return new_info
class UserInformation(TypedDict):
age: int
name: str
class AgentState(TypedDict):
messages: Annotated[list[AnyMessage], add_messages]
user_info: Annotated[UserInformation, update_user_info]In [ ]:
async function processStreamedMessages(client, thread, agent, messages, metadata = {}) {
var answer = [];
try {
const streamResponse = client.runs.stream(
thread["thread_id"],
agent["assistant_id"],
{
input: { messages },
config: {"configurable": metadata}
}
);
for await (const chunk of streamResponse) {
answer.concat(chunk);
// Process each chunk of streamed data here
}
return answer;
} catch (error) {
console.error('Error processing streamed messages:', error);
}
}In [ ]:
var messages = [{ role: "human", content: "My name is Bagatur and I am 26 years old." }];
await processStreamedMessages(client, thread, agent, messages);In [ ]:
var state = await client.threads.getState(thread['thread_id']);
console.log(state);In [ ]:
{
values: {
messages: [ [Object], [Object], [Object] ],
user_info: { age: 26, name: 'Bagatur' }
},
next: [],
config: {
configurable: {
thread_id: '15292d98-6aca-4df8-b2a1-f4508e11abb1',
thread_ts: '1ef2f516-ebd2-68f4-8003-2a1251f89da5'
}
},
metadata: {
step: 3,
run_id: '1ef2f516-e211-6e60-8d19-6a1309008ece',
source: 'loop',
writes: { respond_to_user: [Object] },
user_id: '',
graph_id: 'agent',
thread_id: '15292d98-6aca-4df8-b2a1-f4508e11abb1',
created_by: 'system',
assistant_id: 'fe096781-5601-53d2-b2f6-0d3403f7e9ca'
},
created_at: '2024-06-20T22:07:06.852149+00:00',
parent_config: {
configurable: {
thread_id: '15292d98-6aca-4df8-b2a1-f4508e11abb1',
thread_ts: '1ef2f516-e6c7-6698-8002-80a6bedfa731'
}
}
}In [ ]:
const thread_2 = await client.threads.create();In [ ]:
var messages = [{ role: "human", content: "My name is Bagatur and I am 26 years old." }];
await processStreamedMessages(client, thread_2, agent, messages, {"node_id": 1, "parent_node": null});In [ ]:
messages = [{ role: "human", content: "What is my name?" }];
await processStreamedMessages(client, thread_2, agent, messages, {"node_id": 2, "parent_node": 1});In [ ]:
var state = await client.threads.getState(thread_2['thread_id']);
var graph_messages = state.values['messages'].map((message) => message.content);
console.log(graph_messages);In [ ]:
[
'My name is Bagatur and I am 26 years old.',
'',
'Hello Bagatur! How can I assist you today?',
'What is my name?',
'',
'Your name is Bagatur. How can I assist you today, Bagatur?'
]In [ ]:
var all_history = await client.threads.getHistory(thread_2["thread_id"])In [ ]:
var node_1_history = await client.threads.getHistory(thread_2["thread_id"],{metadata: {'node_id':1}})
var node_1_end_of_run = history.filter(node => node.next.length === 0)[0];In [ ]:
messages = [{ role: "human", content: "What is my age?" }];
await processStreamedMessages(client, thread_2, agent, messages, {"node_id": 3, "parent_node": 1, "thread_ts":node_1_end_of_run.checkpoint_id});In [ ]:
var state = await client.threads.getState(thread_2['thread_id']);
var graph_messages = state.values['messages'].map((message) => message.content);
console.log(graph_messages);In [ ]:
[
'My name is Bagatur and I am 26 years old.',
'',
'Hello Bagatur! How can I assist you today?',
'What is my age?',
'',
'You are 26 years old, Bagatur. How can I assist you today?'
]In [ ]:
await client.threads.updateState(thread_2['thread_id'], { values: { "user_info": { "name": "Bagatur", "age": 35 } } });In [ ]:
var state = await client.threads.getState(thread_2['thread_id']);
console.log(state.values['user_info']);
In [ ]:
{ name: 'Bagatur', age: 35 }In [ ]:
await client.threads.patchState(thread_2['thread_id'],{"node_id": 4});In [ ]:
var state = await client.threads.getState(thread_2['thread_id']);
console.log(state.metadata['node_id']);In [ ]:
4
