Skip to content

Commit

Permalink
When in Node use default axios adapter (for better compat with networ…
Browse files Browse the repository at this point in the history
…ks using proxies etc) (langchain-ai#1144)
  • Loading branch information
nfcampos authored May 6, 2023
1 parent dcef8ea commit 6695de6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
10 changes: 7 additions & 3 deletions langchain/src/chat_models/openai.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isNode } from "browser-or-node";
import {
Configuration,
OpenAIApi,
Expand Down Expand Up @@ -296,6 +297,7 @@ export class ChatOpenAI
},
{
...options,
adapter: fetchAdapter, // default adapter doesn't do streaming
responseType: "stream",
onmessage: (event) => {
if (event.data?.trim?.() === "[DONE]") {
Expand Down Expand Up @@ -450,14 +452,16 @@ export class ChatOpenAI
basePath: endpoint,
baseOptions: {
timeout: this.timeout,
adapter: fetchAdapter,
...this.clientConfig.baseOptions,
},
});
this.client = new OpenAIApi(clientConfig);
}
const axiosOptions = (options ?? {}) as StreamingAxiosConfiguration &
OpenAICallOptions;
const axiosOptions = {
adapter: isNode ? undefined : fetchAdapter,
...this.clientConfig.baseOptions,
...options,
} as StreamingAxiosConfiguration;
if (this.azureOpenAIApiKey) {
axiosOptions.headers = {
"api-key": this.azureOpenAIApiKey,
Expand Down
3 changes: 2 additions & 1 deletion langchain/src/embeddings/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
CreateEmbeddingRequest,
ConfigurationParameters,
} from "openai";
import { isNode } from "browser-or-node";
import type { AxiosRequestConfig } from "axios";
import { AzureOpenAIInput } from "../types/open-ai-types.js";
import fetchAdapter from "../util/axios-fetch-adapter.js";
Expand Down Expand Up @@ -175,7 +176,7 @@ export class OpenAIEmbeddings
basePath: endpoint,
baseOptions: {
timeout: this.timeout,
adapter: fetchAdapter,
adapter: isNode ? undefined : fetchAdapter,
...this.clientConfig.baseOptions,
},
});
Expand Down
10 changes: 7 additions & 3 deletions langchain/src/llms/openai-chat.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isNode } from "browser-or-node";
import {
Configuration,
OpenAIApi,
Expand Down Expand Up @@ -254,6 +255,7 @@ export class OpenAIChat
},
{
...options,
adapter: fetchAdapter, // default adapter doesn't do streaming
responseType: "stream",
onmessage: (event) => {
if (event.data?.trim?.() === "[DONE]") {
Expand Down Expand Up @@ -346,14 +348,16 @@ export class OpenAIChat
basePath: endpoint,
baseOptions: {
timeout: this.timeout,
adapter: fetchAdapter,
...this.clientConfig.baseOptions,
},
});
this.client = new OpenAIApi(clientConfig);
}
const axiosOptions = (options ?? {}) as StreamingAxiosConfiguration &
OpenAICallOptions;
const axiosOptions = {
adapter: isNode ? undefined : fetchAdapter,
...this.clientConfig.baseOptions,
...options,
} as StreamingAxiosConfiguration;
if (this.azureOpenAIApiKey) {
axiosOptions.headers = {
"api-key": this.azureOpenAIApiKey,
Expand Down
10 changes: 7 additions & 3 deletions langchain/src/llms/openai.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TiktokenModel } from "@dqbd/tiktoken";
import { isNode } from "browser-or-node";
import {
Configuration,
ConfigurationParameters,
Expand Down Expand Up @@ -296,6 +297,7 @@ export class OpenAI extends BaseLLM implements OpenAIInput, AzureOpenAIInput {
},
{
...options,
adapter: fetchAdapter, // default adapter doesn't do streaming
responseType: "stream",
onmessage: (event) => {
if (event.data?.trim?.() === "[DONE]") {
Expand Down Expand Up @@ -397,14 +399,16 @@ export class OpenAI extends BaseLLM implements OpenAIInput, AzureOpenAIInput {
basePath: endpoint,
baseOptions: {
timeout: this.timeout,
adapter: fetchAdapter,
...this.clientConfig.baseOptions,
},
});
this.client = new OpenAIApi(clientConfig);
}
const axiosOptions = (options ?? {}) as StreamingAxiosConfiguration &
OpenAICallOptions;
const axiosOptions: StreamingAxiosConfiguration = {
adapter: isNode ? undefined : fetchAdapter,
...this.clientConfig.baseOptions,
...options,
};
if (this.azureOpenAIApiKey) {
axiosOptions.headers = {
"api-key": this.azureOpenAIApiKey,
Expand Down

0 comments on commit 6695de6

Please sign in to comment.