ai_prompt_kit 0.0.2
ai_prompt_kit: ^0.0.2 copied to clipboard
A Flutter-friendly AI helper package for calling LLM APIs using prompt templates and structured responses.
ai_prompt_kit π€β¨ #
A Flutter-friendly AI helper package for calling LLM / AI APIs using prompt templates, clean abstractions, and safe response handling.
This package is designed to make AI integration in Flutter simple, readable, and production-ready.
β¨ Features #
- π OpenAI-style REST API support
- π§ PromptTemplate system (variable-based prompts)
- π¦ Clean AI client abstraction
- β οΈ Graceful error handling (no crashes)
- π― UI-agnostic (works with any Flutter app)
- π pub.devβready example app
π¦ Installation #
Add this to your pubspec.yaml:
dependencies:
ai_prompt_kit: ^0.0.1
Then run:
flutter pub get
π Quick Start #
1οΈβ£ Initialize the AI Client #
final aiClient = AiClient(
AiConfig(
apiKey: "YOUR_API_KEY",
baseUrl: "https://api.openai.com/v1",
),
);
β οΈ Do not hardcode API keys in packages.
Pass the key from your app or environment.
2οΈβ£ Simple Prompt Completion #
final response = await aiClient.complete(
prompt: "Explain Flutter packages in simple words",
);
if (response.hasError) {
print(response.error);
} else {
print(response.text);
}
π§ PromptTemplate (Key Feature) #
PromptTemplate allows you to create reusable prompts using variables instead of string concatenation.
Example: Summarization Prompt #
final prompt = PromptTemplate(
template: "Summarize the following text in {lang}:\n{text}",
variables: {
"lang": "English",
"text": "Flutter packages help developers share reusable components.",
},
);
final response = await aiClient.run(prompt);
if (response.hasError) {
print(response.error);
} else {
print(response.text);
}
βοΈ Why use PromptTemplate? #
- Cleaner prompt construction
- Easy variable replacement
- Reusable & maintainable prompts
- Ideal for production AI systems
π AI Response Model #
All AI calls return an AiResponse object.
class AiResponse {
final String text;
final int? tokens;
final String? error;
bool get hasError => error != null;
}
β οΈ Error Handling (No Crashes) #
Handled cases:
- Missing API key
- Invalid API key (401)
- Network failures
- Invalid or empty AI responses
Example error message:
API key not configured. Please provide a valid API key.
π§ͺ Example App #
This package includes a full Flutter example demonstrating:
- PromptTemplate usage
- AI request lifecycle
- Loading state handling
- Error-safe UI
Run the example:
cd example
flutter run
π API Key Management #
For security reasons:
- β Do not commit API keys to GitHub
- β Do not hardcode API keys in packages
- β Provide API keys from the application layer
.envusage is optional (not required)
π£οΈ Roadmap #
- Streaming AI responses
- Structured JSON output
- Multiple LLM provider support
- Built-in prompt presets
π€ Contributing #
Contributions are welcome!
Feel free to open issues or submit pull requests.
π License #
MIT License