-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
57 additions
and
158 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,65 +1,41 @@ | ||
import { OPEN_SAUCED_AI_PR_DESCRIPTION_ENDPOINT } from "../../constants"; | ||
import type { DescriptionTone } from "./descriptionconfig"; | ||
import { OpenAI, CreateChatCompletionRequest } from "openai-streams"; | ||
|
||
const generatePrompt = ( | ||
locale: string, | ||
maxLength: number, | ||
tone: DescriptionTone, | ||
) => [ | ||
`Generate an apt github PR description written in present tense and ${tone} tone for the given code diff/commit-messages with the specifications mentioned below`, | ||
`Description language: ${locale}`, | ||
`Description must be a maximum of ${maxLength} characters.`, | ||
"Exclude anything unnecessary such as translation. Your entire response will be passed directly into a pull request description", | ||
].join("\n"); | ||
|
||
const createChatCompletion = async ( | ||
apiKey: string, | ||
json: CreateChatCompletionRequest, | ||
): Promise<ReadableStream<Uint8Array>> => { | ||
const stream = await OpenAI("chat", json, { | ||
apiKey, | ||
mode: "tokens", | ||
}); | ||
|
||
return stream; | ||
}; | ||
|
||
export const generateDescription = async ( | ||
apiKey: string, | ||
model: "gpt-3.5-turbo" | "gpt-3.5-turbo-0301", | ||
locale: string, | ||
maxLength: number, | ||
language: string, | ||
descriptionLength: number, | ||
temperature: number, | ||
tone: DescriptionTone, | ||
diff?: string, | ||
commitMessages?: string[], | ||
) => { | ||
const content = `${diff ? `Diff: ${diff}\n` : ""}${commitMessages ? `\nCommit Messages: ${commitMessages.join(",")}` : ""}`; | ||
): Promise<string | undefined> => { | ||
|
||
try { | ||
const completion = await createChatCompletion( | ||
apiKey, | ||
{ | ||
model, | ||
messages: [ | ||
{ | ||
role: "system", | ||
content: generatePrompt(locale, maxLength, tone), | ||
}, | ||
{ | ||
role: "user", | ||
content, | ||
}, | ||
], | ||
temperature, | ||
n: 1, | ||
const response = await fetch(OPEN_SAUCED_AI_PR_DESCRIPTION_ENDPOINT, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
"Authorization": `Bearer ${apiKey}` | ||
}, | ||
); | ||
body: JSON.stringify({ | ||
descriptionLength, | ||
temperature, | ||
tone, | ||
language, | ||
diff, | ||
commitMessages | ||
}) | ||
}); | ||
if (response.status === 201) { | ||
const { description } = await response.json(); | ||
return description; | ||
} | ||
|
||
return completion; | ||
} catch (error: unknown) { | ||
if (error instanceof Error) { | ||
console.error("OpenAI error: ", error.message); | ||
} | ||
console.error("OpenAI error: ", error.message); | ||
} | ||
} | ||
return undefined; | ||
}; |
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
Oops, something went wrong.