mirror of
https://github.com/langchain-ai/docs.git
synced 2026-08-27 02:41:59 -04:00
261 lines
6.2 KiB
Plaintext
261 lines
6.2 KiB
Plaintext
<CodeGroup>
|
|
```ts Google
|
|
import * as z from "zod";
|
|
import { ChatOpenAI } from "@langchain/openai";
|
|
import { createAgent, tool } from "langchain";
|
|
|
|
const getUserName = tool(
|
|
(_, config) => {
|
|
return config.context.user_name;
|
|
},
|
|
{
|
|
name: "get_user_name",
|
|
description: "Get the user's name.",
|
|
schema: z.object({}),
|
|
},
|
|
);
|
|
|
|
const contextSchema = z.object({
|
|
user_name: z.string(),
|
|
});
|
|
|
|
const agent = createAgent({
|
|
model: new ChatOpenAI({ model: "google-genai:gemini-3.6-flash" }),
|
|
tools: [getUserName],
|
|
contextSchema,
|
|
});
|
|
|
|
const result = await agent.invoke(
|
|
{
|
|
messages: [{ role: "user", content: "What is my name?" }],
|
|
},
|
|
{
|
|
configurable: { thread_id: crypto.randomUUID() },
|
|
context: { user_name: "John Smith" },
|
|
},
|
|
);
|
|
```
|
|
|
|
```ts OpenAI
|
|
import * as z from "zod";
|
|
import { ChatOpenAI } from "@langchain/openai";
|
|
import { createAgent, tool } from "langchain";
|
|
|
|
const getUserName = tool(
|
|
(_, config) => {
|
|
return config.context.user_name;
|
|
},
|
|
{
|
|
name: "get_user_name",
|
|
description: "Get the user's name.",
|
|
schema: z.object({}),
|
|
},
|
|
);
|
|
|
|
const contextSchema = z.object({
|
|
user_name: z.string(),
|
|
});
|
|
|
|
const agent = createAgent({
|
|
model: new ChatOpenAI({ model: "openai:gpt-5.5" }),
|
|
tools: [getUserName],
|
|
contextSchema,
|
|
});
|
|
|
|
const result = await agent.invoke(
|
|
{
|
|
messages: [{ role: "user", content: "What is my name?" }],
|
|
},
|
|
{
|
|
configurable: { thread_id: crypto.randomUUID() },
|
|
context: { user_name: "John Smith" },
|
|
},
|
|
);
|
|
```
|
|
|
|
```ts Anthropic
|
|
import * as z from "zod";
|
|
import { ChatOpenAI } from "@langchain/openai";
|
|
import { createAgent, tool } from "langchain";
|
|
|
|
const getUserName = tool(
|
|
(_, config) => {
|
|
return config.context.user_name;
|
|
},
|
|
{
|
|
name: "get_user_name",
|
|
description: "Get the user's name.",
|
|
schema: z.object({}),
|
|
},
|
|
);
|
|
|
|
const contextSchema = z.object({
|
|
user_name: z.string(),
|
|
});
|
|
|
|
const agent = createAgent({
|
|
model: new ChatOpenAI({ model: "anthropic:claude-sonnet-4-6" }),
|
|
tools: [getUserName],
|
|
contextSchema,
|
|
});
|
|
|
|
const result = await agent.invoke(
|
|
{
|
|
messages: [{ role: "user", content: "What is my name?" }],
|
|
},
|
|
{
|
|
configurable: { thread_id: crypto.randomUUID() },
|
|
context: { user_name: "John Smith" },
|
|
},
|
|
);
|
|
```
|
|
|
|
```ts OpenRouter
|
|
import * as z from "zod";
|
|
import { ChatOpenAI } from "@langchain/openai";
|
|
import { createAgent, tool } from "langchain";
|
|
|
|
const getUserName = tool(
|
|
(_, config) => {
|
|
return config.context.user_name;
|
|
},
|
|
{
|
|
name: "get_user_name",
|
|
description: "Get the user's name.",
|
|
schema: z.object({}),
|
|
},
|
|
);
|
|
|
|
const contextSchema = z.object({
|
|
user_name: z.string(),
|
|
});
|
|
|
|
const agent = createAgent({
|
|
model: new ChatOpenAI({ model: "openrouter:openrouter:z-ai/glm-5.2" }),
|
|
tools: [getUserName],
|
|
contextSchema,
|
|
});
|
|
|
|
const result = await agent.invoke(
|
|
{
|
|
messages: [{ role: "user", content: "What is my name?" }],
|
|
},
|
|
{
|
|
configurable: { thread_id: crypto.randomUUID() },
|
|
context: { user_name: "John Smith" },
|
|
},
|
|
);
|
|
```
|
|
|
|
```ts Fireworks
|
|
import * as z from "zod";
|
|
import { ChatOpenAI } from "@langchain/openai";
|
|
import { createAgent, tool } from "langchain";
|
|
|
|
const getUserName = tool(
|
|
(_, config) => {
|
|
return config.context.user_name;
|
|
},
|
|
{
|
|
name: "get_user_name",
|
|
description: "Get the user's name.",
|
|
schema: z.object({}),
|
|
},
|
|
);
|
|
|
|
const contextSchema = z.object({
|
|
user_name: z.string(),
|
|
});
|
|
|
|
const agent = createAgent({
|
|
model: new ChatOpenAI({ model: "fireworks:accounts/fireworks/models/glm-5p2" }),
|
|
tools: [getUserName],
|
|
contextSchema,
|
|
});
|
|
|
|
const result = await agent.invoke(
|
|
{
|
|
messages: [{ role: "user", content: "What is my name?" }],
|
|
},
|
|
{
|
|
configurable: { thread_id: crypto.randomUUID() },
|
|
context: { user_name: "John Smith" },
|
|
},
|
|
);
|
|
```
|
|
|
|
```ts Baseten
|
|
import * as z from "zod";
|
|
import { ChatOpenAI } from "@langchain/openai";
|
|
import { createAgent, tool } from "langchain";
|
|
|
|
const getUserName = tool(
|
|
(_, config) => {
|
|
return config.context.user_name;
|
|
},
|
|
{
|
|
name: "get_user_name",
|
|
description: "Get the user's name.",
|
|
schema: z.object({}),
|
|
},
|
|
);
|
|
|
|
const contextSchema = z.object({
|
|
user_name: z.string(),
|
|
});
|
|
|
|
const agent = createAgent({
|
|
model: new ChatOpenAI({ model: "baseten:zai-org/GLM-5.2" }),
|
|
tools: [getUserName],
|
|
contextSchema,
|
|
});
|
|
|
|
const result = await agent.invoke(
|
|
{
|
|
messages: [{ role: "user", content: "What is my name?" }],
|
|
},
|
|
{
|
|
configurable: { thread_id: crypto.randomUUID() },
|
|
context: { user_name: "John Smith" },
|
|
},
|
|
);
|
|
```
|
|
|
|
```ts Ollama
|
|
import * as z from "zod";
|
|
import { ChatOpenAI } from "@langchain/openai";
|
|
import { createAgent, tool } from "langchain";
|
|
|
|
const getUserName = tool(
|
|
(_, config) => {
|
|
return config.context.user_name;
|
|
},
|
|
{
|
|
name: "get_user_name",
|
|
description: "Get the user's name.",
|
|
schema: z.object({}),
|
|
},
|
|
);
|
|
|
|
const contextSchema = z.object({
|
|
user_name: z.string(),
|
|
});
|
|
|
|
const agent = createAgent({
|
|
model: new ChatOpenAI({ model: "ollama:north-mini-code-1.0" }),
|
|
tools: [getUserName],
|
|
contextSchema,
|
|
});
|
|
|
|
const result = await agent.invoke(
|
|
{
|
|
messages: [{ role: "user", content: "What is my name?" }],
|
|
},
|
|
{
|
|
configurable: { thread_id: crypto.randomUUID() },
|
|
context: { user_name: "John Smith" },
|
|
},
|
|
);
|
|
```
|
|
</CodeGroup>
|