From 77d66f22c154323dd0d538962fe053d8a2c48a7d Mon Sep 17 00:00:00 2001 From: Josh Levinger Date: Thu, 28 Mar 2024 10:03:04 -0400 Subject: [PATCH] react adapater headers --- packages/js/langchain/src/langserve/adapter/adapter.ts | 8 ++++++++ packages/js/langchain/src/langserve/adapter/fetch.ts | 5 ++++- packages/js/langchain/src/langserve/adapter/stream.ts | 5 ++++- .../js/langchain/src/langserve/types/adapterOptions.ts | 2 +- .../js/langchain/src/langserve/utils/getHeadersToUse.ts | 8 ++++++++ packages/react/langchain/src/hooks/getAdapterBuilder.ts | 2 +- packages/react/langchain/src/hooks/useChatAdapter.ts | 2 ++ 7 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 packages/js/langchain/src/langserve/utils/getHeadersToUse.ts diff --git a/packages/js/langchain/src/langserve/adapter/adapter.ts b/packages/js/langchain/src/langserve/adapter/adapter.ts index c1b96866..b10ecda8 100644 --- a/packages/js/langchain/src/langserve/adapter/adapter.ts +++ b/packages/js/langchain/src/langserve/adapter/adapter.ts @@ -9,6 +9,7 @@ import { warn, } from '@nlux/core'; import {ChatAdapterOptions} from '../types/adapterOptions'; +import {LangServeHeaders} from '../types/langServe'; import {LangServeInputPreProcessor} from '../types/inputPreProcessor'; import {LangServeOutputPreProcessor} from '../types/outputPreProcessor'; import {getDataTransferModeToUse} from '../utils/getDataTransferModeToUse'; @@ -16,6 +17,7 @@ import {getEndpointUrlToUse} from '../utils/getEndpointUrlToUse'; import {getRunnableNameToUse} from '../utils/getRunnableNameToUse'; import {getSchemaUrlToUse} from '../utils/getSchemaUrlToUse'; import {transformInputBasedOnSchema} from '../utils/transformInputBasedOnSchema'; +import { getHeadersToUse } from '../utils/getHeadersToUse'; export abstract class LangServeAbstractAdapter implements StandardChatAdapter { static defaultDataTransferMode: DataTransferMode = 'stream'; @@ -25,6 +27,7 @@ export abstract class LangServeAbstractAdapter implements StandardChatAdapter { private readonly theDataTransferModeToUse: DataTransferMode; private readonly theEndpointUrlToUse: string; + private readonly theHeadersToUse: LangServeHeaders; private theInputSchemaToUse: object | undefined; private readonly theInputSchemaUrlToUse: string; private readonly theRunnableNameToUse: string; @@ -35,6 +38,7 @@ export abstract class LangServeAbstractAdapter implements StandardChatAdapter { this.__options = {...options}; this.theDataTransferModeToUse = getDataTransferModeToUse(options); + this.theHeadersToUse = getHeadersToUse(options); this.theUseInputSchemaOptionToUse = (typeof options.useInputSchema === 'boolean') ? options.useInputSchema : true; @@ -54,6 +58,10 @@ export abstract class LangServeAbstractAdapter implements StandardChatAdapter { return this.theEndpointUrlToUse; } + get headers(): LangServeHeaders { + return this.theHeadersToUse; + } + get id(): string { return this.__instanceId; } diff --git a/packages/js/langchain/src/langserve/adapter/fetch.ts b/packages/js/langchain/src/langserve/adapter/fetch.ts index d54026a6..47a23751 100644 --- a/packages/js/langchain/src/langserve/adapter/fetch.ts +++ b/packages/js/langchain/src/langserve/adapter/fetch.ts @@ -10,7 +10,10 @@ export class LangServeFetchAdapter extends LangServeAbstractAdapter { const body = this.getRequestBody(message, extras.conversationHistory); const response = await fetch(this.endpointUrl, { method: 'POST', - headers: {'Content-Type': 'application/json'}, + headers: { + ...this.headers, + 'Content-Type': 'application/json' + }, body, }); diff --git a/packages/js/langchain/src/langserve/adapter/stream.ts b/packages/js/langchain/src/langserve/adapter/stream.ts index a3a02386..6f3aa252 100644 --- a/packages/js/langchain/src/langserve/adapter/stream.ts +++ b/packages/js/langchain/src/langserve/adapter/stream.ts @@ -19,7 +19,10 @@ export class LangServeStreamAdapter extends LangServeAbstractAdapter { const body = this.getRequestBody(message, extras.conversationHistory); fetch(this.endpointUrl, { method: 'POST', - headers: {'Content-Type': 'application/json'}, + headers: { + ...this.headers, + 'Content-Type': 'application/json' + }, body, }) .then(async (response) => { diff --git a/packages/js/langchain/src/langserve/types/adapterOptions.ts b/packages/js/langchain/src/langserve/types/adapterOptions.ts index 21f60d39..defc3c97 100644 --- a/packages/js/langchain/src/langserve/types/adapterOptions.ts +++ b/packages/js/langchain/src/langserve/types/adapterOptions.ts @@ -31,7 +31,7 @@ export type ChatAdapterOptions = { /** * This contains the headers that implementers can use to send additional data such as authentication headers. */ - headers?: LangServeHeaders; + headers?: LangServeHeaders; /** * A function to preprocess the user input before sending it to the LangServe runnable. diff --git a/packages/js/langchain/src/langserve/utils/getHeadersToUse.ts b/packages/js/langchain/src/langserve/utils/getHeadersToUse.ts new file mode 100644 index 00000000..37c59012 --- /dev/null +++ b/packages/js/langchain/src/langserve/utils/getHeadersToUse.ts @@ -0,0 +1,8 @@ +import {ChatAdapterOptions} from '../types/adapterOptions'; +import {LangServeHeaders} from '../types/langServe'; + +export const getHeadersToUse = ( + adapterOptions: ChatAdapterOptions +): LangServeHeaders => { + return adapterOptions.headers || {}; +}; \ No newline at end of file diff --git a/packages/react/langchain/src/hooks/getAdapterBuilder.ts b/packages/react/langchain/src/hooks/getAdapterBuilder.ts index f1de6082..beeca4a5 100644 --- a/packages/react/langchain/src/hooks/getAdapterBuilder.ts +++ b/packages/react/langchain/src/hooks/getAdapterBuilder.ts @@ -27,7 +27,7 @@ export const getAdapterBuilder = (options: ChatAdapterOptions): ChatAdapterBuild } if (headers) { - headers = newAdapter.withHeaders(headers); + newAdapter = newAdapter.withHeaders(headers); } if (inputPreProcessor) { diff --git a/packages/react/langchain/src/hooks/useChatAdapter.ts b/packages/react/langchain/src/hooks/useChatAdapter.ts index 2b162e97..7d6bc0be 100644 --- a/packages/react/langchain/src/hooks/useChatAdapter.ts +++ b/packages/react/langchain/src/hooks/useChatAdapter.ts @@ -11,6 +11,7 @@ export const useChatAdapter = (options: ChatAdapterOptions) => { const { url, dataTransferMode, + headers, inputPreProcessor, outputPreProcessor, useInputSchema, @@ -28,6 +29,7 @@ export const useChatAdapter = (options: ChatAdapterOptions) => { isInitialized, url, dataTransferMode, + headers, inputPreProcessor, outputPreProcessor, useInputSchema,