-
Notifications
You must be signed in to change notification settings - Fork 10
[CM-1087] Implement request #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
35a0bca
[CM-1087] Implement completion request api
osugikoji a49bfcb
Merge remote-tracking branch 'origin' into jira/CM-1087/implement-req…
osugikoji 9565f3f
Add Unit tests
osugikoji 06ca5c4
Add kDoc and adjust chat log storage
osugikoji 7d81715
Remove chat gpt from ios sample
osugikoji 13c2d68
Add kDOc and add use case layer
osugikoji 8a93a86
- Added unit test on CompletionUseCase
osugikoji 3f3bac4
Change enableChatStorage default flag to false
osugikoji File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,17 +1,59 @@ | ||
package co.yml.ychatgpt | ||
|
||
import co.yml.ychatgpt.entrypoint.ChatGptImpl | ||
import co.yml.ychatgpt.entrypoint.CompletionParams | ||
import co.yml.ychatgpt.data.exception.ChatGptException | ||
import co.yml.ychatgpt.entrypoint.impl.ChatGptImpl | ||
import co.yml.ychatgpt.entrypoint.model.CompletionParams | ||
import kotlin.coroutines.cancellation.CancellationException | ||
import kotlin.jvm.Synchronized | ||
import kotlin.jvm.Volatile | ||
import kotlin.native.concurrent.ThreadLocal | ||
|
||
/** | ||
* YChatGPT aims to abstract all API call logic from ChatGPT for multiple platforms. | ||
* | ||
* To initialize the SDK, just call the static method ChatGpt.create(apiKey), informing the api key. | ||
* See [this](https://beta.openai.com/docs/api-reference/authentication) for more details on how | ||
* to get the api key. | ||
*/ | ||
interface ChatGpt { | ||
|
||
/** | ||
* The completions api can be used for a wide variety of tasks. You [input] some text as a | ||
* prompt, and the model will generate a text completion that attempts to match whatever | ||
* context or pattern you gave it. For example, if you give the API the prompt, "As Descartes | ||
* said, I think, therefore", it will return the completion " I am" with high probability. | ||
* | ||
* @param input The prompt(s) to generate completions for. | ||
*/ | ||
@Throws(CancellationException::class, ChatGptException::class) | ||
suspend fun completion(input: String): String | ||
|
||
suspend fun completion(input: String, completionParams: CompletionParams): String | ||
/** | ||
* The completions api can be used for a wide variety of tasks. You [input] some text as a | ||
* prompt, and the model will generate a text completion that attempts to match whatever | ||
* context or pattern you gave it. For example, if you give the API the prompt, "As Descartes | ||
* said, I think, therefore", it will return the completion " I am" with high probability. | ||
* | ||
* You can configure the behavior of the completion editing the [completionParams]. | ||
* | ||
* @param input The prompt(s) to generate completions for. | ||
* @param completionParams Params to set the completion behaviour. | ||
*/ | ||
@Throws(CancellationException::class, ChatGptException::class) | ||
suspend fun completion( | ||
input: String, | ||
completionParams: CompletionParams = CompletionParams() | ||
): String | ||
|
||
@ThreadLocal | ||
companion object { | ||
|
||
@Volatile | ||
private var instance: ChatGpt? = null | ||
|
||
@Synchronized | ||
fun create(apiKey: String): ChatGpt { | ||
return ChatGptImpl(apiKey) | ||
return instance ?: ChatGptImpl(apiKey).also { instance = it } | ||
} | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
chat-gpt/src/commonMain/kotlin/co/yml/ychatgpt/data/api/ChatGptApi.kt
This file contains hidden or 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,10 @@ | ||
package co.yml.ychatgpt.data.api | ||
|
||
import co.yml.ychatgpt.data.dto.CompletionDto | ||
import co.yml.ychatgpt.data.dto.CompletionParamsDto | ||
import co.yml.ychatgpt.data.infrastructure.ApiResult | ||
|
||
internal interface ChatGptApi { | ||
|
||
suspend fun completion(paramsDto: CompletionParamsDto): ApiResult<CompletionDto> | ||
} |
19 changes: 19 additions & 0 deletions
19
chat-gpt/src/commonMain/kotlin/co/yml/ychatgpt/data/api/impl/ChatGptApiImpl.kt
This file contains hidden or 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,19 @@ | ||
package co.yml.ychatgpt.data.api.impl | ||
|
||
import co.yml.ychatgpt.data.api.ChatGptApi | ||
import co.yml.ychatgpt.data.dto.CompletionDto | ||
import co.yml.ychatgpt.data.dto.CompletionParamsDto | ||
import co.yml.ychatgpt.data.infrastructure.ApiExecutor | ||
import co.yml.ychatgpt.data.infrastructure.ApiResult | ||
import io.ktor.http.HttpMethod | ||
|
||
internal class ChatGptApiImpl(private val apiExecutor: ApiExecutor) : ChatGptApi { | ||
|
||
override suspend fun completion(paramsDto: CompletionParamsDto): ApiResult<CompletionDto> { | ||
return apiExecutor | ||
.setEndpoint("v1/completions") | ||
.setHttpMethod(HttpMethod.Post) | ||
.setBody(paramsDto) | ||
.execute() | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
chat-gpt/src/commonMain/kotlin/co/yml/ychatgpt/data/dto/ChoiceDto.kt
This file contains hidden or 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,16 @@ | ||
package co.yml.ychatgpt.data.dto | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
internal data class ChoiceDto( | ||
@SerialName("text") | ||
val text: String, | ||
@SerialName("index") | ||
val index: Int, | ||
@SerialName("logprobs") | ||
val logProbs: Int?, | ||
@SerialName("finish_reason") | ||
val finishReason: String, | ||
) |
16 changes: 16 additions & 0 deletions
16
chat-gpt/src/commonMain/kotlin/co/yml/ychatgpt/data/dto/CompletionDto.kt
This file contains hidden or 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,16 @@ | ||
package co.yml.ychatgpt.data.dto | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
internal data class CompletionDto( | ||
@SerialName("id") | ||
val id: String, | ||
@SerialName("model") | ||
val model: String, | ||
@SerialName("choices") | ||
val choices: List<ChoiceDto>, | ||
@SerialName("usage") | ||
val usage: UsageDto, | ||
) |
20 changes: 20 additions & 0 deletions
20
chat-gpt/src/commonMain/kotlin/co/yml/ychatgpt/data/dto/CompletionParamsDto.kt
This file contains hidden or 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,20 @@ | ||
package co.yml.ychatgpt.data.dto | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
internal data class CompletionParamsDto( | ||
@SerialName("model") | ||
val model: String, | ||
@SerialName("prompt") | ||
val prompt: String, | ||
@SerialName("max_tokens") | ||
val maxTokens: Int, | ||
@SerialName("temperature") | ||
val temperature: Double, | ||
@SerialName("top_p") | ||
val topP: Double, | ||
@SerialName("n") | ||
val completionNumber: Int = 1, | ||
) |
14 changes: 14 additions & 0 deletions
14
chat-gpt/src/commonMain/kotlin/co/yml/ychatgpt/data/dto/UsageDto.kt
This file contains hidden or 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,14 @@ | ||
package co.yml.ychatgpt.data.dto | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
internal data class UsageDto( | ||
@SerialName("prompt_tokens") | ||
val promptToken: Int, | ||
@SerialName("completion_tokens") | ||
val completionTokens: Int, | ||
@SerialName("total_tokens") | ||
val totalTokens: Int, | ||
) |
13 changes: 13 additions & 0 deletions
13
chat-gpt/src/commonMain/kotlin/co/yml/ychatgpt/data/exception/ChatGptException.kt
This file contains hidden or 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,13 @@ | ||
package co.yml.ychatgpt.data.exception | ||
|
||
class ChatGptException( | ||
message: String? = null, | ||
cause: Throwable? = null, | ||
var statusCode: Int? = null, | ||
) : Exception(message, cause) { | ||
|
||
constructor( | ||
cause: Throwable?, | ||
statusCode: Int? = null | ||
) : this(message = null, cause = cause, statusCode = statusCode) | ||
} |
90 changes: 90 additions & 0 deletions
90
chat-gpt/src/commonMain/kotlin/co/yml/ychatgpt/data/infrastructure/ApiExecutor.kt
This file contains hidden or 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,90 @@ | ||
package co.yml.ychatgpt.data.infrastructure | ||
|
||
import co.yml.ychatgpt.data.exception.ChatGptException | ||
import io.ktor.client.HttpClient | ||
import io.ktor.client.call.body | ||
import io.ktor.client.plugins.ResponseException | ||
import io.ktor.client.request.request | ||
import io.ktor.client.request.setBody | ||
import io.ktor.client.statement.HttpResponse | ||
import io.ktor.client.utils.EmptyContent | ||
import io.ktor.http.HttpMethod | ||
import io.ktor.http.isSuccess | ||
import io.ktor.util.toMap | ||
import kotlin.collections.set | ||
|
||
internal class ApiExecutor(private val httpClient: HttpClient) { | ||
|
||
private lateinit var endpoint: String | ||
|
||
private lateinit var httpMethod: HttpMethod | ||
|
||
private var body: Any = EmptyContent | ||
|
||
private var query: HashMap<String, String> = HashMap() | ||
|
||
fun setEndpoint(endpoint: String): ApiExecutor { | ||
this.endpoint = endpoint | ||
return this | ||
} | ||
|
||
fun setHttpMethod(httpMethod: HttpMethod): ApiExecutor { | ||
this.httpMethod = httpMethod | ||
return this | ||
} | ||
|
||
fun setBody(body: Any): ApiExecutor { | ||
this.body = body | ||
return this | ||
} | ||
|
||
fun addQuery(key: String, value: String): ApiExecutor { | ||
this.query[key] = value | ||
return this | ||
} | ||
|
||
fun addQuery(key: String, value: List<String>): ApiExecutor { | ||
this.query[key] = value.joinToString(",") | ||
return this | ||
} | ||
|
||
suspend inline fun <reified T> execute(): ApiResult<T> { | ||
return try { | ||
val response = httpClient.request(endpoint) { | ||
url { query.forEach { parameters.append(it.key, it.value) } } | ||
method = httpMethod | ||
setBody(this@ApiExecutor.body) | ||
} | ||
return response.toApiResult() | ||
} catch (responseException: ResponseException) { | ||
responseException.toApiResult() | ||
} catch (throwable: Throwable) { | ||
throwable.toApiResult() | ||
} | ||
} | ||
|
||
private suspend inline fun <reified T> HttpResponse.toApiResult(): ApiResult<T> { | ||
val headers = this.headers.toMap() | ||
val statusCode = this.status.value | ||
return if (!this.status.isSuccess()) { | ||
val exception = ChatGptException(null, statusCode) | ||
ApiResult(null, headers, statusCode, exception) | ||
} else { | ||
ApiResult(this.body<T>(), headers, statusCode, null) | ||
} | ||
} | ||
|
||
private fun <T> ResponseException.toApiResult(): ApiResult<T> { | ||
return ApiResult( | ||
statusCode = this.response.status.value, | ||
exception = ChatGptException(this.cause, this.response.status.value) | ||
) | ||
} | ||
|
||
private fun <T> Throwable.toApiResult(): ApiResult<T> { | ||
return ApiResult( | ||
statusCode = null, | ||
exception = ChatGptException(this.cause) | ||
) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.