Skip to content

Commit 10944fc

Browse files
committed
update JavaAITest.java
1 parent 07e4224 commit 10944fc

File tree

4 files changed

+256
-220
lines changed

4 files changed

+256
-220
lines changed

README.md

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,72 +6,108 @@
66
## JavaAI is an open source library that allows you to interact with OpenAI models with just a few lines of code
77

88
#### Note:
9-
> I wrote this library for myself since I couldn't find anything more suitable for my project, so I decided to share it with the community. I hope this library will help you.<br>
10-
Any help is welcome. I am always glad to hear new ideas and criticism.
9+
10+
> I wrote this library for myself since I couldn't find anything more suitable for my project, so I decided to share it
11+
> with the community. I hope this library will help you.<br>
12+
> Any help is welcome. I am always glad to hear new ideas and criticism.
1113
1214
## How to use it?
1315

1416
### Import the library
1517

1618
#### Maven:
19+
1720
```xml
21+
1822
<dependency>
1923
<groupId>io.github.artemnefedov</groupId>
2024
<artifactId>javaai</artifactId>
21-
<version>0.3.3</version>
25+
<version>0.3.4</version>
2226
</dependency>
2327
```
28+
2429
#### Gradle:
30+
2531
```groovy
26-
implementation group: 'io.github.artemnefedov', name: 'javaai', version: '0.3.3'
32+
implementation group: 'io.github.artemnefedov', name: 'javaai', version: '0.3.4'
2733
```
2834

29-
### Initialize OpenAI
35+
### Initialize JavaAI
36+
3037
```java
31-
OpenAI openAI = new OpenAIImplementation("YOUR_OPEN_AI_API-KEY");
38+
JavaAI javaAI=javaAiBuilder("YOUR_OPEN_AI_API-KEY");
3239
```
33-
### Set the query parameters and get the result from the OpenAI model
3440

