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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)