mirror of
https://github.com/langchain-ai/langgraph-guardrails-example.git
synced 2026-07-01 07:14:56 -04:00
add remaining scripts
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
// How to create agents with configuration
|
||||
import { Client } from "@langchain/langgraph-sdk";
|
||||
|
||||
/*
|
||||
One of the benefits of LangGraph API is that it lets you create agents with different configurations.
|
||||
This is useful when you want to:
|
||||
|
||||
- Define a cognitive architecture once as a LangGraph
|
||||
- Let that LangGraph be configurable across some attributes (for example, system message or LLM to use)
|
||||
- Let users create agents with arbitrary configurations, save them, and then use them in the future
|
||||
|
||||
In this guide we will show how to do that for the default agent we have built in.
|
||||
|
||||
If you look at the agent we defined, you can see that inside the `call_model` node we have created the model based on some configuration. That node looks like:
|
||||
|
||||
```python
|
||||
def call_model(state, config):
|
||||
messages = state["messages"]
|
||||
model_name = config.get('configurable', {}).get("model_name", "anthropic")
|
||||
model = _get_model(model_name)
|
||||
response = model.invoke(messages)
|
||||
# We return a list, because this will get added to the existing list
|
||||
return {"messages": [response]}
|
||||
```
|
||||
|
||||
We are looking inside the config for a `model_name` parameter (which defaults to `anthropic` if none is found).
|
||||
That means that by default we are using Anthropic as our model provider.
|
||||
In this example we will see an example of how to create an example agent that is configured to use OpenAI.
|
||||
*/
|
||||
|
||||
async function main() {
|
||||
const client = new Client();
|
||||
const assistant = await client.assistants.create({ graphId: "agent", config: { configurable: { model_name: "openai" } }});
|
||||
// We can see that this assistant has saved the config
|
||||
console.log("Assistant", assistant);
|
||||
const thread = await client.threads.create({ metadata: null });
|
||||
const input = { messages: [{ role: "user", content: "who made you?" }] };
|
||||
|
||||
for await (const event of client.runs.stream(thread.thread_id, assistant.assistant_id, { input })) {
|
||||
console.log(`Receiving new event of type: ${event.event}...`);
|
||||
console.log(JSON.stringify(event.data));
|
||||
console.log("\n\n");
|
||||
}
|
||||
}
|
||||
|
||||
main()
|
||||
@@ -0,0 +1,34 @@
|
||||
// How to run multiple agents on the same thread
|
||||
import { Client } from "@langchain/langgraph-sdk";
|
||||
|
||||
/*
|
||||
In LangGraph API, a thread is not explicitly associated with a particular agent.
|
||||
This means that you can run multiple agents on the same thread.
|
||||
In this example, we will create two agents and then call them both on the same thread.
|
||||
*/
|
||||
|
||||
async function main() {
|
||||
const client = new Client();
|
||||
// const openaiAssistant = await client.assistants.create({ graphId: "agent", config: { configurable: { model_name: "openai" } }});
|
||||
const defaultAssistant = await client.assistants.create({ graphId: "agent" })
|
||||
const openaiAssistant = await client.assistants.create({ graphId: "agent", config: { configurable: { model_name: "openai" } }});
|
||||
|
||||
console.log("Default assistant", defaultAssistant);
|
||||
// We can see that this assistant has saved the config
|
||||
console.log("OpenAI assistant", openaiAssistant);
|
||||
const thread = await client.threads.create({ metadata: null });
|
||||
let input = { messages: [{ role: "user", content: "who made you?" }] };
|
||||
|
||||
for await (const event of client.runs.stream(thread.thread_id, openaiAssistant.assistant_id, { input, streamMode: "updates" })) {
|
||||
console.log(JSON.stringify(event.data));
|
||||
console.log("\n\n");
|
||||
}
|
||||
|
||||
input = { messages: [{ role: "user", content: "and you?" }] };
|
||||
for await (const event of client.runs.stream(thread.thread_id, defaultAssistant.assistant_id, { input, streamMode: "updates" })) {
|
||||
console.log(JSON.stringify(event.data));
|
||||
console.log("\n\n");
|
||||
}
|
||||
}
|
||||
|
||||
main()
|
||||
@@ -1,3 +1,4 @@
|
||||
// How to stream messages from your graph
|
||||
import { Client } from "@langchain/langgraph-sdk";
|
||||
|
||||
/*
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
// How to stream updates from your graph
|
||||
import { Client } from "@langchain/langgraph-sdk";
|
||||
|
||||
/*
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
// How to stream values from your graph
|
||||
import { Client } from "@langchain/langgraph-sdk";
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user