Skip to content

Instantly share code, notes, and snippets.

@SametSahin10
Created October 21, 2024 21:04
Show Gist options
  • Save SametSahin10/d58efeca3ddc9b4fad31313131dcd4d7 to your computer and use it in GitHub Desktop.
Save SametSahin10/d58efeca3ddc9b4fad31313131dcd4d7 to your computer and use it in GitHub Desktop.
Future<Map<String, dynamic>> _analyzeWithGPT(String text) async {
final response = await http.post(
Uri.parse('https://api.openai.com/v1/chat/completions'),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer $apiKey',
},
body: jsonEncode({
'model': 'gpt-4o-mini',
'messages': [
{
'role': 'system',
'content':
'You are a helpful assistant that analyzes food product ingredients.',
},
{
'role': 'user',
'content':
'Analyze the following ingredients: $text. Answer these questions in the following format:\n\nIs gluten-free: [result]\nIs halal: [result]\nIs lactose-intolerant friendly: [result]\nIs vegan: [result]\nIs vegetarian: [result]\nIs healthy: [result]\nContains palm oil: [result]',
},
],
}),
);
if (response.statusCode == 200) {
return response.body;
} else {
throw Exception('Failed to analyze with GPT');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment