Langchain/LangGraph

Examples of using the Agentuity JavaScript SDK with Langchain (LangGraph)

Simple LangGraph Agent with structured output

Below is an example of how you can take a LangGraph example, empower it with Agentuity's SDK, and deploy it to Agentuity, all in one go.

// Example borrowed from https://langchain-ai.github.io/langgraphjs/how-tos/react-return-structured-output/
 
import type { AgentRequest, AgentResponse, AgentContext } from "@agentuity/sdk";
import { ChatOpenAI } from "@langchain/openai";
import { createReactAgent } from "@langchain/langgraph/prebuilt";
import { tool } from "@langchain/core/tools";
import { z } from "zod";
 
// Tools for the agent
const weatherTool = tool(
	async (input): Promise<string> => {
		if (input.city === "nyc") {
			return "It might be cloudy in nyc";
		} else if (input.city === "sf") {
			return "It's always sunny in sf";
		} else {
			throw new Error("Unknown city");
		}
	},
	{
		name: "get_weather",
		description: "Use this to get weather information.",
		schema: z.object({
			city: z.enum(["nyc", "sf"]).describe("The city to get weather for"),
		}),
	}
);
 
const langGraphAgent = createReactAgent({
	llm: new ChatOpenAI({ model: "gpt-4o", temperature: 0 }),
	tools: [weatherTool],
	responseFormat: z.object({
		conditions: z.string().describe("Weather conditions"),
	}),
});
 
export default async function AgentHandler(
	req: AgentRequest,
	resp: AgentResponse,
	ctx: AgentContext,
) {
	const response = await langGraphAgent.invoke({
		messages: [
			{
				role: "user",
				content: (await req.data.text()) ?? "What's the weather in NYC?",
			},
		],
	})
 
	return resp.json(response.structuredResponse);
}	

Need Help?

Join our DiscordCommunity for assistance or just to hang with other humans building agents.

Send us an email at hi@agentuity.com if you'd like to get in touch.

Please Follow us on

If you haven't already, please Signup for your free account now and start building your first agent!

On this page