Created with <3 with dartpad.dev.
Created
January 8, 2023 03:47
-
-
Save pgainullin/58d576ced8377a6949e0380abde9ad0c to your computer and use it in GitHub Desktop.
sparkling-utopia-7418
This file contains 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
import 'package:http/http.dart' as http; | |
import 'dart:convert'; | |
void main() async { | |
const String apiKey ='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'; | |
ChatbotClient chatbot = ChatbotClient(apiKey, 'text-davinci-003'); | |
String response = await chatbot.getResponse('who are you?'); | |
print('resp:' + response); // Outputs the chatbot's response | |
} | |
class ChatbotClient { | |
final String _apiKey; | |
final String _model; | |
ChatbotClient(this._apiKey, this._model); | |
Future<String> getResponse(String input) async { | |
var response = await http.post(Uri.parse('https://api.openai.com/v1/completions') | |
, | |
headers: { | |
'Content-Type': 'application/json', | |
'Authorization': 'Bearer $_apiKey', | |
}, | |
body: json.encode({ | |
'model': _model, | |
'prompt': input, | |
'max_tokens': 2048, | |
'temperature': 0.5, | |
}), | |
); | |
if (response.statusCode == 200) { | |
return response.body; | |
} else { | |
throw Exception('Failed to get response from the OpenAI API'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment