Skip to content

Commit

Permalink
Nc/fix axios adapter imports (langchain-ai#149)
Browse files Browse the repository at this point in the history
* Fix axios adapter imports

* Add missing dependency needed for hugginface llm

* Fallback to axios (node-only) for openai streaming mode) as the axios fetch adapter doesn't directly support consuming SSE
  • Loading branch information
nfcampos authored Feb 27, 2023
1 parent 21fb387 commit 772f0cc
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 10 deletions.
1 change: 1 addition & 0 deletions langchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@
"jsonpointer": "^5.0.1",
"p-queue": "^7.3.4",
"sqlite3": "^5.1.4",
"unfetch": "^5.0.0",
"uuid": "^9.0.0",
"yaml": "^2.2.1"
},
Expand Down
12 changes: 8 additions & 4 deletions langchain/src/llms/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,14 @@ export class OpenAI extends BaseLLM implements OpenAIInput {
async completionWithRetry(request: CreateCompletionRequest) {
if (!this.client) {
const { Configuration, OpenAIApi, fetchAdapter } = await OpenAI.imports();
const clientConfig = new Configuration({
...this.clientConfig,
baseOptions: { adapter: fetchAdapter },
});
const clientConfig = new Configuration(
request.stream
? this.clientConfig
: {
...this.clientConfig,
baseOptions: { adapter: fetchAdapter },
}
);
this.client = new OpenAIApi(clientConfig);
}
const makeCompletionRequest = async () =>
Expand Down
20 changes: 19 additions & 1 deletion langchain/src/llms/tests/openai.int.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,26 @@
import { test } from "@jest/globals";
import { test, expect } from "@jest/globals";
import { OpenAI } from "../openai.js";

test("Test OpenAI", async () => {
const model = new OpenAI({ maxTokens: 5, modelName: "text-ada-001" });
const res = await model.call("Print hello world");
console.log({ res });
});

test("Test OpenAI in streaming mode", async () => {
let nrNewTokens = 0;
const model = new OpenAI({
maxTokens: 5,
modelName: "text-ada-001",
streaming: true,
callbackManager: {
handleNewToken() {
nrNewTokens += 1;
},
},
});
const res = await model.call("Print hello world");
console.log({ res });

expect(nrNewTokens > 0).toBe(true);
});
12 changes: 8 additions & 4 deletions langchain/src/util/axios-fetch-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
*/

import axios from "axios";
import settle from "axios/lib/core/settle";
import buildURL from "axios/lib/helpers/buildURL";
import buildFullPath from "axios/lib/core/buildFullPath";
import { isUndefined, isStandardBrowserEnv, isFormData } from "axios/lib/utils";
import settle from "axios/lib/core/settle.js";
import buildURL from "axios/lib/helpers/buildURL.js";
import buildFullPath from "axios/lib/core/buildFullPath.js";
import {
isUndefined,
isStandardBrowserEnv,
isFormData,
} from "axios/lib/utils.js";

/**
* - Create a request object
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@
"dependencies": {
"turbo": "^1.7.4"
}
}
}
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9614,6 +9614,7 @@ __metadata:
typedoc: ^0.23.25
typedoc-plugin-missing-exports: ^1.0.0
typescript: ^4.9.5
unfetch: ^5.0.0
uuid: ^9.0.0
yaml: ^2.2.1
peerDependencies:
Expand Down

0 comments on commit 772f0cc

Please sign in to comment.