mirror of
				https://github.com/freeedcom/ai-codereviewer.git
				synced 2025-10-31 06:00:53 +00:00 
			
		
		
		
	Add fix to parse json object from GPT-3.5 response
This commit is contained in:
		
					parent
					
						
							
								62c5563c93
							
						
					
				
			
			
				commit
				
					
						fcd39ade07
					
				
			
		
					 3 changed files with 22 additions and 8 deletions
				
			
		
							
								
								
									
										14
									
								
								dist/index.js
									
										
									
									
										vendored
									
									
								
							
							
						
						
									
										14
									
								
								dist/index.js
									
										
									
									
										vendored
									
									
								
							|  | @ -135,7 +135,7 @@ ${chunk.changes | ||||||
| `;
 | `;
 | ||||||
| } | } | ||||||
| function getAIResponse(prompt) { | function getAIResponse(prompt) { | ||||||
|     var _a, _b; |     var _a, _b, _c; | ||||||
|     return __awaiter(this, void 0, void 0, function* () { |     return __awaiter(this, void 0, void 0, function* () { | ||||||
|         const queryConfig = { |         const queryConfig = { | ||||||
|             model: OPENAI_API_MODEL, |             model: OPENAI_API_MODEL, | ||||||
|  | @ -154,14 +154,20 @@ function getAIResponse(prompt) { | ||||||
|                 ] })); |                 ] })); | ||||||
|             // Log the raw response for debugging
 |             // Log the raw response for debugging
 | ||||||
|             console.log('Raw response:', JSON.stringify(response, null, 2)); |             console.log('Raw response:', JSON.stringify(response, null, 2)); | ||||||
|             const res = ((_b = (_a = response.choices[0].message) === null || _a === void 0 ? void 0 : _a.content) === null || _b === void 0 ? void 0 : _b.trim()) || "{}"; |             const res = ((_b = (_a = response.choices[0].message) === null || _a === void 0 ? void 0 : _a.content) === null || _b === void 0 ? void 0 : _b.trim()) || ""; | ||||||
|  |             // Extract JSON content from Markdown code block
 | ||||||
|  |             const jsonContent = (_c = res.match(/```json([\s\S]*)```/)) === null || _c === void 0 ? void 0 : _c[1]; | ||||||
|  |             if (!jsonContent) { | ||||||
|  |                 console.error("Failed to extract JSON content from response."); | ||||||
|  |                 return null; | ||||||
|  |             } | ||||||
|             // Attempt to parse JSON
 |             // Attempt to parse JSON
 | ||||||
|             try { |             try { | ||||||
|                 return JSON.parse(res).reviews; |                 return JSON.parse(jsonContent).reviews; | ||||||
|             } |             } | ||||||
|             catch (e) { |             catch (e) { | ||||||
|                 console.error("Failed to parse JSON:", e); |                 console.error("Failed to parse JSON:", e); | ||||||
|                 console.error("Response content:", res); |                 console.error("Response content:", jsonContent); | ||||||
|                 return null; |                 return null; | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  |  | ||||||
							
								
								
									
										2
									
								
								dist/index.js.map
									
										
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								dist/index.js.map
									
										
									
									
										vendored
									
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
							
								
								
									
										14
									
								
								src/main.ts
									
										
									
									
									
								
							
							
						
						
									
										14
									
								
								src/main.ts
									
										
									
									
									
								
							|  | @ -137,14 +137,22 @@ async function getAIResponse(prompt: string): Promise<Array<{ | ||||||
|     // Log the raw response for debugging
 |     // Log the raw response for debugging
 | ||||||
|     console.log('Raw response:', JSON.stringify(response, null, 2)); |     console.log('Raw response:', JSON.stringify(response, null, 2)); | ||||||
| 
 | 
 | ||||||
|     const res = response.choices[0].message?.content?.trim() || "{}"; |     const res = response.choices[0].message?.content?.trim() || ""; | ||||||
|  |      | ||||||
|  |     // Extract JSON content from Markdown code block
 | ||||||
|  |     const jsonContent = res.match(/```json([\s\S]*)```/)?.[1]; | ||||||
|  | 
 | ||||||
|  |     if (!jsonContent) { | ||||||
|  |       console.error("Failed to extract JSON content from response."); | ||||||
|  |       return null; | ||||||
|  |     } | ||||||
| 
 | 
 | ||||||
|     // Attempt to parse JSON
 |     // Attempt to parse JSON
 | ||||||
|     try { |     try { | ||||||
|       return JSON.parse(res).reviews; |       return JSON.parse(jsonContent).reviews; | ||||||
|     } catch (e) { |     } catch (e) { | ||||||
|       console.error("Failed to parse JSON:", e); |       console.error("Failed to parse JSON:", e); | ||||||
|       console.error("Response content:", res); |       console.error("Response content:", jsonContent); | ||||||
|       return null; |       return null; | ||||||
|     } |     } | ||||||
|   } catch (error) { |   } catch (error) { | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue