This is an unofficial Dart client for OpenAI API.
- Fully compatible with the official OpenAI API
- Fully type-safe
- Authentication with organization support
- Chat completions
- Embeddings
- Fine-tuning
- Batch
- Images
- Models
- Moderations
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'],
);
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'],
);
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 callAIModel.chatgpt4oLatest
is a model fromAIModel
enum which lists all of the available models.- You can use different types of messages like
SystemMessage
,UserMessage
,AssistantMessage
,ToolMessage
,FunctionMessage
andMultiModalMessage
to create a conversation.
TL OpenAI Dart Client is licensed under the MIT License.