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

llama.test.ts 4.0 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  1. import { fetchWithCache } from '../../src/cache';
  2. import { LlamaProvider } from '../../src/providers/llama';
  3. import { REQUEST_TIMEOUT_MS } from '../../src/providers/shared';
  4. jest.mock('../../src/cache', () => ({
  5. fetchWithCache: jest.fn(),
  6. }));
  7. describe('LlamaProvider', () => {
  8. const modelName = 'testModel';
  9. const config = {
  10. temperature: 0.7,
  11. };
  12. describe('constructor', () => {
  13. it('should initialize with modelName and config', () => {
  14. const provider = new LlamaProvider(modelName, { config });
  15. expect(provider.modelName).toBe(modelName);
  16. expect(provider.config).toEqual(config);
  17. });
  18. it('should initialize with id function if id is provided', () => {
  19. const id = 'testId';
  20. const provider = new LlamaProvider(modelName, { config, id });
  21. expect(provider.id()).toBe(id);
  22. });
  23. });
  24. describe('id', () => {
  25. it('should return the correct id string', () => {
  26. const provider = new LlamaProvider(modelName);
  27. expect(provider.id()).toBe(`llama:${modelName}`);
  28. });
  29. });
  30. describe('toString', () => {
  31. it('should return the correct string representation', () => {
  32. const provider = new LlamaProvider(modelName);
  33. expect(provider.toString()).toBe(`[Llama Provider ${modelName}]`);
  34. });
  35. });
  36. describe('callApi', () => {
  37. const prompt = 'test prompt';
  38. const response = { data: { content: 'test response' } };
  39. beforeEach(() => {
  40. jest.clearAllMocks();
  41. });
  42. it('should call fetchWithCache with correct parameters', async () => {
  43. jest
  44. .mocked(fetchWithCache)
  45. .mockResolvedValue({ ...response, cached: false, status: 200, statusText: 'OK' });
  46. const provider = new LlamaProvider(modelName, { config });
  47. await provider.callApi(prompt);
  48. expect(fetchWithCache).toHaveBeenCalledWith(
  49. `${process.env.LLAMA_BASE_URL || 'http://localhost:8080'}/completion`,
  50. {
  51. method: 'POST',
  52. headers: {
  53. 'Content-Type': 'application/json',
  54. },
  55. body: JSON.stringify({
  56. prompt,
  57. n_predict: 512,
  58. temperature: config.temperature,
  59. top_k: undefined,
  60. top_p: undefined,
  61. n_keep: undefined,
  62. stop: undefined,
  63. repeat_penalty: undefined,
  64. repeat_last_n: undefined,
  65. penalize_nl: undefined,
  66. presence_penalty: undefined,
  67. frequency_penalty: undefined,
  68. mirostat: undefined,
  69. mirostat_tau: undefined,
  70. mirostat_eta: undefined,
  71. seed: undefined,
  72. ignore_eos: undefined,
  73. logit_bias: undefined,
  74. }),
  75. },
  76. REQUEST_TIMEOUT_MS,
  77. );
  78. });
  79. it('should return the correct response on success', async () => {
  80. jest.mocked(fetchWithCache).mockResolvedValue({
  81. data: { content: 'test response' },
  82. cached: false,
  83. status: 200,
  84. statusText: 'OK',
  85. });
  86. const provider = new LlamaProvider(modelName, { config });
  87. const result = await provider.callApi(prompt);
  88. expect(result).toEqual({ output: response.data.content });
  89. });
  90. it('should return an error if fetchWithCache throws an error', async () => {
  91. const error = new Error('API call error');
  92. jest.mocked(fetchWithCache).mockRejectedValue(error);
  93. const provider = new LlamaProvider(modelName, { config });
  94. const result = await provider.callApi(prompt);
  95. expect(result).toEqual({ error: `API call error: ${String(error)}` });
  96. });
  97. it('should return an error if response data is malformed', async () => {
  98. const malformedResponse = { data: null, cached: false, status: 200, statusText: 'OK' };
  99. jest.mocked(fetchWithCache).mockResolvedValue(malformedResponse);
  100. const provider = new LlamaProvider(modelName, { config });
  101. const result = await provider.callApi(prompt);
  102. expect(result).toEqual({
  103. error: `API response error: TypeError: Cannot read properties of null (reading 'content'): null`,
  104. });
  105. });
  106. });
  107. });
Tip!

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

Comments

Loading...