openai_dart_dio 2.1.0 copy "openai_dart_dio: ^2.1.0" to clipboard
openai_dart_dio: ^2.1.0 copied to clipboard

| `openai_dart_dio` is a type-safe Dart library for interacting with the OpenAI API, built on the Dio HTTP client. It simplifies making requests to OpenAI services like GPT-3, DALL·E, and Codex, with [...]

OpenAi dart sdk use dio #

Features #

  • ✅ chat completion
    • ✅ chat
    • ✅ chat stream
    • ✅ chat image
  • ✅ models
    • ✅ list models
    • ✅ retrieve models
  • ✅ images
    • ✅ images generations
    • ✅ images edits
    • ✅ images variations
  • ✅ audio
    • ✅ audio speech
    • ✅ audio transcriptions
    • ✅ audio translations

Getting started #

dart pub add openai_dart_dio

Usage #

Example:

import 'dart:io';

import 'package:openai_dart_dio/src/api/openai_client.dart';
import 'package:openai_dart_dio/src/model/chat/request/chat_completion_request.dart';
import 'package:openai_dart_dio/src/model/chat/request/chat_message/chat_message.dart';

Future<void> main() async {
  print("Type Api Key please: ");
  final apiKey = stdin.readLineSync()!;

  final client = OpenAiClient(apiKey: apiKey);

  print("""
Choice function please: 
1. Chat Not Stream
2. Chat Stream
3  Chat Image""");
  final option = int.parse(stdin.readLineSync()!);
  switch (option) {
    case 1:
      chatNotStream(client);
    case 2:
      chatStream(client);
    case 3:
      chatImage(client);
  }
}

chatNotStream(OpenAiClient client) async {
  final messages = [
    ChatMessage(role: ChatMessageRole.user, content: "Write a 200 word novel")
  ];

  final request =
  ChatCompletionRequest(messages: messages, model: "gpt-3.5-turbo-1106");
  final resp = await client.chatCompletionApi.createChatCompletion(request);
  print(resp.toJson());
  print(resp.choices.first.message.content);
}

chatStream(OpenAiClient client) async {
  final messages = [
    ChatMessage(role: ChatMessageRole.user, content: "Write a 200 word novel")
  ];
  final request =
  ChatCompletionRequest(messages: messages, model: "gpt-3.5-turbo-1106");
  final stream = client.chatCompletionApi.createChatCompletionStream(request);
  stream.listen((data) {
    final content = data.choices.first.delta.content;
    if (content != null) {
      stdout.write(content);
    }
  });
}

chatImage(OpenAiClient client) async {
  // final base64Image = ImageInfo.fromBase64("base64 text");
  //
  // final file = File("path");
  //
  // final bytes = await file.readAsBytes();
  // final bytesImage = ImageInfo.fromBytes(bytes);
  //
  // final fileStream = File("path").openRead();
  // final streamImage = await ImageInfo.fromStream(fileStream);

  // image from url , http / https / data url
  final urlImage = ImageInfo(
      "https://hips.hearstapps.com/hmg-prod/images/beautiful-smooth-haired-red-cat-lies-on-the-sofa-royalty-free-image-1678488026.jpg?crop=0.88847xw:1xh;center,top&resize=1200:*");

  final messages = [
    ChatMessage(role: ChatMessageRole.user, content: [
      MessageContent.fromText("What is this picture?"),
      MessageContent.fromImage(urlImage)
    ])
  ];

  final request =
  ChatCompletionRequest(messages: messages, model: "gpt-4-vision-preview");
  final stream = client.chatCompletionApi.createChatCompletionStream(request);
  stream.listen((data) {
    final content = data.choices.first.delta.content;
    if (content != null) {
      stdout.write(content);
    }
  });
}

Developer Info #

Generate code

flutter pub run build_runner build
flutter pub run json5_model --src=lib/src/json --dist=lib/src/model  --prefix-regexp "(.+?)_response$"
flutter pub global activate index_generator
flutter pub global run index_generator
2
likes
130
points
325
downloads

Publisher

unverified uploader

Weekly Downloads

| `openai_dart_dio` is a type-safe Dart library for interacting with the OpenAI API, built on the Dio HTTP client. It simplifies making requests to OpenAI services like GPT-3, DALL·E, and Codex, with automatic JSON serialization and robust error handling. The library ensures a streamlined and efficient integration, offering strong type safety and easy-to-use API calls for Dart and Flutter developers.

Repository (GitHub)

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

copy_with_extension, dio, equatable, equatable_annotations, fetch_client, http, json_annotation, pretty_dio_logger

More

Packages that depend on openai_dart_dio