Skip to content

Commit 5c33072

Browse files
committed
improve method names to better match official API
1 parent 3e060b8 commit 5c33072

File tree

6 files changed

+23
-20
lines changed

6 files changed

+23
-20
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import com.github.breadmoirai.githubreleaseplugin.GithubReleaseTask
22

33
group = "com.cjcrafter"
4-
version = "1.2.1"
4+
version = "1.2.2"
55

66
plugins {
77
`java-library`

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ class OpenAI @JvmOverloads constructor(
6565
* @throws IllegalArgumentException If the input arguments are invalid.
6666
*/
6767
@Throws(IOException::class)
68-
fun generateResponse(request: ChatRequest): ChatResponse {
68+
fun createChatCompletion(request: ChatRequest): ChatResponse {
6969
request.stream = false // use streamResponse for stream=true
7070
val httpRequest = buildRequest(request)
7171

7272
// Save the JsonObject to check for errors
73-
var rootObject: JsonObject? = null
73+
var rootObject: JsonObject?
7474
try {
7575
client.newCall(httpRequest).execute().use { response ->
7676

@@ -86,7 +86,7 @@ class OpenAI @JvmOverloads constructor(
8686
}
8787

8888
/**
89-
* This is a helper method that calls [streamResponse], which lets you use
89+
* This is a helper method that calls [streamChatCompletion], which lets you use
9090
* the generated tokens in real time (As ChatGPT generates them).
9191
*
9292
* This method does not block the thread. Method calls to [onResponse] are
@@ -112,8 +112,8 @@ class OpenAI @JvmOverloads constructor(
112112
* @param onResponse The method to call for each chunk.
113113
* @since 1.2.0
114114
*/
115-
fun streamResponseKotlin(request: ChatRequest, onResponse: ChatResponseChunk.() -> Unit) {
116-
streamResponse(request, { it.onResponse() })
115+
fun streamChatCompletionKotlin(request: ChatRequest, onResponse: ChatResponseChunk.() -> Unit) {
116+
streamChatCompletion(request, { it.onResponse() })
117117
}
118118

119119
/**
@@ -122,7 +122,7 @@ class OpenAI @JvmOverloads constructor(
122122
* to update the user without long delays between their input and OpenAI's
123123
* response.
124124
*
125-
* For *"simpler"* calls, you can use [generateResponse] which will block
125+
* For *"simpler"* calls, you can use [createChatCompletion] which will block
126126
* the thread until the entire response is generated.
127127
*
128128
* Instead of using the [ChatResponse], this method uses [ChatResponseChunk].
@@ -137,16 +137,17 @@ class OpenAI @JvmOverloads constructor(
137137
* @param onResponse The method to call for each chunk.
138138
* @param onFailure The method to call if the HTTP fails. This method will
139139
* not be called if OpenAI returns an error.
140-
* @see generateResponse
141-
* @see streamResponseKotlin
140+
* @see createChatCompletion
141+
* @see streamChatCompletionKotlin
142142
* @since 1.2.0
143143
*/
144144
@JvmOverloads
145-
fun streamResponse(
145+
fun streamChatCompletion(
146146
request: ChatRequest,
147147
onResponse: Consumer<ChatResponseChunk>, // use Consumer instead of Kotlin for better Java syntax
148148
onFailure: Consumer<IOException> = Consumer { it.printStackTrace() }
149149
) {
150+
@Suppress("DEPRECATION")
150151
request.stream = true // use requestResponse for stream=false
151152
val httpRequest = buildRequest(request)
152153

src/test/java/JavaChatStreamTest.java

Lines changed: 3 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.chat.*;
23
import io.github.cdimascio.dotenv.Dotenv;
34

@@ -15,15 +16,15 @@ public static void main(String[] args) {
1516
String initialPrompt = "You are a customer support chat-bot. Write brief summaries of the user's questions so that agents can easily find the answer in a database.";
1617
List<ChatMessage> messages = new ArrayList<>(List.of(new ChatMessage(ChatUser.SYSTEM, initialPrompt)));
1718
ChatRequest request = new ChatRequest("gpt-3.5-turbo", messages);
18-
ChatBot bot = new ChatBot(key);
19+
OpenAI openai = new OpenAI(key);
1920

2021
while (true) {
2122
System.out.println("Enter text below:\n\n");
2223
String input = scan.nextLine();
2324

2425
// Generate a response, and print it to the user.
2526
messages.add(new ChatMessage(ChatUser.USER, input));
26-
bot.streamResponse(request, message -> {
27+
openai.streamChatCompletion(request, message -> {
2728
System.out.print(message.get(0).getDelta());
2829

2930
if (message.get(0).getFinishReason() != null) {

src/test/java/JavaChatTest.java

Lines changed: 3 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.chat.*;
23
import io.github.cdimascio.dotenv.Dotenv;
34

@@ -16,15 +17,15 @@ public static void main(String[] args) throws IOException {
1617
String initialPrompt = "You are a customer support chat-bot. Write brief summaries of the user's questions so that agents can easily find the answer in a database.";
1718
List<ChatMessage> messages = new ArrayList<>(List.of(new ChatMessage(ChatUser.SYSTEM, initialPrompt)));
1819
ChatRequest request = new ChatRequest("gpt-3.5-turbo", messages);
19-
ChatBot bot = new ChatBot(key);
20+
OpenAI openai = new OpenAI(key);
2021

2122
while (true) {
2223
System.out.println("Enter text below:\n\n");
2324
String input = scan.nextLine();
2425

2526
// Generate a response, and print it to the user.
2627
messages.add(new ChatMessage(ChatUser.USER, input));
27-
ChatResponse response = bot.generateResponse(request);
28+
ChatResponse response = openai.createChatCompletion(request);
2829
System.out.println("\n" + response.get(0).getMessage().getContent());
2930

3031
// Save the generated message to the bot's conversational memory

src/test/kotlin/KotlinChatStreamTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import com.cjcrafter.openai.chat.ChatBot
1+
import com.cjcrafter.openai.OpenAI
22
import com.cjcrafter.openai.chat.ChatMessage.Companion.toSystemMessage
33
import com.cjcrafter.openai.chat.ChatMessage.Companion.toUserMessage
44
import com.cjcrafter.openai.chat.ChatRequest
@@ -13,15 +13,15 @@ fun main(args: Array<String>) {
1313
val initialPrompt = "Follow the users instructions"
1414
val messages = mutableListOf(initialPrompt.toSystemMessage())
1515
val request = ChatRequest("gpt-3.5-turbo", messages)
16-
val bot = ChatBot(key)
16+
val openai = OpenAI(key)
1717

1818
while (true) {
1919
println("Enter text below:\n")
2020
val input = scan.nextLine()
2121

2222
// Generate a response, and print it to the user.
2323
messages.add(input.toUserMessage())
24-
bot.streamResponseKotlin(request) {
24+
openai.streamChatCompletionKotlin(request) {
2525
print(choices[0].delta)
2626

2727
if (choices[0].finishReason != null) {

src/test/kotlin/KotlinChatTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import com.cjcrafter.openai.chat.ChatBot
1+
import com.cjcrafter.openai.OpenAI
22
import com.cjcrafter.openai.chat.ChatMessage.Companion.toSystemMessage
33
import com.cjcrafter.openai.chat.ChatMessage.Companion.toUserMessage
44
import com.cjcrafter.openai.chat.ChatRequest
@@ -13,15 +13,15 @@ fun main(args: Array<String>) {
1313
val initialPrompt = "You are a customer support chat-bot. Write brief summaries of the user's questions so that agents can easily find the answer in a database."
1414
val messages = mutableListOf(initialPrompt.toSystemMessage())
1515
val request = ChatRequest("gpt-3.5-turbo", messages)
16-
val bot = ChatBot(key)
16+
val openai = OpenAI(key)
1717

1818
while (true) {
1919
println("Enter text below:\n")
2020
val input = scan.nextLine()
2121

2222
// Generate a response, and print it to the user.
2323
messages.add(input.toUserMessage())
24-
val response = bot.generateResponse(request)
24+
val response = openai.createChatCompletion(request)
2525
println("\n${response[0].message.content}\n")
2626

2727
// Save the generated message to the bot's conversational memory

0 commit comments

Comments
 (0)