Skip to content

Commit 36de013

Browse files
authored
refactor: API alignment, beta removals, and legacy tagging (#228)
1 parent e7723a8 commit 36de013

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+279
-281
lines changed

CHANGELOG.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
# Unreleased
2+
3+
### Added
4+
- Introduced `Parameters.Empty` for functions without parameters.
5+
- Added `File.statusDetails` for retrieving file status details.
6+
- `HyperParams` new fields: `computeClassificationMetrics`, `classificationNClasses` and `classificationPositiveClass`.
7+
8+
### Removed
9+
- Removed beta status from the chat and audio features.
10+
11+
### Deprecated
12+
- `completions` is deprecated.
13+
- `edits` as legacy.
14+
15+
### Breaking Changes
16+
- **Audio**: Updated `TranscriptionRequest`'s `responseFormat` type to `AudioResponseFormat`.
17+
- **Fine Tune**: set `HyperParams.learningRateMultiplier` to be non-nullable.
18+
- **Chat**: Multiple changes have been implemented:
19+
- Modified `ChatChoice.finishReason`, `ChatChunk.finishReason`, and `Choice.finishReason` types to `FinishReason`.
20+
- Set `index`, `message`, and `finishReason` fields in `ChatChoice` to be non-nullable.
21+
- Set `index`, `delta`, and `finishReason` fields in `ChatChunk` to be non-nullable.
22+
- Set `ChatCompletionFunction.parameters` to be non-nullable.
23+
- In `FunctionCall`, set `name`, `arguments`, and `argumentsAsJson()` to be non-nullable.
24+
125
# 3.3.2
226
> Published 21 Jul 2023
327

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,20 @@ Use your `OpenAI` instance to make API requests. [Learn more](guides/GettingStar
7474
### Supported features
7575

7676
- [Models](guides/GettingStarted.md#models)
77-
- [Completions](guides/GettingStarted.md#completions)
7877
- [Chat](guides/GettingStarted.md#chat)
79-
- [Edits](guides/GettingStarted.md#edits)
8078
- [Images](guides/GettingStarted.md#images)
8179
- [Embeddings](guides/GettingStarted.md#embeddings)
8280
- [Files](guides/GettingStarted.md#files)
8381
- [Fine-tunes](guides/GettingStarted.md#fine-tunes)
8482
- [Moderations](guides/GettingStarted.md#moderations)
8583
- [Audio](guides/GettingStarted.md#audio)
8684

85+
#### Legacy
86+
- [Completions](guides/GettingStarted.md#completions)
87+
88+
#### Deprecated
89+
- [Edits](guides/GettingStarted.md#edits)
90+
8791
## 📚 Guides
8892

8993
Get started and understand more about how to use OpenAI API client for Kotlin with these guides:

guides/ChatFunctionCall.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,19 +112,19 @@ Send the chat request and handle the response. If there's a function call in the
112112

113113
```kotlin
114114
val response = openAI.chatCompletion(request)
115-
val message = response.choices.first().message ?: error("no response found!")
115+
val message = response.choices.first().message
116116
message.functionCall?.let { functionCall ->
117117
val availableFunctions = mapOf("currentWeather" to ::currentWeather)
118118
val functionToCall = availableFunctions[functionCall.name] ?: error("Function ${functionCall.name} not found")
119-
val functionArgs = functionCall.argumentsAsJson() ?: error("arguments field is missing")
119+
val functionArgs = functionCall.argumentsAsJson()
120120
val functionResponse = functionToCall(
121121
functionArgs.getValue("location").jsonPrimitive.content,
122122
functionArgs["unit"]?.jsonPrimitive?.content ?: "fahrenheit"
123123
)
124124
chatMessages.add(
125125
ChatMessage(
126126
role = message.role,
127-
content = message.content ?: "", // required to not be empty in this case
127+
content = message.content.orEmpty(), // required to not be empty in this case
128128
functionCall = message.functionCall
129129
)
130130
)
@@ -142,7 +142,7 @@ message.functionCall?.let { functionCall ->
142142
}
143143

144144
val secondResponse = openAI.chatCompletion(secondRequest)
145-
println(secondResponse.choices.first().message?.content)
145+
println(secondResponse.choices.first().message.content)
146146
} ?: println(message.content)
147147
```
148148

@@ -156,7 +156,6 @@ message.functionCall?.let { functionCall ->
156156
Below is a complete Kotlin example following the guide:
157157

158158
```kotlin
159-
import com.aallam.openai.api.BetaOpenAI
160159
import com.aallam.openai.api.chat.*
161160
import com.aallam.openai.api.model.ModelId
162161
import com.aallam.openai.client.OpenAI
@@ -209,11 +208,11 @@ suspend fun main() {
209208
}
210209

211210
val response = openAI.chatCompletion(request)
212-
val message = response.choices.first().message ?: error("no response found!")
211+
val message = response.choices.first().message
213212
message.functionCall?.let { functionCall ->
214213
val availableFunctions = mapOf("currentWeather" to ::currentWeather)
215214
val functionToCall = availableFunctions[functionCall.name] ?: error("Function ${functionCall.name} not found")
216-
val functionArgs = functionCall.argumentsAsJson() ?: error("arguments field is missing")
215+
val functionArgs = functionCall.argumentsAsJson()
217216
val functionResponse = functionToCall(
218217
functionArgs.getValue("location").jsonPrimitive.content,
219218
functionArgs["unit"]?.jsonPrimitive?.content ?: "fahrenheit"
@@ -222,7 +221,7 @@ suspend fun main() {
222221
chatMessages.add(
223222
ChatMessage(
224223
role = message.role,
225-
content = message.content ?: "",
224+
content = message.content.orEmpty(),
226225
functionCall = message.functionCall
227226
)
228227
)
@@ -241,7 +240,7 @@ suspend fun main() {
241240
}
242241

243242
val secondResponse = openAI.chatCompletion(secondRequest)
244-
println(secondResponse.choices.first().message?.content)
243+
println(secondResponse.choices.first().message.content)
245244
} ?: println(message.content)
246245
}
247246

guides/GettingStarted.md

Lines changed: 63 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,17 @@ Use your `OpenAI` instance to make API requests.
1313
- [Models](#models)
1414
- [List models](#list-models)
1515
- [Retrieve a model](#retrieve-a-model)
16-
- [Completions](#completions)
17-
- [Create completion](#create-completion)
1816
- [Chat](#chat)
19-
- [Create chat completion](#create-chat-completion-beta)
20-
- [Edits](#edits)
21-
- [Create edits](#create-edits)
17+
- [Create chat completion](#create-chat-completion)
2218
- [Images](#images)
23-
- [Create image](#create-image-beta)
24-
- [Edit images](#edit-images-beta)
25-
- [Create image variation](#create-image-variation-beta)
19+
- [Create image](#create-image)
20+
- [Edit images](#edit-images)
21+
- [Create image variation](#create-image-variation)
2622
- [Embeddings](#embeddings)
2723
- [Create embeddings](#create-embeddings)
2824
- [Audio](#audio)
29-
- [Create transcription](#create-transcription-beta)
30-
- [Create translation](#create-translation-beta)
25+
- [Create transcription](#create-transcription)
26+
- [Create translation](#create-translation)
3127
- [Files](#files)
3228
- [List files](#list-files)
3329
- [Upload file](#upload-file)
@@ -44,6 +40,14 @@ Use your `OpenAI` instance to make API requests.
4440
- [Moderations](#moderations)
4541
- [Create moderation](#create-moderation)
4642

43+
#### Legacy
44+
- [Completions](#completions)
45+
- [Create completion](#create-completion-legacy)
46+
47+
#### Deprecated
48+
- [Edits](#edits)
49+
- [Create edits](#create-edits-deprecated)
50+
4751
## Models
4852

4953
List and describe the various [models](https://platform.openai.com/docs/models) available in the API.
@@ -66,30 +70,11 @@ val id = ModelId("text-ada-001")
6670
val model: Model = openAI.model(id)
6771
```
6872

69-
## Completions
70-
71-
Given a prompt, the model will return one or more predicted completions, and can also return the probabilities of alternative tokens at each position.
72-
73-
### Create Completion
74-
75-
Creates a completion for the provided prompt and parameters
76-
77-
```kotlin
78-
val completionRequest = CompletionRequest(
79-
model = ModelId("text-ada-001"),
80-
prompt = "Somebody once told me the world is gonna roll me",
81-
echo = true
82-
)
83-
val completion: TextCompletion = openAI.completion(completionRequest)
84-
// or, as flow
85-
val completions: Flow<TextCompletion> = openAI.completions(completionRequest)
86-
```
87-
8873
## Chat
8974

9075
Given a chat conversation, the model will return a chat completion response.
9176

92-
### Create chat completion `beta`
77+
### Create chat completion
9378

9479
Creates a completion for the chat message.
9580

@@ -112,30 +97,11 @@ val completion: ChatCompletion = openAI.chatCompletion(chatCompletionRequest)
11297
val completions: Flow<ChatCompletionChunk> = openAI.chatCompletions(chatCompletionRequest)
11398
```
11499

115-
## Edits
116-
117-
Given a prompt and an instruction, the model will return an edited version of the prompt.
118-
119-
### Create edits
120-
121-
Creates a new edit for the provided input, instruction, and parameters.
122-
123-
124-
```kotlin
125-
val edit = openAI.edit(
126-
request = EditsRequest(
127-
model = ModelId("text-davinci-edit-001"),
128-
input = "What day of the wek is it?",
129-
instruction = "Fix the spelling mistakes"
130-
)
131-
)
132-
```
133-
134100
## Images
135101

136102
Given a prompt and/or an input image, the model will generate a new image.
137103

138-
### Create image `beta`
104+
### Create image
139105

140106
Creates an image given a prompt.
141107

@@ -149,7 +115,7 @@ val images = openAI.imageURL( // or openAI.imageJSON
149115
)
150116
````
151117

152-
### Edit images `beta`
118+
### Edit images
153119

154120
Creates an edited or extended image given an original image and a prompt.
155121

@@ -165,7 +131,7 @@ val images = openAI.imageURL( // or openAI.imageJSON
165131
)
166132
````
167133

168-
### Create image variation `beta`
134+
### Create image variation
169135

170136
Creates a variation of a given image.
171137

@@ -200,7 +166,7 @@ val embeddings = openAI.embeddings(
200166

201167
Learn how to turn audio into text.
202168

203-
### Create transcription `Beta`
169+
### Create transcription
204170

205171
Transcribes audio into the input language.
206172

@@ -212,7 +178,7 @@ val request = TranscriptionRequest(
212178
val transcription = openAI.transcription(request)
213179
```
214180

215-
### Create translation `Beta`
181+
### Create translation
216182

217183
Translates audio into English.
218184

@@ -337,11 +303,11 @@ openAI.delete(fileId)
337303

338304
## Moderations
339305

340-
Given a input text, outputs if the model classifies it as violating OpenAI's content policy.
306+
Given an input text, outputs if the model classifies it as violating OpenAI's content policy.
341307

342308
### Create moderation
343309

344-
Given a input text, outputs if the model classifies it as violating OpenAI's content policy.
310+
Classifies if text violates OpenAI's Content Policy
345311

346312
````kotlin
347313
val moderation = openAI.moderations(
@@ -350,3 +316,44 @@ val moderation = openAI.moderations(
350316
)
351317
)
352318
````
319+
320+
---
321+
322+
## Completions
323+
324+
Given a prompt, the model will return one or more predicted completions, and can also return the probabilities of alternative tokens at each position.
325+
326+
### Create Completion `legacy`
327+
328+
Creates a completion for the provided prompt and parameters
329+
330+
```kotlin
331+
val completionRequest = CompletionRequest(
332+
model = ModelId("text-ada-001"),
333+
prompt = "Somebody once told me the world is gonna roll me",
334+
echo = true
335+
)
336+
val completion: TextCompletion = openAI.completion(completionRequest)
337+
// or, as flow
338+
val completions: Flow<TextCompletion> = openAI.completions(completionRequest)
339+
```
340+
341+
---
342+
343+
## Edits
344+
345+
Given a prompt and an instruction, the model will return an edited version of the prompt.
346+
347+
### Create edits `Deprecated`
348+
349+
Creates a new edit for the provided input, instruction, and parameters.
350+
351+
```kotlin
352+
val edit = openAI.edit(
353+
request = EditsRequest(
354+
model = ModelId("text-davinci-edit-001"),
355+
input = "What day of the wek is it?",
356+
instruction = "Fix the spelling mistakes"
357+
)
358+
)
359+
```

openai-client/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ kotlin {
2222
optIn("com.aallam.openai.api.ExperimentalOpenAI")
2323
optIn("com.aallam.openai.api.BetaOpenAI")
2424
optIn("com.aallam.openai.api.InternalOpenAI")
25+
optIn("com.aallam.openai.api.LegacyOpenAI")
2526
}
2627
}
2728
val commonMain by getting {

openai-client/src/commonMain/kotlin/com.aallam.openai.client/Audio.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@ public interface Audio {
1111
/**
1212
* Transcribes audio into the input language.
1313
*/
14-
@BetaOpenAI
1514
public suspend fun transcription(request: TranscriptionRequest): Transcription
1615

1716
/**
1817
* Translates audio into English.
1918
*/
20-
@BetaOpenAI
2119
public suspend fun translation(request: TranslationRequest): Translation
2220
}

openai-client/src/commonMain/kotlin/com.aallam.openai.client/Chat.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,10 @@ public interface Chat {
1414
/**
1515
* Creates a completion for the chat message.
1616
*/
17-
@BetaOpenAI
1817
public suspend fun chatCompletion(request: ChatCompletionRequest): ChatCompletion
1918

2019
/**
2120
* Stream variant of [chatCompletion].
2221
*/
23-
@BetaOpenAI
2422
public fun chatCompletions(request: ChatCompletionRequest): Flow<ChatCompletionChunk>
2523
}

openai-client/src/commonMain/kotlin/com.aallam.openai.client/Completions.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.aallam.openai.client
22

3+
import com.aallam.openai.api.LegacyOpenAI
34
import com.aallam.openai.api.completion.CompletionRequest
45
import com.aallam.openai.api.completion.TextCompletion
56
import kotlinx.coroutines.flow.Flow
@@ -14,10 +15,12 @@ public interface Completions {
1415
* This is the main endpoint of the API. Returns the predicted completion for the given prompt,
1516
* and can also return the probabilities of alternative tokens at each position if requested.
1617
*/
18+
@LegacyOpenAI
1719
public suspend fun completion(request: CompletionRequest): TextCompletion
1820

1921
/**
2022
* Stream variant of [completion].
2123
*/
24+
@LegacyOpenAI
2225
public fun completions(request: CompletionRequest): Flow<TextCompletion>
2326
}

openai-client/src/commonMain/kotlin/com.aallam.openai.client/Edits.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ public interface Edits {
1111
/**
1212
* Creates a new edit for the provided input, instruction, and parameters.
1313
*/
14+
@Deprecated("Edits is deprecated. Chat completions is the recommend replacement.")
1415
public suspend fun edit(request: EditsRequest): Edit
1516
}

0 commit comments

Comments
 (0)