Skip to content

Commit f5a4e97

Browse files
committed
documentation improvements
1 parent d58bca4 commit f5a4e97

File tree

11 files changed

+80
-53
lines changed

11 files changed

+80
-53
lines changed

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ plugins {
88
`maven-publish`
99
signing
1010
kotlin("jvm") version "1.7.20-RC"
11+
id("org.jetbrains.dokka") version "1.8.10" // KDoc Documentation Builder
1112
id("com.github.breadmoirai.github-release") version "2.4.1"
1213
}
1314

src/main/java/com/cjcrafter/openai/chat/package-info.java

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/main/java/com/cjcrafter/openai/completions/package-info.java

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/main/java/com/cjcrafter/openai/exception/package-info.java

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/main/kotlin/com/cjcrafter/openai/chat/ChatBot.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,25 @@ import java.util.concurrent.TimeUnit
1414
/**
1515
* The ChatBot class wraps the OpenAI API and lets you send messages and
1616
* receive responses. For more information on how this works, check out
17-
* the [OpenAI Documentation](https://platform.openai.com/docs/api-reference/completions)).
17+
* the [OpenAI Documentation](https://platform.openai.com/docs/api-reference/chat)).
18+
*
19+
* [com.cjcrafter.openai.chat] differs from [com.cjcrafter.openai.completions].
20+
* Chat has conversational memory, and is best suited to generate human-readable
21+
* text. Think of chat as a human that insists on giving elaborate responses.
22+
* If you are looking for more robotic or specific responses, consider using
23+
* completions instead.
24+
*
25+
* To get your API key:
26+
* 1. Log in to your account: Go to [https://www.openai.com/](openai.com) and
27+
* log in.
28+
* 2. Access the API dashboard: After logging in, click on the "API" tab.
29+
* 3. Choose a subscription plan: Select a suitable plan based on your needs
30+
* and complete the payment process.
31+
* 4. Obtain your API key: After subscribing to a plan, you will be redirected
32+
* to the API dashboard, where you can find your unique API key. Copy and store it securely.
33+
*
34+
* @property apiKey Your OpenAI API key. It starts with `"sk-"` (without the quotes).
35+
* @constructor Create a ChatBot for responding to requests.
1836
*/
1937
class ChatBot(private val apiKey: String) {
2038

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,54 @@
11
package com.cjcrafter.openai.chat
22

3-
import com.google.gson.JsonObject
43
import com.google.gson.annotations.SerializedName
54

65
/**
7-
* These are the arguments that control the result of the output. For more
8-
* information, refer to the [OpenAI Docs](https://platform.openai.com/docs/api-reference/completions/create).
6+
* [ChatRequest] holds the configurable options that can be sent to the OpenAI
7+
* Chat API. For most use cases, you only need to set [model] and [messages].
8+
* For more detailed descriptions for each option, refer to the
9+
* [Chat Uncyclo](https://platform.openai.com/docs/api-reference/chat)
910
*
10-
* @param model The model used to generate the text. Recommended: "gpt-3.5-turbo."
11-
* @param messages All previous messages from the conversation.
12-
* @param temperature How "creative" the results are. [0.0, 2.0].
13-
* @param topP Controls how "on topic" the tokens are.
14-
* @param n Controls how many responses to generate. Numbers >1 will chew through your tokens.
15-
* @param stream **UNTESTED** recommend keeping this false.
16-
* @param stop The sequence used to stop generating tokens.
17-
* @param maxTokens The maximum number of tokens to use.
18-
* @param presencePenalty Prevent talking about duplicate topics.
19-
* @param frequencyPenalty Prevent repeating the same text.
20-
* @param logitBias Control specific tokens from being used.
21-
* @param user Who send this request (for moderation).
11+
* [messages] stores **ALL** previous messages from the conversation. It is
12+
* **YOUR RESPONSIBILITY** to store and update this list for your conversations
13+
* (Check out the [JavaChatTest] or the READEME.md for an example). In general,
14+
* the list should start with a [ChatUser.SYSTEM] message, then alternate between
15+
* [ChatUser.USER] and [ChatUser.ASSISTANT]. Failing to follow this order may
16+
* confuse the model and cause it to apologize for not responding.
17+
*
18+
* It is best practice to store 1 [ChatRequest] for each conversation, and to
19+
* update the variables (especially [messages]) between [ChatBot.generateResponse]
20+
* calls. You can easily store your [ChatRequest] as a string or as a json file
21+
* using google's GSON library to serialize the object as a JSON string.
22+
*
23+
* You should not set [stream]. TODO update docs after adding stream support
24+
*
25+
* @property model The model used to generate the text. Recommended: `"gpt-3.5-turbo"` (without quotes).
26+
* @property messages A mutable list of previous messages from the conversation.
27+
* @property temperature How "creative" the results are. [0.0, 2.0]. Defaults to `1.0`.
28+
* @property topP Controls how "on topic" the tokens are. Defaults to `1.0`.
29+
* @property n Controls how many responses to generate. Numbers >1 will chew through your tokens. Defaults to `1`.
30+
* @property stream true=wait until entire message is generated, false=respond procedurally. Defaults to `false`.
31+
* @property stop The sequence used to stop generating tokens. Defaults to `null`.
32+
* @property maxTokens The maximum number of tokens to use. Defaults to `infinity`.
33+
* @property presencePenalty Prevent talking about duplicate topics. Defaults to `0.0`.
34+
* @property frequencyPenalty Prevent repeating the same text. Defaults to `0.0`.
35+
* @property logitBias Increase/Decrease the chances of a specific token to appear in generated text. Defaults to `null`.
36+
* @property user Who send this request (for moderation).
37+
* @constructor Create a chat request
38+
* @see ChatBot.generateResponse
2239
* @see <a href="https://platform.openai.com/docs/api-reference/completions/create">OpenAI Uncyclo</a>
2340
*/
2441
data class ChatRequest @JvmOverloads constructor(
25-
val model: String,
42+
var model: String,
2643
var messages: MutableList<ChatMessage>,
27-
val temperature: Float = 1.0f,
28-
@field:SerializedName("top_p") val topP: Float = 1.0f,
29-
val n: Int = 1,
30-
val stream: Boolean = false,
31-
val stop: String? = null,
32-
@field:SerializedName("max_tokens") val maxTokens: Int? = null,
33-
@field:SerializedName("presence_penalty") val presencePenalty: Float = 0f,
34-
@field:SerializedName("frequency_penalty") val frequencyPenalty: Float = 0f,
35-
@field:SerializedName("logit_bias") val logitBias: JsonObject? = null,
36-
val user: String? = null
44+
var temperature: Float? = null,
45+
@field:SerializedName("top_p") var topP: Float? = null,
46+
var n: Int? = null,
47+
var stream: Boolean? = null,
48+
var stop: String? = null,
49+
@field:SerializedName("max_tokens") var maxTokens: Int? = null,
50+
@field:SerializedName("presence_penalty") var presencePenalty: Float? = null,
51+
@field:SerializedName("frequency_penalty") var frequencyPenalty: Float? = null,
52+
@field:SerializedName("logit_bias") var logitBias: Map<String, Float>? = null,
53+
var user: String? = null
3754
)

src/main/kotlin/com/cjcrafter/openai/chat/ChatResponse.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ class ChatResponse(
2828
/**
2929
* Shorthand for accessing the generated messages (shorthand for
3030
* [ChatResponse.choices]).
31+
*
32+
* @param index The index of the message (`0` for most use cases).
33+
* @return The generated [ChatChoice] at the index.
3134
*/
3235
operator fun get(index: Int): ChatChoice {
3336
return choices[index]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* @see [OpenAI Uncyclo](https://platform.openai.com/docs/api-reference/completions)
3+
*/
4+
package com.cjcrafter.openai.completions
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Since we are accessing a cloud-based service, there are *many* steps
3+
* that can go wrong. This package contains classes the wraps common error
4+
* codes with instructions to fix or minimize the occurrence of errors. All
5+
* exceptions inherit from [com.cjcrafter.openai.exception.OpenAIException].
6+
*
7+
* @see [OpenAI Uncyclo](https://platform.openai.com/docs/guides/error-codes/api-errors)
8+
*/
9+
package com.cjcrafter.openai.exception

src/test/java/JavaTest.java renamed to src/test/java/JavaChatTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import java.util.List;
77
import java.util.Scanner;
88

9-
public class JavaTest {
9+
public class JavaChatTest {
1010

1111
public static void main(String[] args) throws IOException {
1212
Scanner scan = new Scanner(System.in);
File renamed without changes.

0 commit comments

Comments
 (0)