Skip to content

Commit

Permalink
Merge pull request #99 from ca-dp/add-model-select
Browse files Browse the repository at this point in the history
Add commands for selecting OpenAI models
  • Loading branch information
ouchi2501 authored Nov 21, 2023
2 parents 574273b + bc36699 commit 3ae2477
Show file tree
Hide file tree
Showing 9 changed files with 9,556 additions and 3,316 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/code-butler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
cmd: review
model: gpt-4-1106-preview
chat:
if: startsWith(github.event.comment.body, '/chat')
runs-on: ubuntu-latest
Expand All @@ -27,4 +28,5 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
cmd: chat
model: gpt-4-1106-preview
comment_body: ${{ github.event.comment.body }}
38 changes: 38 additions & 0 deletions __tests__/ai.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,42 @@ describe('completionRequest', () => {
)
consoleLogSpy.mockRestore()
})

it('model gpt4 should be used', async () => {
const apiKey = 'test-api-key'
const systemPrompt = 'System prompt'
const userPrompt = 'User prompt'
const model = 'gpt-4'

// Mock the OpenAI API request
// Mock the OpenAI API request
nock('https://api.openai.com')
.post('/v1/chat/completions', {
messages: [
{ role: 'system', content: systemPrompt },
{ role: 'user', content: userPrompt }
],
model: 'gpt-4'
})
.reply(200, {
choices: [
{
message: {
content: 'Expected completed content'
}
}
]
})

// Call the completionRequest function to test it
const result = await completionRequest(
apiKey,
systemPrompt,
userPrompt,
model
)

// Assert based on the expected result
expect(result).toBe('Expected completed content')
})
})
8 changes: 6 additions & 2 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ describe('run', () => {
return 'mocked-api-key'
case 'cmd':
return 'review'
case 'model':
return 'mock ai model'
default:
return ''
}
Expand All @@ -61,7 +63,8 @@ describe('run', () => {
expect(completionRequestMock).toHaveBeenCalledWith(
'mocked-api-key',
'Mocked system prompt',
'Mocked PR diff'
'Mocked PR diff',
'mock ai model'
)
expect(createGitHubCommentMock).toHaveBeenCalledWith('Mocked AI response')
})
Expand Down Expand Up @@ -159,7 +162,8 @@ describe('run', () => {
expect(completionRequestMock).toHaveBeenCalledWith(
'mocked-api-key',
'Mocked system prompt',
'Mocked issue comment'
'Mocked issue comment',
''
)
expect(createGitHubCommentMock).toHaveBeenCalledWith('Mocked AI response')
})
Expand Down
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12,764 changes: 9,471 additions & 3,293 deletions dist/index.js

Large diffs are not rendered by default.

42 changes: 27 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"@actions/core": "^1.10.1",
"@actions/github": "^5.1.1",
"js-tiktoken": "^1.0.7",
"openai": "^4.10.0"
"openai": "^4.19.1"
},
"devDependencies": {
"@types/jest": "^29.5.8",
Expand Down
6 changes: 4 additions & 2 deletions src/ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ import OpenAI from 'openai'
export async function completionRequest(
apiKey: string,
systemPrompt: string,
userPrompt: string
userPrompt: string,
model: string | undefined = undefined
): Promise<string> {
try {
const openai = new OpenAI({ apiKey })
const requestTimeout = 300 * 1000 // 5 minutes
const targetModel = model || 'gpt-3.5-turbo'
const response = await openai.chat.completions.create(
{
messages: [
{ role: 'system', content: systemPrompt },
{ role: 'user', content: userPrompt }
],
model: 'gpt-3.5-turbo'
model: targetModel
},
{
timeout: requestTimeout
Expand Down
8 changes: 6 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ export async function run(): Promise<void> {

for (const group of groups) {
const diffToSend = group.join('')
const model = core.getInput('model', { required: false })
const messagePromise = ai.completionRequest(
core.getInput('OPENAI_API_KEY', { required: true }),
sysPrompt,
diffToSend
diffToSend,
model
)
const message = await messagePromise

Expand All @@ -46,10 +48,12 @@ export async function run(): Promise<void> {
}

const chatSystemPrompt = prompt.getChatSystemPrompt()
const model = core.getInput('model', { required: false })
const responseMessage = ai.completionRequest(
core.getInput('OPENAI_API_KEY', { required: true }),
chatSystemPrompt,
comment
comment,
model
)
const response = await responseMessage
if (response === '') {
Expand Down

0 comments on commit 3ae2477

Please sign in to comment.