-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GTIOrchestrationService and integrate Gson
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
Showing
2 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
module-server/src/main/kotlin/de/hbt/routing/service/GTIOrchestrationService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |