Skip to content

Commit

Permalink
update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
CJCrafter committed Nov 11, 2023
1 parent b6b117a commit 34bb3ce
Showing 1 changed file with 60 additions and 53 deletions.
113 changes: 60 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# ChatGPT Java API
[![Maven Central](https://img.shields.io/maven-central/v/com.cjcrafter/openai?color=blue&label=Download)](https://central.sonatype.com/namespace/com.cjcrafter)
[![](https://img.shields.io/badge/-docs%20-blueviolet?logo=Kotlin&colorA=gray)](https://openai.cjcrafter.com/)
[![](https://img.shields.io/badge/-examples%20-orange?logo=Read+The+Docs&colorA=gray)](https://github.com/CJCrafter/ChatGPT-Java-API/wiki)
[![](https://img.shields.io/badge/-examples%20-orange?logo=Read+The+Docs&colorA=gray)](https://github.com/CJCrafter/ChatGPT-Java-API/tree/master/examples/src/main)
[![](https://img.shields.io/github/discussions/CJCrafter/ChatGPT-Java-API)](https://github.com/CJCrafter/ChatGPT-Java-API/discussions)
[![License](https://img.shields.io/github/license/CJCrafter/ChatGPT-Java-API)](https://github.com/CJCrafter/ChatGPT-Java-API/blob/master/LICENSE)

Expand All @@ -12,80 +12,87 @@ A community-maintained easy-to-use Java/Kotlin OpenAI API for ChatGPT, Text Comp

## Features
* [Completions](https://platform.openai.com/docs/api-reference/completions)
* Streaming support via `OpenAI#streamCompletion`
* [Chat Completions](https://platform.openai.com/docs/api-reference/chat)
* [Azure OpenAI](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference) support via `AzureOpenAI` class
* Streaming support via `OpenAI#streamChatCompletion`
* Functions support, check out the [java examples](https://github.com/CJCrafter/ChatGPT-Java-API/blob/master/examples/src/main/java/chat/StreamChatCompletionFunction.java#L49) and [kotlin examples](https://github.com/CJCrafter/ChatGPT-Java-API/blob/master/examples/src/main/kotlin/chat/StreamChatCompletionFunction.kt#L37)
* [Azure OpenAI](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference) support via `AzureOpenAI` class

## Installation
For Kotlin DSL (`build.gradle.kts`), add this to your dependencies block:
```kotlin
dependencies {
implementation("com.cjcrafter:openai:1.3.1")
implementation("com.cjcrafter:openai:2.0.0")
}
```
For Maven projects, add this to your `pom.xml` file in the `<dependencies>` block:
```xml
<dependency>
<groupId>com.cjcrafter</groupId>
<artifactId>openai</artifactId>
<version>1.3.1</version>
<version>2.0.0</version>
</dependency>
```
See the [maven repository](https://central.sonatype.com/artifact/com.cjcrafter/openai/1.3.1) for gradle/ant/etc.
See the [maven repository](https://central.sonatype.com/artifact/com.cjcrafter/openai/2.0.0) for gradle/ant/etc.


## Working Example
This is a basic working example. To see more features in action (async calls, streaming)
see the [java examples](https://github.com/CJCrafter/ChatGPT-Java-API/wiki/Java)
and [kotlin examples](https://github.com/CJCrafter/ChatGPT-Java-API/wiki/Kotlin)
This is a simple working example of the ChatGPT API in Java:
```java
public class JavaChatTest {

public static void main(String[] args) throws OpenAIError {
Scanner scan = new Scanner(System.in);

// This is the prompt that the bot will refer back to for every message.
ChatMessage prompt = ChatMessage.toSystemMessage("You are ChatGPT, a helpful chat bot.");

// Use a mutable (modifiable) list! Always! You should be reusing the
// ChatRequest variable, so in order for a conversation to continue
// you need to be able to modify the list.
List<ChatMessage> messages = new ArrayList<>(List.of(prompt));

// ChatRequest is the request we send to OpenAI API. You can modify the
// model, temperature, maxTokens, etc. This should be saved, so you can
// reuse it for a conversation.
ChatRequest request = ChatRequest.builder()
.model("gpt-3.5-turbo")
.messages(messages).build();

// Loads the API key from the .env file in the root directory.
// You should never put your API keys in code, keep your key safe!
String key = Dotenv.load().get("OPENAI_TOKEN");
OpenAI openai = new OpenAI(key);

// The conversation lasts until the user quits the program
while (true) {

// Prompt the user to enter a response
System.out.println("Enter text below:\n\n");
String input = scan.nextLine();

// Add the newest user message to the conversation
messages.add(ChatMessage.toUserMessage(input));

// Use the OpenAI API to generate a response to the current
// conversation. Print the resulting message.
ChatResponse response = openai.createChatCompletion(request);
System.out.println("\n" + response.get(0).getMessage().getContent());

// Save the generated message to the conversational memory. It is
// crucial to save this message, otherwise future requests will be
// confused that there was no response.
messages.add(response.get(0).getMessage());
}

import com.cjcrafter.openai.OpenAI;
import com.cjcrafter.openai.chat.ChatMessage;
import com.cjcrafter.openai.chat.ChatRequest;
import com.cjcrafter.openai.chat.ChatResponse;
import io.github.cdimascio.dotenv.Dotenv;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
* In this Java example, we will be using the Chat API to create a simple chatbot.
*/
public class ChatCompletion {

public static void main(String[] args) {

// To use dotenv, you need to add the "io.github.cdimascio:dotenv-kotlin:version"
// dependency. Then you can add a .env file in your project directory.
String key = Dotenv.load().get("OPENAI_TOKEN");
OpenAI openai = OpenAI.builder()
.apiKey(key)
.build();

List<ChatMessage> messages = new ArrayList<>();
messages.add(ChatMessage.toSystemMessage("Help the user with their problem."));

// Here you can change the model's settings, add tools, and more.
ChatRequest request = ChatRequest.builder()
.model("gpt-3.5-turbo")
.messages(messages)
.build();

Scanner scan = new Scanner(System.in);
while (true) {
System.out.println("What are you having trouble with?");
String input = scan.nextLine();

messages.add(ChatMessage.toUserMessage(input));
ChatResponse response = openai.createChatCompletion(request);

System.out.println("Generating Response...");
System.out.println(response.get(0).getMessage().getContent());

// Make sure to add the response to the messages list!
messages.add(response.get(0).getMessage());
}
}
}
```

For more examples, check out [examples](https://github.com/CJCrafter/ChatGPT-Java-API/tree/master/examples/src/main).

> **Note**: OpenAI recommends using environment variables for your API token
([Read more](https://help.openai.com/en/articles/5112595-best-practices-for-api-key-safety)).

Expand Down

0 comments on commit 34bb3ce

Please sign in to comment.