API 1.2+3 open ai chat completion
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
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.web.bind.annotation.*
|
||||
@@ -41,6 +45,21 @@ class OpenAIController() {
|
||||
return result
|
||||
}
|
||||
|
||||
@OptIn(BetaOpenAI::class, DelicateCoroutinesApi::class)
|
||||
@PostMapping("/openai/chat_completion")
|
||||
fun getOpenAIChatCompletion(@RequestBody openai: OpenAIChat) : String {
|
||||
var result = ""
|
||||
val openAIService = OpenAIService(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> {
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.aitrainer.api.model
|
||||
|
||||
import com.aallam.openai.api.BetaOpenAI
|
||||
import com.aallam.openai.api.chat.ChatMessage
|
||||
import com.google.gson.annotations.Expose
|
||||
import jakarta.persistence.*
|
||||
import org.springframework.lang.NonNull
|
||||
|
||||
@Entity
|
||||
data class OpenAIChat @OptIn(BetaOpenAI::class) constructor(
|
||||
@Expose @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @get: NonNull var id: Long = 0,
|
||||
@Expose @get: NonNull var messages: String,
|
||||
@Expose @get: NonNull var modelName: String? = null,
|
||||
@Expose @get: NonNull var temperature: Double? = null,
|
||||
)
|
||||
@@ -1,5 +1,9 @@
|
||||
package com.aitrainer.api.openai
|
||||
|
||||
import com.aallam.openai.api.BetaOpenAI
|
||||
import com.aallam.openai.api.chat.ChatCompletion
|
||||
import com.aallam.openai.api.chat.ChatCompletionRequest
|
||||
import com.aallam.openai.api.chat.ChatMessage
|
||||
import com.aallam.openai.client.OpenAI
|
||||
import com.aallam.openai.api.completion.CompletionRequest
|
||||
import com.aallam.openai.api.completion.TextCompletion
|
||||
@@ -7,6 +11,7 @@ import com.aallam.openai.api.logging.LogLevel
|
||||
import com.aallam.openai.api.model.Model
|
||||
import com.aallam.openai.api.model.ModelId
|
||||
import com.aallam.openai.client.OpenAIConfig
|
||||
import com.google.gson.Gson
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.util.Properties
|
||||
@@ -36,6 +41,12 @@ class OpenAIService(private val modelName: String?, private val temperature: Dou
|
||||
|
||||
}
|
||||
|
||||
|
||||
/* models:
|
||||
gpt-3.5-turbo chat/completion
|
||||
text-davinci-003 completion
|
||||
*/
|
||||
|
||||
suspend fun completion(question: String): String {
|
||||
return withContext(Dispatchers.IO) {
|
||||
var realModelName = "text-davinci-003"
|
||||
@@ -64,6 +75,41 @@ class OpenAIService(private val modelName: String?, private val temperature: Dou
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(BetaOpenAI::class)
|
||||
suspend fun chatCompletion(chatMessagesJson: String): String? {
|
||||
return withContext(Dispatchers.IO) {
|
||||
val gson = Gson()
|
||||
val messages = gson.fromJson(chatMessagesJson, Array<ChatMessage>::class.java).toList()
|
||||
val lastQuestion = messages.last().content
|
||||
|
||||
var lengthQuestion = 0
|
||||
for ( message in messages ) {
|
||||
lengthQuestion += message.content.length
|
||||
}
|
||||
|
||||
val realModelName = "gpt-3.5-turbo"
|
||||
var realTemperature = 0.1
|
||||
if ( temperature != null ) {
|
||||
realTemperature = temperature
|
||||
}
|
||||
if (openAI == null) {
|
||||
connect(realModelName)
|
||||
}
|
||||
println("OpenAI Chat Last Question: $lastQuestion")
|
||||
val completionRequest = ChatCompletionRequest(
|
||||
model = ModelId(realModelName),
|
||||
messages = messages,
|
||||
maxTokens = 4096 - lengthQuestion,
|
||||
temperature = realTemperature,
|
||||
)
|
||||
val completion: ChatCompletion = openAI!!.chatCompletion(completionRequest)
|
||||
|
||||
val result = completion.choices[0].message?.content
|
||||
print(result)
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun getModels(): MutableList<String> {
|
||||
return withContext(Dispatchers.IO) {
|
||||
if (openAI == null) {
|
||||
|
||||
Reference in New Issue
Block a user