@typia/langchain
LangChain.js integration for typia.
Converts typia controllers to LangChain DynamicStructuredTool[] with automatic validation.
npm install @typia/langchain @langchain/core
npm install typia
npx typia setup
import { ChainValues, Runnable } from "@langchain/core";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { DynamicStructuredTool } from "@langchain/core/tools";
import { ChatOpenAI } from "@langchain/openai";
import { toLangChainTools } from "@typia/langchain";
import { AgentExecutor, createToolCallingAgent } from "langchain/agents";
import typia from "typia";
const tools: DynamicStructuredTool[] = toLangChainTools({
controllers: [
typia.llm.controller<Calculator>("Calculator", new Calculator()),
],
});
const agent: Runnable = createToolCallingAgent({
llm: new ChatOpenAI({ model: "gpt-4o" }),
tools,
prompt: ChatPromptTemplate.fromMessages([
["system", "You are a helpful assistant."],
["human", "{input}"],
["placeholder", "{agent_scratchpad}"],
]),
});
const executor: AgentExecutor = new AgentExecutor({ agent, tools });
const result: ChainValues = await executor.invoke({ input: "What is 10 + 5?" });
import { DynamicStructuredTool } from "@langchain/core/tools";
import { toLangChainTools } from "@typia/langchain";
import { HttpLlm } from "@typia/utils";
const tools: DynamicStructuredTool[] = toLangChainTools({
controllers: [
HttpLlm.controller({
name: "petStore",
document: yourOpenApiDocument,
connection: { host: "https://api.example.com" },
}),
],
});
Use typia.llm.parameters<T>() with LangChain's withStructuredOutput() to generate structured output with validation:
import { ChatOpenAI } from "@langchain/openai";
import { dedent, LlmJson } from "@typia/utils";
import typia, { tags } from "typia";
interface IMember {
email: string & tags.Format<"email">;
name: string;
age: number & tags.Minimum<0> & tags.Maximum<100>;
hobbies: string[];
joined_at: string & tags.Format<"date">;
}
const model = new ChatOpenAI({ model: "gpt-4o" }).withStructuredOutput(
typia.llm.parameters<IMember>(),
);
const member: IMember = await model.invoke(dedent`
I am a new member of the community.
My name is John Doe, and I am 25 years old.
I like playing basketball and reading books,
and joined to this community at 2022-01-01.
`);
// Validate the result
const result = typia.validate<IMember>(member);
if (!result.success) {
console.error(LlmJson.stringify(result));
}
typia.llm.controller) and HTTP-based (HttpLlm.controller) controllers