Skip to content

Instantly share code, notes, and snippets.

View Toglefritz's full-sized avatar

Scott Hatfield Toglefritz

View GitHub Profile
@Toglefritz
Toglefritz / partly_windy_get_new_forecast_completion.dart
Last active July 30, 2022 01:59
Gets a "weather forecast" from the OpenAI completions endpoint for use in the Partly Windy app.
/// Gets a "weather forecast" from the OpenAI completions endpoint
static Future<CompletionsResponse> getNewForecast() async {
debugPrint('Getting a new weather forecast');
CompletionsRequest request = CompletionsRequest(
model: 'text-curie-001',
prompt: 'Today's forecast is',
maxTokens: 9,
temperature: 0.6,
);
@Toglefritz
Toglefritz / openai_completions_headers.dart
Created July 30, 2022 01:02
The headers used in requests to the OpenAI completions endpoint.
// The headers for the completions endpoint, which are the same for all requests
static final Map<String, String> headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer $openAIApiKey',
};
@Toglefritz
Toglefritz / openai_completions_endpoint.dart
Created July 30, 2022 00:36
The POST endpoint used to interact with the OpenAI completions API.
// The completions endpoint
static final Uri completionsEndpoint = Uri.parse('https://api.openai.com/v1/completions');
@Toglefritz
Toglefritz / openai_completions_response_factory_constructor.dart
Created July 29, 2022 11:21
A factory constructor for the CompletionsResponse class in the Partly Windy app.
/// Returns a [CompletionResponse] from the JSON obtained from the
/// completions endpoint.
factory CompletionsResponse.fromResponse(Response response) {
// Get the response body in JSON format
Map<String, dynamic> responseBody = json.decode(response.body);
// Parse out information from the response
Map<String, dynamic> usage = responseBody['usage'];
// Parse out the choices
@Toglefritz
Toglefritz / openai_completions_response.dart
Last active July 29, 2022 10:56
A class in the Partly Windy app that represents a response received from the OpenAI completions endpoint.
class CompletionsResponse {
final String? id;
final String object;
final int? created;
final String? model;
final List<dynamic>? choices; // This list contains the completions
final Map<String, dynamic>? usage;
final int? promptTokens;
final int? completionTokens;
final int? totalTokens;
@Toglefritz
Toglefritz / openai_completions_request_to_json.dart
Created July 29, 2022 10:46
A method to convert a CompletionsRequest object into a JSON-formatted String to be sent in a RESTful API request.
String toJson() {
Map<String, dynamic> jsonBody = {
'model': model,
'prompt': prompt,
'max_tokens': maxTokens,
};
if (temperature != null) {
jsonBody.addAll({'temperature': temperature});
}
@Toglefritz
Toglefritz / openai_completions_request.dart
Created July 29, 2022 10:40
A class in the Partly Windy app that represents the parameters used in the body of a request to the OpenAI completions endpoint.
/// Represents the parameters used in the body of a request to the OpenAI completions endpoint.
class CompletionsRequest {
final String model;
final String prompt;
final int maxTokens;
final double? temperature;
final int? topP;
final int? n;
final bool? stream;
final int? longprobs;
@Toglefritz
Toglefritz / daily_forecast_active_view.dart
Created July 24, 2022 14:15
The view rendered by the Partly Windy app for Wear OS devices in active mode.
/// View for [DailyForecastRoute] for watch-sized devices while the watch
/// is in an active mode.
class DailyForecastViewWatchActive extends StatelessWidget {
final DailyForecastController state;
const DailyForecastViewWatchActive(this.state, {Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
@Toglefritz
Toglefritz / daily_forecast_ambient_view.dart
Last active July 24, 2022 14:07
The view rendered by the Partly Windy app for Wear OS devices while they are in ambient mode.
/// View for [DailyForecastRoute] for watch-sized devices while the watch
/// is in an ambient mode.
class DailyForecastViewWatchAmbient extends StatelessWidget {
final DailyForecastController state;
const DailyForecastViewWatchAmbient(this.state, {Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
@Toglefritz
Toglefritz / daily_forecast_wear_os_mode_detection.dart
Created July 24, 2022 13:49
A Flutter widget that loads different views for Wear OS devices depending on whether they are in ambient or active mode.
AmbientMode(
builder: (BuildContext context, WearMode mode, child) {
return mode == WearMode.active
? DailyForecastViewWatchActive(state)
: DailyForecastViewWatchAmbient(state);
},
);