vit_gpt_dart_api 6.1.0 copy "vit_gpt_dart_api: ^6.1.0" to clipboard
vit_gpt_dart_api: ^6.1.0 copied to clipboard

A streamlined Dart package for accessing OpenAI's API directly from your Dart and Flutter applications. This client simplifies the integration of powerful AI technologies with features including model [...]

example/vit_gpt_dart_api_example.dart

import 'dart:async';

import 'package:vit_gpt_dart_api/factories/create_completion_repository.dart';
import 'package:vit_gpt_dart_api/vit_gpt_dart_api.dart';

Future<void> main() async {
  String token = 'MY OPEN AI TOKEN';
  await updateApiToken(token);

  // Simple response from AI

  CompletionModel completion = createCompletionRepository();

  var newMessage = await completion.fetch(
    previousMessages: [
      Message.user(
        message: 'How much is 2 + 2?',
      ),
    ],
  );

  print(newMessage.text);

  // Reading response as stream

  var stream = completion.fetchStream(
    previousMessages: [
      Message.user(
        message: 'How much is 2 + 2?',
      ),
    ],
  );

  await for (var chunk in stream) {
    print(chunk);
  }

  // Using custom completion model

  DynamicFactories.completion = () => CustomCompletionModel();

  completion = createCompletionRepository();

  newMessage = await completion.fetch(
    previousMessages: [
      Message.user(
        message: 'How much is 2 + 2?',
      ),
    ],
  );
  print(newMessage.text);
}

class CustomCompletionModel extends CompletionModel {
  @override
  // Only relevant if working with OpenAI threads and using the assistant run.
  bool get addsPreviousMessagesToThread => false;

  @override
  // Only relevant if working with OpenAI threads and using the assistant run.
  bool get addsResponseAutomatically => false;

  @override
  Future<Message> fetch({List<Message>? previousMessages}) async {
    return Message.assistant(
      message: 'This is a custom response',
    );
  }

  @override
  Stream<String> fetchStream(
      {List<Message>? previousMessages,
      int retries = 2,
      FutureOr<void> Function(CompletionException error, int retriesRemaning)?
          onError,
      void Function(Map<String, dynamic> chunk)? onJsonComplete}) {
    // TODO: implement fetchStream
    throw UnimplementedError();
  }
}
0
likes
130
points
465
downloads

Publisher

unverified uploader

Weekly Downloads

A streamlined Dart package for accessing OpenAI's API directly from your Dart and Flutter applications. This client simplifies the integration of powerful AI technologies with features including model customization, interactive assistants, conversation management, streaming support, and persistent configurations to maintain session continuity.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

dio, logger, vit_dart_extensions, web_socket_channel

More

Packages that depend on vit_gpt_dart_api