Skip to content

Commit 571dddd

Browse files
authored
Merge pull request #57 from yml-org/feature/CM-1279/retrieve-model
[CM-1279] Implements retrieve model entrypoint
2 parents 0da7aba + ca72906 commit 571dddd

File tree

20 files changed

+376
-38
lines changed

20 files changed

+376
-38
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ try {
8686
### Features
8787

8888
- [ListModels](guides/Features.md#listmodels)
89+
- [RetrieveModel](guides/Features.md#retrieveModel)
8990
- [Completions](guides/Features.md#completion)
9091
- [ChatCompletions](guides/Features.md#chatcompletions)
9192
- [ImageGenerations](guides/Features.md#imagegenerations)

guides/Features.md

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Features
22

33
- [ListModels](#listModels)
4+
- [RetrieveModel](#retrieveModel)
45
- [Completion](#completion)
56
- [ChatCompletions](#chatcompletions)
67
- [ImageGenerations](#imagegenerations)
@@ -39,6 +40,39 @@ try {
3940
}
4041
```
4142

43+
## RetrieveModel
44+
45+
The retrieveModel api retrieve the currently model based on the given id, and provides basic information about it such as the owner and availability.
46+
47+
### Swift
48+
49+
```swift
50+
var yChat: YChat {
51+
YChatCompanion.shared.create(apiKey: "your-api-key")
52+
}
53+
54+
do {
55+
let result = try await yChat.retrieveModel().execute(id: "babbage")
56+
} catch {
57+
// catch any error that may occurs on api call.
58+
}
59+
```
60+
61+
### Kotlin
62+
63+
```kotlin
64+
val yChat by lazy {
65+
YChat.create("your-api-key")
66+
}
67+
68+
try {
69+
val result = yChat.retrieveModel().execute("babbage")
70+
71+
} catch (e: exception) {
72+
// catch any error that may occurs on api call.
73+
}
74+
```
75+
4276
## Completion
4377

4478
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.
@@ -51,7 +85,7 @@ var yChat: YChat {
5185
}
5286

5387
do {
54-
let result = try await chatGpt.completion()
88+
let result = try await yChat.completion()
5589
.setInput(input: "Say this is a test.")
5690
.setMaxTokens(tokens: 1024)
5791
.set... // you can set more parameters
@@ -74,7 +108,6 @@ try {
74108
.setMaxTokens(1024)
75109
.set... // you can set more parameters
76110
.execute()
77-
78111
} catch (e: exception) {
79112
// catch any error that may occurs on api call.
80113
}
@@ -92,7 +125,7 @@ var yChat: YChat {
92125
}
93126

94127
do {
95-
let result = try await chatGpt.chatCompletions()
128+
let result = try await yChat.chatCompletions()
96129
.setMaxTokens(tokens: 1024)
97130
.addMessage(
98131
role: "assistant",
@@ -121,7 +154,6 @@ try {
121154
)
122155
.set... // you can set more parameters
123156
.execute("What is the best exercise for building muscle?")
124-
125157
} catch (e: exception) {
126158
// catch any error that may occurs on api call.
127159
}
@@ -139,7 +171,7 @@ var yChat: YChat {
139171
}
140172

141173
do {
142-
let result = try await chatGpt.imageGenerations()
174+
let result = try await yChat.imageGenerations()
143175
.setResults(results: 2)
144176
.setSize(size: "1024x1024")
145177
.set... // you can set more parameters
@@ -162,7 +194,6 @@ try {
162194
.setSize("1024x1024")
163195
.set... // you can set more parameters
164196
.execute("ocean")
165-
166197
} catch (e: exception) {
167198
// catch any error that may occurs on api call.
168199
}
@@ -180,7 +211,7 @@ var yChat: YChat {
180211
}
181212

182213
do {
183-
let result = try await chatGpt.edits()
214+
let result = try await yChat.edits()
184215
.setInput(input: "What day of the wek is it?")
185216
.setResults(result: 1)
186217
.set... // you can set more parameters
@@ -203,7 +234,6 @@ try {
203234
.setResults(1)
204235
.set... // you can set more parameters
205236
.execute("Fix the spelling mistakes")
206-
207237
} catch (e: exception) {
208238
// catch any error that may occurs on api call.
209239
}

sample/jvm/README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,14 @@ This endpoint retrieve a list of currently available artificial intelligence mod
8181

8282
##### Example:
8383

84-
`GET http://localhost:8080/api/ychat/models`
84+
`GET http://localhost:8080/api/ychat/models`
85+
86+
### Model Endpoint
87+
88+
This endpoint retrieve the artificial intelligence model based on the given ID.
89+
90+
##### Endpoint: http://localhost:[port_number]/api/ychat/models/{modelID}
91+
92+
##### Example:
93+
94+
`GET http://localhost:8080/api/ychat/models/babbage`

sample/jvm/src/main/java/co/yml/ychat/jvm/controller/YChatController.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.springframework.beans.factory.annotation.Autowired;
77
import org.springframework.http.ResponseEntity;
88
import org.springframework.web.bind.annotation.GetMapping;
9+
import org.springframework.web.bind.annotation.PathVariable;
910
import org.springframework.web.bind.annotation.RequestMapping;
1011
import org.springframework.web.bind.annotation.RequestParam;
1112
import org.springframework.web.bind.annotation.RestController;
@@ -57,6 +58,12 @@ public ResponseEntity<List<AIModel>> models() throws Exception {
5758
return ResponseEntity.ok(result);
5859
}
5960

61+
@GetMapping("models/{id}")
62+
public ResponseEntity<AIModel> model(@PathVariable String id) throws Exception {
63+
AIModel result = YChatService.getModel(id);
64+
return ResponseEntity.ok(result);
65+
}
66+
6067
private static class Defaults {
6168
static final String COMPLETION_INPUT = "Say this is a test.";
6269
static final String CHAT_COMPLETION_INPUT = "Tell me one strength exercise";

sample/jvm/src/main/java/co/yml/ychat/jvm/services/YChatService.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package co.yml.ychat.jvm.services;
22

3+
import co.yml.ychat.YChat;
34
import co.yml.ychat.domain.model.AIModel;
45
import co.yml.ychat.domain.model.ChatMessage;
56
import java.util.List;
7+
import java.util.concurrent.CompletableFuture;
68
import org.jetbrains.annotations.NotNull;
79
import org.springframework.beans.factory.annotation.Autowired;
810
import org.springframework.stereotype.Service;
9-
import java.util.concurrent.CompletableFuture;
10-
import co.yml.ychat.YChat;
1111

1212
@Service
1313
public class YChatService {
@@ -58,6 +58,13 @@ public List<AIModel> getModels() throws Exception {
5858
return future.get();
5959
}
6060

61+
public AIModel getModel(String id) throws Exception {
62+
final CompletableFuture<AIModel> future = new CompletableFuture<>();
63+
ychat.retrieveModel()
64+
.execute(id, new CompletionCallbackResult<>(future));
65+
return future.get();
66+
}
67+
6168
private static class CompletionCallbackResult<T> implements YChat.Callback<T> {
6269

6370
private final CompletableFuture<T> future;

ychat/src/commonMain/kotlin/co/yml/ychat/YChat.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import co.yml.ychat.entrypoint.features.Completion
55
import co.yml.ychat.entrypoint.features.Edits
66
import co.yml.ychat.entrypoint.features.ImageGenerations
77
import co.yml.ychat.entrypoint.features.ListModels
8+
import co.yml.ychat.entrypoint.features.RetrieveModel
89
import co.yml.ychat.entrypoint.impl.YChatImpl
910
import kotlin.jvm.JvmStatic
1011
import kotlin.jvm.Volatile
@@ -32,6 +33,19 @@ interface YChat {
3233
*/
3334
fun listModels(): ListModels
3435

36+
/**
37+
* The retrieveModel api retrieve the currently model based on the given id, and provides basic
38+
* information about it such as the owner and availability.
39+
*
40+
* Example usage:
41+
* ```
42+
* val result = YChat.create(apiKey)
43+
* .retrieveModel()
44+
* .execute("gpt-3")
45+
* ```
46+
*/
47+
fun retrieveModel(): RetrieveModel
48+
3549
/**
3650
* The completions api can be used for a wide variety of tasks. You input some text as a
3751
* prompt, and the model will generate a text completion that attempts to match whatever

ychat/src/commonMain/kotlin/co/yml/ychat/data/api/ChatGptApi.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import co.yml.ychat.data.dto.EditsDto
88
import co.yml.ychat.data.dto.EditsParamsDto
99
import co.yml.ychat.data.dto.ImageGenerationsDto
1010
import co.yml.ychat.data.dto.ImageGenerationsParamsDto
11+
import co.yml.ychat.data.dto.ModelDto
1112
import co.yml.ychat.data.dto.ModelListDto
1213
import co.yml.ychat.data.infrastructure.ApiResult
1314

@@ -22,4 +23,6 @@ internal interface ChatGptApi {
2223
suspend fun edits(paramsDto: EditsParamsDto): ApiResult<EditsDto>
2324

2425
suspend fun models(): ApiResult<ModelListDto>
26+
27+
suspend fun model(id: String): ApiResult<ModelDto>
2528
}

ychat/src/commonMain/kotlin/co/yml/ychat/data/api/impl/ChatGptApiImpl.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import co.yml.ychat.data.dto.EditsDto
99
import co.yml.ychat.data.dto.EditsParamsDto
1010
import co.yml.ychat.data.dto.ImageGenerationsDto
1111
import co.yml.ychat.data.dto.ImageGenerationsParamsDto
12+
import co.yml.ychat.data.dto.ModelDto
1213
import co.yml.ychat.data.dto.ModelListDto
1314
import co.yml.ychat.data.infrastructure.ApiExecutor
1415
import co.yml.ychat.data.infrastructure.ApiResult
@@ -54,4 +55,11 @@ internal class ChatGptApiImpl(private val apiExecutor: ApiExecutor) : ChatGptApi
5455
.setHttpMethod(HttpMethod.Get)
5556
.execute()
5657
}
58+
59+
override suspend fun model(id: String): ApiResult<ModelDto> {
60+
return apiExecutor
61+
.setEndpoint("v1/models/$id")
62+
.setHttpMethod(HttpMethod.Get)
63+
.execute()
64+
}
5765
}

ychat/src/commonMain/kotlin/co/yml/ychat/di/module/LibraryModule.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,19 @@ import co.yml.ychat.domain.usecases.CompletionUseCase
1010
import co.yml.ychat.domain.usecases.EditsUseCase
1111
import co.yml.ychat.domain.usecases.ImageGenerationsUseCase
1212
import co.yml.ychat.domain.usecases.ListModelsUseCase
13+
import co.yml.ychat.domain.usecases.RetrieveModelUseCase
1314
import co.yml.ychat.entrypoint.features.ChatCompletions
1415
import co.yml.ychat.entrypoint.features.Completion
1516
import co.yml.ychat.entrypoint.features.Edits
1617
import co.yml.ychat.entrypoint.features.ImageGenerations
1718
import co.yml.ychat.entrypoint.features.ListModels
19+
import co.yml.ychat.entrypoint.features.RetrieveModel
1820
import co.yml.ychat.entrypoint.impl.ChatCompletionsImpl
1921
import co.yml.ychat.entrypoint.impl.CompletionImpl
2022
import co.yml.ychat.entrypoint.impl.EditsImpl
2123
import co.yml.ychat.entrypoint.impl.ImageGenerationsImpl
2224
import co.yml.ychat.entrypoint.impl.ListModelsImpl
25+
import co.yml.ychat.entrypoint.impl.RetrieveModelImpl
2326
import kotlinx.coroutines.Dispatchers
2427
import org.koin.core.module.Module
2528
import org.koin.dsl.module
@@ -31,6 +34,7 @@ internal class LibraryModule(private val apiKey: String) {
3134

3235
private val entrypointModule = module {
3336
factory<ListModels> { ListModelsImpl(Dispatchers.Default, get()) }
37+
factory<RetrieveModel> { RetrieveModelImpl(Dispatchers.Default, get()) }
3438
factory<Completion> { CompletionImpl(Dispatchers.Default, get()) }
3539
factory<ChatCompletions> { ChatCompletionsImpl(Dispatchers.Default, get()) }
3640
factory<ImageGenerations> { ImageGenerationsImpl(Dispatchers.Default, get()) }
@@ -39,6 +43,7 @@ internal class LibraryModule(private val apiKey: String) {
3943

4044
private val domainModule = module {
4145
factory { ListModelsUseCase(get()) }
46+
factory { RetrieveModelUseCase(get()) }
4247
factory { CompletionUseCase(get(), get()) }
4348
factory { ChatCompletionsUseCase(get()) }
4449
factory { ImageGenerationsUseCase(get()) }

ychat/src/commonMain/kotlin/co/yml/ychat/domain/usecases/ChatCompletionsUseCase.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import co.yml.ychat.domain.mapper.toChatMessages
66
import co.yml.ychat.domain.model.ChatCompletionsParams
77
import co.yml.ychat.domain.model.ChatMessage
88

9-
internal data class ChatCompletionsUseCase(private val chatGptApi: ChatGptApi) {
9+
internal class ChatCompletionsUseCase(private val chatGptApi: ChatGptApi) {
1010

1111
suspend fun requestChatCompletions(params: ChatCompletionsParams): List<ChatMessage> {
1212
val requestDto = params.toChatCompletionParamsDto()

0 commit comments

Comments
 (0)