mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-11-13 04:33:42 +08:00
feat: claude function call
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import { BaseLanguageModel } from "@langchain/core/language_models/base";
|
||||
import { Embeddings } from "@langchain/core/embeddings";
|
||||
import { ArxivAPIWrapper } from "@/app/api/langchain-tools/arxiv";
|
||||
import { DallEAPIWrapper } from "@/app/api/langchain-tools/dalle_image_generator";
|
||||
import { StableDiffusionWrapper } from "@/app/api/langchain-tools/stable_diffusion_image_generator";
|
||||
import { BaseLanguageModel } from "langchain/dist/base_language";
|
||||
import { Calculator } from "langchain/tools/calculator";
|
||||
import { Calculator } from "@langchain/community/tools/calculator";
|
||||
import { WebBrowser } from "langchain/tools/webbrowser";
|
||||
import { Embeddings } from "langchain/dist/embeddings/base.js";
|
||||
import { WolframAlphaTool } from "@/app/api/langchain-tools/wolframalpha";
|
||||
import { BilibiliVideoInfoTool } from "./bilibili_vid_info";
|
||||
import { BilibiliVideoSearchTool } from "./bilibili_vid_search";
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { Tool } from "@langchain/core/tools";
|
||||
import { CallbackManagerForToolRun } from "@langchain/core/callbacks/manager";
|
||||
import { BaseLanguageModel } from "langchain/dist/base_language";
|
||||
import { BaseLanguageModel } from "@langchain/core/language_models/base";
|
||||
import { Embeddings } from "@langchain/core/embeddings";
|
||||
import { formatDocumentsAsString } from "langchain/util/document";
|
||||
import { Embeddings } from "langchain/dist/embeddings/base.js";
|
||||
import { getServerSideConfig } from "@/app/config/server";
|
||||
import { SupabaseVectorStore } from "@langchain/community/vectorstores/supabase";
|
||||
import { createClient } from "@supabase/supabase-js";
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { BaseLanguageModel } from "langchain/dist/base_language";
|
||||
import { BaseLanguageModel } from "@langchain/core/language_models/base";
|
||||
import { Embeddings } from "@langchain/core/embeddings";
|
||||
import { PDFBrowser } from "@/app/api/langchain-tools/pdf_browser";
|
||||
import { Embeddings } from "langchain/dist/embeddings/base.js";
|
||||
import { ArxivAPIWrapper } from "@/app/api/langchain-tools/arxiv";
|
||||
import { DallEAPINodeWrapper } from "@/app/api/langchain-tools/dalle_image_generator_node";
|
||||
import { StableDiffusionNodeWrapper } from "@/app/api/langchain-tools/stable_diffusion_image_generator_node";
|
||||
import { Calculator } from "langchain/tools/calculator";
|
||||
import { Calculator } from "@langchain/community/tools/calculator";
|
||||
import { WebBrowser } from "langchain/tools/webbrowser";
|
||||
import { WolframAlphaTool } from "@/app/api/langchain-tools/wolframalpha";
|
||||
import { BilibiliVideoInfoTool } from "./bilibili_vid_info";
|
||||
|
||||
@@ -8,9 +8,9 @@ import {
|
||||
} from "langchain/text_splitter";
|
||||
|
||||
import { CallbackManagerForToolRun } from "@langchain/core/callbacks/manager";
|
||||
import { BaseLanguageModel } from "langchain/dist/base_language";
|
||||
import { BaseLanguageModel } from "@langchain/core/language_models/base";
|
||||
import { Embeddings } from "@langchain/core/embeddings";
|
||||
import { formatDocumentsAsString } from "langchain/util/document";
|
||||
import { Embeddings } from "langchain/dist/embeddings/base.js";
|
||||
import { RunnableSequence } from "@langchain/core/runnables";
|
||||
import { StringOutputParser } from "@langchain/core/output_parsers";
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ import S3FileStorage from "@/app/utils/s3_file_storage";
|
||||
import { OllamaEmbeddings } from "@langchain/community/embeddings/ollama";
|
||||
import { SupabaseVectorStore } from "@langchain/community/vectorstores/supabase";
|
||||
import { createClient } from "@supabase/supabase-js";
|
||||
import { Embeddings } from "langchain/dist/embeddings/base";
|
||||
import { Embeddings } from "@langchain/core/embeddings";
|
||||
|
||||
interface RequestBody {
|
||||
sessionId: string;
|
||||
|
||||
@@ -104,6 +104,7 @@ export class AgentApi {
|
||||
var controller = this.controller;
|
||||
return BaseCallbackHandler.fromMethods({
|
||||
async handleLLMNewToken(token: string) {
|
||||
console.log(token);
|
||||
if (token && !controller.signal.aborted) {
|
||||
var response = new ResponseBody();
|
||||
response.message = token;
|
||||
@@ -220,13 +221,14 @@ export class AgentApi {
|
||||
baseUrl = reqBaseUrl;
|
||||
if (!baseUrl.endsWith("/v1"))
|
||||
baseUrl = baseUrl.endsWith("/") ? `${baseUrl}v1` : `${baseUrl}/v1`;
|
||||
console.log("[baseUrl]", baseUrl);
|
||||
console.log("[openai baseUrl]", baseUrl);
|
||||
return baseUrl;
|
||||
}
|
||||
|
||||
getLLM(reqBody: RequestBody, apiKey: string, baseUrl: string) {
|
||||
const serverConfig = getServerSideConfig();
|
||||
if (reqBody.isAzure || serverConfig.isAzure)
|
||||
if (reqBody.isAzure || serverConfig.isAzure) {
|
||||
console.log("[use Azure ChatOpenAI]");
|
||||
return new ChatOpenAI({
|
||||
temperature: reqBody.temperature,
|
||||
streaming: reqBody.stream,
|
||||
@@ -240,7 +242,9 @@ export class AgentApi {
|
||||
azureOpenAIApiDeploymentName: reqBody.model,
|
||||
azureOpenAIBasePath: baseUrl,
|
||||
});
|
||||
if (reqBody.provider === ServiceProvider.OpenAI)
|
||||
}
|
||||
if (reqBody.provider === ServiceProvider.OpenAI) {
|
||||
console.log("[use ChatOpenAI]");
|
||||
return new ChatOpenAI(
|
||||
{
|
||||
modelName: reqBody.model,
|
||||
@@ -253,7 +257,9 @@ export class AgentApi {
|
||||
},
|
||||
{ basePath: baseUrl },
|
||||
);
|
||||
if (reqBody.provider === ServiceProvider.Anthropic)
|
||||
}
|
||||
if (reqBody.provider === ServiceProvider.Anthropic) {
|
||||
console.log("[use ChatAnthropic]");
|
||||
return new ChatAnthropic({
|
||||
model: reqBody.model,
|
||||
apiKey: apiKey,
|
||||
@@ -265,6 +271,7 @@ export class AgentApi {
|
||||
baseURL: baseUrl,
|
||||
},
|
||||
});
|
||||
}
|
||||
throw new Error("Unsupported model providers");
|
||||
}
|
||||
|
||||
@@ -294,7 +301,10 @@ export class AgentApi {
|
||||
) {
|
||||
baseUrl = reqBody.baseUrl;
|
||||
}
|
||||
if (!isAzure && !baseUrl.endsWith("/v1")) {
|
||||
if (
|
||||
reqBody.provider === ServiceProvider.OpenAI &&
|
||||
!baseUrl.endsWith("/v1")
|
||||
) {
|
||||
baseUrl = baseUrl.endsWith("/") ? `${baseUrl}v1` : `${baseUrl}/v1`;
|
||||
}
|
||||
if (!reqBody.isAzure && serverConfig.isAzure) {
|
||||
@@ -408,8 +418,7 @@ export class AgentApi {
|
||||
typeof lastMessageContent === "string"
|
||||
? new HumanMessage(lastMessageContent)
|
||||
: new HumanMessage({ content: lastMessageContent });
|
||||
|
||||
const agent = await createToolCallingAgent({
|
||||
const agent = createToolCallingAgent({
|
||||
llm,
|
||||
tools,
|
||||
prompt,
|
||||
@@ -423,7 +432,7 @@ export class AgentApi {
|
||||
{
|
||||
input: lastMessageContent,
|
||||
chat_history: pastMessages,
|
||||
signal: this.controller.signal,
|
||||
// signal: this.controller.signal,
|
||||
},
|
||||
{ callbacks: [handler] },
|
||||
)
|
||||
|
||||
@@ -4,8 +4,8 @@ import { auth } from "@/app/api/auth";
|
||||
import { NodeJSTool } from "@/app/api/langchain-tools/nodejs_tools";
|
||||
import { ModelProvider } from "@/app/constant";
|
||||
import { ChatOpenAI, OpenAIEmbeddings } from "@langchain/openai";
|
||||
import { Embeddings } from "langchain/dist/embeddings/base";
|
||||
import { OllamaEmbeddings } from "@langchain/community/embeddings/ollama";
|
||||
import { Embeddings } from "@langchain/core/embeddings";
|
||||
|
||||
async function handle(req: NextRequest) {
|
||||
if (req.method === "OPTIONS") {
|
||||
|
||||
Reference in New Issue
Block a user