Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

matchers.ts 1.7 KB

You have to be logged in to leave a comment. Sign In
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
  1. import matchers from '../../dist/src/assertions';
  2. import type { GradingConfig } from '../../dist/src/types';
  3. declare global {
  4. namespace jest {
  5. interface Matchers<R> {
  6. toMatchSemanticSimilarity(expected: string, threshold?: number): R;
  7. toPassLLMRubric(expected: string, gradingConfig: GradingConfig): R;
  8. }
  9. }
  10. }
  11. export function installJestMatchers() {
  12. expect.extend({
  13. async toMatchSemanticSimilarity(
  14. received: string,
  15. expected: string,
  16. threshold: number = 0.8,
  17. ): Promise<jest.CustomMatcherResult> {
  18. const result = await matchers.matchesSimilarity(received, expected, threshold);
  19. const pass = received === expected || result.pass;
  20. if (pass) {
  21. return {
  22. message: () => `expected ${received} not to match semantic similarity with ${expected}`,
  23. pass: true,
  24. };
  25. } else {
  26. return {
  27. message: () =>
  28. `expected ${received} to match semantic similarity with ${expected}, but it did not. Reason: ${result.reason}`,
  29. pass: false,
  30. };
  31. }
  32. },
  33. async toPassLLMRubric(
  34. received: string,
  35. expected: string,
  36. gradingConfig: GradingConfig,
  37. ): Promise<jest.CustomMatcherResult> {
  38. const gradingResult = await matchers.matchesLlmRubric(expected, received, gradingConfig);
  39. if (gradingResult.pass) {
  40. return {
  41. message: () => `expected ${received} not to pass LLM Rubric with ${expected}`,
  42. pass: true,
  43. };
  44. } else {
  45. return {
  46. message: () =>
  47. `expected ${received} to pass LLM Rubric with ${expected}, but it did not. Reason: ${gradingResult.reason}`,
  48. pass: false,
  49. };
  50. }
  51. },
  52. });
  53. }
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...