-
Notifications
You must be signed in to change notification settings - Fork 378
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: max-length
config
#194
Merged
privatenumber
merged 11 commits into
Nutlope:develop
from
rocktimsaikia:max-length-flag
Apr 25, 2023
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a9e95e6
feat: length config
rocktimsaikia 9986526
Update default commit message length to 80 chars
rocktimsaikia 6e34e8b
Add function to calculate the number of tokens
rocktimsaikia c166d02
add comments
rocktimsaikia 2ab697c
accept length of characters not tokens
rocktimsaikia d2c6dab
create test
rocktimsaikia 268fe94
refactors
rocktimsaikia b59cc56
wip
privatenumber b555433
Merge branch 'develop' of github.com:Nutlope/aicommits into max-lengt…
privatenumber 67a72f3
rename to max-length
privatenumber add8d7e
wip
privatenumber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
import https from 'https'; | ||
import type { ClientRequest, IncomingMessage } from 'http'; | ||
import type { CreateChatCompletionRequest, CreateChatCompletionResponse } from 'openai'; | ||
import { type TiktokenModel } from '@dqbd/tiktoken'; | ||
import { | ||
TiktokenModel, | ||
// eslint-disable-next-line camelcase | ||
encoding_for_model, | ||
} from '@dqbd/tiktoken'; | ||
import createHttpsProxyAgent from 'https-proxy-agent'; | ||
import { KnownError } from './error.js'; | ||
|
||
|
@@ -100,18 +104,42 @@ const sanitizeMessage = (message: string) => message.trim().replace(/[\n\r]/g, ' | |
|
||
const deduplicateMessages = (array: string[]) => Array.from(new Set(array)); | ||
|
||
const getPrompt = (locale: string, diff: string) => `Write a git commit message in present tense for the following diff without prefacing it with anything. Do not be needlessly verbose and make sure the answer is concise and to the point. The response must be in the language ${locale}:\n${diff}`; | ||
const getPrompt = (locale: string, diff: string, length: number) => `Write a git commit message in present tense for the following diff without prefacing it with anything. Do not be needlessly verbose and make sure the answer is concise and to the point. The response must be no longer than ${length} characters. The response must be in the language ${locale}:\n${diff}`; | ||
|
||
const generateStringFromLength = (length: number) => { | ||
let result = ''; | ||
const highestTokenChar = 'z'; | ||
for (let i = 0; i < length; i += 1) { | ||
result += highestTokenChar; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the highest token character? I think we can just use that instead of generating a random value every time. |
||
return result; | ||
}; | ||
|
||
const getTokens = (prompt: string, model: TiktokenModel) => { | ||
const encoder = encoding_for_model(model); | ||
const tokens = encoder.encode(prompt).length; | ||
// Free the encoder to avoid possible memory leaks. | ||
encoder.free(); | ||
return tokens; | ||
}; | ||
|
||
export const generateCommitMessage = async ( | ||
apiKey: string, | ||
model: TiktokenModel, | ||
locale: string, | ||
diff: string, | ||
completions: number, | ||
length: number, | ||
timeout: number, | ||
proxy?: string, | ||
) => { | ||
const prompt = getPrompt(locale, diff); | ||
const prompt = getPrompt(locale, diff, length); | ||
|
||
// Padded by 5 for more room for the completion. | ||
const stringFromLength = generateStringFromLength(length + 5); | ||
|
||
// The token limit is shared between the prompt and the completion. | ||
const maxTokens = getTokens(stringFromLength + prompt, model); | ||
|
||
try { | ||
const completion = await createChatCompletion( | ||
|
@@ -126,7 +154,7 @@ export const generateCommitMessage = async ( | |
top_p: 1, | ||
frequency_penalty: 0, | ||
presence_penalty: 0, | ||
max_tokens: 200, | ||
max_tokens: maxTokens, | ||
stream: false, | ||
n: completions, | ||
}, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we pad it by 5 in the prompt limit(
${length}
) too? RN we are only padding it in char to token conversion. But padding here might result in 50+ characters in a commit message eventually.