Zack Saadioui
8/27/2024
1
2
bash
brew install ollama
1
2
bash
ollama list
1
2
bash
ollama pull phi3
1
build.gradle.kts
1
2
3
4
5
6
7
8
9
10
11
kotlin
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib")
implementation("com.squareup.okhttp3:okhttp:4.9.1")
implementation("org.json:json:20210307")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2")
testImplementation(kotlin("test"))
testImplementation("org.junit.jupiter:junit-jupiter-api:5.9.0")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.9.0")
testImplementation("com.squareup.okhttp3:mockwebserver:4.9.1")
}
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
fun streamResponse(prompt: String, onResponse: (String) -> Unit, onComplete: () -> Unit, onError: (Exception) -> Unit) { val requestBody = JSONObject().put("model", "phi3").put("prompt", prompt).put("stream", true).toString() .toRequestBody("application/json".toMediaType()) val request = Request.Builder().url(baseUrl).post(requestBody).build() client.newCall(request).enqueue(object : Callback { override fun onFailure(call: Call, e: IOException) { onError(e) } override fun onResponse(call: Call, response: Response) { if (!response.isSuccessful) { onError(IOException("Unexpected code $response")) return } response.body?.use { responseBody -> val source = responseBody.source() while (!source.exhausted()) { val line = source.readUtf8Line() if (line != null) { val jsonResponse = JSONObject(line) if (jsonResponse.has("response")) { onResponse(jsonResponse.getString("response")) } } } onComplete() } } }) }
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
fun start() = runBlocking { while (true) { print("You: ") val userInput = readLine() if (userInput.isNullOrEmpty()) break conversationHistory.add("You: $userInput") val context = conversationHistory.joinToString("\n") var completeResponse = "" ollamaClient.streamResponse(context, onResponse = { responseFragment -> completeResponse += responseFragment print("\rOllama: $completeResponse") }, onComplete = { println() conversationHistory.add("Ollama: $completeResponse") print("You: ") }, onError = { e -> println("\nOllama: Error - ${e.message}") print("You: ") }) } }
1
2
3
4
5
6
kotlin
fun main() {
val ollamaClient = OllamaClient()
val conversationHandler = ConversationHandler(ollamaClient)
conversationHandler.start()
}
1
2
3
4
5
6
7
8
9
10
11
12
13
kotlin
@Test
fun `test streamResponse returns expected response`() {
val responseChunks = listOf(
JSONObject().put("response", "Hello").toString(),
JSONObject().put("response", " there").toString(),
JSONObject().put("response", ", how can I help?").toString()
)
responseChunks.forEach { chunk ->
mockWebServer.enqueue(MockResponse().setBody(chunk).setResponseCode(200))
}
... // setup similar to earlier tests
}
Copyright © Arsturn 2025