Skip to content

Commit d55bdb4

Browse files
committed
update to 0.4.0
1 parent 50cf630 commit d55bdb4

File tree

13 files changed

+109
-415
lines changed

13 files changed

+109
-415
lines changed

README.md

Lines changed: 58 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,93 @@
11
![JavaAI logo](https://github.com/artemnefedov/JavaAI/blob/resource/img/javaAi_logo.png?raw=true)
22

3-
4-
[![GitHub release (latest by date)](https://img.shields.io/github/v/release/artemnefedov/JavaAI?logo=GitHub)](https://github.com/artemnefedov/JavaAI/releases)
53
[![Maven Central](https://img.shields.io/maven-central/v/io.github.artemnefedov/javaai.svg?label=Maven%20Central&logo=apachemaven)](https://central.sonatype.com/artifact/io.github.artemnefedov/javaai/)
64
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/artemnefedov/JavaAI/blob/main/LICENSE)
75
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/1194ce221f4f46ed950d4b05e6fd248c)](https://app.codacy.com/gh/artemnefedov/JavaAI/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
8-
![GitHub repo size](https://img.shields.io/github/repo-size/artemnefedov/JavaAI)
96

107

118

129

13-
## JavaAI is an open source library that allows you to interact with OpenAI models with just a few lines of code
10+
## Is a lightweight and easy to use library that allows you to interact with OpenAI models with just a few lines of code
1411

15-
#### Note:
12+
## How to use?
1613

17-
> I wrote this library for myself since I couldn't find anything more suitable for my project, so I decided to share it
18-
> with the community. I hope this library will help you.<br>
19-
> Any help is welcome. I am always glad to hear new ideas and criticism.
14+
### Integration
2015

21-
## How to use it?
16+
#### _Maven_
2217

23-
### Integration
18+
```xml
19+
<dependency>
20+
<groupId>io.github.artemnefedov</groupId>
21+
<artifactId>javaai</artifactId>
22+
<version>0.4.0</version>
23+
</dependency>
24+
```
25+
___
26+
#### _Gradle_
2427

25-
>#### _Maven_
26-
>
27-
>```xml
28-
><dependency>
29-
> <groupId>io.github.artemnefedov</groupId>
30-
> <artifactId>javaai</artifactId>
31-
> <version>0.3.5</version>
32-
></dependency>
33-
>```
34-
> ___
35-
>#### _Gradle_
36-
>
37-
>```groovy
38-
>implementation 'io.github.artemnefedov:javaai:0.3.5'
39-
>```
28+
```groovy
29+
implementation 'io.github.artemnefedov:javaai:0.4.0'
30+
```
4031

4132
### Initialize JavaAI
4233

43-
> You can pass your _**API-Key**_ in java code
44-
>```java
45-
>JavaAI javaAI = javaAiBuilder("YOUR_API-KEY");
46-
>```
47-
> ___
48-
>
49-
> Or paste it into the api-key field in the _**javaai.yaml**_, everyone loves yaml
50-
>```yaml
51-
>openai:
52-
> api-key: "YOUR_API-KEY"
53-
>```
54-
>```java
55-
>JavaAI javaAI = javaAiBuilder();
56-
>```
34+
```java
35+
JavaAI javaAI = javaAiBuilder("YOUR_API-KEY");
36+
```
5737

5838
## Java AI example
5939

6040
### ChatGPT
6141

62-
>You have 2 options to use JavaAI to work with ChatGPT.
42+
You have 2 options to use JavaAI to work with ChatGPT.
6343
You can make a first request using List<ChatMessage> to set the context and get a response, and after that use a string.
6444
<br>Both options retain the message history.
6545
<br>The "**assistant**" role is used by default for answers, be careful.
66-
>```java
67-
>var messages = List.of(
68-
> new ChatMessage("user","Hello!"),
69-
> new ChatMessage("assistant","Hello! How can I assist you today?"));
70-
>
71-
>String chatResponse = javaAI.chat(messages);
72-
>```
73-
>#### _OR_
74-
>```java
75-
>javaAI.chat("What's 2 2?");
76-
>javaAI.chat("What did I ask in the last question?");
77-
>```
78-
> > **user:** What's 2 2?<br>
79-
> > **assistant:** 2 + 2 equals 4<br>
80-
> > **user**: What did I ask in the last question?<br>
81-
> > **assistant**: In your last question, you asked "What's 2 2?"<br>
46+
```java
47+
var messages = List.of(
48+
new ChatMessage("user","Hello!"),
49+
new ChatMessage("assistant","Hello! How can I assist you today?"));
50+
51+
String chatResponse = javaAI.chat(messages);
52+
```
53+
#### _OR_
54+
```java
55+
javaAI.chat("What's 2 2?");
56+
javaAI.chat("What did I ask in the last question?");
57+
```
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>
8263
---
8364
### DALL·E 2
84-
>Image generation, the model will return a URL to the result, as a List of String
85-
>```java
86-
>String imgUrl = javaAI.generateImage("cat sitting next to a cup of coffee");
87-
>```
88-
> ![cat_image](https://github.com/artemnefedov/JavaAI/blob/resource/img/cat_%20of_coffee.png?raw=true)
65+
Image generation, the model will return a URL to the result, as a List of String
66+
```java
67+
String imgUrl = javaAI.generateImage("cat sitting next to a cup of coffee");
68+
```
69+
![cat_image](https://github.com/artemnefedov/JavaAI/blob/resource/img/cat_%20of_coffee.png?raw=true)
8970
---
9071
### Completions
91-
>Text generation, the model will return the response as a String
92-
>```java
93-
>String response = javaAI.generateText("Say this is a test");
94-
>```
72+
Text generation, the model will return the response as a String
73+
```java
74+
String response = javaAI.generateText("Say this is a test");
75+
```
9576
---
96-
**Notice:**
9777

98-
> You can always set your parameters for the models
78+
### You can always set your parameters for the models
9979

10080
Example for Chat:
101-
> _In java code_
102-
>```java
103-
>javaAI.setChat(
104-
> Chat.builder()
105-
> .messages(new ArrayList<>())
106-
> .model("gpt-3.5-turbo")
107-
> .maxTokens(2000)
108-
> .n(1)
109-
> .build()
110-
> );
111-
>```
112-
> _or in **javaai.yaml**_(if you use javaai.yaml to pass api-key)
113-
> ```yaml
114-
> chat:
115-
> model: gpt-3.5-turbo
116-
> temperature: 0.9
117-
> top_p: 1
118-
> n: 1
119-
> stream: false
120-
> stop: \n
121-
> max_tokens: 2000
122-
> user:
123-
> ```
81+
```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+
);
90+
```
12491
---
12592

12693
## Models that JavaAI works with:
@@ -132,4 +99,4 @@ Example for Chat:
13299
---
133100
## License
134101

135-
#### Distributed under the [MIT License](./LICENSE)
102+
#### Distributed under the [MIT License](./LICENSE)

pom.xml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.github.artemnefedov</groupId>
88
<artifactId>javaai</artifactId>
9-
<version>0.3.5</version>
9+
<version>0.4.0</version>
1010
<packaging>jar</packaging>
1111

1212
<name>JavaAI</name>
@@ -74,12 +74,6 @@
7474
<version>5.10.0</version>
7575
<scope>test</scope>
7676
</dependency>
77-
78-
<dependency>
79-
<groupId>org.yaml</groupId>
80-
<artifactId>snakeyaml</artifactId>
81-
<version>2.2</version>
82-
</dependency>
8377
</dependencies>
8478

8579
<build>

src/main/java/io/github/artemnefedov/javaai/dto/Chat.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,21 +93,13 @@ public record ChatResponse(
9393
/**
9494
* The type Chat choice.
9595
*/
96-
public record ChatChoice(
97-
int index,
98-
ChatMessage message,
99-
String finishReason
100-
) {
96+
public record ChatChoice(int index, ChatMessage message, String finishReason) {
10197
}
10298

10399
/**
104100
* The type Usage.
105101
*/
106-
record Usage(
107-
int promptTokens,
108-
int completionTokens,
109-
int totalTokens
110-
) {
102+
record Usage(int promptTokens, int completionTokens, int totalTokens) {
111103
}
112104

113105
/**
@@ -116,7 +108,6 @@ record Usage(
116108
* @return the response
117109
*/
118110
public String getResponse() {
119-
120111
return this.choices().get(0).message().getContent().trim();
121112
}
122113
}

src/main/java/io/github/artemnefedov/javaai/dto/ChatMessage.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,12 @@
2828
import lombok.Getter;
2929
import lombok.NoArgsConstructor;
3030
import lombok.Setter;
31-
import lombok.ToString;
3231

3332
/**
3433
* Helper class for {@link io.github.artemnefedov.javaai.dto.Chat}
3534
*/
3635
@Getter
3736
@Setter
38-
@ToString
3937
@AllArgsConstructor
4038
@NoArgsConstructor
4139
public class ChatMessage {

src/main/java/io/github/artemnefedov/javaai/dto/Completions.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,22 +101,13 @@ public record LanguageModelResponse(
101101
/**
102102
* The type Choice.
103103
*/
104-
record Choice(
105-
String text,
106-
int index,
107-
Object logprobs,
108-
String finishReason
109-
) {
104+
record Choice(String text, int index, Object logprobs, String finishReason) {
110105
}
111106

112107
/**
113108
* The type Usage.
114109
*/
115-
record Usage(
116-
int promptTokens,
117-
int completionTokens,
118-
int totalTokens
119-
) {
110+
record Usage(int promptTokens, int completionTokens, int totalTokens) {
120111
}
121112

122113
/**

src/main/java/io/github/artemnefedov/javaai/dto/ErrorDetails.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,6 @@ public String getErrorDetails() {
6868
/**
6969
* The type Error.
7070
*/
71-
record Error(
72-
String message,
73-
String type,
74-
String param,
75-
String code
76-
) {
71+
record Error(String message, String type, String param, String code) {
7772
}
7873
}

src/main/java/io/github/artemnefedov/javaai/dto/ImageBuilder.java

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,29 @@
2525
package io.github.artemnefedov.javaai.dto;
2626

2727
import com.google.gson.annotations.SerializedName;
28-
import lombok.AllArgsConstructor;
29-
import lombok.Builder;
30-
import lombok.Data;
31-
import lombok.NoArgsConstructor;
28+
import lombok.Getter;
29+
import lombok.Setter;
3230

3331
import java.util.List;
3432

3533
/**
3634
* dto to send a request to the <a href="https://platform.openai.com/docs/api-reference/images/create">Create image</a>.
3735
*/
38-
@Data
39-
@Builder
40-
@AllArgsConstructor
41-
@NoArgsConstructor
36+
@Getter
37+
@Setter
4238
public class ImageBuilder implements OpenAIModel {
4339

4440
private String prompt;
45-
private final int n = 1;
46-
private final String size = "1024x1024";
41+
private final int n;
42+
private final String size;
4743
@SerializedName("response_format")
48-
private final String responseFormat = "url";
44+
private final String responseFormat;
45+
46+
public ImageBuilder() {
47+
this.n = 1;
48+
this.size = "1024x1024";
49+
this.responseFormat = "url";
50+
}
4951

5052
@Override
5153
public Class<? extends ModelResponse> getResponseModel() {
@@ -57,23 +59,17 @@ public Class<? extends ModelResponse> getResponseModel() {
5759
*/
5860
public record ImageModelResponse(
5961
long created,
60-
List<ImageData> data
61-
62-
) implements ModelResponse {
62+
List<ImageData> data) implements ModelResponse {
6363

6464
@Override
6565
public String getResponse() {
66-
6766
return this.data.get(0).url();
6867
}
6968

7069
/**
7170
* The type Image data.
7271
*/
73-
public record ImageData(
74-
String url,
75-
String b64Json
76-
) {
72+
public record ImageData(String url, String b64Json) {
7773
}
7874
}
7975
}

src/main/java/io/github/artemnefedov/javaai/service/JavaAI.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,6 @@ static JavaAI javaAiBuilder(String apiKey) {
8989
return new JavaAIImplementation(apiKey);
9090
}
9191

92-
/**
93-
* JavaAI builder
94-
*
95-
* @return the JavaAI implementation
96-
*/
97-
static JavaAI javaAiBuilder() {
98-
return new JavaAIImplementation();
99-
}
100-
10192
/**
10293
* Sets your dto to work with the API
10394
*

0 commit comments

Comments
 (0)