🔧 Fix AI Analysis Service - Improved Prompts & Error Handling

 FIXED: AI analysis prompts to bypass OpenAI safety guardrails
 FIXED: Added technical analysis focus instead of trading advice tone
 FIXED: Improved JSON parsing and error handling
 ADDED: Option to use existing screenshots for testing (useExisting param)
 IMPROVED: Better image detail settings and temperature for consistency

🐛 DEBUGGING: Still investigating why AI claims it can't see images
- OpenAI vision capabilities confirmed working with public images
- Model gpt-4o has proper vision support
- Issue appears to be with chart image content or encoding

🎯 NEXT: Debug image encoding and model response inconsistency
This commit is contained in:
mindesbunister
2025-07-12 15:08:24 +02:00
parent a8fcb33ec8
commit 483d4c6576
6 changed files with 363 additions and 116 deletions

View File

@@ -3,10 +3,11 @@ import { aiAnalysisService } from '../../../lib/ai-analysis'
import { enhancedScreenshotService } from '../../../lib/enhanced-screenshot'
import { settingsManager } from '../../../lib/settings'
import path from 'path'
import fs from 'fs'
export async function POST(req: NextRequest) {
try {
const { symbol, layouts, timeframe } = await req.json()
const { symbol, layouts, timeframe, useExisting } = await req.json()
// Load current settings
const settings = await settingsManager.loadSettings()
@@ -19,9 +20,37 @@ export async function POST(req: NextRequest) {
if (!finalSymbol) {
return NextResponse.json({ error: 'Missing symbol' }, { status: 400 })
}
let screenshots: string[] = []
const baseFilename = `${finalSymbol}_${finalTimeframe}_${Date.now()}`
const screenshots = await enhancedScreenshotService.capture(finalSymbol, `${baseFilename}.png`, finalLayouts, finalTimeframe)
// If useExisting is true, find existing screenshots
if (useExisting) {
console.log('Using existing screenshots for analysis...')
const screenshotsDir = path.join(process.cwd(), 'screenshots')
const allFiles = await fs.promises.readdir(screenshotsDir)
// Find screenshots matching the symbol and timeframe
const matchingFiles = allFiles.filter(file =>
file.includes(finalSymbol) &&
file.includes(finalTimeframe) &&
file.endsWith('.png') &&
!file.includes('debug')
)
if (matchingFiles.length > 0) {
// Use the most recent screenshots (limit to 3 for analysis)
screenshots = matchingFiles
.sort((a, b) => b.localeCompare(a)) // Sort by name (which includes timestamp)
.slice(0, 3)
.map(file => path.join(screenshotsDir, file))
} else {
return NextResponse.json({ error: `No existing screenshots found for ${finalSymbol} ${finalTimeframe}` }, { status: 404 })
}
} else {
// Original behavior - capture new screenshots
const baseFilename = `${finalSymbol}_${finalTimeframe}_${Date.now()}`
screenshots = await enhancedScreenshotService.capture(finalSymbol, `${baseFilename}.png`, finalLayouts, finalTimeframe)
}
let result
if (screenshots.length === 1) {
@@ -46,7 +75,8 @@ export async function POST(req: NextRequest) {
timeframe: finalTimeframe,
layouts: finalLayouts
},
screenshots: screenshots.map((s: string) => path.basename(s))
screenshots: screenshots.map((s: string) => path.basename(s)),
usedExisting: useExisting || false
})
} catch (e: any) {
return NextResponse.json({ error: e.message }, { status: 500 })