Skip to content

Commit

Permalink
react adapater headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Levinger authored and salmenus committed Mar 29, 2024
1 parent 96fe5ed commit 77d66f2
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 4 deletions.
8 changes: 8 additions & 0 deletions packages/js/langchain/src/langserve/adapter/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ 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';
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';
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
}
Expand Down
5 changes: 4 additions & 1 deletion packages/js/langchain/src/langserve/adapter/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});

Expand Down
5 changes: 4 additions & 1 deletion packages/js/langchain/src/langserve/adapter/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions packages/js/langchain/src/langserve/utils/getHeadersToUse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {ChatAdapterOptions} from '../types/adapterOptions';
import {LangServeHeaders} from '../types/langServe';

export const getHeadersToUse = (
adapterOptions: ChatAdapterOptions
): LangServeHeaders => {
return adapterOptions.headers || {};
};
2 changes: 1 addition & 1 deletion packages/react/langchain/src/hooks/getAdapterBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const getAdapterBuilder = (options: ChatAdapterOptions): ChatAdapterBuild
}

if (headers) {
headers = newAdapter.withHeaders(headers);
newAdapter = newAdapter.withHeaders(headers);
}

if (inputPreProcessor) {
Expand Down
2 changes: 2 additions & 0 deletions packages/react/langchain/src/hooks/useChatAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const useChatAdapter = (options: ChatAdapterOptions) => {
const {
url,
dataTransferMode,
headers,
inputPreProcessor,
outputPreProcessor,
useInputSchema,
Expand All @@ -28,6 +29,7 @@ export const useChatAdapter = (options: ChatAdapterOptions) => {
isInitialized,
url,
dataTransferMode,
headers,
inputPreProcessor,
outputPreProcessor,
useInputSchema,
Expand Down

0 comments on commit 77d66f2

Please sign in to comment.