generate method
Future<String>
generate(
- String prompt, {
- String systemPrompt = '',
- int maxTokens = 1024,
- bool withThinking = true,
- bool jsonResponse = false,
override
Implementation
@override
Future<String> generate(
String prompt, {
String systemPrompt = '',
int maxTokens = 1024,
bool withThinking = true,
bool jsonResponse = false,
}) async {
try {
_openAiClient.apiKey = _apiKey;
final res = await _openAiClient.createChatCompletion(
request: CreateChatCompletionRequest(
maxTokens: maxTokens,
model: ChatCompletionModel.modelId(_model),
messages: [
ChatCompletionMessage.system(content: systemPrompt),
ChatCompletionMessage.user(
content: ChatCompletionUserMessageContent.string(prompt),
),
],
),
);
return res.choices.first.message.content ?? '';
} catch (e) {
throw Exception('OpenAI API error: $e');
}
}