Skip to content

Commit

Permalink
Nc/chatgpt (langchain-ai#181)
Browse files Browse the repository at this point in the history
* Make openai a direct dependency

* Improve streaming test

* Add chatgpt LLM

* Add doc
  • Loading branch information
nfcampos authored Mar 1, 2023
1 parent e1fbb45 commit fd0ac58
Show file tree
Hide file tree
Showing 9 changed files with 424 additions and 75 deletions.
29 changes: 29 additions & 0 deletions docs/docs/modules/llms/openai-chat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# OpenAI Chat

This guide goes through how to use the OpenAI Chat LLM wrapper. This is designed to use the ChatGPT family of models from OpenAI. You can find more information on them [here](https://platform.openai.com/docs/guides/chat).

```typescript
import { OpenAIChat } from "langchain/llms";

const model = new OpenAIChat({ modelName: "gpt-3.5-turbo" });
const res = await model.call(
"What would be a good company name a company that makes colorful socks?"
);
console.log({ res });
```

## Prefix messages

You can also pass messages that the model should add before your prompt. This is useful for adding context to the model, or past conversation history.

```typescript
const model = new OpenAIChat({
modelName: "gpt-3.5-turbo",
prefixMessages: [
{ role: "user", content: "My name is John" },
{ role: "assistant", content: "Hi there" },
],
});
const res = await model.call("What is my name");
console.log({ res });
```
8 changes: 2 additions & 6 deletions langchain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@
"husky": "^8.0.3",
"jest": "^29.4.2",
"lint-staged": "^13.1.1",
"openai": "^3.1.0",
"prettier": "^2.8.3",
"serpapi": "^1.1.1",
"srt-parser-2": "^1.2.2",
Expand All @@ -98,7 +97,6 @@
"cohere-ai": "^5.0.2",
"hnswlib-node": "^1.3.0",
"huggingface": "^1.4.0",
"openai": "^3.1.0",
"serpapi": "^1.1.1",
"srt-parser-2": "^1.2.2"
},
Expand All @@ -121,9 +119,6 @@
"hnswlib-node": {
"optional": true
},
"openai": {
"optional": true
},
"serpapi": {
"optional": true
},
Expand All @@ -139,6 +134,7 @@
"expr-eval": "^2.0.2",
"gpt-3-encoder": "^1.1.4",
"jsonpointer": "^5.0.1",
"openai": "^3.2.0",
"p-queue": "^7.3.4",
"sqlite3": "^5.1.4",
"unfetch": "^5.0.0",
Expand Down Expand Up @@ -222,4 +218,4 @@
"import": "./document_loaders.js"
}
}
}
}
31 changes: 3 additions & 28 deletions langchain/src/embeddings/openai.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import type {
Configuration as ConfigurationT,
OpenAIApi as OpenAIApiT,
CreateEmbeddingRequest,
} from "openai";
import { Configuration, OpenAIApi, CreateEmbeddingRequest } from "openai";
import { backOff } from "exponential-backoff";
import type fetchAdapterT from "../util/axios-fetch-adapter.js";
import fetchAdapter from "../util/axios-fetch-adapter.js";
import { chunkArray } from "../util/index.js";
import { Embeddings } from "./base.js";

Expand All @@ -21,7 +17,7 @@ export class OpenAIEmbeddings extends Embeddings implements ModelParams {

private apiKey: string;

private client: OpenAIApiT;
private client: OpenAIApi;

constructor(
fields?: Partial<ModelParams> & {
Expand Down Expand Up @@ -73,8 +69,6 @@ export class OpenAIEmbeddings extends Embeddings implements ModelParams {

private async embeddingWithRetry(request: CreateEmbeddingRequest) {
if (!this.client) {
const { Configuration, OpenAIApi, fetchAdapter } =
await OpenAIEmbeddings.imports();
const clientConfig = new Configuration({
apiKey: this.apiKey,
baseOptions: { adapter: fetchAdapter },
Expand All @@ -88,23 +82,4 @@ export class OpenAIEmbeddings extends Embeddings implements ModelParams {
numOfAttempts: this.maxRetries,
});
}

static async imports(): Promise<{
Configuration: typeof ConfigurationT;
OpenAIApi: typeof OpenAIApiT;
fetchAdapter: typeof fetchAdapterT;
}> {
try {
const { Configuration, OpenAIApi } = await import("openai");
const { default: fetchAdapter } = await import(
"../util/axios-fetch-adapter.js"
);
return { Configuration, OpenAIApi, fetchAdapter };
} catch (err) {
console.error(err);
throw new Error(
"Please install openai as a dependency with, e.g. `npm install -S openai`"
);
}
}
}
1 change: 1 addition & 0 deletions langchain/src/llms/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { BaseLLM, LLM, SerializedLLM } from "./base.js";
export { OpenAI, PromptLayerOpenAI } from "./openai.js";
export { OpenAIChat } from "./openai-chat.js";
export { Cohere } from "./cohere.js";
export { HuggingFaceInference } from "./hf.js";
export { loadLLM } from "./load.js";
Expand Down
Loading

0 comments on commit fd0ac58

Please sign in to comment.