chatgpt_completions 1.0.4 copy "chatgpt_completions: ^1.0.4" to clipboard
chatgpt_completions: ^1.0.4 copied to clipboard

Dart client for the unofficial ChatGPT API. Support Text Completion and stream response from v1/completions.

example/lib/main.dart

// ignore_for_file: avoid_print

import 'dart:async';

import 'package:chatgpt_completions/chatgpt_completions.dart';

void main() async {
  /// Generate api key from openai console: https://platform.openai.com/account/api-keys
  ChatGPTCompletions.instance.initialize(apiKey: '<openai_api_key>');

  print("Generating answer without stream...");

  // Text completions without stream response (stream: false)
  String? responseWithoutStream =
      await ChatGPTCompletions.instance.textCompletions(TextCompletionsParams(
    prompt: "What's Flutter?",
    model: GPTModel.davinci,
    stream: true,
  ));

  print("OpenAI: $responseWithoutStream");

  print("\n\n-> Generating answer with stream...");
  await Future.delayed(const Duration(seconds: 2));

  // Text completions with stream response (stream: true)
  String responseWithStream = "";

  // If you want implement feature "stop generating", need record stream subcription when you want to stop.
  // Let call responseSubscription?.cancel();

  StreamSubscription? responseSubscription;

  await ChatGPTCompletions.instance.textCompletions(
    TextCompletionsParams(
      prompt: "What's Flutter?",
      model: GPTModel.davinci,
    ),
    onStreamValue: (characters) {
      responseWithStream += characters;
      print(responseWithStream);
    },
    onStreamCreated: (subscription) {
      responseSubscription = subscription;
    },
    // Debounce 200ms for receive next value
    debounce: const Duration(milliseconds: 200),
  );

  // Stop generating
  responseSubscription?.cancel();

  // Using Chat Completion API: GPT-3.5-Turbo, GPT-4,...
  await ChatGPTCompletions.instance.textCompletions(
    TextCompletionsParams(
      // prompt: "What's Flutter?",
      messagesTurbo: [
        MessageTurbo(
          role: TurboRole.user,
          content: "Where is the tallest building in the world?",
        ),
      ],
      model: GPTModel.gpt3p5turbo,
    ),
    onStreamValue: (characters) {
      responseWithStream += characters;
      print(characters);
    },
    onStreamCreated: (subscription) {
      responseSubscription = subscription;
    },
    // Debounce 100ms for receive next value
    debounce: const Duration(milliseconds: 100),
  );
}
40
likes
130
pub points
74%
popularity

Publisher

verified publisherwaterbus.tech

Dart client for the unofficial ChatGPT API. Support Text Completion and stream response from v1/completions.

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

dio, flutter, rxdart

More

Packages that depend on chatgpt_completions