-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
334 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -29,4 +29,5 @@ log/ | |
target/ | ||
|
||
# ChatGPT-Java-API Specific Ignore | ||
.env | ||
.env | ||
debug.log |
This file contains 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 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 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 @@ | ||
<configuration> | ||
<appender name="FILE" class="ch.qos.logback.core.FileAppender"> | ||
<file>debug.log</file> | ||
<append>false</append> | ||
<encoder> | ||
<pattern>%date %level [%thread] %logger{10} %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<root level="DEBUG"> | ||
<appender-ref ref="FILE"/> | ||
</root> | ||
</configuration> |
This file contains 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 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 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,15 @@ | ||
package com.cjcrafter.openai | ||
|
||
import org.jetbrains.annotations.Contract | ||
|
||
/** | ||
* Builds an [OpenAI] instance using the default implementation. | ||
*/ | ||
@Contract(pure = true) | ||
fun openAI(init: OpenAI.Builder.() -> Unit) = OpenAI.builder().apply(init).build() | ||
|
||
/** | ||
* Builds an [OpenAI] instance using the Azure implementation. | ||
*/ | ||
@Contract(pure = true) | ||
fun azureOpenAI(init: OpenAI.AzureBuilder.() -> Unit) = OpenAI.azureBuilder().apply(init).build() |
This file contains 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
111 changes: 111 additions & 0 deletions
111
src/test/kotlin/com/cjcrafter/openai/chat/ChatRequestTest.kt
This file contains 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,111 @@ | ||
package com.cjcrafter.openai.chat | ||
|
||
import com.cjcrafter.openai.OpenAI | ||
import com.cjcrafter.openai.chat.ChatMessage.Companion.toSystemMessage | ||
import org.intellij.lang.annotations.Language | ||
import org.junit.jupiter.api.Assertions.* | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.Arguments | ||
import org.junit.jupiter.params.provider.MethodSource | ||
import java.util.stream.Stream | ||
|
||
class ChatRequestTest { | ||
|
||
@ParameterizedTest | ||
@MethodSource("provide_serialize") | ||
fun `test deserialize to json`(obj: Any, json: String) { | ||
val objectMapper = OpenAI.createObjectMapper() | ||
val expected = objectMapper.readTree(json) | ||
val actual = objectMapper.readTree(objectMapper.writeValueAsString(obj)) | ||
assertEquals(expected, actual) | ||
} | ||
|
||
@ParameterizedTest | ||
@MethodSource("provide_serialize") | ||
fun `test serialize from json`(expected: Any, json: String) { | ||
val objectMapper = OpenAI.createObjectMapper() | ||
val actual = objectMapper.readValue(json, expected::class.java) | ||
assertEquals(expected, actual) | ||
} | ||
|
||
companion object { | ||
@JvmStatic | ||
fun provide_serialize(): Stream<Arguments> { | ||
return buildList<Arguments> { | ||
|
||
@Language("JSON") | ||
var json = """ | ||
{ | ||
"messages": [ | ||
{ | ||
"role": "system", | ||
"content": "Be as helpful as possible" | ||
} | ||
], | ||
"model": "gpt-3.5-turbo" | ||
} | ||
""".trimIndent() | ||
add(Arguments.of( | ||
ChatRequest.builder() | ||
.model("gpt-3.5-turbo") | ||
.messages(mutableListOf("Be as helpful as possible".toSystemMessage())) | ||
.build(), | ||
json | ||
)) | ||
|
||
json = """ | ||
{ | ||
"messages": [ | ||
{ | ||
"role": "system", | ||
"content": "Be as helpful as possible" | ||
}, | ||
{ | ||
"role": "user", | ||
"content": "What is 2 + 2?" | ||
} | ||
], | ||
"model": "gpt-3.5-turbo", | ||
"tools": [ | ||
{ | ||
"type": "function", | ||
"function": { | ||
"name": "solve_math_problem", | ||
"parameters": { | ||
"type": "object", | ||
"properties": { | ||
"equation": { | ||
"type": "string", | ||
"description": "The math problem for you to solve" | ||
} | ||
}, | ||
"required": [ | ||
"equation" | ||
] | ||
}, | ||
"description": "Returns the result of a math problem as a double" | ||
} | ||
} | ||
] | ||
} | ||
""".trimIndent() | ||
add(Arguments.of( | ||
chatRequest { | ||
model("gpt-3.5-turbo") | ||
messages(mutableListOf( | ||
ChatMessage(ChatUser.SYSTEM, "Be as helpful as possible"), | ||
ChatMessage(ChatUser.USER, "What is 2 + 2?") | ||
)) | ||
function { | ||
name("solve_math_problem") | ||
description("Returns the result of a math problem as a double") | ||
addStringParameter("equation", "The math problem for you to solve", true) | ||
} | ||
}, | ||
json | ||
)) | ||
|
||
}.stream() | ||
} | ||
} | ||
} |
Oops, something went wrong.