35-
```java
36-
//Text generation, the model will return the response as a String
37-
String response = openAI.generateText("Say this is a test"))
41+
## Java AI example
3842

39-
//Image generation, the model will return a URL/ to the result, as a List of String
40-
List<String> imges = openAI.generateImage("A cute baby sea otter"));
43+
### ChatGPT
4144

42-
//Text generation, taking into account the dialogue (GPT-3.5), the model will return the answer as a String
43-
List<ChatMessage> messages = new ArrayList<>();
45+
>You have 2 options to use JavaAI to work with ChatGPT.
46+
You can make a first request using List<ChatMessage> to set the context and get a response, and after that use a string.
47+
<br>Both options retain the message history.
48+
<br>The "**assistant**" role is used by default for answers, be careful.
49+
50+
```java
51+
var messages=List.of(
52+
new ChatMessage("user","Hello!"),
53+
new ChatMessage("assistant","Hello! How can I assist you today?"));
54+
55+
String chatResponse = javaAI.chat(messages);
56+
```
57+
#### _OR_
58+
```java
59+
javaAI.chat("What's 2 2?"); // answer: 2 + 2 equals 4.
60+
javaAI.chat("What did I ask in the last question?"); //answer: In your last question, you asked "What's 2 2?"
61+
```
4462

45-
messages.add(new ChatMessage("user", "Hello!"));
63+
### Completions
64+
```java
65+
//Text generation, the model will return the response as a String
66+
String response = javaAI.generateText("Say this is a test");
67+
```
4668

47-
String chatResponse = openAI.chat(messages);
69+
### DALL·E 2
70+
```java
71+
//Image generation, the model will return a URL/ to the result, as a List of String
72+
String imgUrl=javaAI.generateImage("A cute baby sea otter");
4873
```
49-
#### Just in case, I added a [demo](https://github.com/artemnefedov/JavaAI/tree/demo)
50-
<br>
74+
5175
---
5276

5377
**Notice:**
54-
<br>
78+
5579
> You can always set your parameters for the models
5680
57-
Example for ImageBuilder:
81+
Example for Chat:
82+
5883
```java
59-
ImageBuilder imageBuilder = new ImageBuilder( 1, "1024x1024", "url");
60-
openAI.customImageBuilderConfig(imageBuilder);
84+
javaAI.setChat(
85+
Chat.builder()
86+
.messages(new ArrayList<>())
87+
.model("gpt-3.5-turbo")
88+
.maxTokens(2000)
89+
.n(1)
90+
.build()
91+
);
6192
```
6293

6394
---
95+
6496
## Models that JavaAI works with:
97+
6598
1. [x] [Completions](https://platform.openai.com/docs/api-reference/completions)
6699
2. [x] [Chat-GPT](https://platform.openai.com/docs/api-reference/chat)
67100
3. [x] [Create image](https://platform.openai.com/docs/api-reference/images/create)
68101

69102
---
103+
70104
## Outside Dependencies.
71-
#### This library uses:
72-
* [Gson](https://github.com/google/gson), to convert JSON to Java objects.<br>
73-
* [lombok](https://github.com/projectlombok/lombok) to make the code clearer.<br>
74-
* [junit](https://github.com/junit-team/junit5) for testing.
105+
106+
* [Gson](https://github.com/google/gson)
107+
* [lombok](https://github.com/projectlombok/lombok)
108+
* [junit](https://github.com/junit-team/junit5)
109+
* [logback-classic](https://mvnrepository.com/artifact/ch.qos.logback/logback-classic)
75110

76111
## License
112+
77113
#### Distributed under the [MIT License](https://github.com/artemnefedov/JavaAI/blob/main/LICENSE)
Lines changed: 96 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,96 @@
1-
/*
2-
* MIT License
3-
*
4-
* Copyright (c) 2023. Artyom Nefedov
5-
*
6-
* Permission is hereby granted, free of charge, to any person obtaining a copy
7-
* of this software and associated documentation files (the "Software"), to deal
8-
* in the Software without restriction, including without limitation the rights
9-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10-
* copies of the Software, and to permit persons to whom the Software is
11-
* furnished to do so, subject to the following conditions:
12-
*
13-
* The above copyright notice and this permission notice shall be included in all
14-
* copies or substantial portions of the Software.
15-
*
16-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22-
* SOFTWARE.
23-
*/
24-
25-
package io.github.artemnefedov.javaai.service;
26-
27-
import io.github.artemnefedov.javaai.dto.ChatMessage;
28-
import io.github.artemnefedov.javaai.dto.Chat;
29-
import io.github.artemnefedov.javaai.dto.Completions;
30-
31-
import java.util.List;
32-
33-
/**
34-
* The main class for interacting with JavaAI implements the {@link JavaAI} interface.
35-
*/
36-
public interface JavaAI {
37-
38-
/**
39-
* The method that generates the text interacts with the
40-
* <a href="https://platform.openai.com/docs/api-reference/completions">Completions</a>.
41-
*
42-
* @param prompt parameters for text generation.
43-
* @return the response from the API as a string
44-
*/
45-
String generateText(String prompt);
46-
47-
/**
48-
* The method that generates the image interacts with the
49-
* <a href="https://platform.openai.com/docs/api-reference/images/create">Create image</a>.
50-
*
51-
* @param prompt parameters for image generation.
52-
* @return the response from the API as a string(url)
53-
*/
54-
String generateImage(String prompt);
55-
56-
/**
57-
* The method that generates text, taking into account chat messages,
58-
* uses a <a href="https://platform.openai.com/docs/api-reference/chat">Chat</a>.
59-
*
60-
* @param messages List of {@link ChatMessage} containing your chat by role.
61-
* @return the response from the API as a string
62-
*/
63-
String chat(List<ChatMessage> messages);
64-
65-
/**
66-
* The method that generates text, taking into account chat messages,
67-
* uses a <a href="https://platform.openai.com/docs/api-reference/chat">Chat</a>.
68-
*
69-
* @param userMessage your message with the user role.
70-
* @return the response from the API as a string
71-
*/
72-
String chat(String userMessage);
73-
74-
/**
75-
* Sets your dto to work with the API
76-
* @param completions dto with your options.
77-
*/
78-
void setCompletions(Completions completions);
79-
80-
/**
81-
* Sets your dto to work with the API
82-
*
83-
* @param chat dto with your options.
84-
*/
85-
void setChat(Chat chat);
86-
87-
/**
88-
* Sets default options for dto, usually this should be enough to get the job done.
89-
*/
90-
void defaultCompetitionsConfig();
91-
92-
/**
93-
* Sets default options for dto, usually this should be enough to get the job done.
94-
*/
95-
void defaultChatConfig();
96-
}
1+
///*
2+
// * MIT License
3+
// *
4+
// * Copyright (c) 2023. Artyom Nefedov
5+
// *
6+
// * Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// * of this software and associated documentation files (the "Software"), to deal
8+
// * in the Software without restriction, including without limitation the rights
9+
// * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// * copies of the Software, and to permit persons to whom the Software is
11+
// * furnished to do so, subject to the following conditions:
12+
// *
13+
// * The above copyright notice and this permission notice shall be included in all
14+
// * copies or substantial portions of the Software.
15+
// *
16+
// * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// * SOFTWARE.
23+
// */
24+
//
25+
//package io.github.artemnefedov.javaai.service;
26+
//
27+
//import io.github.artemnefedov.javaai.dto.ChatMessage;
28+
//import io.github.artemnefedov.javaai.dto.Chat;
29+
//import io.github.artemnefedov.javaai.dto.Completions;
30+
//
31+
//import java.util.List;
32+
//
33+
///**
34+
// * The main class for interacting with JavaAI implements the {@link JavaAI} interface.
35+
// */
36+
//public interface JavaAI {
37+
//
38+
// /**
39+
// * The method that generates the text interacts with the
40+
// * <a href="https://platform.openai.com/docs/api-reference/completions">Completions</a>.
41+
// *
42+
// * @param prompt parameters for text generation.
43+
// * @return the response from the API as a string
44+
// */
45+
// String generateText(String prompt);
46+
//
47+
// /**
48+
// * The method that generates the image interacts with the
49+
// * <a href="https://platform.openai.com/docs/api-reference/images/create">Create image</a>.
50+
// *
51+
// * @param prompt parameters for image generation.
52+
// * @return the response from the API as a string(url)
53+
// */
54+
// String generateImage(String prompt);
55+
//
56+
// /**
57+
// * The method that generates text, taking into account chat messages,
58+
// * uses a <a href="https://platform.openai.com/docs/api-reference/chat">Chat</a>.
59+
// *
60+
// * @param messages List of {@link ChatMessage} containing your chat by role.
61+
// * @return the response from the API as a string
62+
// */
63+
// String chat(List<ChatMessage> messages);
64+
//
65+
// /**
66+
// * The method that generates text, taking into account chat messages,
67+
// * uses a <a href="https://platform.openai.com/docs/api-reference/chat">Chat</a>.
68+
// *
69+
// * @param userMessage your message with the user role.
70+
// * @return the response from the API as a string
71+
// */
72+
// String chat(String userMessage);
73+
//
74+
// /**
75+
// * Sets your dto to work with the API
76+
// * @param completions dto with your options.
77+
// */
78+
// void setCompletions(Completions completions);
79+
//
80+
// /**
81+
// * Sets your dto to work with the API
82+
// *
83+
// * @param chat dto with your options.
84+
// */
85+
// void setChat(Chat chat);
86+
//
87+
// /**
88+
// * Sets default options for dto, usually this should be enough to get the job done.
89+
// */
90+
// void defaultCompetitionsConfig();
91+
//
92+
// /**
93+
// * Sets default options for dto, usually this should be enough to get the job done.
94+
// */
95+
// void defaultChatConfig();
96+
//}

0 commit comments

Comments
 (0)