Spaces:
Build error
Build error
File size: 9,995 Bytes
f0499d2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
import { NextRequest, NextResponse } from "next/server";
import { getServerSideConfig } from "@/app/config/server";
import { auth } from "../../../auth";
import { ChatOpenAI } from "langchain/chat_models/openai";
import { BaseCallbackHandler } from "langchain/callbacks";
import { AIMessage, HumanMessage, SystemMessage } from "langchain/schema";
import { BufferMemory, ChatMessageHistory } from "langchain/memory";
import { initializeAgentExecutorWithOptions } from "langchain/agents";
import { ACCESS_CODE_PREFIX } from "@/app/constant";
import { OpenAI } from "langchain/llms/openai";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
import * as langchainTools from "langchain/tools";
import { HttpGetTool } from "@/app/api/langchain-tools/http_get";
import { DuckDuckGo } from "@/app/api/langchain-tools/duckduckgo_search";
import { WebBrowser } from "langchain/tools/webbrowser";
import { Calculator } from "langchain/tools/calculator";
import { DynamicTool, Tool } from "langchain/tools";
import { BaiduSearch } from "@/app/api/langchain-tools/baidu_search";
import { GoogleSearch } from "@/app/api/langchain-tools/google_search";
import { ArxivAPIWrapper } from "@/app/api/langchain-tools/arxiv";
const serverConfig = getServerSideConfig();
interface RequestMessage {
role: string;
content: string;
}
interface RequestBody {
messages: RequestMessage[];
model: string;
stream?: boolean;
temperature: number;
presence_penalty?: number;
frequency_penalty?: number;
top_p?: number;
baseUrl?: string;
apiKey?: string;
maxIterations: number;
returnIntermediateSteps: boolean;
useTools: (undefined | string)[];
}
class ResponseBody {
isSuccess: boolean = true;
message!: string;
isToolMessage: boolean = false;
toolName?: string;
}
interface ToolInput {
input: string;
}
async function handle(req: NextRequest) {
if (req.method === "OPTIONS") {
return NextResponse.json({ body: "OK" }, { status: 200 });
}
try {
const authResult = auth(req);
if (authResult.error) {
return NextResponse.json(authResult, {
status: 401,
});
}
const encoder = new TextEncoder();
const transformStream = new TransformStream();
const writer = transformStream.writable.getWriter();
const reqBody: RequestBody = await req.json();
const authToken = req.headers.get("Authorization") ?? "";
const token = authToken.trim().replaceAll("Bearer ", "").trim();
const isOpenAiKey = !token.startsWith(ACCESS_CODE_PREFIX);
let useTools = reqBody.useTools ?? [];
let apiKey = serverConfig.apiKey;
if (isOpenAiKey && token) {
apiKey = token;
}
// support base url
let baseUrl = "https://api.openai.com/v1";
if (serverConfig.baseUrl) baseUrl = serverConfig.baseUrl;
if (
reqBody.baseUrl?.startsWith("http://") ||
reqBody.baseUrl?.startsWith("https://")
)
baseUrl = reqBody.baseUrl;
if (!baseUrl.endsWith("/v1"))
baseUrl = baseUrl.endsWith("/") ? `${baseUrl}v1` : `${baseUrl}/v1`;
console.log("[baseUrl]", baseUrl);
const handler = BaseCallbackHandler.fromMethods({
async handleLLMNewToken(token: string) {
// console.log("[Token]", token);
if (token) {
var response = new ResponseBody();
response.message = token;
await writer.ready;
await writer.write(
encoder.encode(`data: ${JSON.stringify(response)}\n\n`),
);
}
},
async handleChainError(err, runId, parentRunId, tags) {
console.log("[handleChainError]", err, "writer error");
var response = new ResponseBody();
response.isSuccess = false;
response.message = err;
await writer.ready;
await writer.write(
encoder.encode(`data: ${JSON.stringify(response)}\n\n`),
);
await writer.close();
},
async handleChainEnd(outputs, runId, parentRunId, tags) {
console.log("[handleChainEnd]");
await writer.ready;
await writer.close();
},
async handleLLMEnd() {
// await writer.ready;
// await writer.close();
},
async handleLLMError(e: Error) {
console.log("[handleLLMError]", e, "writer error");
var response = new ResponseBody();
response.isSuccess = false;
response.message = e.message;
await writer.ready;
await writer.write(
encoder.encode(`data: ${JSON.stringify(response)}\n\n`),
);
await writer.close();
},
handleLLMStart(llm, _prompts: string[]) {
// console.log("handleLLMStart: I'm the second handler!!", { llm });
},
handleChainStart(chain) {
// console.log("handleChainStart: I'm the second handler!!", { chain });
},
async handleAgentAction(action) {
try {
console.log("[handleAgentAction]", action.tool);
if (!reqBody.returnIntermediateSteps) return;
var response = new ResponseBody();
response.isToolMessage = true;
response.message = JSON.stringify(action.toolInput);
response.toolName = action.tool;
await writer.ready;
await writer.write(
encoder.encode(`data: ${JSON.stringify(response)}\n\n`),
);
} catch (ex) {
console.error("[handleAgentAction]", ex);
var response = new ResponseBody();
response.isSuccess = false;
response.message = (ex as Error).message;
await writer.ready;
await writer.write(
encoder.encode(`data: ${JSON.stringify(response)}\n\n`),
);
await writer.close();
}
},
handleToolStart(tool, input) {
console.log("[handleToolStart]", { tool });
},
async handleToolEnd(output, runId, parentRunId, tags) {
console.log("[handleToolEnd]", { output, runId, parentRunId, tags });
},
handleAgentEnd(action, runId, parentRunId, tags) {
console.log("[handleAgentEnd]");
},
});
let searchTool: Tool = new DuckDuckGo();
if (process.env.CHOOSE_SEARCH_ENGINE) {
switch (process.env.CHOOSE_SEARCH_ENGINE) {
case "google":
searchTool = new GoogleSearch();
break;
case "baidu":
searchTool = new BaiduSearch();
break;
}
}
if (process.env.BING_SEARCH_API_KEY) {
let bingSearchTool = new langchainTools["BingSerpAPI"](
process.env.BING_SEARCH_API_KEY,
);
searchTool = new DynamicTool({
name: "bing_search",
description: bingSearchTool.description,
func: async (input: string) => bingSearchTool.call(input),
});
}
if (process.env.SERPAPI_API_KEY) {
let serpAPITool = new langchainTools["SerpAPI"](
process.env.SERPAPI_API_KEY,
);
searchTool = new DynamicTool({
name: "google_search",
description: serpAPITool.description,
func: async (input: string) => serpAPITool.call(input),
});
}
const model = new OpenAI(
{
temperature: 0,
modelName: reqBody.model,
openAIApiKey: apiKey,
},
{ basePath: baseUrl },
);
const embeddings = new OpenAIEmbeddings(
{
openAIApiKey: apiKey,
},
{ basePath: baseUrl },
);
const tools = [
// new RequestsGetTool(),
// new RequestsPostTool(),
];
const webBrowserTool = new WebBrowser({ model, embeddings });
const calculatorTool = new Calculator();
const arxivAPITool = new ArxivAPIWrapper();
if (useTools.includes("web-search")) tools.push(searchTool);
if (useTools.includes(webBrowserTool.name)) tools.push(webBrowserTool);
if (useTools.includes(calculatorTool.name)) tools.push(calculatorTool);
if (useTools.includes(arxivAPITool.name)) tools.push(arxivAPITool);
useTools.forEach((toolName) => {
if (toolName) {
var tool = langchainTools[
toolName as keyof typeof langchainTools
] as any;
if (tool) {
tools.push(new tool());
}
}
});
const pastMessages = new Array();
reqBody.messages
.slice(0, reqBody.messages.length - 1)
.forEach((message) => {
if (message.role === "system")
pastMessages.push(new SystemMessage(message.content));
if (message.role === "user")
pastMessages.push(new HumanMessage(message.content));
if (message.role === "assistant")
pastMessages.push(new AIMessage(message.content));
});
const memory = new BufferMemory({
memoryKey: "chat_history",
returnMessages: true,
inputKey: "input",
outputKey: "output",
chatHistory: new ChatMessageHistory(pastMessages),
});
const llm = new ChatOpenAI(
{
modelName: reqBody.model,
openAIApiKey: apiKey,
temperature: reqBody.temperature,
streaming: reqBody.stream,
topP: reqBody.top_p,
presencePenalty: reqBody.presence_penalty,
frequencyPenalty: reqBody.frequency_penalty,
},
{ basePath: baseUrl },
);
const executor = await initializeAgentExecutorWithOptions(tools, llm, {
agentType: "openai-functions",
returnIntermediateSteps: reqBody.returnIntermediateSteps,
maxIterations: reqBody.maxIterations,
memory: memory,
});
executor.call(
{
input: reqBody.messages.slice(-1)[0].content,
},
[handler],
);
console.log("returning response");
return new Response(transformStream.readable, {
headers: { "Content-Type": "text/event-stream" },
});
} catch (e) {
return new Response(JSON.stringify({ error: (e as any).message }), {
status: 500,
headers: { "Content-Type": "application/json" },
});
}
}
export const GET = handle;
export const POST = handle;
export const runtime = "edge"; |