-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
768 additions
and
29 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
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
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
25 changes: 25 additions & 0 deletions
25
openai-client/src/commonMain/kotlin/com.aallam.openai.client/Chat.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,25 @@ | ||
package com.aallam.openai.client | ||
|
||
import com.aallam.openai.api.BetaOpenAI | ||
import com.aallam.openai.api.chat.ChatCompletion | ||
import com.aallam.openai.api.chat.ChatCompletionChunk | ||
import com.aallam.openai.api.chat.ChatCompletionRequest | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
/** | ||
* Given a chat conversation, the model will return a chat completion response. | ||
*/ | ||
public interface Chat { | ||
|
||
/** | ||
* Creates a completion for the chat message. | ||
*/ | ||
@BetaOpenAI | ||
public suspend fun chatCompletion(request: ChatCompletionRequest): ChatCompletion | ||
|
||
/** | ||
* Stream variant of [chatCompletion]. | ||
*/ | ||
@BetaOpenAI | ||
public fun chatCompletions(request: ChatCompletionRequest): Flow<ChatCompletionChunk> | ||
} |
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
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
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
48 changes: 48 additions & 0 deletions
48
openai-client/src/commonMain/kotlin/com.aallam.openai.client/internal/api/ChatApi.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,48 @@ | ||
package com.aallam.openai.client.internal.api | ||
|
||
import com.aallam.openai.api.chat.ChatCompletion | ||
import com.aallam.openai.api.chat.ChatCompletionChunk | ||
import com.aallam.openai.api.chat.ChatCompletionRequest | ||
import com.aallam.openai.client.Chat | ||
import com.aallam.openai.client.internal.extension.streamEventsFrom | ||
import com.aallam.openai.client.internal.extension.streamRequestOf | ||
import com.aallam.openai.client.internal.http.HttpRequester | ||
import com.aallam.openai.client.internal.http.perform | ||
import io.ktor.client.call.* | ||
import io.ktor.client.request.* | ||
import io.ktor.http.* | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.flow | ||
|
||
internal class ChatApi(private val requester: HttpRequester) : Chat { | ||
override suspend fun chatCompletion(request: ChatCompletionRequest): ChatCompletion { | ||
return requester.perform { | ||
it.post { | ||
url(path = ChatCompletionsPathV1) | ||
setBody(request) | ||
contentType(ContentType.Application.Json) | ||
}.body() | ||
} | ||
} | ||
|
||
override fun chatCompletions(request: ChatCompletionRequest): Flow<ChatCompletionChunk> { | ||
val builder = HttpRequestBuilder().apply { | ||
method = HttpMethod.Post | ||
url(path = ChatCompletionsPathV1) | ||
setBody(streamRequestOf(request)) | ||
contentType(ContentType.Application.Json) | ||
accept(ContentType.Text.EventStream) | ||
headers { | ||
append(HttpHeaders.CacheControl, "no-cache") | ||
append(HttpHeaders.Connection, "keep-alive") | ||
} | ||
} | ||
return flow { | ||
requester.perform(builder) { response -> streamEventsFrom(response) } | ||
} | ||
} | ||
|
||
companion object { | ||
private const val ChatCompletionsPathV1 = "v1/chat/completions" | ||
} | ||
} |
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
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
51 changes: 51 additions & 0 deletions
51
openai-client/src/commonTest/kotlin/com/aallam/openai/client/TestChatCompletions.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,51 @@ | ||
package com.aallam.openai.client | ||
|
||
import com.aallam.openai.api.chat.ChatCompletion | ||
import com.aallam.openai.api.chat.ChatCompletionChunk | ||
import com.aallam.openai.api.chat.ChatRole | ||
import com.aallam.openai.api.chat.chatCompletionRequest | ||
import com.aallam.openai.api.model.ModelId | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.onEach | ||
import kotlinx.coroutines.test.runTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertNotEquals | ||
import kotlin.test.assertTrue | ||
|
||
class TestChatCompletions : TestOpenAI() { | ||
|
||
@Test | ||
fun chatCompletions() { | ||
runTest { | ||
val request = chatCompletionRequest { | ||
model = ModelId("gpt-3.5-turbo") | ||
messages { | ||
message { | ||
role = ChatRole.System | ||
content = "You are a helpful assistant.!" | ||
} | ||
message { | ||
role = ChatRole.User | ||
content = "Who won the world series in 2020?" | ||
} | ||
message { | ||
role = ChatRole.Assistant | ||
content = "The Los Angeles Dodgers won the World Series in 2020." | ||
} | ||
message { | ||
role = ChatRole.User | ||
content = "Where was it played?" | ||
} | ||
} | ||
} | ||
|
||
val completion = openAI.chatCompletion(request) | ||
assertTrue { completion.choices.isNotEmpty() } | ||
|
||
val results = mutableListOf<ChatCompletionChunk>() | ||
openAI.chatCompletions(request).onEach { results += it }.launchIn(this).join() | ||
|
||
assertNotEquals(0, results.size) | ||
} | ||
} | ||
} |
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
27 changes: 27 additions & 0 deletions
27
openai-core/src/commonMain/kotlin/com.aallam.openai.api/BetaOpenAI.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,27 @@ | ||
package com.aallam.openai.api | ||
|
||
import kotlin.annotation.AnnotationTarget.* | ||
|
||
/** | ||
* This annotation marks a library API as beta. | ||
* | ||
* Any usage of a declaration annotated with `@BetaOpenAI` must be accepted either by annotating that | ||
* usage with the [OptIn] annotation, e.g. `@OptIn(BetaOpenAI::class)`, or by using the compiler | ||
* argument `-Xopt-in=com.aallam.openai.api.BetaOpenAI`. | ||
*/ | ||
@Target( | ||
CLASS, | ||
ANNOTATION_CLASS, | ||
PROPERTY, | ||
FIELD, | ||
LOCAL_VARIABLE, | ||
VALUE_PARAMETER, | ||
CONSTRUCTOR, | ||
FUNCTION, | ||
PROPERTY_GETTER, | ||
PROPERTY_SETTER, | ||
TYPEALIAS | ||
) | ||
@Retention(AnnotationRetention.BINARY) | ||
@RequiresOptIn(message = "This API is marked beta by OpenAI, It can be incompatibly changed in the future.") | ||
public annotation class BetaOpenAI |
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
28 changes: 28 additions & 0 deletions
28
openai-core/src/commonMain/kotlin/com.aallam.openai.api/chat/ChatChoice.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,28 @@ | ||
package com.aallam.openai.api.chat; | ||
|
||
import com.aallam.openai.api.BetaOpenAI | ||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
/** | ||
* A completion generated by OpenAI. | ||
* | ||
* [documentation](https://platform.openai.com/docs/api-reference/chat/create) | ||
*/ | ||
@BetaOpenAI | ||
@Serializable | ||
public data class ChatChoice internal constructor( | ||
/** | ||
* Chat choice index. | ||
*/ | ||
@SerialName("index") public val index: Int? = null, | ||
/** | ||
* The generated chat message. | ||
*/ | ||
@SerialName("message") public val message: ChatMessage? = null, | ||
|
||
/** | ||
* The reason why OpenAI stopped generating. | ||
*/ | ||
@SerialName("finish_reason") public val finishReason: String? = null, | ||
) |
Oops, something went wrong.