Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use new gpt-3.5-turbo model #123

Merged
merged 5 commits into from
Mar 10, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/utils/openai.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import https from 'https';
import type { CreateCompletionRequest, CreateCompletionResponse } from 'openai';
import type { CreateChatCompletionRequest, CreateChatCompletionResponse } from 'openai';
import { encoding_for_model as encodingForModel } from '@dqbd/tiktoken';
import { KnownError } from './error.js';

const createCompletion = (
apiKey: string,
json: CreateCompletionRequest,
) => new Promise<CreateCompletionResponse>((resolve, reject) => {
json: CreateChatCompletionRequest,
) => new Promise<CreateChatCompletionResponse>((resolve, reject) => {
const postContent = JSON.stringify(json);
const request = https.request(
{
port: 443,
hostname: 'api.openai.com',
path: '/v1/completions',
path: '/v1/chat/completions',
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down Expand Up @@ -60,8 +60,9 @@ const deduplicateMessages = (array: string[]) => Array.from(new Set(array));

const getPrompt = (locale: string, diff: string) => `Write an insightful but concise Git commit message in a complete sentence in present tense for the following diff without prefacing it with anything, the response must be in the language ${locale}:\n${diff}`;

const model = 'text-davinci-003';
const encoder = encodingForModel(model);
const model = 'gpt-3.5-turbo';
// TODO: update for the new gpt-3.5 model
const encoder = encodingForModel('text-davinci-003');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was waiting for the new one to be released but I guess there's no point in blocking the feature.


export const generateCommitMessage = async (
apiKey: string,
Expand All @@ -82,7 +83,10 @@ export const generateCommitMessage = async (
try {
const completion = await createCompletion(apiKey, {
model,
prompt,
messages: [{
role: 'user',
content: prompt,
}],
temperature: 0.7,
top_p: 1,
frequency_penalty: 0,
Expand All @@ -94,7 +98,7 @@ export const generateCommitMessage = async (

return deduplicateMessages(
completion.choices
.map(choice => sanitizeMessage(choice.text!)),
.map(choice => sanitizeMessage(choice.message?.content ?? 'No Commit Message')),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should throw an error instead of falling back to a made up commit message.

);
} catch (error) {
const errorAsAny = error as any;
Expand Down