Skip to content

Commit 2896257

Browse files
committed
add kdocs + mutability
1 parent cd506a2 commit 2896257

File tree

12 files changed

+65
-27
lines changed

12 files changed

+65
-27
lines changed

src/main/kotlin/com/cjcrafter/openai/AzureOpenAI.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import okhttp3.OkHttpClient
44
import okhttp3.Request
55
import okhttp3.RequestBody
66
import okhttp3.RequestBody.Companion.toRequestBody
7+
import org.jetbrains.annotations.ApiStatus
78

89
/**
910
* The Azure OpenAI API client.
@@ -16,7 +17,7 @@ import okhttp3.RequestBody.Companion.toRequestBody
1617
* @property apiVersion The API version to use. Defaults to 2023-03-15-preview.
1718
* @property modelName The model name to use. This is the name of the model deployed to Azure.
1819
*/
19-
class AzureOpenAI @JvmOverloads constructor(
20+
class AzureOpenAI @ApiStatus.Internal constructor(
2021
apiKey: String,
2122
organization: String? = null,
2223
client: OkHttpClient = OkHttpClient(),

src/main/kotlin/com/cjcrafter/openai/chat/tool/ChatMessageDelta.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@ package com.cjcrafter.openai.chat.tool
33
import com.cjcrafter.openai.chat.ChatUser
44
import com.fasterxml.jackson.annotation.JsonProperty
55

6+
/**
7+
* Represents the "delta," or changes of a chat message. This is used by streams
8+
* stream 1 token at a time.
9+
*
10+
* @property role Who sent the message. Will always be [ChatUser.ASSISTANT] for the first message, then `null`
11+
* @property content 1 token of the message. Will always be `null` for tool calls
12+
* @property toolCalls Modifications to the tool calls. Will always be `null` when content is not `null`
13+
*/
614
data class ChatMessageDelta(
715
val role: ChatUser? = null,
816
val content: String? = null,
917
@JsonProperty("tool_calls") val toolCalls: List<ToolCallDelta>? = null,
10-
) {
11-
}
18+
)

src/main/kotlin/com/cjcrafter/openai/chat/tool/FunctionCall.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ data class FunctionCall(
2323
var name: String,
2424
var arguments: String,
2525
) {
26-
27-
@ApiStatus.Internal
28-
fun update(delta: FunctionCallDelta) {
26+
internal fun update(delta: FunctionCallDelta) {
2927
// The only field that updates is arguments
3028
arguments += delta.arguments
3129
}

src/main/kotlin/com/cjcrafter/openai/chat/tool/FunctionCallDelta.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package com.cjcrafter.openai.chat.tool
22

3-
import org.jetbrains.annotations.ApiStatus
4-
3+
/**
4+
* Represents the "delta," or changes of a function call. This is used by streams
5+
* to stream 1 token at a time.
6+
*
7+
* @property name The name of the function to call. Will always be `null` except for the first call.
8+
* @property arguments 1 token of the arguments. Well be delivered as a JSON string.
9+
*/
510
data class FunctionCallDelta(
611
val name: String?,
712
val arguments: String,
@@ -10,8 +15,7 @@ data class FunctionCallDelta(
1015
/**
1116
* Returns an **incomplete** function call.
1217
*/
13-
@ApiStatus.Internal
14-
fun toFunctionCall(): FunctionCall {
18+
internal fun toFunctionCall(): FunctionCall {
1519
return FunctionCall(
1620
name ?: throw IllegalStateException("name must be set"),
1721
arguments

src/main/kotlin/com/cjcrafter/openai/chat/tool/FunctionParameters.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ package com.cjcrafter.openai.chat.tool
1111
* @property properties The map of method parameters.
1212
*/
1313
data class FunctionParameters internal constructor(
14-
val type: String = "object",
15-
val properties: MutableMap<String, FunctionProperty> = mutableMapOf(),
16-
val required: MutableSet<String> = mutableSetOf(),
14+
var type: String = "object",
15+
var properties: MutableMap<String, FunctionProperty> = mutableMapOf(),
16+
var required: MutableSet<String> = mutableSetOf(),
1717
) {
1818
/**
1919
* Require that the given parameter is used by ChatGPT.

src/main/kotlin/com/cjcrafter/openai/chat/tool/FunctionTool.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import com.cjcrafter.openai.util.RegexInternals
1717
* @property description A description of the function
1818
*/
1919
data class FunctionTool internal constructor(
20-
@FunctionTag val name: String,
21-
val parameters: FunctionParameters,
22-
val description: String? = null,
20+
@FunctionTag var name: String,
21+
var parameters: FunctionParameters,
22+
var description: String? = null,
2323
) : AbstractTool() {
2424

2525
init {

src/main/kotlin/com/cjcrafter/openai/chat/tool/Tool.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ package com.cjcrafter.openai.chat.tool
1010
* @property function The function. This is only used if [type] is [ToolType.FUNCTION].
1111
*/
1212
data class Tool(
13-
val type: ToolType,
14-
val function: FunctionTool,
13+
var type: ToolType,
14+
var function: FunctionTool,
1515
)

src/main/kotlin/com/cjcrafter/openai/chat/tool/ToolCall.kt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
package com.cjcrafter.openai.chat.tool
22

3-
import org.jetbrains.annotations.ApiStatus
4-
3+
/**
4+
* Wraps a tool call by ChatGPT. You should check the [type] of the tool call,
5+
* and handle the request. For example, if the type is [ToolType.FUNCTION], you
6+
* should call the function and return the result.
7+
*
8+
* When making subsequent requests to chat completions, you should make sure to
9+
* pass the message that contained this tool call, and the result of the tool
10+
* call.
11+
*
12+
* @property id The id of this call. You should use this to construct a [com.cjcrafter.openai.chat.ChatUser.TOOL] message.
13+
* @property type The type of tool call. Currently, the only type is [ToolType.FUNCTION].
14+
* @property function The function call containing the function name and arguments.
15+
*/
516
data class ToolCall(
617
var id: String,
718
var type: ToolType,
819
var function: FunctionCall,
920
) {
10-
@ApiStatus.Internal
1121
internal fun update(delta: ToolCallDelta) {
1222
// The only field that updates is function
1323
if (delta.function != null)

src/main/kotlin/com/cjcrafter/openai/chat/tool/ToolCallDelta.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
package com.cjcrafter.openai.chat.tool
22

3-
import org.jetbrains.annotations.ApiStatus
4-
3+
/**
4+
* Represents the "delta," or changes of a tool call. This is used by streams
5+
* to stream 1 token at a time.
6+
*
7+
* @property index The index of the tool call we are modifying.
8+
* @property id The tool call id used in replies. Will always be `null` except for the first call.
9+
* @property type The type of tool call. Will always be `null` except for the first call.
10+
* @property function The modifications to the function call.
11+
*/
512
data class ToolCallDelta(
613
val index: Int,
714
val id: String? = null,
815
val type: ToolType? = null,
916
val function: FunctionCallDelta? = null,
1017
) {
11-
@ApiStatus.Internal
12-
fun toToolCall() = ToolCall(
18+
internal fun toToolCall() = ToolCall(
1319
id = id ?: throw IllegalStateException("id must be set"),
1420
type = type ?: throw IllegalStateException("type must be set"),
1521
function = function?.toFunctionCall() ?: throw IllegalStateException("function must be set"),

src/main/kotlin/com/cjcrafter/openai/chat/tool/ToolChoice.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package com.cjcrafter.openai.chat.tool
33
import com.cjcrafter.openai.jackson.ToolChoiceDeserializer
44
import com.cjcrafter.openai.jackson.ToolChoiceSerializer
55

6+
/**
7+
* Represents the configuration for tool choice. Defaults to [Auto].
8+
*/
69
sealed class ToolChoice {
710

811
/**

src/main/kotlin/com/cjcrafter/openai/chat/tool/ToolType.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ package com.cjcrafter.openai.chat.tool
22

33
import com.fasterxml.jackson.annotation.JsonProperty
44

5+
/**
6+
* Represents the type of tool. Currently, the only type of tool is a function.
7+
* In the future, this may include Data Analysis and DALL-E.
8+
*/
59
enum class ToolType {
610

711
/**

src/test/java/JavaTest.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import com.cjcrafter.openai.OpenAI;
12
import com.cjcrafter.openai.OpenAIImpl;
23
import com.cjcrafter.openai.chat.ChatMessage;
34
import com.cjcrafter.openai.chat.ChatRequest;
@@ -71,7 +72,9 @@ public static void doCompletion(boolean stream) {
7172

7273
// Loads the API key from the .env file in the root directory.
7374
String key = Dotenv.load().get("OPENAI_TOKEN");
74-
OpenAIImpl openai = new OpenAIImpl(key);
75+
OpenAI openai = OpenAI.builder()
76+
.apiKey(key)
77+
.build();
7578
System.out.println(RESET + "Generating Response" + PURPLE);
7679

7780
// Generate a print the message
@@ -104,7 +107,9 @@ public static void doChat(boolean stream) {
104107

105108
// Loads the API key from the .env file in the root directory.
106109
String key = Dotenv.load().get("OPENAI_TOKEN");
107-
OpenAIImpl openai = new OpenAIImpl(key);
110+
OpenAI openai = OpenAI.builder()
111+
.apiKey(key)
112+
.build();
108113

109114
// The conversation lasts until the user quits the program
110115
while (true) {

0 commit comments

Comments
 (0)