flutter_ai_agent_tool 0.1.0
flutter_ai_agent_tool: ^0.1.0 copied to clipboard
A comprehensive toolkit for building AI-powered Flutter applications with multi-provider support, streaming responses, memory management, and extensible tool system.
Flutter AI Agent Tool #
A comprehensive toolkit for building AI-powered Flutter applications with multi-provider support, streaming responses, memory management, and an extensible tool system.
Features #
- Multi-Provider Support: Seamlessly integrate with OpenAI, Anthropic (Claude), Google AI (Gemini), xAI (Grok), and Groq
- Streaming Responses: Real-time streaming of AI responses with proper state management
- Memory Management: Multiple memory strategies including conversation history, persistent storage, and sliding window
- Tool/Function Calling: Extensible tool system for giving AI agents custom capabilities
- Pre-built UI Components: Ready-to-use chat widgets for quick integration
- Type Safety: Full null safety support with comprehensive type definitions
- Production Ready: Includes error handling, state management, and proper resource cleanup
Getting Started #
Installation #
Add this to your package's pubspec.yaml file:
dependencies:
flutter_ai_agent_tool: ^0.1.0
Then run:
flutter pub get
Prerequisites #
You'll need API keys from one or more of these providers:
Usage #
Basic Chat Example #
import 'package:flutter_ai_agent_tool/flutter_ai_agent_tool.dart';
// Create a provider
final provider = OpenAIProvider(
config: OpenAIConfig(
apiKey: 'your-api-key',
model: 'gpt-4',
),
);
// Create an agent with memory
final agent = AIAgent(
provider: provider,
config: AIAgentConfig(
systemPrompt: 'You are a helpful assistant.',
),
memoryManager: ConversationMemory(maxMessages: 50),
);
// Send a message
final response = await agent.chat('Hello! How are you?');
print(response);
// Stream a response
agent.chatStream('Tell me a story').listen((chunk) {
print(chunk);
});
Using the Chat UI #
import 'package:flutter/material.dart';
import 'package:flutter_ai_agent_tool/flutter_ai_agent_tool.dart';
class ChatScreen extends StatefulWidget {
@override
_ChatScreenState createState() => _ChatScreenState();
}
class _ChatScreenState extends State<ChatScreen> {
late AIAgent agent;
@override
void initState() {
super.initState();
agent = AIAgent(
provider: OpenAIProvider(
config: OpenAIConfig(apiKey: 'your-api-key'),
),
memoryManager: ConversationMemory(),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: ChatView(
agent: agent,
title: 'AI Assistant',
),
);
}
@override
void dispose() {
agent.dispose();
super.dispose();
}
}
Adding Tools to Your Agent #
import 'package:flutter_ai_agent_tool/flutter_ai_agent_tool.dart';
// Create agent with tools
final agent = AIAgent(
provider: provider,
memoryManager: ConversationMemory(),
);
// Add built-in tools
agent.addTool(CalculatorTool());
agent.addTool(DateTimeTool());
// Create custom tool
class WeatherTool extends Tool {
@override
String get name => 'get_weather';
@override
String get description => 'Get current weather for a location';
@override
Map<String, dynamic> get parametersSchema => {
'type': 'object',
'properties': {
'location': {
'type': 'string',
'description': 'City name',
},
},
'required': ['location'],
};
@override
Future<String> execute(Map<String, dynamic> arguments) async {
final location = arguments['location'];
// Implement weather API call here
return 'Weather in $location: Sunny, 72°F';
}
}
agent.addTool(WeatherTool());
Multiple Providers #
// Use Anthropic (Claude)
final anthropicAgent = AIAgent(
provider: AnthropicProvider(
config: AnthropicConfig(
apiKey: 'your-anthropic-key',
model: 'claude-3-5-sonnet-20241022',
),
),
);
// Use Google AI (Gemini)
final googleAgent = AIAgent(
provider: GoogleAIProvider(
config: GoogleAIConfig(
apiKey: 'your-google-key',
model: 'gemini-pro',
),
),
);
// Use xAI (Grok)
final grokAgent = AIAgent(
provider: GrokProvider(
config: GrokConfig(
apiKey: 'your-xai-key',
model: 'grok-beta',
),
),
);
// Use Groq (Ultra-fast inference)
final groqAgent = AIAgent(
provider: GroqProvider(
config: GroqConfig(
apiKey: 'your-groq-key',
model: 'llama-3.3-70b-versatile',
),
),
);
Memory Strategies #
// In-memory conversation history
final conversationMemory = ConversationMemory(maxMessages: 100);
// Persistent storage
final persistentMemory = PersistentMemory(
conversationId: 'user-123',
maxMessages: 200,
);
// Sliding window (keeps most recent messages)
final slidingMemory = SlidingWindowMemory(
windowSize: 10,
keepSystemMessages: true,
);
final agent = AIAgent(
provider: provider,
memoryManager: persistentMemory,
);
Examples #
Check out the example directory for complete working examples:
- Basic chat application
- Custom tools implementation
- Multiple provider usage
- Advanced memory management
Architecture #
flutter_ai_toolkit/
├── core/ # Core interfaces and agent logic
├── providers/ # AI provider implementations
├── memory/ # Memory management strategies
├── tools/ # Tool system and built-in tools
└── ui/ # Pre-built UI components
Contributing #
Contributions are welcome! Please feel free to submit a Pull Request.
License #
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments #
Built with Flutter and powered by leading AI providers:
- OpenAI
- Anthropic
- Google AI
- xAI
- Groq