You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
liwei1dao 8104a5d34e 上传mac断开链接 的处理优化 1 year ago
..
android 上传mac断开链接 的处理优化 1 year ago
ios 上传图片理解修复 1 year ago
lib add 1 year ago
LICENSE add 1 year ago
README.md add 1 year ago
pubspec.yaml add 1 year ago

README.md

chat_api

基于 MacPaw/OpenAI 库的 Flutter ChatAPI 插件。

功能特性

  • 基于 MacPaw/OpenAI Swift 包实现
  • 支持流式和非流式对话
  • 支持函数调用(工具调用)
  • 支持多模态内容(文本和图像)
  • 完整的 MCP (Model Context Protocol) 客户端支持
  • 内置系统功能集成(电话、短信、位置、提醒、媒体控制等)
  • 与 open_ai_service 完全兼容的接口

平台支持

  • iOS 16.0+
  • Android (API 21+)

MCP 功能

系统功能

插件内置了丰富的系统功能,包括:

  • 通信功能:发送短信、拨打电话
  • 日程管理:设置提醒、获取当前时间
  • 位置服务:获取当前地理位置
  • 媒体控制:播放、暂停、上一首、下一首
  • 应用控制:打开录音机、进入翻译模式
  • 交互管理:结束当前交互

MCP 服务器连接

支持连接外部 MCP 服务器,扩展更多工具和功能:

// 初始化时配置 MCP 服务器
await chatApi.initialize(
  apiKey: 'your-openai-api-key',
  mcpServer: '{"mcpServers": {"amap": {"url": "your-mcp-server-url"}}}',
);

// 检查 MCP 状态
bool isConnected = await chatApi.isMcpInitialized();

使用方法

import 'package:chat_api/chat_api.dart';

final chatApi = ChatApiService();

// 初始化
await chatApi.initialize(
  apiKey: 'your-openai-api-key',
  baseUrl: 'https://api.openai.com/v1/chat/completions', // 可选
  model: 'gpt-3.5-turbo', // 可选
  mcpServer: '{"mcpServers": {"local": {"url": "local://functions"}}}', // 可选
);

// 发送消息
final response = await chatApi.sendMessage(
  messages: [
    {'role': 'user', 'content': 'Hello!'}
  ],
);

// 流式对话
chatApi.eventStream.listen((event) {
  switch (event.type) {
    case ChatApiEventType.token:
      print('Token: ${event.content}');
      break;
    case ChatApiEventType.complete:
      print('Complete');
      break;
    case ChatApiEventType.error:
      print('Error: ${event.content}');
      break;
    case ChatApiEventType.functionCall:
      print('Function call: ${event.content}');
      // 系统功能会自动处理,无需手动干预
      break;
  }
});

await chatApi.sendMessageStream(
  messages: [
    {'role': 'user', 'content': '帮我设置一个明天下午3点的提醒'}
  ],
);

// 处理自定义函数调用
chatApi.eventStream.listen((event) {
  if (event.type == ChatApiEventType.functionCall) {
    final functionCall = chatApi.parseFunctionCall(event.content);
    
    // 使用辅助方法处理函数调用
    chatApi.handleFunctionCall(
      functionCall: functionCall,
      messages: currentMessages,
      localFunctionHandler: (name, arguments) async {
        // 处理自定义本地函数
        switch (name) {
          case 'custom_function':
            return 'Function executed successfully';
          default:
            return 'Unknown function: $name';
        }
      },
    );
  }
});

内置系统功能列表

功能名称 描述 参数
exit_interaction 结束当前交互 无
enter_translation_mode 进入翻译模式 无
send_text_message 发送短信 contact, message
make_phone_call 拨打电话 contact
set_reminder 设置提醒 title, time, content(可选)
get_current_time 获取当前时间 format(可选)
get_current_location 获取当前位置 无
media_play 播放媒体 无
media_pause 暂停媒体 无
media_previous 上一首 无
media_next 下一首 无
open_recorder 打开录音机 无

依赖项

  • MacPaw/OpenAI (通过 Swift Package Manager)
  • Flutter 3.3.0+

架构

iOS

ChatApiService (Flutter层)
    ↓
ChatApiPlugin (iOS插件桥接)
    ↓
ChatApiService (iOS原生服务)
    ├── OpenAI客户端 (MacPaw/OpenAI)
    └── MCPClient (MCP协议客户端)
        ├── MCPSubClient (外部MCP服务器)
        └── SystemFunctionHandler (内置系统功能)

Android

ChatApiService (Flutter层)
    ↓
ChatApiPlugin (Android插件桥接)
    ↓
ChatApiService (Android原生服务)
    ├── OpenAI客户端 (aallam/openai-kotlin 4.0.1)
    └── MCP功能 (暂时留空,接口预留)