Created
October 21, 2024 21:04
-
-
Save SametSahin10/d58efeca3ddc9b4fad31313131dcd4d7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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