From 0a643dc71dc9f40e79d1561ea90da132258fb248 Mon Sep 17 00:00:00 2001 From: Hk-Gosuto Date: Sun, 11 Aug 2024 14:40:57 +0000 Subject: [PATCH] feat: claude function call --- app/api/langchain/tool/agent/agentapi.ts | 13 +++++++++++-- app/client/api.ts | 3 +-- app/client/platforms/anthropic.ts | 2 +- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/app/api/langchain/tool/agent/agentapi.ts b/app/api/langchain/tool/agent/agentapi.ts index 876d2c990..2a20faa53 100644 --- a/app/api/langchain/tool/agent/agentapi.ts +++ b/app/api/langchain/tool/agent/agentapi.ts @@ -105,7 +105,6 @@ 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; @@ -276,6 +275,15 @@ export class AgentApi { throw new Error("Unsupported model providers"); } + getAuthHeader(reqBody: RequestBody): string { + const serverConfig = getServerSideConfig(); + return reqBody.isAzure || serverConfig.isAzure + ? "api-key" + : reqBody.provider === ServiceProvider.Anthropic + ? "x-api-key" + : "Authorization"; + } + async getApiHandler( req: NextRequest, reqBody: RequestBody, @@ -288,7 +296,7 @@ export class AgentApi { // const reqBody: RequestBody = await req.json(); // ui set azure model provider const isAzure = reqBody.isAzure; - const authHeaderName = isAzure ? "api-key" : "Authorization"; + const authHeaderName = this.getAuthHeader(reqBody); const authToken = req.headers.get(authHeaderName) ?? ""; const token = authToken.trim().replaceAll("Bearer ", "").trim(); @@ -427,6 +435,7 @@ export class AgentApi { const agentExecutor = new AgentExecutor({ agent, tools, + maxIterations: reqBody.maxIterations, }); await agentExecutor .invoke( diff --git a/app/client/api.ts b/app/client/api.ts index 217317e2b..42c0348c4 100644 --- a/app/client/api.ts +++ b/app/client/api.ts @@ -227,7 +227,7 @@ export class ClientApi { } } -export function getHeaders(ignoreHeaders?: boolean, isFunctionCall?: boolean) { +export function getHeaders(ignoreHeaders?: boolean) { const accessStore = useAccessStore.getState(); const chatStore = useChatStore.getState(); let headers: Record = {}; @@ -285,7 +285,6 @@ export function getHeaders(ignoreHeaders?: boolean, isFunctionCall?: boolean) { } function getAuthHeader(): string { - if (isFunctionCall) return "Authorization"; return isAzure ? "api-key" : isAnthropic ? "x-api-key" : "Authorization"; } diff --git a/app/client/platforms/anthropic.ts b/app/client/platforms/anthropic.ts index 36218afa0..dc8cb5228 100644 --- a/app/client/platforms/anthropic.ts +++ b/app/client/platforms/anthropic.ts @@ -146,7 +146,7 @@ export class ClaudeApi implements LLMApi { method: "POST", body: JSON.stringify(requestPayload), signal: controller.signal, - headers: getHeaders(false, true), + headers: getHeaders(), }; // make a fetch request