ai_box
This package provides a base class and test utilities for all AI provider classes that inherit from LLMAIBase.
LLMAIBase
An abstract class that all AI providers (such as ChatGPT, Claude, Gemini, etc.) should inherit from.
Required Methods
chat(): Chat functionalitygetModelIds(): Get a list of available model IDsvalidateKey(): Validate the API key
Provided Utility Methods
chatWithStrings(): Chat with an array of stringsgenerateText(): Simple text generation
Common Tests
By using the runLLMAIBaseCommonTests() function, you can ensure that all LLMAIBase implementation classes are tested to the same standard.
Usage Example
import 'package:test/test.dart';
import 'package:your_ai_package/your_ai_package.dart';
// Import the test utilities from ai_box
import '../ai_box/test/test_utils.dart';
void main() {
// Run the common tests
runLLMAIBaseCommonTests(
createInstance: () => YourAIClass(apiKey: 'your-api-key'),
instanceName: 'YourAI',
testModel: 'your-model-name',
skipApiTests: false, // Set to false to perform actual API calls
);
// Add any additional specific tests here
group('YourAI Specific Tests', () {
// ...
});
}
Parameters
createInstance: A function that creates an instance of the LLMAIBase to be tested.instanceName: The name of the class being tested (for display purposes in tests).testModel: The name of the model to be used for testing.skipApiTests: Whether to skip tests that involve API calls (default: false).
Test Contents
The common tests include the following:
Basic Tests
- The instance is created correctly.
- The API key is set.
API Functionality Tests (if skipApiTests=false)
- The API key can be validated.
- Available model IDs can be retrieved.
- The chat functionality works.
- The chat functionality with strings works.
- The text generation functionality works.
- The
maxTokensparameter is applied.
Class Tests
LLMContentis created correctly.- The values of
LLMRoleare correct.
Error Handling Tests
- An error occurs with an empty message list.
- An error occurs with an invalid model name.
Adding a New AI Provider
- Create a class that inherits from
LLMAIBase. - Implement the required methods.
- Import
../ai_box/test/test_utils.dartin the test file. - Call
runLLMAIBaseCommonTests(). - Add specific tests as needed.
This ensures that all AI providers maintain consistent quality.
Notes
- The test utilities are located in the
testfolder and are not included in the mainlibfolder. - This prevents the
testpackage dependency from being included in the production code.