revert: setting max_tokens based on message size (#207)
This commit is contained in:
@@ -2,9 +2,8 @@ import https from 'https';
|
|||||||
import type { ClientRequest, IncomingMessage } from 'http';
|
import type { ClientRequest, IncomingMessage } from 'http';
|
||||||
import type { ChatCompletionRequestMessage, CreateChatCompletionRequest, CreateChatCompletionResponse } from 'openai';
|
import type { ChatCompletionRequestMessage, CreateChatCompletionRequest, CreateChatCompletionResponse } from 'openai';
|
||||||
import {
|
import {
|
||||||
TiktokenModel,
|
type TiktokenModel,
|
||||||
// eslint-disable-next-line camelcase
|
// encoding_for_model,
|
||||||
encoding_for_model,
|
|
||||||
} from '@dqbd/tiktoken';
|
} from '@dqbd/tiktoken';
|
||||||
import createHttpsProxyAgent from 'https-proxy-agent';
|
import createHttpsProxyAgent from 'https-proxy-agent';
|
||||||
import { KnownError } from './error.js';
|
import { KnownError } from './error.js';
|
||||||
@@ -151,22 +150,22 @@ const getExtraContextForConventionalCommits = () => (
|
|||||||
}`
|
}`
|
||||||
);
|
);
|
||||||
|
|
||||||
const generateStringFromLength = (length: number) => {
|
// const generateStringFromLength = (length: number) => {
|
||||||
let result = '';
|
// let result = '';
|
||||||
const highestTokenChar = 'z';
|
// const highestTokenChar = 'z';
|
||||||
for (let i = 0; i < length; i += 1) {
|
// for (let i = 0; i < length; i += 1) {
|
||||||
result += highestTokenChar;
|
// result += highestTokenChar;
|
||||||
}
|
// }
|
||||||
return result;
|
// return result;
|
||||||
};
|
// };
|
||||||
|
|
||||||
const getTokens = (prompt: string, model: TiktokenModel) => {
|
// const getTokens = (prompt: string, model: TiktokenModel) => {
|
||||||
const encoder = encoding_for_model(model);
|
// const encoder = encoding_for_model(model);
|
||||||
const tokens = encoder.encode(prompt).length;
|
// const tokens = encoder.encode(prompt).length;
|
||||||
// Free the encoder to avoid possible memory leaks.
|
// // Free the encoder to avoid possible memory leaks.
|
||||||
encoder.free();
|
// encoder.free();
|
||||||
return tokens;
|
// return tokens;
|
||||||
};
|
// };
|
||||||
|
|
||||||
export const generateCommitMessage = async (
|
export const generateCommitMessage = async (
|
||||||
apiKey: string,
|
apiKey: string,
|
||||||
@@ -198,12 +197,6 @@ export const generateCommitMessage = async (
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
// Padded by 5 for more room for the completion.
|
|
||||||
const stringFromLength = generateStringFromLength(maxLength + 5);
|
|
||||||
|
|
||||||
// The token limit is shared between the prompt and the completion.
|
|
||||||
const maxTokens = getTokens(stringFromLength + prompt, model);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const completion = await createChatCompletion(
|
const completion = await createChatCompletion(
|
||||||
apiKey,
|
apiKey,
|
||||||
@@ -214,7 +207,7 @@ export const generateCommitMessage = async (
|
|||||||
top_p: 1,
|
top_p: 1,
|
||||||
frequency_penalty: 0,
|
frequency_penalty: 0,
|
||||||
presence_penalty: 0,
|
presence_penalty: 0,
|
||||||
max_tokens: maxTokens,
|
max_tokens: 200,
|
||||||
stream: false,
|
stream: false,
|
||||||
n: completions,
|
n: completions,
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user