Skip to content

Commit

Permalink
Add GTIOrchestrationService and integrate Gson
Browse files Browse the repository at this point in the history
Implemented a new service, GTIOrchestrationService, to handle orchestration logic. Added Gson dependency for JSON processing and integrated it into the chat method to convert GTI responses to JSON. This enhances the functionality by allowing JSON responses to be handled more effectively.
  • Loading branch information
ljhbt committed Sep 18, 2024
1 parent 88f49b0 commit f599581
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
3 changes: 3 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ plugin-version-catalog = "0.8.0"
plugin-kotlin-gradle = "1.9.21"
plugin-openapi-generator = "6.6.0"
plugin-jib = "3.4.3"
gson = "2.8.5"

[libraries]
spring-boot = { module = "org.springframework.boot:spring-boot-dependencies", version.ref = "spring-boot" }
Expand Down Expand Up @@ -94,6 +95,7 @@ plugin-version-catalog = { module = "nl.littlerobots.vcu:plugin", version.ref =
plugin-kotlin-gradle = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "plugin-kotlin-gradle" }
plugin-openapi-generator = { module = "org.openapitools:openapi-generator-gradle-plugin", version.ref = "plugin-openapi-generator" }
plugin-jib = { module = "com.google.cloud.tools:jib-gradle-plugin", version.ref = "plugin-jib" }
gson = {module = "com.google.code.gson:gson", version.ref = "gson"}

[bundles]
logging = [
Expand Down Expand Up @@ -127,6 +129,7 @@ spring-boot-server = [
"spring-cloud-starter",
"spring-sec",
"spring-ui",
"gson"
]
spring-boot-security = [
"keycloak-core",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package de.hbt.routing.service

import com.nimbusds.jose.shaded.gson.Gson
import de.hbt.geofox.gti.model.GRResponse
import de.hbt.geofox.gti.model.GTITime
import de.hbt.geofox.gti.model.SDName
import de.hbt.routing.openai.dto.ChatRequest
import de.hbt.routing.openai.dto.ChatResponse
import de.hbt.routing.openai.dto.Message
import mu.KotlinLogging
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.stereotype.Service
import org.springframework.web.client.RestTemplate

@Service
class GTIOrchestrationService(private val gtiService: GTIService,
@Qualifier("openaiRestTemplate") private val restTemplate: RestTemplate) {

data class ParsedInfo(val from: String, val to: String, val time: String)

fun orchestrate(input: ParsedInfo): String {
val grResponse = doTheGRRequest(input)
val gson = Gson()
val response = chat(gson.toJson(grResponse))
return response
}

fun doTheGRRequest(parsedJson: ParsedInfo): GRResponse {
val start = SDName(name = parsedJson.from, type = SDName.Type.uNKNOWN)
val dest = SDName(name = parsedJson.to, type = SDName.Type.uNKNOWN)
val time = GTITime(date = "heute", time = parsedJson.time)
val gtiResponse = gtiService.getRoute(start, dest, time)
return gtiResponse
}

fun chat(prompt: String): String {
// create a request
val request = ChatRequest(MODEL, prompt, "user")
request.messages.addFirst(SYSTEM_PROMPT_MESSAGE)

// call the API
val response = restTemplate.postForObject(COMPLETIONS_API, request, ChatResponse::class.java)

if (response?.choices == null || response.choices.isEmpty()) {
return "No response"
}

// return the first response
return response.choices.first().message.content
}

companion object {
private val log = KotlinLogging.logger {}
private const val MODEL = "gpt-4o"
private const val COMPLETIONS_API = "https://api.openai.com/v1/chat/completions"
private const val SYSTEM_PROMPT = """
You are a smart assistant that helps users with parsing some routing information.
Your goal is to receive a json object and convert the information to natural language.
"""
private val SYSTEM_PROMPT_MESSAGE = Message("system", SYSTEM_PROMPT)
}
}

0 comments on commit f599581

Please sign in to comment.