搜尋套件、儲存庫與 APP 分類

套件詳情v8.0.0

dart_openai

openAI Api 的 Dart SDK(GPT-3 與 DALL-E),輕鬆將 OpenAI 最先進的 AI 模型功能整合到您的 Dart 應用程式中。

AndroidiOSmacOSWindowsLinux
近 30 天下載
1.8萬
喜歡數
576
Pub 分數
160

安裝套件

flutter pub add dart_openai

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

dart_openai 套件為 Dart 應用程式提供了一個整合 OpenAI 先進 AI 模型(包含 GPT-3 和 DALL-E)的 Dart SDK。它支援與 OpenAI API 的無縫通訊,讓開發者能產生文字、創造圖像,並實作對話式 AI 功能。此套件支援多種平台,如 Android、iOS、macOS、Windows 和 Linux,非常適合跨平台應用。透過簡單明確的 API 存取與清楚的文件,它簡化了在 Flutter 及其他基於 Dart 的專案中整合生成式 AI 功能的過程。

適用情境

  • 使用 GPT-3 進行文字生成
  • 使用 DALL-E 創建圖像
  • 建構人工智慧驅動的聊天機器人
  • 將人工智慧整合到行動與桌面應用程式中
  • 建立內容自動化工具

主要特色

  • 支援 GPT-3 和 DALL-E API
  • 跨平台相容性
  • 簡單直覺的 Dart 接口
  • 即時 API 互動
  • 全面的錯誤處理

適合對象

  • Flutter 開發者
  • AI應用創作者
  • 全端開發人員
  • 初創公司正在建構人工智慧功能
  • 教育或原型專案

使用建議

  • 需要一個 OpenAI API 金鑰
  • 基於訂閱應用速率限制
  • 需要網絡連接
  • API 回應中的潛在延遲
  • 超出免費層級的使用成本

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

dart_openai

Pub Version Pub Likes Pub Points Tests License

Unofficial Dart/Flutter SDK for the OpenAI API. Typed clients for every major API surface: Responses, Chat Completions, Realtime, Videos, Batch, Fine-tuning, Vector Stores, Evals, Administration and more. Compiles and runs on every platform: Android, iOS, macOS, Linux, Windows, web, and server-side Dart.

Maintained by Anas Fikhi (@anasfik).

Installation

dependencies:
  dart_openai: ^8.0.0
dart pub get

Quickstart

import 'package:dart_openai/dart_openai.dart';

Future<void> main() async {
  final client = OpenAIClient(apiKey: Platform.environment['OPENAI_API_KEY']!);

  final completion = await client.chat.create(
    model: 'gpt-4o',
    messages: [
      const OpenAIChatCompletionChoiceMessageModel(
        role: OpenAIChatMessageRole.user,
        content: 'Say hello in five words.',
      ),
    ],
  );

  print(completion.choices.first.message.content);
}

Prefer the global facade? It still works:

OpenAI.apiKey = 'sk-...';
await OpenAI.instance.chat.create(model: 'gpt-4o', messages: [...]);

Multiple clients and compatible providers

Every OpenAIClient owns its configuration. Run several accounts, Azure resources, or OpenAI-compatible providers side by side without global state:

final production = OpenAIClient(apiKey: 'sk-prod');

final deepseek = OpenAIClient(
  apiKey: 'ds-...',
  baseUrl: 'https://api.deepseek.com',
);

final localLlama = OpenAIClient(
  apiKey: 'not-needed',
  baseUrl: 'http://localhost:1234/v1',
);

Works with any provider that implements the OpenAI wire format: DeepSeek, LM Studio, Ollama, Groq, Together, Azure OpenAI gateways, and others.

Streaming

One SSE engine backs every streaming endpoint. It closes on [DONE], never duplicates events, and surfaces errors as exceptions instead of swallowing them.

Responses API events:

final events = client.responses.createStream(
  model: 'gpt-4o',
  input: 'Write a haiku about databases.',
);

await for (final event in events) {
  if (event['type'] == 'response.output_text.delta') {
    stdout.write(event['delta']);
  }
}

Chat Completions deltas:

final chunks = client.chat.createStream(model: 'gpt-4o', messages: messages);

await for (final chunk in chunks) {
  stdout.write(chunk.choices.first.delta?.content);
}

Images emit partial results as they render (createStream), and stored completions support retrieval and listing.

API coverage

