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

utils.ts 1.4 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
  1. import type { ApiProvider, ProviderResponse } from '../../src/types';
  2. export class TestGrader implements ApiProvider {
  3. async callApi(): Promise<ProviderResponse> {
  4. return {
  5. output: JSON.stringify({ pass: true, reason: 'Test grading output' }),
  6. tokenUsage: { total: 10, prompt: 5, completion: 5 },
  7. };
  8. }
  9. id(): string {
  10. return 'TestGradingProvider';
  11. }
  12. }
  13. export function createMockResponse(
  14. options: {
  15. ok?: boolean;
  16. body?: any;
  17. statusText?: string;
  18. status?: number;
  19. headers?: Headers;
  20. text?: () => Promise<string>;
  21. json?: () => Promise<any>;
  22. } = { ok: true },
  23. ): Response {
  24. const isOk = options.ok ?? (options.status ? options.status < 400 : true);
  25. const mockResponse: Response = {
  26. ok: isOk,
  27. status: options.status || (isOk ? 200 : 400),
  28. statusText: options.statusText || (isOk ? 'OK' : 'Bad Request'),
  29. headers: options.headers || new Headers(),
  30. redirected: false,
  31. type: 'basic',
  32. url: 'https://example.com',
  33. json: options.json || (() => Promise.resolve(options.body || {})),
  34. text: options.text || (() => Promise.resolve('')),
  35. blob: () => Promise.resolve(new Blob()),
  36. arrayBuffer: () => Promise.resolve(new ArrayBuffer(0)),
  37. formData: () => Promise.resolve(new FormData()),
  38. bodyUsed: false,
  39. body: null,
  40. clone() {
  41. return createMockResponse(options);
  42. },
  43. } as Response;
  44. return mockResponse;
  45. }
Tip!

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

Comments

Loading...