Skip to content

Commit 148258c

Browse files
committed
added simple tests
1 parent 87881e1 commit 148258c

File tree

3 files changed

+101
-10
lines changed

3 files changed

+101
-10
lines changed

src/main/java/io/github/artemnefedov/javaai/service/impl/OpenAIImplementation.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,12 @@
4646
@Setter
4747
public class OpenAIImplementation implements OpenAI {
4848

49-
private String baseURL = Config.getInstance()
50-
.getProperties("url.openai.base");
51-
private String compURL = baseURL + Config.getInstance()
52-
.getProperties("url.openai.completions");
53-
private String imgBuilderURL = baseURL + Config.getInstance()
54-
.getProperties("url.openai.image_generator");
55-
private String chatURL = baseURL + Config.getInstance()
56-
.getProperties("url.openai.chat");
49+
private String baseURL = Config.getInstance().getProperties("url.openai.base");
50+
private String compURL = baseURL + Config.getInstance().getProperties("url.openai.completions");
51+
private String imgBuilderURL = baseURL + Config.getInstance().getProperties("url.openai.image_generator");
52+
private String chatURL = baseURL + Config.getInstance().getProperties("url.openai.chat");
53+
54+
5755

5856
protected Completions completions;
5957
protected ImageBuilder imageBuilder;
@@ -94,8 +92,6 @@ public String generateImage(String prompt) {
9492
ImageModelResponse imageResponse = connections
9593
.postStream(imageBuilder, imgBuilderURL, ImageModelResponse.class);
9694

97-
System.out.println(imageResponse);
98-
9995
if (imageBuilder.getResponse_format().equals("url")) {
10096

10197
return imageResponse.getData().get(0).getUrl();
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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.language.ChatMessage;
28+
import io.github.artemnefedov.javaai.dto.language.request.Completions;
29+
import io.github.artemnefedov.javaai.service.impl.OpenAIImplementation;
30+
import io.github.artemnefedov.javaai.util.Config;
31+
import org.junit.jupiter.api.AfterEach;
32+
import org.junit.jupiter.api.BeforeEach;
33+
import org.junit.jupiter.api.Test;
34+
35+
import java.util.ArrayList;
36+
import java.util.List;
37+
38+
import static org.junit.jupiter.api.Assertions.assertEquals;
39+
import static org.junit.jupiter.api.Assertions.assertTrue;
40+
41+
42+
class OpenAITest {
43+
44+
OpenAI openAI = new OpenAIImplementation(null);
45+
46+
47+
@BeforeEach
48+
void newOpenAI() {
49+
50+
OpenAI openAI = new OpenAIImplementation(Config.getInstance().getProperties("api_key.openai"));
51+
}
52+
53+
@Test
54+
void generateTextTest() {
55+
56+
assertEquals("This is indeed a test.", openAI.generateText("Say this is a test"));
57+
}
58+
59+
@Test
60+
void generateImageTest() {
61+
62+
String response = openAI.generateImage("A cute baby sea otter");
63+
assertTrue( response != null && response.matches("^(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]"));
64+
}
65+
66+
@Test
67+
void chatTest() {
68+
69+
List<ChatMessage> messages = new ArrayList<>();
70+
messages.add(new ChatMessage("user", "What is the name of the capital of Armenia?"));
71+
72+
assertEquals("The capital of Armenia is Yerevan.", openAI.chat(messages));
73+
}
74+
75+
@Test
76+
void customCompetitionsConfigTest() {
77+
78+
Completions completions =
79+
new Completions("text-curie-001", 7, 0.1f, (byte) 1, 1);
80+
openAI.customCompetitionsConfig(completions);
81+
82+
assertEquals("This is a test.", openAI.generateText("Say this is a test"));
83+
}
84+
85+
@AfterEach
86+
void clearOpenAI() {
87+
88+
openAI = null;
89+
}
90+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
url.openai.base=https://api.openai.com
2+
url.openai.completions=/v1/completions
3+
url.openai.image_generator=/v1/images/generations
4+
url.openai.chat=/v1/chat/completions
5+
api_key.openai=

0 commit comments

Comments
 (0)