Skip to content

Commit

Permalink
feat(FIM): legacy completions api supported for FIM
Browse files Browse the repository at this point in the history
 - refactor chat api name for better distinguish with legacy completions api
  • Loading branch information
hanrw committed Jun 1, 2024
1 parent 017e26c commit 5f4fefb
Show file tree
Hide file tree
Showing 28 changed files with 595 additions and 90 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Gradle
org.gradle.jvmargs=-Xmx2048M -Dfile.encoding=UTF-8 -Dkotlin.daemon.jvm.options\="-Xmx2048M"
org.gradle.jvmargs=-Xmx4048M -Dfile.encoding=UTF-8 -Dkotlin.daemon.jvm.options\="-Xmx4048M"
org.gradle.caching=true
org.gradle.configuration-cache=true
#Kotlin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package com.tddworks.openai.api
import com.tddworks.di.getInstance
import com.tddworks.openai.api.chat.api.Chat
import com.tddworks.openai.api.images.api.Images
import com.tddworks.openai.api.legacy.completions.api.Completion
import com.tddworks.openai.api.legacy.completions.api.Completions

interface OpenAI : Chat, Images {
interface OpenAI : Chat, Images, Completions {
companion object {
const val BASE_URL = "api.openai.com"
}
}

class OpenAIApi : OpenAI, Chat by getInstance(), Images by getInstance()
class OpenAIApi : OpenAI, Chat by getInstance(), Images by getInstance(),
Completions by getInstance()
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ interface Chat {
* @param request The request to create a chat completion.
* @return The chat completion.
*/
suspend fun completions(request: ChatCompletionRequest): ChatCompletion
suspend fun chatCompletions(request: ChatCompletionRequest): ChatCompletion


/**
* Stream a chat completion.
* @param request The request to stream a chat completion.
* @return The chat completion chunks as a stream
*/
fun streamCompletions(request: ChatCompletionRequest): Flow<ChatCompletionChunk>
fun streamChatCompletions(request: ChatCompletionRequest): Flow<ChatCompletionChunk>

companion object {
const val CHAT_COMPLETIONS_PATH = "/v1/chat/completions"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ data class ChatCompletionRequest(
*/
@SerialName("top_p")
val topP: Double? = null,
val v: String? = null,
val stream: Boolean? = null,

/**
* array
Expand Down Expand Up @@ -251,7 +253,7 @@ data class ChatCompletionRequest(
@SerialName("user")
val user: String? = null,

) : StreamableRequest {
) {

companion object {
fun chatCompletionRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ value class Model(val value: String) {
*/
val GPT_3_5_TURBO_0125 = Model("gpt-3.5-turbo-0125")

/**
* Similar capabilities as GPT-3 era models. Compatible with legacy Completions endpoint and not Chat Completions.
* 4,096 tokens Up to Sep 2021
*/
val GPT_3_5_TURBO_INSTRUCT = Model("gpt-3.5-turbo-instruct")

/**
*
* GPT-4 Turbo with Vision
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import kotlinx.serialization.ExperimentalSerializationApi
class DefaultChatApi(
private val requester: HttpRequester,
) : Chat {
override suspend fun completions(request: ChatCompletionRequest): ChatCompletion {
override suspend fun chatCompletions(request: ChatCompletionRequest): ChatCompletion {
return requester.performRequest<ChatCompletion> {
method = HttpMethod.Post
url(path = CHAT_COMPLETIONS_PATH)
Expand All @@ -31,11 +31,11 @@ class DefaultChatApi(
}
}

override fun streamCompletions(request: ChatCompletionRequest): Flow<ChatCompletionChunk> {
override fun streamChatCompletions(request: ChatCompletionRequest): Flow<ChatCompletionChunk> {
return requester.streamRequest<ChatCompletionChunk> {
method = HttpMethod.Post
url(path = CHAT_COMPLETIONS_PATH)
setBody(request.asStreamRequest())
setBody(request.copy(stream = true))
contentType(ContentType.Application.Json)
accept(ContentType.Text.EventStream)
headers {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.tddworks.openai.api.legacy.completions.api

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class Completion(
val id: String,
val choices: List<CompletionChoice>,
val created: Int,
val model: String,
@SerialName("system_fingerprint")
val systemFingerprint: String?,
val `object`: String?,
val usage: Usage?
) {
companion object {
fun dummy() = Completion(
id = "id",
choices = emptyList(),
created = 0,
model = "model",
systemFingerprint = "systemFingerprint",
`object` = "object",
usage = Usage()
)
}
}

@Serializable
data class CompletionChoice(
@SerialName("text")
val text: String,
@SerialName("index")
val index: Int,
@SerialName("logprobs")
val logprobs: Map<String, String>?,
@SerialName("finish_reason")
val finishReason: String
)

@Serializable
data class Usage(
@SerialName("prompt_tokens")
val promptTokens: Int? = null,
@SerialName("completion_tokens")
val completionTokens: Int? = null,
@SerialName("total_tokens")
val totalTokens: Int? = null,
)
Loading

0 comments on commit 5f4fefb

Please sign in to comment.