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.
|
|
1 year ago | |
|---|---|---|
| .. | ||
| src/main | 1 year ago | |
| README.md | 1 year ago | |
| build.gradle.kts | 1 year ago | |
| settings.gradle.kts | 1 year ago | |
README.md
ChatAPI Android 实现
基于 openai-kotlin 4.0.1 的 ChatAPI 插件 Android 实现。
特性
- ✅ 基于 openai-kotlin 4.0.1 的 OpenAI API 客户端
- ✅ 与 iOS 版本接口完全一致
- ✅ 支持流式和非流式对话
- ✅ 支持多模态内容(文本和图像识别)
- ✅ 工具调用架构预留(MCP 功能暂时留空)
- ✅ 协程支持和异步处理
- ✅ 完整的错误处理和取消机制
依赖
- openai-kotlin 4.0.1
- ktor-client-okhttp 2.3.2
- kotlinx-coroutines
- gson
主要文件
ChatApiPlugin.kt- Flutter 插件桥接层ChatApiService.kt- 核心 ChatAPI 服务实现StreamCallback.kt- 流式回调接口
与 iOS 版本的一致性
Android 实现完全遵循 iOS 版本的接口定义:
- 初始化方法:
initialize(apiKey, baseUrl, model, mcpServer) - 消息创建:
createUserMessage(),createAssistantMessage() - 消息发送:
sendMessage()(非流式),sendMessageStream()(流式) - 函数注册:
registerFunction() - MCP 相关: 接口保留,功能暂时留空
- 流式事件: 完全一致的事件类型和回调
MCP 功能状态
MCP (Model Context Protocol) 相关功能在 Android 版本中暂时留空,包括:
initializeMcpClient()- 返回 falseisMcpInitialized()- 返回 falsehandleMcpToolCall()- 返回占位符消息- 工具调用会通知上层但不会实际执行
使用示例
// 初始化
chatApiService.initialize(
apiKey = "your-api-key",
baseUrl = "https://api.openai.com/v1/",
model = "gpt-3.5-turbo",
mcpServer = ""
)
// 流式对话
chatApiService.sendMessageStream(listOf(
mapOf("role" to "user", "content" to "Hello!")
))
注意事项
- 需要网络权限
INTERNET和ACCESS_NETWORK_STATE - 所有网络请求在后台线程执行
- 流式回调在主线程触发
- 支持取消正在进行的请求