Skip to content

Commit 78c082f

Browse files
authored
add readme
1 parent 35f8a57 commit 78c082f

File tree

1 file changed

+85
-1
lines changed

1 file changed

+85
-1
lines changed

README.md

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,86 @@
11
# ChatGPT-Java-API
2-
ChatGPT API written for Java
2+
This is an easy-to-use "drag and drop" API that allows you to use OpenAI's new ChatGPT API. This API
3+
works by wrapping HTTPS requests with java variables, making the generated results much easier to control.
4+
5+
Feel free to use, modify, and distribute this code as needed.
6+
7+
# Working Example
8+
```java
9+
import java.io.IOException;
10+
import java.util.List;
11+
import java.util.Scanner;
12+
13+
public class Main {
14+
public static void main(String[] args) throws IOException {
15+
Scanner scan = new Scanner(System.in);
16+
String key = "sk-YOUR KEY HERE"; // TODO Add your open ai key here
17+
18+
// Create the initial prompt, we will reuse it later.
19+
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.";
20+
List<ChatBot.ChatMessage> messages = List.of(new ChatBot.ChatMessage("system", initialPrompt));
21+
ChatBot.ChatCompletionRequest request = new ChatBot.ChatCompletionRequest("gpt-3.5-turbo", messages);
22+
ChatBot bot = new ChatBot(key);
23+
24+
// ChatCompletionRequest copies the list, so let's modify the request's
25+
// copy of the list.
26+
messages = request.getMessages();
27+
28+
while (true) {
29+
System.out.println("Enter text below:\n\n");
30+
String input = scan.nextLine();
31+
32+
// Generate a response, and print it to the user.
33+
messages.add(new ChatBot.ChatMessage("user", input));
34+
ChatBot.ChatCompletionResponse response = bot.generateResponse(request);
35+
System.out.println("\n" + response.getChoices().get(0).getMessage().getContent());
36+
37+
// Save the generated message to the bot's conversational memory
38+
messages.add(response.getChoices().get(0).getMessage());
39+
}
40+
}
41+
}
42+
```
43+
44+
# Installation
45+
1. Add [okhttp](https://square.github.io/okhttp/) and [gson](https://github.com/google/gson) as dependencies (see below)
46+
2. Drag and drop the [`ChatBot.java`](https://github.com/CJCrafter/ChatGPT-Java-API/blob/master/ChatBot.java) file into your project
47+
48+
Maven:
49+
```xml
50+
<dependencies>
51+
<dependency>
52+
<groupId>com.squareup.okhttp3</groupId>
53+
<artifactId>okhttp</artifactId>
54+
<version>4.9.2</version>
55+
</dependency>
56+
<dependency>
57+
<groupId>com.google.code.gson</groupId>
58+
<artifactId>gson</artifactId>
59+
<version>2.8.9</version>
60+
</dependency>
61+
</dependencies>
62+
```
63+
64+
Gradle KTS:
65+
```gradle
66+
repositories {
67+
mavenCentral()
68+
}
69+
70+
dependencies {
71+
implementation("com.squareup.okhttp3:okhttp:4.9.2")
72+
implementation("com.google.code.gson:gson:2.8.9")
73+
}
74+
```
75+
76+
# More
77+
1. I also wrote a [Kotlin Version]() of the API.
78+
2. Need inspiration for prompts? Check out [awesome prompts](https://github.com/f/awesome-chatgpt-prompts).
79+
3. Looking for the official API? OpenAI only officially supports [python](https://github.com/openai/openai-python).
80+
81+
# Support
82+
If I have saved you time, please consider [sponsoring me](https://github.com/sponsors/CJCrafter).
83+
If you cannot financially support me, consider leaving a star on the repository and sharing it. Thanks!
84+
85+
# License
86+
ChatGPT-Kotlin-API is licensed under the [MIT License](https://github.com/CJCrafter/ChatGPT-Java-API/blob/master/LICENSE).

0 commit comments

Comments
 (0)