API Accessor
Responses (incl. streaming, compact) client.responses
Chat Completions (tools, vision, reasoning params, stored completions) client.chat
Conversations client.conversations
Audio (speech, transcription, translation, voices, consents) client.audio
Images (generation, edit, variation, streaming partials) client.image
Embeddings client.embedding
Files (incl. byte uploads) client.file
Uploads (multipart sessions) client.uploads
Batch client.batch
Vector Stores (+ files, + file batches) client.vectorStores
Containers (+ files) client.container
Models / Moderation client.model / client.moderation
Evals client.evals
Graders (incl. run and validate) client.graders
Fine-tuning jobs (current API) client.fineTuning
Videos (generate, remix, download) client.videos
Realtime sessions and client secrets client.realtime
Skills client.skills
Content provenance checks client.provenance
Administration (projects, users, invites, audit logs, costs, rate limits, API keys) client.organization
Completions and Edits (legacy) client.completion / client.edit

Not planned: Assistants v1 and Threads (superseded by the Responses API), ChatKit.

Resilience

Requests retry automatically on transient failures (connection errors, HTTP 408/429/5xx). GET requests always retry; POST retries only on rate limits and server errors. Backoff is exponential with jitter and the server's Retry-After header takes precedence.

// Default policy: 2 total attempts.
final client = OpenAIClient(apiKey: 'sk-...');

// Custom:
final aggressive = OpenAIClient(
  apiKey: 'sk-...',
  retryPolicy: const OpenAIRetryPolicy(maxAttempts: 4),
);

Streaming has an idle watchdog: a connection that stops delivering bytes fails with StreamTimedOutException after the request timeout instead of hanging forever. After any call, quota is observable:

await client.model.list();
print(OpenAIResponseMeta.lastRateLimit?.remainingRequests);

Azure OpenAI

final azure = OpenAIClient(
  apiKey: '<azure-key>',
  azure: const OpenAIAzure(
    resource: 'my-resource',
    apiVersion: '2024-10-21',
    deployments: {'gpt-4o': 'gpt4o-prod'},
  ),
);

// model is rewritten to your deployment automatically:
await azure.chat.create(model: 'gpt-4o', messages: messages);

Files on every platform

Upload APIs take a platform-neutral file type — no dart:io, so the same code runs on web:

// From disk (native):
final file = await loadOpenAIFile('training.jsonl');
await client.file.upload(file: file, purpose: 'fine-tune');

// From memory (anywhere, including web):
await client.file.uploadBytes(
  bytes: utf8.encode(jsonl),
  fileName: 'training.jsonl',
  purpose: 'fine-tune',
);

Error handling

All failures throw typed exceptions you can catch precisely:

try {
  await client.chat.create(model: 'gpt-4o', messages: messages);
} on RequestFailedException catch (e) {
  // Non-2xx from the API: e.message, e.statusCode
} on MissingApiKeyException catch (e) {
  // No key configured for this client
} on OpenAIUnexpectedException catch (e) {
  // Malformed response that is not an API error payload
}

Error payloads are normalized across providers. Whether a provider returns {"error": {"message": ...}}, a bare string error, or an HTML error page, you get a RequestFailedException with the body preserved.

Configuration

Per-client options: apiKey, organization, baseUrl, version, requestsTimeOut, extraHeaders.

Global facade equivalents: OpenAI.apiKey, OpenAI.organization, OpenAI.baseUrl, OpenAI.requestsTimeOut, plus OpenAI.showLogs and OpenAI.showResponsesLogs for request debugging.

Reasoning models (o-series, GPT-5 family): pass reasoningEffort: 'low' and use maxTokens, which maps to max_completion_tokens. Incompatible sampling parameters are rejected by the API; use extraParams to pass anything not yet modeled.

Migrating from 6.x / 7.x

Nothing breaks if you used the global facade. To adopt per-client instances, replace global configuration with construction:

// Before (still supported)
OpenAI.apiKey = 'sk-...';
await OpenAI.instance.chat.create(...);

// After
final client = OpenAIClient(apiKey: 'sk-...');
await client.chat.create(...);

8.0.0 replaces dart:io File parameters with the platform-neutral OpenAIFile type (await loadOpenAIFile(path) on native, OpenAIFile(bytes:, fileName:) anywhere), and audio.createSpeech now returns bytes.

Full list of changes: CHANGELOG.md.

Testing

The suite runs entirely against mocked HTTP — request shapes, SSE decoding, malformed-payload fuzzing, retry behavior. No API keys required:

dart test

Live integration tests run only when OPEN_AI_API_KEY is present in the environment.

Contributing

Bug reports and feature requests go through GitHub Issues. Pull requests welcome: keep diffs focused, add or update tests for changed behavior, and follow the existing module layout (lib/src/instance/<module>/ with models under lib/src/core/models/<module>/). Commit messages use Conventional Commits.

License

MIT. See LICENSE.

Support

所屬分類

依賴

依賴清單 5 項

原始碼儲存庫

使用此套件的專案