File tree Expand file tree Collapse file tree 12 files changed +65
-27
lines changed
main/kotlin/com/cjcrafter/openai Expand file tree Collapse file tree 12 files changed +65
-27
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ import okhttp3.OkHttpClient
4
4
import okhttp3.Request
5
5
import okhttp3.RequestBody
6
6
import okhttp3.RequestBody.Companion.toRequestBody
7
+ import org.jetbrains.annotations.ApiStatus
7
8
8
9
/* *
9
10
* The Azure OpenAI API client.
@@ -16,7 +17,7 @@ import okhttp3.RequestBody.Companion.toRequestBody
16
17
* @property apiVersion The API version to use. Defaults to 2023-03-15-preview.
17
18
* @property modelName The model name to use. This is the name of the model deployed to Azure.
18
19
*/
19
- class AzureOpenAI @JvmOverloads constructor(
20
+ class AzureOpenAI @ApiStatus. Internal constructor(
20
21
apiKey : String ,
21
22
organization : String? = null ,
22
23
client : OkHttpClient = OkHttpClient (),
Original file line number Diff line number Diff line change @@ -3,9 +3,16 @@ package com.cjcrafter.openai.chat.tool
3
3
import com.cjcrafter.openai.chat.ChatUser
4
4
import com.fasterxml.jackson.annotation.JsonProperty
5
5
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
+ */
6
14
data class ChatMessageDelta (
7
15
val role : ChatUser ? = null ,
8
16
val content : String? = null ,
9
17
@JsonProperty(" tool_calls" ) val toolCalls : List <ToolCallDelta >? = null ,
10
- ) {
11
- }
18
+ )
Original file line number Diff line number Diff line change @@ -23,9 +23,7 @@ data class FunctionCall(
23
23
var name : String ,
24
24
var arguments : String ,
25
25
) {
26
-
27
- @ApiStatus.Internal
28
- fun update (delta : FunctionCallDelta ) {
26
+ internal fun update (delta : FunctionCallDelta ) {
29
27
// The only field that updates is arguments
30
28
arguments + = delta.arguments
31
29
}
Original file line number Diff line number Diff line change 1
1
package com.cjcrafter.openai.chat.tool
2
2
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
+ */
5
10
data class FunctionCallDelta (
6
11
val name : String? ,
7
12
val arguments : String ,
@@ -10,8 +15,7 @@ data class FunctionCallDelta(
10
15
/* *
11
16
* Returns an **incomplete** function call.
12
17
*/
13
- @ApiStatus.Internal
14
- fun toFunctionCall (): FunctionCall {
18
+ internal fun toFunctionCall (): FunctionCall {
15
19
return FunctionCall (
16
20
name ? : throw IllegalStateException (" name must be set" ),
17
21
arguments
Original file line number Diff line number Diff line change @@ -11,9 +11,9 @@ package com.cjcrafter.openai.chat.tool
11
11
* @property properties The map of method parameters.
12
12
*/
13
13
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(),
17
17
) {
18
18
/* *
19
19
* Require that the given parameter is used by ChatGPT.
Original file line number Diff line number Diff line change @@ -17,9 +17,9 @@ import com.cjcrafter.openai.util.RegexInternals
17
17
* @property description A description of the function
18
18
*/
19
19
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 ,
23
23
) : AbstractTool() {
24
24
25
25
init {
Original file line number Diff line number Diff line change @@ -10,6 +10,6 @@ package com.cjcrafter.openai.chat.tool
10
10
* @property function The function. This is only used if [type] is [ToolType.FUNCTION].
11
11
*/
12
12
data class Tool (
13
- val type : ToolType ,
14
- val function : FunctionTool ,
13
+ var type : ToolType ,
14
+ var function : FunctionTool ,
15
15
)
Original file line number Diff line number Diff line change 1
1
package com.cjcrafter.openai.chat.tool
2
2
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
+ */
5
16
data class ToolCall (
6
17
var id : String ,
7
18
var type : ToolType ,
8
19
var function : FunctionCall ,
9
20
) {
10
- @ApiStatus.Internal
11
21
internal fun update (delta : ToolCallDelta ) {
12
22
// The only field that updates is function
13
23
if (delta.function != null )
Original file line number Diff line number Diff line change 1
1
package com.cjcrafter.openai.chat.tool
2
2
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
+ */
5
12
data class ToolCallDelta (
6
13
val index : Int ,
7
14
val id : String? = null ,
8
15
val type : ToolType ? = null ,
9
16
val function : FunctionCallDelta ? = null ,
10
17
) {
11
- @ApiStatus.Internal
12
- fun toToolCall () = ToolCall (
18
+ internal fun toToolCall () = ToolCall (
13
19
id = id ? : throw IllegalStateException (" id must be set" ),
14
20
type = type ? : throw IllegalStateException (" type must be set" ),
15
21
function = function?.toFunctionCall() ? : throw IllegalStateException (" function must be set" ),
Original file line number Diff line number Diff line change @@ -3,6 +3,9 @@ package com.cjcrafter.openai.chat.tool
3
3
import com.cjcrafter.openai.jackson.ToolChoiceDeserializer
4
4
import com.cjcrafter.openai.jackson.ToolChoiceSerializer
5
5
6
+ /* *
7
+ * Represents the configuration for tool choice. Defaults to [Auto].
8
+ */
6
9
sealed class ToolChoice {
7
10
8
11
/* *
Original file line number Diff line number Diff line change @@ -2,6 +2,10 @@ package com.cjcrafter.openai.chat.tool
2
2
3
3
import com.fasterxml.jackson.annotation.JsonProperty
4
4
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
+ */
5
9
enum class ToolType {
6
10
7
11
/* *
Original file line number Diff line number Diff line change
1
+ import com .cjcrafter .openai .OpenAI ;
1
2
import com .cjcrafter .openai .OpenAIImpl ;
2
3
import com .cjcrafter .openai .chat .ChatMessage ;
3
4
import com .cjcrafter .openai .chat .ChatRequest ;
@@ -71,7 +72,9 @@ public static void doCompletion(boolean stream) {
71
72
72
73
// Loads the API key from the .env file in the root directory.
73
74
String key = Dotenv .load ().get ("OPENAI_TOKEN" );
74
- OpenAIImpl openai = new OpenAIImpl (key );
75
+ OpenAI openai = OpenAI .builder ()
76
+ .apiKey (key )
77
+ .build ();
75
78
System .out .println (RESET + "Generating Response" + PURPLE );
76
79
77
80
// Generate a print the message
@@ -104,7 +107,9 @@ public static void doChat(boolean stream) {
104
107
105
108
// Loads the API key from the .env file in the root directory.
106
109
String key = Dotenv .load ().get ("OPENAI_TOKEN" );
107
- OpenAIImpl openai = new OpenAIImpl (key );
110
+ OpenAI openai = OpenAI .builder ()
111
+ .apiKey (key )
112
+ .build ();
108
113
109
114
// The conversation lasts until the user quits the program
110
115
while (true ) {
You can’t perform that action at this time.
0 commit comments