import { tool } from "langchain";
import { ChatOpenAI } from "@langchain/openai";
import * as z from "zod";
const llm = new ChatOpenAI({
model: process.env.OPENAI_MODEL || "gpt-4o-mini",
apiKey: process.env.OPENAI_API_KEY,
configuration: {
baseURL: process.env.OPENAI_BASE_URL,
},
});
const writePoem = tool(
async ({ topic }) => {
return (
await llm.invoke([
{
role: "system",
content:
"You are an expert poet. Write a poem about the topic provided.",
},
{
role: "user",
content: topic,
},
])
).content as string;
},
{
name: "writePoem",
description: "Write a poem",
schema: z.object({
topic: z.string().describe("The topic of the poem"),
}),
}
);
const writeStory = tool(
async ({ topic }) => {
return (
await llm.invoke([
{
role: "system",
content:
"You are an expert storyteller. Write a story about the topic provided.",
},
{
role: "user",
content: topic,
},
])
).content as string;
},
{
name: "writeStory",
description: "Write a story",
schema: z.object({
topic: z.string().describe("The topic of the story"),
}),
}
);
const writeJoke = tool(
async ({ topic }) => {
return (
await llm.invoke([
{
role: "system",
content:
"You are an expert comedian. Write a joke about the topic provided.",
},
{
role: "user",
content: topic,
},
])
).content as string;
},
{
name: "writeJoke",
description: "Write a joke",
schema: z.object({
topic: z.string().describe("The topic of the joke"),
}),
}
);