Skip to content

Commit

Permalink
Allow calling chatgpt models from main OpenAI LLM (langchain-ai#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
nfcampos authored Mar 2, 2023
1 parent e948c22 commit 2b647c4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
7 changes: 6 additions & 1 deletion langchain/src/llms/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import { backOff } from "exponential-backoff";
import fetchAdapter from "../util/axios-fetch-adapter.js";
import { chunkArray } from "../util/index.js";
import { BaseLLM } from "./base.js";
import { LLMResult, LLMCallbackManager } from "./index.js";
import type { LLMResult, LLMCallbackManager } from "./index.js";
import { OpenAIChat } from "./openai-chat.js";

interface ModelParams {
/** Sampling temperature to use */
Expand Down Expand Up @@ -141,6 +142,10 @@ export class OpenAI extends BaseLLM implements OpenAIInput {
},
configuration?: ConfigurationParameters
) {
if (fields?.modelName?.startsWith("gpt-3.5-turbo")) {
// eslint-disable-next-line no-constructor-return
return new OpenAIChat(fields, configuration) as any as OpenAI;
}
super(
fields?.callbackManager,
fields?.verbose,
Expand Down
9 changes: 9 additions & 0 deletions langchain/src/llms/tests/openai.int.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { test, expect } from "@jest/globals";
import { OpenAIChat } from "../openai-chat.js";
import { OpenAI } from "../openai.js";

test("Test OpenAI", async () => {
Expand All @@ -7,6 +8,14 @@ test("Test OpenAI", async () => {
console.log({ res });
});

test("Test OpenAI with chat model returns OpenAIChat", async () => {
const model = new OpenAI({ modelName: "gpt-3.5-turbo" });
expect(model).toBeInstanceOf(OpenAIChat);
const res = await model.call("Print hello world");
console.log({ res });
expect(typeof res).toBe("string");
});

test("Test OpenAI in streaming mode", async () => {
let nrNewTokens = 0;
let streamedCompletion = "";
Expand Down
2 changes: 1 addition & 1 deletion test-exports/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
"prettier": "^2.8.3",
"typescript": "^4.9.5"
}
}
}

0 comments on commit 2b647c4

Please sign in to comment.