NEW: ChatGPT & Whisper APIs are added to the library and can be used directly.
An open-source Client package that allows developers to easily integrate the power of OpenAI's state-of-the-art AI models into their Dart/Flutter applications.
This library provides simple and intuitive methods for making requests to OpenAI's various APIs, including the GPT-3 language model, DALL-E image generation, and more.
The package is designed to be lightweight and easy to use, so you can focus on building your application, rather than worrying about the complexities and errors caused by dealing with HTTP requests.
Unofficial
OpenAI does not have any official Dart library.
Please note that this client SDK connects directly to OpenAI APIs using HTTP requests.
- Easy to use methods that reflect exactly the OpenAI documentation, with additional functionalities that make it better to use with Dart Programming Language.
- Authorize just once, use it anywhere and at any time in your application.
- Developer-friendly.
Stream
functionality for completions API & fine-tune events API.
- Authentication
- Models
- Completions
- With
Stream
responses.
- With
- Chat (chatGPT)
- With
Stream
responses.
- With
- Edits
- Images
- Embeddings
- Audio
- Files
- Fine-tunes
- With events
Stream
responses.
- With events
- Moderation
- Authentication
- Models
- Completions
- chat (chatGPT)
- Edits
- Images
- Embeddings
- Audio
- Files
- Fine-tunes
- Moderation
For the full documentation about all members this library offers, check here.
The OpenAI API uses API keys for authentication. you can get your account APU key by visiting API keys of your account.
We highly recommend loading your secret key at runtime from a .env
file, you can use the envied package.
// .env
OPEN_AI_API_KEY=<REPLACE WITH YOUR API KEY>
// lib/env/env.dart
import 'package:envied/envied.dart';
part 'env.g.dart';
@Envied(path: ".env")
abstract class Env {
@EnviedField(varName: 'OPEN_AI_API_KEY') // the .env variable.
static const apiKey = _Env.apiKey;
}
// lib/main.dart
void main() {
OpenAI.apiKey = Env.apiKey; // Initializes the package with that API key
// ..
}
if no apiKey
is set, and you tried to access OpenAI.instance
, a MissingApiKeyException
will be thrown even before making the actual request.
if the apiKey
is set, but it is invalid when making requests, a RequestFailedException
will be thrown in your app, check the error handling section for more info.
if you belong to a specific organization, you can pass its id to OpenAI.organization
like this:
OpenAI.organization = "ORGANIZATION ID";
If you don't belong actually to any organization, you can just ignore this section, or set it to null
.
Lists the currently available models, and provides basic information about each one such as the owner and availability.
List<OpenAIModelModel> models = await OpenAI.instance.model.list();
OpenAIModelModel firstModel = models.first;
print(firstModel.id); // ...
Retrieves a single model by its id and gets additional pieces of information about it.
OpenAIModelModel model = await OpenAI.instance.model.retrieve("text-davinci-003");
print(model.id);
If the model id does not exist, a RequestFailedException
will be thrown, check Error Handling section.
Creates a Completion based on the provided properties model
, prompt
& other properties.
OpenAICompletionModel completion = await OpenAI.instance.completion.create(
model: "text-davinci-003",
prompt: "Dart is a progr",
maxTokens: 20,
temperature: 0.5,
n: 1,
stop: ["\n"],
echo: true,
);
if the request failed (as an example, if you did pass an invalid model id...), a RequestFailedException
will be thrown, check Error Handling section.
In addition to calling the OpenAI.instance.completion.create()
which is a Future
and will not return an actual value until the completion is ended, you can get a Stream
of values as they are generated:
Stream<OpenAIStreamCompletionModel> completionStream = OpenAI.instance.completion.createStream(
model: "text-davinci-003",
prompt: "Github is ",
maxTokens: 100,
temperature: 0.5,
topP: 1,
);
completionStream.listen((event) {
final firstCompletionChoice = event.choices.first;
print(firstCompletionChoice.text); // ...
});
Creates a completion for the chat message, note you need to set each message as a OpenAIChatCompletionChoiceMessageModel
object.
OpenAIChatCompletionModel chatCompletion = await OpenAI.instance.chat.create(
model: "gpt-3.5-turbo",
messages: [
OpenAIChatCompletionChoiceMessageModel(
content: "hello, what is Flutter and Dart ?",
role: OpenAIChatMessageRole.user,
),
],
);
in addition to calling OpenAI.instance.chat.create()
which is a Future
and will resolve only after the whole chat is generated, you can get a Stream
of results:
OpenAIStreamChatCompletionModel chatStream = OpenAI.instance.chat.createStream(
model: "gpt-3.5-turbo",
messages: [
OpenAIChatCompletionChoiceMessageModel(
content: "hello",
role: OpenAIChatMessageRole.user,
)
],
);
chatStream.listen((chatStreamEvent) {
print(chatStreamEvent); // ...
});
Creates an edited version of the given prompt based on the used model.
OpenAIEditModel edit = await OpenAI.instance.edit.create(
model: "text-davinci-edit-001";
instruction: "remote all '!'from input text",
input: "Hello!!, I! need to be ! somethi!ng"
n: 1,
temperature: 0.8,
);
Generates a new image based on a prompt given.
OpenAIImageModel image = await OpenAI.instance.image.create(
prompt: 'an astronaut on the sea',
n: 1,
size: OpenAIImageSize.size1024,
responseFormat: OpenAIImageResponseFormat.url,
);
Creates an edited or extended image given an original image and a prompt.
OpenAiImageEditModel imageEdit = await OpenAI.instance.image.edit(
file: File(/* IMAGE PATH HERE */),
mask: File(/* MASK PATH HERE */),
prompt: "mask the image with a dinosaur",
n: 1,
size: OpenAIImageSize.size1024,
responseFormat: OpenAIImageResponseFormat.url,
);
Creates a variation of a given image.
OpenAIImageVariationModel imageVariation = await OpenAI.instance.image.variation(
image: File(/* IMAGE PATH HERE */),
n: 1,
size: OpenAIImageSize.size1024,
responseFormat: OpenAIImageResponseFormat.url,
);
Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms.
OpenAIEmbeddingsModel embeddings = await OpenAI.instance.embedding.create(
model: "text-embedding-ada-002",
input: "This is a text input just to test",
);
for transcribing an audio File
, you can use the createTranscription()
method directly by providing the file
property:
OpenAIAudioModel transcription = OpenAI.instance.audio.createTranscription(
file: /* THE AUDIO FILE HERE */,
model: "whisper-1",
responseFormat: OpenAIAudioResponseFormat.json,
);
to get access to the translation API, and translate an audio file to english, you can use the createTranslation()
method, by providing the `file`` property:
OpenAIAudioModel translation = await OpenAI.instance.audio.createTranslation(
file: /* THE AUDIO FILE HERE */,
model: "whisper-1",
responseFormat: OpenAIAudioResponseFormat.text,
);
Files are used to upload documents that can be used with features like Fine-tuning.
Get a list of all the uploaded files o-to your OpenAI account.
List<OpenAIFileModel> files = await OpenAI.instance.file.list();
print(files.first.fileName); // ...
print(files.first.id); // ...
Upload a file that contains document(s) to be used across various endpoints/features. Currently, the size of all the files uploaded by one organization can be up to 1 GB. Please contact us if you need to increase the storage limit.
OpenAIFileModel uploadedFile = await OpenAI.instance.file.upload(
file: File("/* FILE PATH HERE */"),
purpose: "fine-tuning",
);
print(uploadedFile.id); // ...
Deletes an existent file by it's id.
bool isFileDeleted = await OpenAI.instance.file.delete("/* FILE ID */");
print(isFileDeleted);
Fetches for a single file by it's id and returns informations about it.
OpenAIFileModel file = await OpenAI.instance.file.retrieve("FILE ID");
print(file.id);
Fetches for a single file content by it's id.
dynamic fileContent = await OpenAI.instance.file.retrieveContent("FILE ID");
print(fileContent);
Creates a job that fine-tunes a specified model from a given dataset, and returns a fine-tuned object about the enqueued job.
OpenAIFineTuneModel fineTune = await OpenAI.instance.fineTune.create(
trainingFile: "FILE ID",
);
print(fineTune.status); // ...
List your organization's fine-tuning jobs.
List<OpenAIFineTuneModel> fineTunes = await OpenAI.instance.fineTune.list();
print(fineTunes.first); // ...
Retrieves a fine-tune by its id.
OpenAIFineTuneModel fineTune = await OpenAI.instance.fineTune.retrieve("FINE TUNE ID");
print(fineTune.id); // ...
Cancels a fine-tune job by its id, and returns it.
OpenAIFineTuneModel cancelledFineTune = await OpenAI.instance.fineTune.cancel("FINE TUNE ID");
print(cancelledFineTune.status); // ...
Lists a single fine-tune progress events by it's id.
List<OpenAIFineTuneEventModel> events = await OpenAI.instance.fineTune.listEvents("FINE TUNE ID");
print(events.first.message); // ...
Streams all events of a fine-tune job by its id, as they happen.
This is a long-running operation that will not return until the fine-tune job is terminated.
The stream will emit an event every time a new event is available.
Stream<OpenAIFineTuneEventStreamModel> eventsStream = OpenAI.instance.fineTune.listEventsStream("FINE TUNE ID");
eventsStream.listen((event) {
print(event.message);
});
Deletes a fine-tune job by its id.
bool deleted = await OpenAI.instance.fineTune.delete("FINE TUNE ID");
print(deleted); // ...
Classifies if text violates OpenAI's Content Policy
OpenAIModerationModel moderation = await OpenAI.instance.moderation.create(
input: "I want to kill him",
);
print(moderation.results); // ...
print(moderation.results.first.categories.hate); // ...
Any time an error happens from the OpenAI API ends (As Example: when you try to create an image variation from a non-image file.. , a RequestFailedException
will be thrown automatically inside your Flutter / Dart app, you can use a try-catch
to catch that error, and make an action based on it:
try {
// This will throw an error.
final errorVariation = await OpenAI.instance.image.variation(
image: File(/*PATH OF NON-IMAGE FILE*/),
);
} on RequestFailedException catch(e) {
print(e.message);
print(e.statusCode);
}
Please, Just remember that any kind of help related to these tasks are welcome, for the sake of the community.
- Writing documentation: if you see any class, property, method.. that you know what does and it is undocumented, please take from your time 2 minutes and help another developer that doesn't.
- Code Refactoring: I know this is my job not yours :), but if you can and want, you're more than welcome.
- Reviewing code: if it happens that there is a better way to make something happen in the SDK, please just let me know.
- if you tried any sample of use cases, examples of yours and wanted to include it in the examples/, please go ahead.