langchain-ai / langserve

LangServe 🦜️🏓
Other
1.93k stars 214 forks source link

LangServe JS client - forward Http-Only cookies when calling stream #712

Open jccarles opened 3 months ago

jccarles commented 3 months ago

Hello,

We are using langserve js client the following way

const remoteChain = new RemoteRunnable({
    url: url,
    options: {
      headers:someHeader,
    },
  });

Then we stream the request as

remoteChain.stream(
        {
          chat_history: chat_history,
          question: question
        },
        {
          metadata: {
            run_id: id,
          },
        },
      );

Digging in the code it looks like stream is calling under the hood js fetch function the following way as seen here: https://github.com/langchain-ai/langchainjs/blob/dbb3b59623ee5ce4bf675bbf41a6e3e044ae0802/langchain-core/src/runnables/remote.ts#L278

private async post<Body>(path: string, body: Body) {
    return fetch(`${this.url}${path}`, {
      method: "POST",
      body: JSON.stringify(serialize(body)),
      headers: {
        "Content-Type": "application/json",
        ...this.options?.headers,
      },
      signal: AbortSignal.timeout(this.options?.timeout ?? 60000),
    });
  }

The best would be to be able to pass an additional parameter to fetch: credentials see here section how to pass cookies. The updated version could be:

private async post<Body>(path: string, body: Body) {
    return fetch(`${this.url}${path}`, {
      method: "POST",
      body: JSON.stringify(serialize(body)),
      headers: {
        "Content-Type": "application/json",
        ...this.options?.headers,
      },
      signal: AbortSignal.timeout(this.options?.timeout ?? 60000),
      credentials: this.options?.credentials ?? 'same-origin',  // same-origin is fetch default credentials value
    });
  }

with and update to remoteRunnableOption as

type RemoteRunnableOptions = {
  timeout?: number;
  headers?: Record<string, unknown>;
  credentials?: string;
};

or hardcoded for example:

private async post<Body>(path: string, body: Body) {
    return fetch(`${this.url}${path}`, {
      method: "POST",
      body: JSON.stringify(serialize(body)),
      headers: {
        "Content-Type": "application/json",
        ...this.options?.headers,
      },
      signal: AbortSignal.timeout(this.options?.timeout ?? 60000),
      credentials: 'include',
    });
  }

Thank you !

maxms93 commented 2 months ago

You can configure this in the fetchRequestOptions

https://v02.api.js.langchain.com/classes/_langchain_core.runnables_remote.RemoteRunnable.html

const remoteChain = new RemoteRunnable({
   url: url,
    fetchRequestOptions: {credentials: 'include'}
});