Skip to content

Commit

Permalink
Merge pull request #1 from ArtBlocks/OpenAIChat-token-estimation-fix
Browse files Browse the repository at this point in the history
Add token count estimation to OpenAIChat LLM instance, to ensure parity w/ OpenAI LLM instance.
  • Loading branch information
jakerockland authored Apr 28, 2023
2 parents 4e7ea75 + 551d363 commit a3b7f13
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions langchain/src/llms/openai-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import type { AxiosRequestConfig } from "axios";
import type { StreamingAxiosConfiguration } from "../util/axios-types.js";
import fetchAdapter from "../util/axios-fetch-adapter.js";
import { calculateMaxTokens } from "../base_language/count_tokens.js";
import { BaseLLMCallOptions, BaseLLMParams, LLM } from "./base.js";
import { CallbackManagerForLLMRun } from "../callbacks/manager.js";

Expand Down Expand Up @@ -235,6 +236,20 @@ export class OpenAIChat extends LLM implements OpenAIChatInput {
const params = this.invocationParams();
params.stop = stop ?? params.stop;

if (params.max_tokens === -1) {
// Include prefixes in the token count, as this will be enforced
// (if prefixes exist) by the OpenAI API token limits:
// https://platform.openai.com/docs/api-reference/completions/create#max_tokens
// Dump this all into one big blob of text with `.strigify` to
// ensure that we are pessimistic about the token count and don't
// leave anything out!
const promptAndPrefixes = JSON.stringify(this.formatMessages(prompt));
params.max_tokens = await calculateMaxTokens({
prompt: promptAndPrefixes,
// Cast here to allow for other models that may not fit the union
modelName: this.modelName,
});
}
const data = params.stream
? await new Promise<CreateChatCompletionResponse>((resolve, reject) => {
let response: CreateChatCompletionResponse;
Expand Down

0 comments on commit a3b7f13

Please sign in to comment.