mcpField
D
dart-genkit

dart-genkit

github.com/dhruvanbhalara开发3039 次安装8 周前更新
代码仓库

关于 dart-genkit

面向 Dart 开发者的 Genkit AI 开发指南,介绍如何构建类型安全、可观测的 AI 工作流与 Agent。内容涵盖提示词、模型、自定义工具和流程编排。

主要功能

  1. 01结构化提示词与模型调用
  2. 02定义带输入输出模式的工具
  3. 03编排带模式的工作流
  4. 04内置遥测与错误处理建议
  5. 05追踪 Agent 决策和工具调用

适用场景

  1. 01构建类型安全的 AI 工作流
  2. 02为 Agent 添加自定义任务工具
  3. 03检查开发期间的 Agent 执行轨迹

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/dhruvanbhalara/skills --skill dart-genkit

安装后在 Claude Code 中输入/dart-genkit确认已加载。

详细介绍

使用 Genkit Dart 进行 AI 工程开发

使用 Genkit Dart SDK 构建结构化、类型安全且可观测的 AI 驱动工作流和 Agent。

目录


项目设置

  1. 将 Genkit 软件包添加到 Dart 项目的 pubspec.yaml 中:
    dependencies:
      genkit: ^0.1.0-preview  # Replace with the latest version
      google_generative_ai: ^0.4.0
    
  2. 在环境中设置 API 凭据(例如 Gemini API 密钥):
    export GEMINI_API_KEY="your_api_key_here"
    

定义提示词与模型

Genkit 使用结构化提示词,将模型调用和提示词结构与核心应用逻辑分离。

import 'package:genkit/genkit.dart';
import 'package:google_generative_ai/google_generative_ai.dart';

void main() async {
  // Initialize Genkit
  final ai = Genkit(
    model: 'gemini-1.5-flash',
    apiKey: String.fromEnvironment('GEMINI_API_KEY'),
  );

  // Invoke the model with a simple prompt
  final response = await ai.generate(
    prompt: 'Explain the concept of monads in Dart.',
  );
  
  print(response.text);
}

创建自定义工具

Genkit Agent 使用工具执行任务(例如数据库查询、网页抓取和数学计算)。

import 'package:genkit/genkit.dart';

// 1. Define schemas for input and output
final additionInputSchema = Schema.object({
  'a': Schema.number(description: 'First number'),
  'b': Schema.number(description: 'Second number'),
});

// 2. Define the tool
final addTool = ai.defineTool(
  name: 'addNumbers',
  description: 'Adds two numbers together.',
  inputSchema: additionInputSchema,
  action: (input) async {
    final a = input['a'] as num;
    final b = input['b'] as num;
    return {'result': a + b};
  },
);

编排工作流

工作流是可执行管道,支持结构化的输入和输出模式、内置遥测及错误处理。

import 'package:genkit/genkit.dart';

// Define the input and output schemas
final jokeRequestSchema = Schema.object({
  'topic': Schema.string(description: 'The topic for the joke'),
});

final jokeResponseSchema = Schema.object({
  'setup': Schema.string(),
  'punchline': Schema.string(),
});

// Define the Flow
final jokeFlow = ai.defineFlow(
  name: 'jokeFlow',
  inputSchema: jokeRequestSchema,
  outputSchema: jokeResponseSchema,
  action: (input) async {
    final topic = input['topic'] as String;
    
    final response = await ai.generate(
      prompt: 'Tell me a structured joke about $topic.',
      responseSchema: jokeResponseSchema,
    );
    
    return response.structuredOutput!;
  },
);

void main() async {
  // Run the flow
  final result = await jokeFlow.run({'topic': 'coding'});
  print('Setup: ${result['setup']}');
  print('Punchline: ${result['punchline']}');
}

最佳实践

  • 全面定义模式:始终为工具和工作流显式定义 inputSchema 和 outputSchema,确保 LLM 生成结构正确的参数。
  • 隔离环境:不要将 API 密钥硬编码。使用环境变量或安全的凭据存储。
  • 跟踪可观测性:在开发过程中启用 Genkit 的跟踪可观测性,以检查 Agent 决策树和工具调用。
  • 错误处理:使用标准的 try-catch 块封装工具执行,并妥善返回错误详情,以便 Agent 自行纠正。

评论

支持 Markdown