Skip to content

Commit 30d61bc

Browse files
committed
📝: update readme
1 parent c2c82ab commit 30d61bc

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,48 @@ Note: All `use` functions also have the signature of `remember`. If you prefer C
150150

151151
> Functions marked with `*` can only be used on Android
152152
153+
### AI Module
154+
155+
A separate AI module providing hooks for AI chat completions with OpenAI-compatible APIs.
156+
157+
**Add AI module dependency:**
158+
```kotlin
159+
implementation("xyz.junerver.compose:hooks2-ai:<latest_release>")
160+
```
161+
162+
| hook name | description |
163+
| --------- | ----------- |
164+
| [useChat](https://github.com/junerver/ComposeHooks/blob/master/app/src/commonMain/kotlin/xyz/junerver/composehooks/example/UseChatExample.kt) | A hook for managing chat conversations with OpenAI-compatible APIs, supporting streaming responses with typewriter effect. |
165+
166+
**Features:**
167+
- Streaming responses (SSE) with real-time typewriter effect
168+
- Message state management
169+
- Loading and error states
170+
- Control functions (send, stop, reload)
171+
- Configurable options (temperature, maxTokens, timeout, etc.)
172+
- Lifecycle callbacks (onFinish, onError, onStream)
173+
174+
**Example:**
175+
```kotlin
176+
val (messages, isLoading, error, sendMessage, _, _, reload, stop) = useChat {
177+
baseUrl = "https://api.openai.com/v1"
178+
apiKey = "your-api-key"
179+
model = "gpt-3.5-turbo"
180+
systemPrompt = "You are a helpful assistant."
181+
onFinish = { message, usage, reason ->
182+
println("Completed: ${message.content}")
183+
}
184+
}
185+
186+
// Send a message
187+
sendMessage("Hello!")
188+
189+
// Display messages with streaming effect
190+
messages.value.forEach { message ->
191+
Text("${message.role}: ${message.content}")
192+
}
193+
```
194+
153195
## Add to dependencies
154196

155197
**KMP project**

README.zh-CN.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,48 @@ implementation("xyz.junerver.compose:hooks2:<latest_release>")
157157

158158
> 注意,标记 `*` 的函数,只可以在 Android 中使用
159159
160+
### AI 模块
161+
162+
独立的 AI 模块,提供与 OpenAI 兼容 API 进行 AI 聊天的 Hook。
163+
164+
**添加 AI 模块依赖:**
165+
```kotlin
166+
implementation("xyz.junerver.compose:hooks2-ai:<latest_release>")
167+
```
168+
169+
| Hook 名称 | 描述 |
170+
| --------- | ---- |
171+
| [useChat](https://github.com/junerver/ComposeHooks/blob/master/app/src/commonMain/kotlin/xyz/junerver/composehooks/example/UseChatExample.kt) | 用于管理与 OpenAI 兼容 API 聊天对话的 Hook,支持流式响应的打字机效果。 |
172+
173+
**功能特性:**
174+
- 流式响应 (SSE),实时打字机效果
175+
- 消息状态管理
176+
- 加载和错误状态
177+
- 控制函数(发送、停止、重新加载)
178+
- 可配置选项(temperature、maxTokens、timeout 等)
179+
- 生命周期回调(onFinish、onError、onStream)
180+
181+
**示例:**
182+
```kotlin
183+
val (messages, isLoading, error, sendMessage, _, _, reload, stop) = useChat {
184+
baseUrl = "https://api.openai.com/v1"
185+
apiKey = "your-api-key"
186+
model = "gpt-3.5-turbo"
187+
systemPrompt = "You are a helpful assistant."
188+
onFinish = { message, usage, reason ->
189+
println("完成: ${message.content}")
190+
}
191+
}
192+
193+
// 发送消息
194+
sendMessage("你好!")
195+
196+
// 显示消息(带流式效果)
197+
messages.value.forEach { message ->
198+
Text("${message.role}: ${message.content}")
199+
}
200+
```
201+
160202
## 添加依赖
161203

162204
**KMP项目**

0 commit comments

Comments
 (0)