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

perplexity.test.ts 10 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
  1. import { OpenAiChatCompletionProvider } from '../../src/providers/openai/chat';
  2. import {
  3. PerplexityProvider,
  4. calculatePerplexityCost,
  5. createPerplexityProvider,
  6. } from '../../src/providers/perplexity';
  7. jest.mock('../../src/providers/openai/chat');
  8. describe('Perplexity Provider', () => {
  9. beforeEach(() => {
  10. jest.resetAllMocks();
  11. });
  12. describe('createPerplexityProvider', () => {
  13. it('should create a provider with default settings', () => {
  14. const provider = createPerplexityProvider('perplexity:sonar');
  15. expect(provider).toBeInstanceOf(PerplexityProvider);
  16. });
  17. it('should use sonar as the default model if none is specified', () => {
  18. const provider = createPerplexityProvider('perplexity:');
  19. expect(provider).toBeInstanceOf(PerplexityProvider);
  20. // @ts-ignore - accessing private property for testing
  21. expect(provider.modelName).toBe('sonar');
  22. });
  23. it('should handle specific Perplexity models', () => {
  24. const models = [
  25. 'sonar-pro',
  26. 'sonar-reasoning',
  27. 'sonar-reasoning-pro',
  28. 'sonar-deep-research',
  29. 'r1-1776',
  30. ];
  31. for (const model of models) {
  32. const provider = createPerplexityProvider(`perplexity:${model}`);
  33. expect(provider).toBeInstanceOf(PerplexityProvider);
  34. // @ts-ignore - accessing private property for testing
  35. expect(provider.modelName).toBe(model);
  36. }
  37. });
  38. it('should pass through configuration options', () => {
  39. const config = {
  40. temperature: 0.7,
  41. max_tokens: 1000,
  42. search_domain_filter: ['example.com'],
  43. search_recency_filter: 'week',
  44. return_related_questions: true,
  45. };
  46. const provider = createPerplexityProvider('perplexity:sonar', {
  47. config: { config },
  48. });
  49. expect(provider).toBeInstanceOf(PerplexityProvider);
  50. // Verify config was passed through to constructor
  51. // @ts-ignore - accessing private property for testing
  52. expect(provider.config).toMatchObject(expect.objectContaining(config));
  53. });
  54. });
  55. describe('PerplexityProvider', () => {
  56. it('should initialize with the correct API base URL and key environment variable', () => {
  57. const provider = new PerplexityProvider('sonar');
  58. // @ts-ignore - accessing private properties for testing
  59. expect(provider.config.apiBaseUrl).toBe('https://api.perplexity.ai');
  60. // @ts-ignore - accessing private properties for testing
  61. expect(provider.config.apiKeyEnvar).toBe('PERPLEXITY_API_KEY');
  62. });
  63. it('should set the correct usage tier', () => {
  64. const tiers = ['high', 'medium', 'low'] as const;
  65. for (const tier of tiers) {
  66. const provider = new PerplexityProvider('sonar', {
  67. config: {
  68. usage_tier: tier,
  69. },
  70. });
  71. // @ts-ignore - accessing private property for testing
  72. expect(provider.usageTier).toBe(tier);
  73. }
  74. });
  75. it('should default to "medium" usage tier if not specified', () => {
  76. const provider = new PerplexityProvider('sonar');
  77. // @ts-ignore - accessing private property for testing
  78. expect(provider.usageTier).toBe('medium');
  79. });
  80. it('should have the correct id() method', () => {
  81. const provider = new PerplexityProvider('sonar-pro');
  82. expect(provider.id()).toBe('sonar-pro');
  83. });
  84. it('should have the correct toString() method', () => {
  85. const provider = new PerplexityProvider('sonar');
  86. expect(provider.toString()).toBe('[Perplexity Provider sonar]');
  87. });
  88. it('should have the correct toJSON() method', () => {
  89. const provider = new PerplexityProvider('sonar-pro', {
  90. config: {
  91. temperature: 0.7,
  92. max_tokens: 1000,
  93. },
  94. });
  95. expect(provider.toJSON()).toEqual({
  96. provider: 'perplexity',
  97. model: 'sonar-pro',
  98. config: expect.objectContaining({
  99. temperature: 0.7,
  100. max_tokens: 1000,
  101. apiKey: undefined,
  102. }),
  103. });
  104. });
  105. it('should override callApi to calculate costs correctly', async () => {
  106. // Mock the parent class callApi method
  107. jest.spyOn(OpenAiChatCompletionProvider.prototype, 'callApi').mockResolvedValueOnce({
  108. output: 'Test output',
  109. tokenUsage: {
  110. total: 20,
  111. prompt: 10,
  112. completion: 10,
  113. },
  114. });
  115. const provider = new PerplexityProvider('sonar-pro');
  116. const result = await provider.callApi('Test prompt');
  117. // Verify the response has our custom cost calculation
  118. expect(result.cost).toBeDefined();
  119. expect(result.cost).toBe(0.00018); // (10/1M * $3) + (10/1M * $15) = $0.00018
  120. });
  121. it('should handle cached responses correctly', async () => {
  122. // Mock the parent class callApi method with a cached response
  123. jest.spyOn(OpenAiChatCompletionProvider.prototype, 'callApi').mockResolvedValueOnce({
  124. output: 'Cached output',
  125. tokenUsage: {
  126. total: 20,
  127. cached: 20,
  128. },
  129. cached: true,
  130. });
  131. const provider = new PerplexityProvider('sonar');
  132. const result = await provider.callApi('Test prompt');
  133. // Verify cached response is returned unchanged
  134. expect(result.cached).toBe(true);
  135. expect(result.tokenUsage?.cached).toBe(20);
  136. // Cost should not be calculated for cached responses
  137. expect(result.cost).toBeUndefined();
  138. });
  139. it('should pass through error responses', async () => {
  140. // Mock the parent class callApi method with an error
  141. jest.spyOn(OpenAiChatCompletionProvider.prototype, 'callApi').mockResolvedValueOnce({
  142. error: 'API error',
  143. });
  144. const provider = new PerplexityProvider('sonar');
  145. const result = await provider.callApi('Test prompt');
  146. // Verify error is passed through
  147. expect(result.error).toBe('API error');
  148. expect(result.cost).toBeUndefined();
  149. });
  150. });
  151. describe('calculatePerplexityCost', () => {
  152. it('should return 0 if no tokens are provided', () => {
  153. expect(calculatePerplexityCost('sonar')).toBe(0);
  154. expect(calculatePerplexityCost('sonar', 0, 0)).toBe(0);
  155. expect(calculatePerplexityCost('sonar', undefined, undefined)).toBe(0);
  156. });
  157. it('should calculate costs for sonar model', () => {
  158. // sonar: $1 per million input tokens, $1 per million output tokens
  159. expect(calculatePerplexityCost('sonar', 1000000, 1000000)).toBe(2);
  160. expect(calculatePerplexityCost('sonar', 500000, 500000)).toBe(1);
  161. expect(calculatePerplexityCost('sonar', 2000000, 0)).toBe(2);
  162. expect(calculatePerplexityCost('sonar', 0, 3000000)).toBe(3);
  163. });
  164. it('should calculate costs for sonar-pro model', () => {
  165. // sonar-pro: $3 per million input tokens, $15 per million output tokens
  166. expect(calculatePerplexityCost('sonar-pro', 1000000, 1000000)).toBe(18);
  167. expect(calculatePerplexityCost('sonar-pro', 500000, 500000)).toBe(9);
  168. expect(calculatePerplexityCost('sonar-pro', 2000000, 0)).toBe(6);
  169. expect(calculatePerplexityCost('sonar-pro', 0, 2000000)).toBe(30);
  170. });
  171. it('should calculate costs for sonar-reasoning model', () => {
  172. // sonar-reasoning: $1 per million input tokens, $5 per million output tokens
  173. expect(calculatePerplexityCost('sonar-reasoning', 1000000, 1000000)).toBe(6);
  174. expect(calculatePerplexityCost('sonar-reasoning', 500000, 500000)).toBe(3);
  175. expect(calculatePerplexityCost('sonar-reasoning', 2000000, 0)).toBe(2);
  176. expect(calculatePerplexityCost('sonar-reasoning', 0, 2000000)).toBe(10);
  177. });
  178. it('should calculate costs for sonar-reasoning-pro model', () => {
  179. // sonar-reasoning-pro: $2 per million input tokens, $8 per million output tokens
  180. expect(calculatePerplexityCost('sonar-reasoning-pro', 1000000, 1000000)).toBe(10);
  181. expect(calculatePerplexityCost('sonar-reasoning-pro', 500000, 500000)).toBe(5);
  182. expect(calculatePerplexityCost('sonar-reasoning-pro', 2000000, 0)).toBe(4);
  183. expect(calculatePerplexityCost('sonar-reasoning-pro', 0, 2000000)).toBe(16);
  184. });
  185. it('should calculate costs for sonar-deep-research model', () => {
  186. // sonar-deep-research: $2 per million input tokens, $8 per million output tokens
  187. expect(calculatePerplexityCost('sonar-deep-research', 1000000, 1000000)).toBe(10);
  188. expect(calculatePerplexityCost('sonar-deep-research', 500000, 500000)).toBe(5);
  189. expect(calculatePerplexityCost('sonar-deep-research', 2000000, 0)).toBe(4);
  190. expect(calculatePerplexityCost('sonar-deep-research', 0, 2000000)).toBe(16);
  191. });
  192. it('should calculate costs for r1-1776 model', () => {
  193. // r1-1776: $2 per million input tokens, $8 per million output tokens
  194. expect(calculatePerplexityCost('r1-1776', 1000000, 1000000)).toBe(10);
  195. expect(calculatePerplexityCost('r1-1776', 500000, 500000)).toBe(5);
  196. expect(calculatePerplexityCost('r1-1776', 2000000, 0)).toBe(4);
  197. expect(calculatePerplexityCost('r1-1776', 0, 2000000)).toBe(16);
  198. });
  199. it('should handle unknown models by defaulting to sonar pricing', () => {
  200. expect(calculatePerplexityCost('unknown-model', 1000000, 1000000)).toBe(2);
  201. expect(calculatePerplexityCost('custom-model', 500000, 500000)).toBe(1);
  202. });
  203. it('should handle case insensitivity in model names', () => {
  204. expect(calculatePerplexityCost('SONAR-PRO', 1000000, 1000000)).toBe(18);
  205. expect(calculatePerplexityCost('Sonar-Reasoning', 1000000, 1000000)).toBe(6);
  206. expect(calculatePerplexityCost('sonar-DEEP-research', 1000000, 1000000)).toBe(10);
  207. });
  208. it('should handle different usage tiers', () => {
  209. // Test one model with different tiers (the tier doesn't affect the token price calculation)
  210. const model = 'sonar-pro';
  211. const inputTokens = 1000000;
  212. const outputTokens = 1000000;
  213. // All tiers should calculate the same token costs
  214. expect(calculatePerplexityCost(model, inputTokens, outputTokens, 'high')).toBe(18);
  215. expect(calculatePerplexityCost(model, inputTokens, outputTokens, 'medium')).toBe(18);
  216. expect(calculatePerplexityCost(model, inputTokens, outputTokens, 'low')).toBe(18);
  217. });
  218. });
  219. });
Tip!

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

Comments

Loading...