Spaces:
Sleeping
Sleeping
File size: 11,829 Bytes
9026aea |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 |
# ChatBIA ์๋๋ก์ด๋ ์คํธ๋ฆฌ๋ฐ ์ฐ๋ ๊ฐ์ด๋
## ๐ก API ์๋ํฌ์ธํธ
### 1. ์ผ๋ฐ ์ฑํ
(๋น์คํธ๋ฆฌ๋ฐ)
```
POST /chat
```
- **ํ์์์ ์ํ**: ๊ธด ์๋ต ์ ํ์์์ ๋ฐ์ ๊ฐ๋ฅ
- **์๋๋ก์ด๋์์ ๊ถ์ฅํ์ง ์์**
### 2. ์คํธ๋ฆฌ๋ฐ ์ฑํ
โ
**๊ถ์ฅ**
```
POST /chat/stream
```
- **ํ์์์ ๋ฐฉ์ง**: ํ ํฐ ๋จ์๋ก ์ค์๊ฐ ์์
- **์๋๋ก์ด๋์ ์ต์ ํ**
- **SSE (Server-Sent Events)** ๋ฐฉ์
---
## ๐ง ์๋๋ก์ด๋ ๊ตฌํ (Kotlin)
### 1. build.gradle ์์กด์ฑ ์ถ๊ฐ
```gradle
dependencies {
// OkHttp for SSE streaming
implementation("com.squareup.okhttp3:okhttp:4.12.0")
implementation("com.squareup.okhttp3:okhttp-sse:4.12.0")
// JSON ํ์ฑ
implementation("com.google.code.gson:gson:2.10.1")
// Coroutines
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3")
}
```
### 2. ๋ฐ์ดํฐ ๋ชจ๋ธ
```kotlin
// ChatRequest.kt
data class ChatRequest(
val message: String,
val mode: String = "bsl", // "bsl" or "general"
val max_tokens: Int = 1024,
val temperature: Float = 0.7f
)
// StreamingResponse.kt
data class StreamingResponse(
val token: String = "",
val done: Boolean = false,
val token_count: Int = 0,
val mode: String = "",
val error: String? = null
)
```
### 3. ChatBIA API ํด๋ผ์ด์ธํธ
```kotlin
// ChatBiaApiClient.kt
import com.google.gson.Gson
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import okhttp3.*
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.RequestBody.Companion.toRequestBody
import okhttp3.sse.EventSource
import okhttp3.sse.EventSourceListener
import okhttp3.sse.EventSources
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine
class ChatBiaApiClient(private val baseUrl: String) {
private val client = OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS) // ์คํธ๋ฆฌ๋ฐ์ ๊ธด ํ์์์
.writeTimeout(30, TimeUnit.SECONDS)
.build()
private val gson = Gson()
/**
* ์คํธ๋ฆฌ๋ฐ ์ฑํ
(๊ถ์ฅ)
* Flow๋ฅผ ํตํด ํ ํฐ ๋จ์๋ก ์ค์๊ฐ ์์
*/
fun chatStream(request: ChatRequest): Flow<StreamingResponse> = flow {
suspendCoroutine<Unit> { continuation ->
val url = "$baseUrl/chat/stream"
// JSON ์์ฒญ body
val jsonBody = gson.toJson(request)
val requestBody = jsonBody.toRequestBody("application/json".toMediaType())
val httpRequest = Request.Builder()
.url(url)
.post(requestBody)
.addHeader("Accept", "text/event-stream")
.build()
// SSE EventSource ์์ฑ
val eventSource = EventSources.createFactory(client)
.newEventSource(httpRequest, object : EventSourceListener() {
override fun onOpen(eventSource: EventSource, response: Response) {
// ์ฐ๊ฒฐ ์ฑ๊ณต
}
override fun onEvent(
eventSource: EventSource,
id: String?,
type: String?,
data: String
) {
try {
val response = gson.fromJson(data, StreamingResponse::class.java)
// Flow๋ก emit
trySend(response)
// ์๋ฃ ์ ์ฐ๊ฒฐ ์ข
๋ฃ
if (response.done) {
eventSource.cancel()
continuation.resume(Unit)
}
} catch (e: Exception) {
eventSource.cancel()
continuation.resumeWithException(e)
}
}
override fun onFailure(
eventSource: EventSource,
t: Throwable?,
response: Response?
) {
continuation.resumeWithException(
t ?: Exception("SSE ์ฐ๊ฒฐ ์คํจ: ${response?.code}")
)
}
override fun onClosed(eventSource: EventSource) {
if (!continuation.isCompleted) {
continuation.resume(Unit)
}
}
})
}
}
/**
* ์ผ๋ฐ ์ฑํ
(๋น์คํธ๋ฆฌ๋ฐ)
* ๊ธด ์๋ต ์ ํ์์์ ์ํ ์์
*/
suspend fun chat(request: ChatRequest): ChatResponse = suspendCoroutine { continuation ->
val url = "$baseUrl/chat"
val jsonBody = gson.toJson(request)
val requestBody = jsonBody.toRequestBody("application/json".toMediaType())
val httpRequest = Request.Builder()
.url(url)
.post(requestBody)
.build()
client.newCall(httpRequest).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
continuation.resumeWithException(e)
}
override fun onResponse(call: Call, response: Response) {
if (response.isSuccessful) {
val body = response.body?.string()
val chatResponse = gson.fromJson(body, ChatResponse::class.java)
continuation.resume(chatResponse)
} else {
continuation.resumeWithException(
Exception("HTTP ${response.code}: ${response.message}")
)
}
}
})
}
data class ChatResponse(
val response: String,
val mode: String,
val tokens: Int
)
}
```
### 4. ViewModel ์ฌ์ฉ ์์
```kotlin
// ChatViewModel.kt
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.launch
class ChatViewModel : ViewModel() {
private val apiClient = ChatBiaApiClient("https://your-hf-space.hf.space")
private val _chatState = MutableStateFlow("")
val chatState: StateFlow<String> = _chatState
private val _isLoading = MutableStateFlow(false)
val isLoading: StateFlow<Boolean> = _isLoading
/**
* ์คํธ๋ฆฌ๋ฐ ์ฑํ
์ ์ก
*/
fun sendStreamingMessage(message: String, mode: String = "bsl") {
viewModelScope.launch {
_isLoading.value = true
_chatState.value = "" // ์ด๊ธฐํ
val request = ChatRequest(
message = message,
mode = mode,
max_tokens = 1024,
temperature = 0.7f
)
apiClient.chatStream(request)
.catch { e ->
_chatState.value = "์ค๋ฅ: ${e.message}"
_isLoading.value = false
}
.collect { response ->
if (response.error != null) {
_chatState.value = "์๋ฒ ์ค๋ฅ: ${response.error}"
_isLoading.value = false
} else if (response.done) {
// ์๋ฃ
_isLoading.value = false
} else {
// ํ ํฐ ์ถ๊ฐ
_chatState.value += response.token
}
}
}
}
}
```
### 5. Compose UI ์์
```kotlin
// ChatScreen.kt
import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
@Composable
fun ChatScreen(viewModel: ChatViewModel = viewModel()) {
val chatState by viewModel.chatState.collectAsState()
val isLoading by viewModel.isLoading.collectAsState()
var inputText by remember { mutableStateOf("") }
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp)
) {
// ์ฑํ
์ถ๋ ฅ
Card(
modifier = Modifier
.fillMaxWidth()
.weight(1f)
) {
Text(
text = chatState,
modifier = Modifier.padding(16.dp)
)
}
Spacer(modifier = Modifier.height(16.dp))
// ์
๋ ฅ ํ๋
Row(
modifier = Modifier.fillMaxWidth()
) {
OutlinedTextField(
value = inputText,
onValueChange = { inputText = it },
modifier = Modifier.weight(1f),
placeholder = { Text("๋ฉ์์ง ์
๋ ฅ...") },
enabled = !isLoading
)
Spacer(modifier = Modifier.width(8.dp))
Button(
onClick = {
if (inputText.isNotBlank()) {
viewModel.sendStreamingMessage(inputText)
inputText = ""
}
},
enabled = !isLoading
) {
Text(if (isLoading) "์ ์ก ์ค..." else "์ ์ก")
}
}
}
}
```
---
## ๐งช ํ
์คํธ ๋ฐฉ๋ฒ
### 1. ๋ก์ปฌ ์๋ฒ ์คํ
```bash
cd ChatBIA-Server
uvicorn main:app --host 0.0.0.0 --port 8000
```
### 2. Python ํ
์คํธ
```bash
python test_streaming.py
```
### 3. ์๋๋ก์ด๋ ์ฑ์์ ์ฐ๊ฒฐ
```kotlin
// ๋ก์ปฌ ํ
์คํธ (์๋ฎฌ๋ ์ดํฐ)
val apiClient = ChatBiaApiClient("http://10.0.2.2:8000")
// ์ค์ ๋๋ฐ์ด์ค (๊ฐ์ ๋คํธ์ํฌ)
val apiClient = ChatBiaApiClient("http://YOUR_IP:8000")
// Hugging Face Spaces (๋ฐฐํฌ ํ)
val apiClient = ChatBiaApiClient("https://your-space.hf.space")
```
---
## ๐ ์๋ต ํ์
### ์คํธ๋ฆฌ๋ฐ ์๋ต (SSE)
```
data: {"token":"์๋
","done":false,"token_count":1}
data: {"token":"ํ์ธ์","done":false,"token_count":2}
data: {"token":"!","done":false,"token_count":3}
data: {"token":"","done":true,"token_count":3,"mode":"bsl"}
```
### ์ต์ข
์๋ต
```json
{
"token": "",
"done": true,
"token_count": 150,
"mode": "bsl"
}
```
### ์ค๋ฅ ์๋ต
```json
{
"error": "์ค๋ฅ ๋ฉ์์ง",
"done": true
}
```
---
## โก ์ฑ๋ฅ ์ต์ ํ ํ
1. **ํ์์์ ์ค์ **
- Connect: 30์ด
- Read: 60์ด (์คํธ๋ฆฌ๋ฐ)
- Write: 30์ด
2. **์ฌ์ฐ๊ฒฐ ๋ก์ง**
```kotlin
fun retryOnFailure(maxRetries: Int = 3) {
var attempts = 0
while (attempts < maxRetries) {
try {
chatStream(request).collect { }
break
} catch (e: Exception) {
attempts++
delay(2000 * attempts) // ์ง์ ๋ฐฑ์คํ
}
}
}
```
3. **๋ฉ๋ชจ๋ฆฌ ๊ด๋ฆฌ**
- Flow๋ฅผ ์ฌ์ฉํ์ฌ ๋ฉ๋ชจ๋ฆฌ ํจ์จ์ ์ผ๋ก ์ฒ๋ฆฌ
- UI ์
๋ฐ์ดํธ๋ StateFlow๋ก ์ต์ ํ
---
## ๐ ๋ฐฐํฌ ํ ์ฌ์ฉ
Hugging Face Spaces์ ๋ฐฐํฌ ํ:
```kotlin
val BASE_URL = "https://your-username-chatbia-server.hf.space"
val apiClient = ChatBiaApiClient(BASE_URL)
```
**์ฃผ์**: Hugging Face Spaces ๋ฌด๋ฃ ํ๋์ CPU๋ง ์ ๊ณต๋๋ฏ๋ก ์๋ต ์๋๊ฐ ๋๋ฆด ์ ์์ต๋๋ค. ์คํธ๋ฆฌ๋ฐ ๋ฐฉ์์ด ๋์ฑ ์ค์ํฉ๋๋ค!
---
## ๐ ๊ด๋ จ ๋ฌธ์
- [FastAPI Streaming](https://fastapi.tiangolo.com/advanced/custom-response/#streamingresponse)
- [OkHttp SSE](https://square.github.io/okhttp/recipes/#server-sent-events)
- [Kotlin Flow](https://kotlinlang.org/docs/flow.html)
|