summarize method
Summarizes the provided text into a concise 3-line summary.
Returns a Future that completes with the summary string.
Implementation
Future<String> summarize(String text) async {
final response = await http.post(
Uri.parse(openAiEndpoint),
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer $apiKey',
},
body: jsonEncode({
'model': 'gpt-3.5-turbo',
'messages': [
{
'role': 'system',
'content': 'Summarize the following text into a 3-line summary.',
},
{
'role': 'user',
'content': text,
},
],
}),
);
if (response.statusCode == 200) {
final data = jsonDecode(response.body);
return data['choices'][0]['message']['content'].trim();
} else {
throw Exception('Failed to fetch summary: ${response.body}');
}
}