Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding testcase for llamaclient, and minor refactor #251

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@ public EdgeChain<List<Llama2ChatCompletionResponse>> createChatCompletion(
logger.info("==============REQUEST DATA================");
logger.info(request.toString());

// Llama2ChatCompletionRequest llamaRequest = new
// Llama2ChatCompletionRequest();
//
// llamaRequest.setInputs(request.getInputs());
//
// llamaRequest.setParameters(request.getParameters());

// Create headers
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
Expand All @@ -51,7 +44,8 @@ public EdgeChain<List<Llama2ChatCompletionResponse>> createChatCompletion(

List<Llama2ChatCompletionResponse> chatCompletionResponse =
objectMapper.readValue(
response, new TypeReference<List<Llama2ChatCompletionResponse>>() {});
response, new TypeReference<>() {
});
emitter.onNext(chatCompletionResponse);
emitter.onComplete();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public Llama2ChatCompletionRequest(String inputs, JSONObject parameters) {

@Override
public String toString() {
return new StringJoiner(", ", Llama2ChatCompletionRequest.class.getSimpleName() + "[{", "}]")
.add("\"inputs:\"" + inputs)
.add("\"parameters:\"" + parameters)
return new StringJoiner(", ", Llama2ChatCompletionRequest.class.getSimpleName() + "{", "}")
.add("\"inputs\":" + inputs)
.add("\"parameters\":" + parameters)
.toString();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.edgechain.llama;

import com.edgechain.lib.llama2.request.Llama2ChatCompletionRequest;
import org.json.JSONObject;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
public class Llama2ClientTest {

Logger logger = LoggerFactory.getLogger(getClass());

@Test
@DisplayName("Test LLamaClient - Request Format")
void TestLLamaClient_LLamaRequest_ShouldMatchFormat() {

Llama2ChatCompletionRequest llama2ChatCompletionRequest = Llama2ChatCompletionRequest.builder()
.inputs("<s>[INST]<<SYS>>What is the color of sky?<</SYS>>")
.parameters(getJsonObject())
.build();

logger.info("llama completion request data {} ", llama2ChatCompletionRequest);


String result = String.valueOf(new JSONObject(llama2ChatCompletionRequest));
String expected = getRequestBody();

Assertions.assertEquals(expected, result);
}
private static JSONObject getJsonObject() {
JSONObject parameters = new JSONObject();
parameters.put("do_sample", true);
parameters.put("top_p", 50);
parameters.put("temperature", 0.7);
parameters.put("top_k", 2);
parameters.put("max_new_tokens", 512);
parameters.put("repetition_penalty", 0.6);
parameters.put("stop", List.of("</s>"));
return parameters;
}

private static String getRequestBody(){
return "{\"inputs\":\"<s>[INST]<<SYS>>What is the color of sky?<<\\/SYS>>\"," +
"\"parameters\":{\"top_p\":50,\"stop\":[\"<\\/s>\"],\"max_new_tokens\":512," +
"\"top_k\":2,\"temperature\":0.7,\"do_sample\":true,\"repetition_penalty\":0.6}}";
}
}
Loading