From 56bcf4aaaa583417ac096de8284073b93e668645 Mon Sep 17 00:00:00 2001 From: Enrico Costanzi Date: Mon, 20 May 2024 16:25:48 +0200 Subject: [PATCH] refactor + frontend --- pom.xml | 5 ++ src/main/kotlin/ChatGPTChatClient.kt | 21 ++----- src/main/kotlin/Configs.kt | 2 +- src/main/kotlin/Main.kt | 16 ++--- src/main/kotlin/OpenllamaChatClient.kt | 19 ++---- src/main/resources/application.properties | 8 +-- src/main/resources/public/index.html | 77 +++++++++++++++++++++++ 7 files changed, 108 insertions(+), 40 deletions(-) create mode 100644 src/main/resources/public/index.html diff --git a/pom.xml b/pom.xml index bccb190..3335963 100644 --- a/pom.xml +++ b/pom.xml @@ -121,6 +121,11 @@ json-path 2.9.0 + + com.fasterxml.jackson.module + jackson-module-kotlin + 2.17.1 + org.jetbrains.kotlin kotlin-test-junit5 diff --git a/src/main/kotlin/ChatGPTChatClient.kt b/src/main/kotlin/ChatGPTChatClient.kt index 3159138..8b53dc6 100644 --- a/src/main/kotlin/ChatGPTChatClient.kt +++ b/src/main/kotlin/ChatGPTChatClient.kt @@ -14,7 +14,7 @@ class ChatGPTChatClient(private val apiKey: String, private val model: String) : private val client = OkHttpClient() - override fun chat(prompt: String): String { + override fun chat(prompt: String) : String { val url = "https://api.openai.com/v1/chat/completions" val requestBody = """ @@ -40,21 +40,12 @@ class ChatGPTChatClient(private val apiKey: String, private val model: String) : .post(requestBody.toRequestBody("application/json".toMediaType())) .build() - try { - client.newCall(request).execute().use { response -> - if (!response.isSuccessful) throw IOException("Unexpected code $response") - - val responseBody = response.body?.string() - if (responseBody != null) { - return JsonPath.read(responseBody, "$.choices[0].message.content") - } + client.newCall(request).execute().use { response -> + if (!response.isSuccessful) { + logger.error("Unexpected response") + throw RuntimeException("Unexpected code $response") } - } catch (e: Exception) { - logger.error("ChatGPT error", e) - return "Error querying ChatGPT: ${e.message}" + return JsonPath.read(response.body?.string(), "$.choices[0].message.content") } - - logger.warn("No response from ChatGPT") - return "Error: No response from the server." } } diff --git a/src/main/kotlin/Configs.kt b/src/main/kotlin/Configs.kt index ff510b5..13fb4f8 100644 --- a/src/main/kotlin/Configs.kt +++ b/src/main/kotlin/Configs.kt @@ -58,7 +58,7 @@ class Configs { } fun getProperty(propertyName: String): String { - return properties.getProperty(propertyName); + return properties.getProperty(propertyName, "NONE"); } fun getIntegerProperty(propertyName: String): Int { diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt index 0d8f3cb..7b1c706 100644 --- a/src/main/kotlin/Main.kt +++ b/src/main/kotlin/Main.kt @@ -1,5 +1,6 @@ package org.example +import com.jayway.jsonpath.JsonPath import io.javalin.Javalin import org.slf4j.LoggerFactory @@ -9,20 +10,21 @@ fun main() { val config = Configs(); - val app = Javalin.create(/*config*/) - .get("/") { ctx -> ctx.result(config.getProperty("CUSTOM_MESSAGE")) } + val app = Javalin.create { config -> + config.staticFiles.add("/public") + } .start(config.getIntegerProperty("SERVER_PORT")) - app.get("/chat") { ctx -> - val prompt = ctx.queryParam("prompt") + app.post("/chat") { ctx -> + val prompt = JsonPath.read(ctx.body(), "$.prompt") logger.info("Querying chat proxy with promt '$prompt'") var chat = ChatProxy() - val resultString = prompt?.let { chat.chat(it) } + val resultString = prompt?.let { chat.chat(prompt) } logger.info("Received results from chat client") if (resultString != null) { - ctx.result(resultString) + ctx.json(mapOf("result" to resultString)) } else { - ctx.result("no result") + ctx.json(mapOf("result" to "no result")) } } diff --git a/src/main/kotlin/OpenllamaChatClient.kt b/src/main/kotlin/OpenllamaChatClient.kt index e09edce..a936d8e 100644 --- a/src/main/kotlin/OpenllamaChatClient.kt +++ b/src/main/kotlin/OpenllamaChatClient.kt @@ -6,7 +6,6 @@ import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.RequestBody.Companion.toRequestBody import org.slf4j.LoggerFactory -import java.io.IOException import java.time.Duration class OpenllamaChatClient(private val baseUrl: String, private val model: String) : ChatClient { @@ -36,20 +35,14 @@ class OpenllamaChatClient(private val baseUrl: String, private val model: String .post(requestBody.toRequestBody("application/json".toMediaType())) .build() - try { - client.newCall(request).execute().use { response -> - if (!response.isSuccessful) throw IOException("Unexpected code $response") + client.newCall(request).execute().use { response -> - val responseBody = response.body?.string() - if (responseBody != null) { - return JsonPath.read(responseBody, "$.response") - } + if (!response.isSuccessful) { + logger.error("Error while fetching response: " + response.body?.string()) + throw RuntimeException("Unexpected code $response") } - } catch (e: Exception) { - logger.error("Error querying openllama: ${e.message}", e) - return "Error querying openllama: ${e.message}" + + return JsonPath.read(response.body?.string(), "$.response") } - logger.warn("No response from the server.") - return "Error: No response from the server." } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 76bcc2f..c919e31 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,10 +1,10 @@ SERVER_PORT=8080 CUSTOM_MESSAGE="Hello World From Configs" -#CHAT_CLIENT=CHATGPT -#MODEL=gpt-3.5-turbo +CHAT_CLIENT=CHATGPT +MODEL=gpt-3.5-turbo CHATGPT_API_KEY=NOT HERE! -CHAT_CLIENT=OPENLLAMA +#CHAT_CLIENT=OPENLLAMA OPENLLAMA_URL=http://127.0.0.1:11434 -MODEL=llama3 +#MODEL=llama3 diff --git a/src/main/resources/public/index.html b/src/main/resources/public/index.html new file mode 100644 index 0000000..7b470df --- /dev/null +++ b/src/main/resources/public/index.html @@ -0,0 +1,77 @@ + + + + + + AI Proxy + + + + + +
+

AI Proxy

+
+ +
+ +
+
+ + + + + + + +