-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSpringaiApplicationTests.java
66 lines (49 loc) · 1.94 KB
/
SpringaiApplicationTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package dev.arubino.springai;
import dev.arubino.springai.configuration.*;
import dev.arubino.springai.configuration.Model;
import dev.arubino.springai.controller.byod.*;
import dev.arubino.springai.service.*;
import org.junit.jupiter.api.Test;
import org.springframework.ai.chat.client.*;
import org.springframework.ai.chat.client.advisor.*;
import org.springframework.ai.chat.model.*;
import org.springframework.ai.evaluation.*;
import org.springframework.ai.model.*;
import org.springframework.ai.vectorstore.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.*;
import static org.junit.jupiter.api.Assertions.assertTrue;
@SpringBootTest
class SpringaiApplicationTests {
@Autowired
private ChatClientService chatClientService;
@Autowired
private ChatModelConfig chatModelConfig;
@Autowired
private RagController ragController;
@Autowired
private VectorStoreConfig vectorStoreConfig;
@Test
void testEvaluation() {
ragController.load(Model.OLLAMA, false);
ragController.load(Model.OPENAI, false);
var userPromptTemplate = """
What is the story of Elara?
""";
ChatResponse response = chatClientService.getChatClient(Model.OLLAMA)
.prompt()
.advisors(new QuestionAnswerAdvisor(vectorStoreConfig.getVectorStore(Model.OLLAMA), SearchRequest.defaults()))
.user(userSpec -> userSpec
.text(userPromptTemplate)
)
.call()
.chatResponse();
var relevancyEvaluator = new RelevancyEvaluator(ChatClient.builder(chatModelConfig.get(Model.OPENAI)));
EvaluationRequest evaluationRequest = new EvaluationRequest(userPromptTemplate,
(List<Content>) response.getMetadata().get(QuestionAnswerAdvisor.RETRIEVED_DOCUMENTS),
response);
EvaluationResponse evaluationResponse = relevancyEvaluator.evaluate(evaluationRequest);
assertTrue(evaluationResponse.isPass(), "Response is not relevant to the question");
}
}