gemini_openai_claude 1.0.0
gemini_openai_claude: ^1.0.0 copied to clipboard
Structured JSON output from Gemini, OpenAI and Claude with one schema. Images, actionable errors, no vendor SDKs. Unofficial.
// Run: GEMINI_API_KEY=... dart run example/main.dart
// (or OPENAI_API_KEY / ANTHROPIC_API_KEY). With no key it uses DemoProvider.
import 'dart:io';
import 'package:gemini_openai_claude/gemini_openai_claude.dart';
Future<void> main() async {
final env = Platform.environment;
final (AiAdapter ai, String key) = switch (env) {
{'GEMINI_API_KEY': final k} => (GeminiProvider(), k),
{'OPENAI_API_KEY': final k} => (OpenAiProvider(), k),
{'ANTHROPIC_API_KEY': final k} => (ClaudeProvider(), k),
_ => (const DemoProvider(), ''),
};
final spec = PromptSpec(
instruction: 'Suggest a short title and three tags for this note: "Fix the bike brakes before Sunday."',
schema: const {
'type': 'object',
'properties': {
'title': {'type': 'string'},
'tags': {
'type': 'array',
'items': {'type': 'string'},
},
},
'required': ['title', 'tags'],
},
demo: () => {
'title': 'Bike brakes',
'tags': ['bike', 'repair', 'weekend'],
},
);
try {
final json = await ai.complete(spec: spec, apiKey: key);
stdout.writeln('${ai.label()}: $json');
} on ProviderException catch (e) {
stderr.writeln('${e.kind.name}: ${e.message}');
if (e.detail != null) stderr.writeln(e.detail);
exitCode = 1;
} finally {
ai.dispose();
}
}