genkit_openai 0.0.1-dev.1 copy "genkit_openai: ^0.0.1-dev.1" to clipboard
genkit_openai: ^0.0.1-dev.1 copied to clipboard

OpenAI-compatible API plugin for Genkit Dart.

example/example.dart

// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import 'dart:io';

import 'package:genkit/genkit.dart';
import 'package:genkit_openai/genkit_openai.dart';
import 'package:schemantic/schemantic.dart';

part 'example.g.dart';

@Schematic()
abstract class $CalculatorInput {
  int get a;
  int get b;
}

@Schematic()
abstract class $Person {
  String get name;
  int get age;
}

void main(List<String> args) {
  configureCollectorExporter();

  final ai = Genkit(
    plugins: [openAI(apiKey: Platform.environment['OPENAI_API_KEY'])],
  );

  // --- Basic Generate Flow ---
  ai.defineFlow(
    name: 'basicGenerate',
    inputSchema: stringSchema(defaultValue: 'Hello Genkit for Dart!'),
    outputSchema: stringSchema(),
    fn: (input, context) async {
      final response = await ai.generate(
        model: openAI.model('gpt-4o'),
        prompt: input,
      );
      return response.text;
    },
  );

  // --- Streaming Flow ---
  ai.defineFlow(
    name: 'streaming',
    inputSchema: stringSchema(defaultValue: 'Count to 5'),
    outputSchema: stringSchema(),
    streamSchema: stringSchema(),
    fn: (input, ctx) async {
      final stream = ai.generateStream(
        model: openAI.model('gpt-4o'),
        prompt: input,
      );

      await for (final chunk in stream) {
        if (ctx.streamingRequested) {
          ctx.sendChunk(chunk.text);
        }
      }
      return (await stream.onResult).text;
    },
  );

  // --- Tool Calling Flow ---
  ai.defineTool(
    name: 'calculator',
    description: 'Multiplies two numbers',
    inputSchema: CalculatorInput.$schema,
    outputSchema: intSchema(),
    fn: (input, _) async => input.a * input.b,
  );

  ai.defineFlow(
    name: 'toolCalling',
    inputSchema: stringSchema(defaultValue: 'What is 123 * 456?'),
    outputSchema: stringSchema(),
    fn: (prompt, context) async {
      final response = await ai.generate(
        model: openAI.model('gpt-4o'),
        prompt: prompt,
        toolNames: ['calculator'],
      );
      return response.text;
    },
  );

  // --- Structured Output Flow ---
  ai.defineFlow(
    name: 'structuredOutput',
    inputSchema: stringSchema(
      defaultValue: 'Generate a person named John Doe, age 30',
    ),
    outputSchema: Person.$schema,
    streamSchema: Person.$schema,
    fn: (prompt, ctx) async {
      final response = await ai.generate(
        model: openAI.model('gpt-4o'),
        prompt: prompt,
        outputSchema: Person.$schema,
        onChunk: (chunk) {
          ctx.sendChunk(chunk.output!);
        },
      );
      return response.output!;
    },
  );
}
0
likes
0
points
123
downloads

Publisher

verified publishergenkit.dev

Weekly Downloads

OpenAI-compatible API plugin for Genkit Dart.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

genkit, meta, openai_dart, schemantic

More

Packages that depend on genkit_openai