API 1.2+4 open ai chat completion fix
This commit is contained in:
@@ -1,13 +1,12 @@
|
||||
package com.aitrainer.api.controller
|
||||
|
||||
import com.aallam.openai.api.BetaOpenAI
|
||||
import com.aallam.openai.api.chat.ChatMessage
|
||||
import com.aitrainer.api.model.OpenAI
|
||||
import com.aitrainer.api.model.OpenAIChat
|
||||
import com.aitrainer.api.openai.OpenAIService
|
||||
import com.google.gson.Gson
|
||||
import kotlinx.coroutines.*
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.beans.factory.annotation.Value
|
||||
import org.springframework.web.bind.annotation.*
|
||||
|
||||
@RestController
|
||||
@@ -17,9 +16,9 @@ class OpenAIController() {
|
||||
|
||||
@OptIn(DelicateCoroutinesApi::class)
|
||||
@PostMapping("/openai/completion")
|
||||
fun getOpenAIResponse(@RequestBody question: String) : String {
|
||||
var result = ""
|
||||
val openAIService = OpenAIService(null, null)
|
||||
fun getOpenAIResponse(@RequestBody question: String, @Value("\${openai.key}") openaiKey: String) : String {
|
||||
var result: String
|
||||
val openAIService = OpenAIService(openaiKey, null, null)
|
||||
val deferred = GlobalScope.async {
|
||||
openAIService.completion(question)
|
||||
}
|
||||
@@ -32,9 +31,9 @@ class OpenAIController() {
|
||||
|
||||
@OptIn(DelicateCoroutinesApi::class)
|
||||
@PostMapping("/openai/completion_with_model")
|
||||
fun getOpenAIResponseWithModel(@RequestBody openai: OpenAI) : String {
|
||||
var result = ""
|
||||
val openAIService = OpenAIService(openai.modelName, openai.temperature)
|
||||
fun getOpenAIResponseWithModel(@RequestBody openai: OpenAI, @Value("\${openai.key}") openaiKey: String) : String {
|
||||
var result: String
|
||||
val openAIService = OpenAIService(openaiKey, openai.modelName, openai.temperature)
|
||||
val deferred = GlobalScope.async {
|
||||
openAIService.completion(openai.question)
|
||||
}
|
||||
@@ -47,24 +46,23 @@ class OpenAIController() {
|
||||
|
||||
@OptIn(BetaOpenAI::class, DelicateCoroutinesApi::class)
|
||||
@PostMapping("/openai/chat_completion")
|
||||
fun getOpenAIChatCompletion(@RequestBody openai: OpenAIChat) : String {
|
||||
var result = ""
|
||||
val openAIService = OpenAIService(openai.modelName, openai.temperature)
|
||||
fun getOpenAIChatCompletion(@RequestBody openai: OpenAIChat, @Value("\${openai.key}") openaiKey: String, ) : String {
|
||||
var result: String
|
||||
val openAIService = OpenAIService(openaiKey, openai.modelName, openai.temperature)
|
||||
val deferred = GlobalScope.async {
|
||||
openAIService.chatCompletion(openai.messages)
|
||||
}
|
||||
runBlocking {
|
||||
result = deferred.await().toString()
|
||||
println("Result: $result" )
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
@OptIn(DelicateCoroutinesApi::class)
|
||||
@GetMapping("/openai/list_models")
|
||||
fun getOpenAIModels(): MutableList<String> {
|
||||
var result = mutableListOf<String>()
|
||||
val openAIService = OpenAIService(null, null)
|
||||
fun getOpenAIModels(@Value("\${openai.key}") openaiKey: String): MutableList<String> {
|
||||
var result: MutableList<String>
|
||||
val openAIService = OpenAIService(openaiKey,null, null)
|
||||
val deferred = GlobalScope.async {
|
||||
openAIService.getModels()
|
||||
}
|
||||
|
||||
@@ -14,31 +14,23 @@ import com.aallam.openai.client.OpenAIConfig
|
||||
import com.google.gson.Gson
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.util.Properties
|
||||
|
||||
class OpenAIService(private val modelName: String?, private val temperature: Double?) {
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value
|
||||
import org.springframework.stereotype.Service
|
||||
@Service
|
||||
class OpenAIService(@Value("\${openai.key}") private val openaiKey: String, private val modelName: String?, private val temperature: Double?) {
|
||||
|
||||
private var openAI: OpenAI? = null
|
||||
var model: Model? = null
|
||||
private val properties = Properties()
|
||||
|
||||
init {
|
||||
val inputStream = ClassLoader.getSystemResourceAsStream("application.properties")
|
||||
properties.load(inputStream)
|
||||
inputStream?.close()
|
||||
}
|
||||
|
||||
private var modelId: ModelId? = null
|
||||
private suspend fun connect(modelName: String) {
|
||||
val config = OpenAIConfig(
|
||||
token = properties.getProperty("openai.key"),
|
||||
token = openaiKey,
|
||||
logLevel = LogLevel.All
|
||||
)
|
||||
openAI = OpenAI(config)
|
||||
modelId = ModelId(modelName)
|
||||
model = openAI!!.model(modelId!!)
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -112,11 +104,12 @@ class OpenAIService(private val modelName: String?, private val temperature: Dou
|
||||
|
||||
suspend fun getModels(): MutableList<String> {
|
||||
return withContext(Dispatchers.IO) {
|
||||
val list: MutableList<String> = mutableListOf()
|
||||
if (openAI == null) {
|
||||
openAI = OpenAI(token = properties.getProperty("openai.key"))
|
||||
openAI = OpenAI(token = openaiKey)
|
||||
}
|
||||
|
||||
val list: MutableList<String> = mutableListOf()
|
||||
|
||||
openAI!!.models().forEach {
|
||||
println(it)
|
||||
list.add(it.id.id)
|
||||
|
||||
Reference in New Issue
Block a user