Official Flutter SDK for the Rax AI Platform - OpenAI-compatible AI API client.
- β OpenAI-compatible API
- β Simple, intuitive API
- β Full Dart/Flutter support
- β Comprehensive error handling
- β Lightweight and fast
Add this to your pubspec.yaml:
dependencies:
rax_ai: ^1.0.0Then run:
flutter pub getimport 'package:rax_ai/rax_ai.dart';
void main() async {
final client = RaxAI(
apiKey: 'your-api-key-here',
baseUrl: 'https://your-rax-ai-instance.com/api/v1', // Optional
);
try {
final completion = await client.createChatCompletion(
model: 'rax-4.0',
messages: [
Message.user('Hello, how are you?'),
],
);
print(completion.choices.first.message.content);
} catch (e) {
print('Error: $e');
} finally {
client.dispose();
}
}import 'package:flutter/material.dart';
import 'package:rax_ai/rax_ai.dart';
class ChatWidget extends StatefulWidget {
@override
_ChatWidgetState createState() => _ChatWidgetState();
}
class _ChatWidgetState extends State<ChatWidget> {
final RaxAI _client = RaxAI(apiKey: 'your-api-key');
final TextEditingController _controller = TextEditingController();
String _response = '';
bool _loading = false;
Future<void> _sendMessage() async {
if (_controller.text.isEmpty) return;
setState(() {
_loading = true;
});
try {
final completion = await _client.createChatCompletion(
model: 'rax-4.0',
messages: [Message.user(_controller.text)],
);
setState(() {
_response = completion.choices.first.message.content;
_loading = false;
});
} catch (e) {
setState(() {
_response = 'Error: $e';
_loading = false;
});
}
_controller.clear();
}
@override
Widget build(BuildContext context) {
return Column(
children: [
TextField(
controller: _controller,
decoration: InputDecoration(
hintText: 'Enter your message...',
suffixIcon: IconButton(
onPressed: _loading ? null : _sendMessage,
icon: Icon(Icons.send),
),
),
),
SizedBox(height: 16),
if (_loading)
CircularProgressIndicator()
else
Text(_response),
],
);
}
@override
void dispose() {
_client.dispose();
_controller.dispose();
super.dispose();
}
}Main client class for interacting with the Rax AI API.
RaxAI({
required String apiKey,
String baseUrl = 'https://api.raxai.com/v1',
http.Client? httpClient,
})Future<ChatCompletion> createChatCompletion({
required String model,
required List<Message> messages,
double? temperature,
int? maxTokens,
})Represents a chat message.
Message.user(String content)
Message.assistant(String content)
Message.system(String content)ChatCompletion- Response from chat completion APIChoice- Individual choice in completion responseUsage- Token usage informationRaxAIException- SDK-specific exceptions
The SDK throws RaxAIException for API errors:
try {
final completion = await client.createChatCompletion(
model: 'rax-4.0',
messages: [Message.user('Hello')],
);
} on RaxAIException catch (e) {
print('Rax AI Error: ${e.message}');
} catch (e) {
print('Other error: $e');
}MIT License - see LICENSE file for details.