1 Star 0 Fork 0

43038038/langchain-nextjs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
route.ts 2.78 KB
一键复制 编辑 原始数据 按行查看 历史
jacoblee93 提交于 2024-01-20 02:47 +08:00 . Error handling improvements, version bump
import { NextRequest, NextResponse } from "next/server";
import { z } from "zod";
import { zodToJsonSchema } from "zod-to-json-schema";
import { ChatOpenAI } from "@langchain/openai";
import { PromptTemplate } from "@langchain/core/prompts";
import { JsonOutputFunctionsParser } from "langchain/output_parsers";
export const runtime = "edge";
const TEMPLATE = `Extract the requested fields from the input.
The field "entity" refers to the first mentioned entity in the input.
Input:
{input}`;
/**
* This handler initializes and calls an OpenAI Functions powered
* structured output chain. See the docs for more information:
*
* https://js.langchain.com/docs/modules/chains/popular/structured_output
*/
export async function POST(req: NextRequest) {
try {
const body = await req.json();
const messages = body.messages ?? [];
const currentMessageContent = messages[messages.length - 1].content;
const prompt = PromptTemplate.fromTemplate(TEMPLATE);
/**
* Function calling is currently only supported with ChatOpenAI models
*/
const model = new ChatOpenAI({
temperature: 0.8,
modelName: "gpt-3.5-turbo-1106",
});
/**
* We use Zod (https://zod.dev) to define our schema for convenience,
* but you can pass JSON Schema directly if desired.
*/
const schema = z.object({
tone: z
.enum(["positive", "negative", "neutral"])
.describe("The overall tone of the input"),
entity: z.string().describe("The entity mentioned in the input"),
word_count: z.number().describe("The number of words in the input"),
chat_response: z.string().describe("A response to the human's input"),
final_punctuation: z
.optional(z.string())
.describe("The final punctuation mark in the input, if any."),
});
/**
* Bind the function and schema to the OpenAI model.
* Future invocations of the returned model will always use these arguments.
*
* Specifying "function_call" ensures that the provided function will always
* be called by the model.
*/
const functionCallingModel = model.bind({
functions: [
{
name: "output_formatter",
description: "Should always be used to properly format output",
parameters: zodToJsonSchema(schema),
},
],
function_call: { name: "output_formatter" },
});
/**
* Returns a chain with the function calling model.
*/
const chain = prompt
.pipe(functionCallingModel)
.pipe(new JsonOutputFunctionsParser());
const result = await chain.invoke({
input: currentMessageContent,
});
return NextResponse.json(result, { status: 200 });
} catch (e: any) {
return NextResponse.json({ error: e.message }, { status: e.status ?? 500 });
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/haishang001/langchain-nextjs.git
git@gitee.com:haishang001/langchain-nextjs.git
haishang001
langchain-nextjs
langchain-nextjs
main

搜索帮助