Skip to content

Commit 133e616

Browse files
committed
Update README.md
1 parent 13318ad commit 133e616

File tree

1 file changed

+189
-47
lines changed

1 file changed

+189
-47
lines changed

README.md

Lines changed: 189 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,97 +6,239 @@
66

77

88

9+
<details> <summary>Table of Contents</summary>
10+
11+
- [About](#about)
12+
- [Usage](#usage)
13+
- [Prerequisites](#prerequisites)
14+
- [Integration](#integration)
15+
- [Maven](#maven)
16+
- [Gradle](#gradle)
17+
- [Initialize JavaAI](#initialize-javaai)
18+
- [Passing the API key directly to the constructor](#passing-the-api-key-directly-to-the-constructor)
19+
- [Using environment variable](#using-environment-variable)
20+
- [Example](#example)
21+
- [ChatGPT](#chatgpt)
22+
- [DALL-E](#dall-e)
23+
- [TTS](#tts)
24+
- [Configuration](#configuration)
25+
- [Features](#features)
26+
- [License](#license)
27+
28+
</details>
29+
30+
## About
31+
32+
> JavaAi is a lightweight and easy-to-use library for the JVM. It allows you to interact with OpenAI models with just a
33+
> couple of lines.
34+
---
35+
36+
## Usage
937

10-
## Is a lightweight and easy to use library that allows you to interact with OpenAI models with just a few lines of code
38+
### Prerequisites
1139

12-
## How to use?
40+
- Java 21
41+
- Maven or Gradle
42+
- OpenAI API key
43+
- Internet connection
1344

1445
### Integration
1546

16-
#### _Maven_
47+
#### Maven
1748

1849
```xml
50+
1951
<dependency>
2052
<groupId>io.github.artemnefedov</groupId>
2153
<artifactId>javaai</artifactId>
2254
<version>0.4.0</version>
2355
</dependency>
2456
```
25-
___
26-
#### _Gradle_
57+
58+
#### Gradle
2759

2860
```groovy
2961
implementation 'io.github.artemnefedov:javaai:0.4.0'
3062
```
3163

32-
### Initialize JavaAI
64+
---
65+
66+
## Initialize JavaAI
67+
68+
> #### You can initialize JavaAI in two ways: by directly passing the API key to the constructor or by adding environment variables with the key to your system, naming it OPENAI_API_KEY as recommended by [OpenAi](https://help.openai.com/en/articles/5112595-best-practices-for-api-key-safety#h_a1ab3ba7b2)
69+
70+
### Passing the API key directly to the constructor
3371

3472
```java
35-
JavaAI javaAI = javaAiBuilder("YOUR_API-KEY");
36-
```
73+
import io.github.artemnefedov.javaai.service.JavaAI;
3774

38-
## Java AI example
75+
var javaAi = JavaAI.javaAiBuilder("YOUR_API_KEY");
76+
```
3977

40-
### ChatGPT
78+
### Using environment variable
4179

42-
You have 2 options to use JavaAI to work with ChatGPT.
43-
You can make a first request using List<ChatMessage> to set the context and get a response, and after that use a string.
44-
<br>Both options retain the message history.
45-
<br>The "**assistant**" role is used by default for answers, be careful.
4680
```java
47-
var messages = List.of(
48-
new ChatMessage("user","Hello!"),
49-
new ChatMessage("assistant","Hello! How can I assist you today?"));
81+
import io.github.artemnefedov.javaai.service.JavaAI;
5082

51-
String chatResponse = javaAI.chat(messages);
83+
var javaAI = JavaAI.javaAiBuilder();
5284
```
53-
#### _OR_
85+
86+
---
87+
88+
## Example
89+
90+
### ChatGPT
91+
92+
> You can use two ways to interact with ChatGPT:
93+
>1. Pass the user's message, as a string, to the `chat()` method.
94+
>```java
95+
> javaAi.chat("YOUR_QUESTION");
96+
>```
97+
>
98+
>2. Pass a saved conversation to the method as a `List<ChatMessage>`.
99+
>```java
100+
> var messages = List.of(
101+
> new ChatMessage("user", "what is the meaning of life?"),
102+
> new ChatMessage("AI", "The meaning of life is to be happy."),
103+
> new ChatMessage("user", "are you sure?")
104+
>);
105+
>
106+
>javaAI.chat(messages);
107+
> ```
108+
>
109+
> Depending on the value of `n` you [set](#configuration), you can use either the `chat()` method, which returns
110+
> a `String` response from the api, or the `chatWithChoices()` method, which returns multiple responses from the API
111+
> as `List<String>`, depending on the value of `n` you set.
112+
>
113+
---
114+
115+
### DALL-E
116+
117+
> You can use the `generateImage()` method to generate an image from a text prompt. The model will return a URL to the
118+
> result, as a List of String.
119+
> ```java
120+
> javaAI.generateImage("Computes science cat, photo on fujifilm x100v, 2024");
121+
> ```
122+
> <details><summary>Response</summary>
123+
>
124+
> ![CS cat](https://github.com/artemnefedov/JavaAI/blob/resource/img/cs-cat.jpg?raw=true)
125+
></details>
126+
127+
128+
---
129+
130+
### TTS
131+
132+
> To translate text to speech, you must pass to the `textToSpeech()` method a `string` containing the text you want to
133+
> voice and a `string` containing the location where the audio file will be saved.
134+
> ```java
135+
> javaAI.textToSpeech("Hi, my name is Atryom, and I made this piece of... code.", "path/to/save/audio.mp3");
136+
>```
137+
> <details><summary>Response</summary>
138+
>
139+
> </details>
140+
141+
142+
---
143+
144+
## Configuration
145+
146+
> You can specify different settings for each model, via the `setChatConfig()`, `setDalleConfig()`, and `setTtsConfig()`
147+
> methods. Accepting records `ChatConfig`, `DalleConfig` and `TtsConfig` respectively.
148+
149+
<details><summary>Config records view</summary>
150+
151+
---
152+
`ChatConfig.java`
153+
54154
```java
55-
javaAI.chat("What's 2 2?");
56-
javaAI.chat("What did I ask in the last question?");
155+
public record ChatConfig(
156+
Model model,
157+
float temperature,
158+
int topP,
159+
int n,
160+
boolean stream,
161+
String stop,
162+
int maxTokens,
163+
float presencePenalty,
164+
float frequencyPenalty,
165+
Map<Integer, Integer> logitBias,
166+
String user) {
167+
}
57168
```
58-
> ### Example of communication
59-
> **user:** What's 2 2?<br>
60-
> **assistant:** 2 + 2 equals 4<br>
61-
> **user**: What did I ask in the last question?<br>
62-
> **assistant**: In your last question, you asked "What's 2 2?"<br>
169+
170+
Parameters in [OpenAI API docs](https://platform.openai.com/docs/api-reference/chat/create)
171+
63172
---
64-
### DALL·E 2
65-
Image generation, the model will return a URL to the result, as a List of String
173+
`DalleConfig.java`
174+
66175
```java
67-
String imgUrl = javaAI.generateImage("cat sitting next to a cup of coffee");
176+
public record DalleConfig(
177+
DalleModel model,
178+
int n,
179+
String quality,
180+
ResponseFormat responseFormat,
181+
Size size,
182+
Style style,
183+
String user) {
184+
}
68185
```
69-
![cat_image](https://github.com/artemnefedov/JavaAI/blob/resource/img/cat_%20of_coffee.png?raw=true)
186+
187+
Parameters in [OpenAI API docs](https://platform.openai.com/docs/api-reference/images)
188+
70189
---
71-
### Completions
72-
Text generation, the model will return the response as a String
190+
`TtsConfig.java`
191+
73192
```java
74-
String response = javaAI.generateText("Say this is a test");
193+
public record TtsConfig(
194+
TtsModel model,
195+
Voice voice,
196+
VoiceResponseFormat responseFormat,
197+
float speed
198+
) {
199+
}
75200
```
201+
202+
Parameters in [OpenAI API docs](https://platform.openai.com/docs/api-reference/audio/createSpeech)
203+
76204
---
205+
</details>
77206
78-
### You can always set your parameters for the models
207+
### Example for Chat:
79208
80-
Example for Chat:
81209
```java
82-
javaAI.setChat(
83-
Chat.builder()
84-
.messages(new ArrayList<>())
85-
.model("gpt-3.5-turbo")
86-
.maxTokens(2000)
87-
.n(1)
88-
.build()
89-
);
210+
import io.github.artemnefedov.javaai.model.chat.ChatConfig;
211+
212+
var customChatConfig = new ChatConfig(
213+
ChatConfig.Model.GPT_3_5_TURBO,
214+
1F,
215+
1,
216+
1,
217+
false,
218+
"\n",
219+
2000,
220+
0F,
221+
0F,
222+
new HashMap<>(),
223+
UUID.randomUUID().toString()
224+
);
225+
226+
javaAi.
227+
228+
setChatConfig(customChatConfig)
90229
```
230+
91231
---
92232
93-
## Models that JavaAI works with:
233+
## Features
94234
95-
1. [x] [Completions](https://platform.openai.com/docs/api-reference/completions)
96-
2. [x] [Chat-GPT](https://platform.openai.com/docs/api-reference/chat)
97-
3. [x] [Create image](https://platform.openai.com/docs/api-reference/images/create)
235+
1. [x] [GPT](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo)
236+
2. [x] [DALL·E](https://platform.openai.com/docs/models/dall-e)
237+
3. [x] [TTS](https://platform.openai.com/docs/models/tts)
238+
4. [ ] [Whisper](https://platform.openai.com/docs/models/whisper)
98239
99240
---
241+
100242
## License
101243
102244
#### Distributed under the [MIT License](./LICENSE)

0 commit comments

Comments
 (0)