1
1
package com.xebia.functional.xef.conversation.llm.openai
2
2
3
3
import com.aallam.openai.api.BetaOpenAI
4
+ import com.aallam.openai.api.LegacyOpenAI
4
5
import com.aallam.openai.api.chat.*
5
6
import com.aallam.openai.api.chat.ChatChunk as OpenAIChatChunk
6
7
import com.aallam.openai.api.chat.ChatCompletionChunk as OpenAIChatCompletionChunk
@@ -58,9 +59,10 @@ class OpenAIModel(
58
59
headers = mapOf (" Authorization" to " Bearer $openAI .token" )
59
60
)
60
61
62
+ @OptIn(LegacyOpenAI ::class )
61
63
override suspend fun createCompletion (request : CompletionRequest ): CompletionResult {
62
64
fun completionChoice (it : OpenAIChoice ): CompletionChoice =
63
- CompletionChoice (it.text, it.index, null , it.finishReason)
65
+ CompletionChoice (it.text, it.index, null , it.finishReason.value )
64
66
65
67
val response = client.completion(toCompletionRequest(request))
66
68
return CompletionResult (
@@ -73,7 +75,6 @@ class OpenAIModel(
73
75
)
74
76
}
75
77
76
- @OptIn(BetaOpenAI ::class )
77
78
override suspend fun createChatCompletion (
78
79
request : ChatCompletionRequest
79
80
): ChatCompletionResponse {
@@ -86,8 +87,8 @@ class OpenAIModel(
86
87
87
88
fun toChoice (choice : ChatChoice ): Choice =
88
89
Choice (
89
- message = choice.message?. let { chatMessage(it) } ,
90
- finishReason = choice.finishReason,
90
+ message = chatMessage( choice.message) ,
91
+ finishReason = choice.finishReason.value ,
91
92
index = choice.index,
92
93
)
93
94
@@ -102,7 +103,6 @@ class OpenAIModel(
102
103
)
103
104
}
104
105
105
- @OptIn(BetaOpenAI ::class )
106
106
override suspend fun createChatCompletions (
107
107
request : ChatCompletionRequest
108
108
): Flow <ChatCompletionChunk > {
@@ -114,7 +114,7 @@ class OpenAIModel(
114
114
)
115
115
116
116
fun chatChunk (chunk : OpenAIChatChunk ): ChatChunk =
117
- ChatChunk (chunk.index, chunk.delta?. let { chatDelta(it) } , chunk.finishReason)
117
+ ChatChunk (chunk.index, chatDelta( chunk.delta) , chunk.finishReason?.value )
118
118
119
119
fun chatCompletionChunk (response : OpenAIChatCompletionChunk ): ChatCompletionChunk =
120
120
ChatCompletionChunk (
@@ -128,7 +128,6 @@ class OpenAIModel(
128
128
return client.chatCompletions(toChatCompletionRequest(request)).map { chatCompletionChunk(it) }
129
129
}
130
130
131
- @OptIn(BetaOpenAI ::class )
132
131
override suspend fun createChatCompletionWithFunctions (
133
132
request : ChatCompletionRequest
134
133
): ChatCompletionResponseWithFunctions {
@@ -169,8 +168,8 @@ class OpenAIModel(
169
168
170
169
fun choiceWithFunctions (choice : ChatChoice ): ChoiceWithFunctions =
171
170
ChoiceWithFunctions (
172
- message = choice.message?. let { fromOpenAI(it) } ,
173
- finishReason = choice.finishReason,
171
+ message = fromOpenAI( choice.message) ,
172
+ finishReason = choice.finishReason.value ,
174
173
index = choice.index,
175
174
)
176
175
@@ -243,7 +242,6 @@ class OpenAIModel(
243
242
totalTokens = usage?.totalTokens,
244
243
)
245
244
246
- @OptIn(BetaOpenAI ::class )
247
245
private fun toRole (it : ChatRole ? ) =
248
246
when (it) {
249
247
ChatRole .User -> Role .USER
@@ -253,15 +251,13 @@ class OpenAIModel(
253
251
else -> Role .ASSISTANT
254
252
}
255
253
256
- @OptIn(BetaOpenAI ::class )
257
254
private fun fromRole (it : Role ) =
258
255
when (it) {
259
256
Role .USER -> ChatRole .User
260
257
Role .ASSISTANT -> ChatRole .Assistant
261
258
Role .SYSTEM -> ChatRole .System
262
259
}
263
260
264
- @OptIn(BetaOpenAI ::class )
265
261
private fun toChatCompletionRequest (request : ChatCompletionRequest ): OpenAIChatCompletionRequest =
266
262
chatCompletionRequest {
267
263
model = ModelId (request.model)
0 commit comments