|
| 1 | +const core = require("@actions/core"); |
1 | 2 | const { OpenAI } = require('openai'); |
2 | 3 | const BaseAIAgent = require("./base-ai-agent"); |
3 | 4 |
|
4 | | -const c_max_completion_tokens = 8192; |
5 | | - |
6 | 5 | class OpenAIAgent extends BaseAIAgent { |
7 | | - constructor(apiKey, fileContentGetter, fileCommentator, model) { |
| 6 | + constructor(apiKey, fileContentGetter, fileCommentator, model, baseURL = null) { |
8 | 7 | super(apiKey, fileContentGetter, fileCommentator, model); |
9 | | - this.openai = new OpenAI({ apiKey }); |
| 8 | + |
| 9 | + if (baseURL == null || baseURL === undefined || baseURL.trim() === '' ) { |
| 10 | + core.info("Using default OpenAI API URL"); |
| 11 | + this.openai = new OpenAI({ apiKey}); |
| 12 | + } |
| 13 | + else { |
| 14 | + core.info(`Using custom baseUrl: ${baseURL}`); |
| 15 | + this.openai = new OpenAI({ apiKey : apiKey, baseURL : baseURL}); |
| 16 | + } |
| 17 | + |
10 | 18 | this.tools = [ |
11 | 19 | { |
12 | 20 | type: "function", |
@@ -128,14 +136,23 @@ class OpenAIAgent extends BaseAIAgent { |
128 | 136 | } |
129 | 137 |
|
130 | 138 | // Proceed to the next API call |
131 | | - const nextResponse = await this.openai.chat.completions.create({ |
132 | | - model: this.model, |
133 | | - messages: [{ role: 'system', content: this.getSystemPrompt() }, ...reviewState.messageHistory], |
134 | | - tools: this.tools, |
135 | | - max_completion_tokens: c_max_completion_tokens |
136 | | - }); |
137 | | - const nextMessage = nextResponse.choices[0].message; |
138 | | - return await this.handleMessageResponse(nextMessage, reviewState); |
| 139 | + try { |
| 140 | + core.info(`Sending follow-up request to OpenAI with model: ${this.model}`); |
| 141 | + const nextResponse = await this.openai.chat.completions.create({ |
| 142 | + model: this.model, |
| 143 | + messages: [{ role: 'system', content: this.getSystemPrompt() }, ...reviewState.messageHistory], |
| 144 | + tools: this.tools |
| 145 | + }); |
| 146 | + const nextMessage = nextResponse.choices[0].message; |
| 147 | + return await this.handleMessageResponse(nextMessage, reviewState); |
| 148 | + } catch (error) { |
| 149 | + core.error(`OpenAI API error in follow-up request: ${error.message}`); |
| 150 | + if (error.response) { |
| 151 | + core.error(`Status: ${error.response.status}`); |
| 152 | + core.error(`Data: ${JSON.stringify(error.response.data)}`); |
| 153 | + } |
| 154 | + throw error; |
| 155 | + } |
139 | 156 | } else { |
140 | 157 | if (message.content && !reviewState.summary) { |
141 | 158 | reviewState.summary = message.content; |
@@ -173,15 +190,24 @@ class OpenAIAgent extends BaseAIAgent { |
173 | 190 |
|
174 | 191 | reviewState.messageHistory.push(initialUserMessage); |
175 | 192 |
|
176 | | - const initialResponse = await this.openai.chat.completions.create({ |
177 | | - model: this.model, |
178 | | - messages: [{ role: 'system', content: this.getSystemPrompt() }, ...reviewState.messageHistory], |
179 | | - tools: this.tools, |
180 | | - max_completion_tokens: c_max_completion_tokens |
181 | | - }); |
182 | | - const initialMessage = initialResponse.choices[0].message; |
183 | | - reviewSummary = await this.handleMessageResponse(initialMessage, reviewState); |
184 | | - return reviewSummary; |
| 193 | + try { |
| 194 | + core.info(`Sending initial request to OpenAI with model: ${this.model}`); |
| 195 | + const initialResponse = await this.openai.chat.completions.create({ |
| 196 | + model: this.model, |
| 197 | + messages: [{ role: 'system', content: this.getSystemPrompt() }, ...reviewState.messageHistory], |
| 198 | + tools: this.tools |
| 199 | + }); |
| 200 | + const initialMessage = initialResponse.choices[0].message; |
| 201 | + reviewSummary = await this.handleMessageResponse(initialMessage, reviewState); |
| 202 | + return reviewSummary; |
| 203 | + } catch (error) { |
| 204 | + core.error(`OpenAI API error: ${error.message}`); |
| 205 | + if (error.response) { |
| 206 | + core.error(`Status: ${error.response.status}`); |
| 207 | + core.error(`Data: ${JSON.stringify(error.response.data)}`); |
| 208 | + } |
| 209 | + throw error; |
| 210 | + } |
185 | 211 | } |
186 | 212 |
|
187 | 213 | async getFileContent(args) { |
|
0 commit comments