🔧 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

113
debug-ai-analysis.js Normal file
View File

@@ -0,0 +1,113 @@
const fs = require('fs')
const path = require('path')
const OpenAI = require('openai').default
// Initialize OpenAI
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
})
async function testAIAnalysis() {
try {
console.log('🔍 Testing AI analysis with a real screenshot...')
// Use one of the existing screenshots
const screenshotsDir = path.join(process.cwd(), 'screenshots')
const testImage = 'SOLUSD_60_1752324455391_ai.png'
const imagePath = path.join(screenshotsDir, testImage)
console.log(`📸 Using screenshot: ${imagePath}`)
// Check if file exists
if (!fs.existsSync(imagePath)) {
console.error('❌ Screenshot file not found!')
return
}
console.log('✅ Screenshot file found')
// Read and encode image
const imageBuffer = fs.readFileSync(imagePath)
const base64Image = imageBuffer.toString('base64')
console.log(`📊 Image size: ${imageBuffer.length} bytes`)
console.log(`🔒 Base64 length: ${base64Image.length} characters`)
console.log(`🎯 Base64 preview: ${base64Image.substring(0, 100)}...`)
// Improved prompt for testing
const prompt = `You are a technical chart analysis expert. Please analyze this TradingView chart image and provide objective technical analysis data.
**Important**: This is for educational and research purposes only. Please analyze the technical indicators, price levels, and chart patterns visible in the image.
Examine the chart and identify:
- Current price action and trend direction
- Key support and resistance levels visible on the chart
- Technical indicator readings (RSI, moving averages, volume if visible)
- Chart patterns or formations
- Market structure elements
Provide your analysis in this exact JSON format (replace values with your analysis):
{
"summary": "Objective description of what you observe in the chart",
"marketSentiment": "BULLISH",
"recommendation": "BUY",
"confidence": 75
}
Return only the JSON object with your technical analysis.`
console.log('🤖 Sending request to OpenAI...')
const response = await openai.chat.completions.create({
model: "gpt-4o", // Latest vision model
messages: [
{
role: "user",
content: [
{ type: "text", text: prompt },
{
type: "image_url",
image_url: {
url: `data:image/png;base64,${base64Image}`,
detail: "low" // Add detail parameter to reduce token usage
}
}
]
}
],
max_tokens: 500,
temperature: 0.1
})
console.log('✅ Response received from OpenAI')
const content = response.choices[0]?.message?.content
console.log('📝 Full response content:')
console.log(content)
// Try to extract JSON
const jsonMatch = content?.match(/\{[\s\S]*\}/)
if (jsonMatch) {
console.log('✅ JSON found in response:')
console.log(jsonMatch[0])
try {
const parsed = JSON.parse(jsonMatch[0])
console.log('✅ JSON successfully parsed:')
console.log(JSON.stringify(parsed, null, 2))
} catch (e) {
console.error('❌ Failed to parse JSON:', e.message)
}
} else {
console.error('❌ No JSON found in response')
}
} catch (error) {
console.error('❌ Error testing AI analysis:', error.message)
console.error('Full error:', error)
}
}
// Run the test
testAIAnalysis()