tl_flutter_openai 1.0.19+20 copy "tl_flutter_openai: ^1.0.19+20" to clipboard
tl_flutter_openai: ^1.0.19+20 copied to clipboard

Flutter library for work with OpenAI API. You can easily create AI prototype with this library.

Tark Laboratory Flutter OpenAI Client #

This is an unofficial Dart client for OpenAI API.

Demo

Features #

  • Fully compatible with the official OpenAI API
  • Fully type-safe
  • Authentication with organization support

Supported endpoints #

  • Chat completions

Endpoints in progress #

  • Embeddings
  • Fine-tuning
  • Batch
  • Images
  • Models
  • Moderations

Table of contents #

Usage #

Authentication #

To authenticate in, you will need a key. You can get it from your personal account settings.

Remember that your API key is a secret!

Do not share it with others or expose it in any client-side code (browsers, apps). Production requests must be routed through your own backend server where your API key can be securely loaded from an environment variable or key management service.

final openAI = OpenAI(
  apiKey: dotenv.env['OPENAI_API_KEY'],
);

Organization (optional)

For users who belong to multiple organizations or are accessing their projects through their legacy user API key, you can pass a header to specify which organization and project is used for an API request. Usage from these API requests will count as usage for the specified organization and project.

final openAI = OpenAI(
  apiKey: dotenv.env['OPENAI_API_KEY'],
  organizationId: dotenv.env['OPENAI_ORGANIZATION_ID'],
  projectId: dotenv.env['OPENAI_PROJECT_ID'],
);

Chat #

Given a list of messages comprising a conversation, the model will return a response.

Related guide: Chat Completions

Create chat completion:


final response = await openAI.createChatCompletion(ChatCompletionRequest(
  model: AIModel.chatgpt4oLatest,
  messages: [
    SystemMessage(
      content: 'You are a friend',
    ),
    UserMessage(
      content: 'Hey, how are you?',
    ),
  ],
));

print(response.choices.first.message.content);
// Hello! How can I assist you today?

  • ChatCompletionRequest is the main object used for making the Chat completion call
  • AIModel.chatgpt4oLatest is a model from AIModel enum which lists all of the available models.
  • You can use different types of messages like SystemMessage, UserMessage, AssistantMessage, ToolMessage, FunctionMessage and MultiModalMessage to create a conversation.

License #

TL OpenAI Dart Client is licensed under the MIT License.

7
likes
50
points
0
downloads

Publisher

unverified uploader

Weekly Downloads

Flutter library for work with OpenAI API. You can easily create AI prototype with this library.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

cupertino_icons, dio, flutter, flutter_dotenv, flutter_localizations, freezed_annotation, intl, json_annotation

More

Packages that depend on tl_flutter_openai