Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
tutn-axonivy committed Jan 11, 2024
1 parent 44fab07 commit 21a3abc
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,19 @@ public class MockAI {
"assist-edit-reponse", json(load("assist-edit-response.json"))
);

private final Map<String, JsonNode> openAIExamples = Map.of(
"completions", json(load("completions.json")),
"completions-response", json(load("completions-response.json")),
"mail-generator", json(load("mail-generator.json")),
"mail-generator-response", json(load("mail-generator-response.json"))
);

@POST
@Path("completions")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response assist(JsonNode request) {
var in = input(request);
var in = input(request, examples);
Ivy.log().info("in="+in+" /from="+request);
var node= examples.get(in+"-reponse");
return Response.ok()
Expand All @@ -69,15 +76,15 @@ public Response assist(JsonNode request) {
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response edit(JsonNode request) {
var in = input(request);
var in = input(request, examples);
Ivy.log().info("in="+in+" /from="+request);
var node= examples.get(in+"-reponse");
return Response.ok()
.entity(node)
.build();
}

private String input(JsonNode request) {
private String input(JsonNode request, Map<String, JsonNode> examples) {
for(var entry : examples.entrySet()) {
if (Objects.equals(entry.getValue(), request)) {
return entry.getKey();
Expand All @@ -86,14 +93,15 @@ private String input(JsonNode request) {
return null;
}


@POST
@POST
@Path("chat/completions")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response chat() {
public Response chat(JsonNode request) {
var in = input(request, openAIExamples);
var node = openAIExamples.get(in+"-response");
return Response.ok()
.entity(load("completions.json"))
.entity(node)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"id": "chatcmpl-772HYqavHUb3RclST47IzpTO9uDhQ",
"object": "chat.completion",
"created": 1681911424,
"model": "gpt-3.5-turbo-0301",
"usage": {
"prompt_tokens": 11,
"completion_tokens": 123,
"total_tokens": 134
},
"choices": [
{
"message": {
"role": "assistant",
"content": "As an AI language model, I cannot have a personal experience and thoughts, but the meaning of life is a philosophical and existential question that has puzzled humans since the beginning of time. It refers to the purpose, significance, or reason for existence, and it's a question that has generated many different answers and perspectives from different cultures, religions, and belief systems. While some people believe that the meaning of life is to fulfill a divine or spiritual purpose, others see it as a pursuit of happiness or personal fulfillment. Ultimately, the meaning of life is a deeply personal and subjective matter that each individual must find for themselves."
},
"finish_reason": "stop",
"index": 0
}
]
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
{
"id": "chatcmpl-772HYqavHUb3RclST47IzpTO9uDhQ",
"object": "chat.completion",
"created": 1681911424,
"model": "gpt-3.5-turbo-0301",
"usage": {
"prompt_tokens": 11,
"completion_tokens": 123,
"total_tokens": 134
},
"choices": [
{
"message": {
"role": "assistant",
"content": "As an AI language model, I cannot have a personal experience and thoughts, but the meaning of life is a philosophical and existential question that has puzzled humans since the beginning of time. It refers to the purpose, significance, or reason for existence, and it's a question that has generated many different answers and perspectives from different cultures, religions, and belief systems. While some people believe that the meaning of life is to fulfill a divine or spiritual purpose, others see it as a pursuit of happiness or personal fulfillment. Ultimately, the meaning of life is a deeply personal and subjective matter that each individual must find for themselves."
},
"finish_reason": "stop",
"index": 0
}
]
}
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "the meaning of life",
"name": "Unknown User (Session 2)"
}
],
"temperature": 1,
"top_p": 1,
"n": 1,
"stream": false,
"stop": null,
"max_tokens": null,
"presence_penalty": 0,
"frequency_penalty": 0,
"logit_bias": {
"50256": -100
},
"user": "Unknown User (Session 2)"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"id": "chatcmpl-772HYqavHUb3RclST47IzpTO9uDhQ",
"object": "chat.completion",
"created": 1681911424,
"model": "gpt-3.5-turbo-0301",
"usage": {
"prompt_tokens": 11,
"completion_tokens": 123,
"total_tokens": 134
},
"choices": [
{
"message": {
"role": "assistant",
"content": "As an AI language model, I cannot have a personal experience and thoughts, but the meaning of life is a philosophical and existential question that has puzzled humans since the beginning of time. It refers to the purpose, significance, or reason for existence, and it's a question that has generated many different answers and perspectives from different cultures, religions, and belief systems. While some people believe that the meaning of life is to fulfill a divine or spiritual purpose, others see it as a pursuit of happiness or personal fulfillment. Ultimately, the meaning of life is a deeply personal and subjective matter that each individual must find for themselves."
},
"finish_reason": "stop",
"index": 0
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "The candidate Name is 'Jack',\nCandidate most important skill is 'Backend',\nInterviewers Name is 'Bob',\nJob Position is 'Software Developer',\nInterview date is '2023-12-19',\ncompany is 'Test'.\nPlease create a 'APPROVE' follow up email, written in the name of the interviewer.\nThe position of the Interviewer is 'Network Engineer'.\nReturn in json string with attributes subject, content\"\"\"\n",
"name": "Developer"
}
],
"temperature": 1,
"top_p": 1,
"n": 1,
"stream": false,
"stop": null,
"max_tokens": null,
"presence_penalty": 0,
"frequency_penalty": 0,
"logit_bias": {
"50256": -100
},
"user": "Developer"
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ void chat() {
.contains("Inline styling should be avoided");
}

@Test
void mailGenerator() {
JsonNode chat = json(load("mail-generator.json"));
JsonNode result = chatAssist(chat);
assertThat(result.toPrettyString())
.isNotEmpty();
}

private static JsonNode assist(JsonNode quest) {
WebTarget client = Ivy.rest().client(OPEN_AI);
Entity<JsonNode> request = Entity.entity(quest, MediaType.APPLICATION_JSON);
Expand All @@ -94,4 +102,12 @@ private static JsonNode assist(JsonNode quest) {
return result;
}

private static JsonNode chatAssist(JsonNode quest) {
WebTarget client = Ivy.rest().client(OPEN_AI);
Entity<JsonNode> request = Entity.entity(quest, MediaType.APPLICATION_JSON);
JsonNode result = client.path("chat/completions").request()
.post(request).readEntity(JsonNode.class);
return result;
}

}

0 comments on commit 21a3abc

Please sign in to comment.