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

providers.llama.test.ts 3.9 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
  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.mocked(fetchWithCache).mockResolvedValue({ ...response, cached: false });
  44. const provider = new LlamaProvider(modelName, { config });
  45. await provider.callApi(prompt);
  46. expect(fetchWithCache).toHaveBeenCalledWith(
  47. `${process.env.LLAMA_BASE_URL || 'http://localhost:8080'}/completion`,
  48. {
  49. method: 'POST',
  50. headers: {
  51. 'Content-Type': 'application/json',
  52. },
  53. body: JSON.stringify({
  54. prompt,
  55. n_predict: 512,
  56. temperature: config.temperature,
  57. top_k: undefined,
  58. top_p: undefined,
  59. n_keep: undefined,
  60. stop: undefined,
  61. repeat_penalty: undefined,
  62. repeat_last_n: undefined,
  63. penalize_nl: undefined,
  64. presence_penalty: undefined,
  65. frequency_penalty: undefined,
  66. mirostat: undefined,
  67. mirostat_tau: undefined,
  68. mirostat_eta: undefined,
  69. seed: undefined,
  70. ignore_eos: undefined,
  71. logit_bias: undefined,
  72. }),
  73. },
  74. REQUEST_TIMEOUT_MS,
  75. );
  76. });
  77. it('should return the correct response on success', async () => {
  78. jest
  79. .mocked(fetchWithCache)
  80. .mockResolvedValue({ data: { content: 'test response' }, cached: false });
  81. const provider = new LlamaProvider(modelName, { config });
  82. const result = await provider.callApi(prompt);
  83. expect(result).toEqual({ output: response.data.content });
  84. });
  85. it('should return an error if fetchWithCache throws an error', async () => {
  86. const error = new Error('API call error');
  87. jest.mocked(fetchWithCache).mockRejectedValue(error);
  88. const provider = new LlamaProvider(modelName, { config });
  89. const result = await provider.callApi(prompt);
  90. expect(result).toEqual({ error: `API call error: ${String(error)}` });
  91. });
  92. it('should return an error if response data is malformed', async () => {
  93. const malformedResponse = { data: null, cached: false };
  94. jest.mocked(fetchWithCache).mockResolvedValue(malformedResponse);
  95. const provider = new LlamaProvider(modelName, { config });
  96. const result = await provider.callApi(prompt);
  97. expect(result).toEqual({
  98. error: `API response error: TypeError: Cannot read properties of null (reading 'content'): null`,
  99. });
  100. });
  101. });
  102. });
Tip!

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

Comments

Loading...