feat: 网络访问更换为WebBrowser插件

This commit is contained in:
Hk-Gosuto
2023-08-21 12:50:52 +08:00
parent fdc43976ee
commit 00479d5b32
4 changed files with 112 additions and 27 deletions

View File

@@ -20,6 +20,9 @@ import { Calculator } from "langchain/tools/calculator";
import { DuckDuckGo } from "@/app/api/langchain-tools/duckduckgo_search";
import { HttpGetTool } from "@/app/api/langchain-tools/http_get";
import { ACCESS_CODE_PREFIX } from "@/app/constant";
import { OpenAI } from "langchain/llms/openai";
import { OpenAIEmbeddings } from "langchain/embeddings/openai";
import { WebBrowser } from "langchain/tools/webbrowser";
const serverConfig = getServerSideConfig();
@@ -77,6 +80,18 @@ async function handle(req: NextRequest) {
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) {
if (token) {
@@ -176,9 +191,24 @@ async function handle(req: NextRequest) {
});
}
const model = new OpenAI(
{
temperature: 0,
modelName: reqBody.model,
openAIApiKey: apiKey,
},
{ basePath: baseUrl },
);
const embeddings = new OpenAIEmbeddings(
{
openAIApiKey: apiKey,
},
{ basePath: baseUrl },
);
const tools = [
searchTool,
new HttpGetTool(),
new WebBrowser({ model, embeddings }),
// new RequestsGetTool(),
// new RequestsPostTool(),
new Calculator(),
@@ -204,17 +234,7 @@ async function handle(req: NextRequest) {
outputKey: "output",
chatHistory: new ChatMessageHistory(pastMessages),
});
// 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 llm = new ChatOpenAI(
{
modelName: reqBody.model,