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

customProvider.ts 1.8 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
  1. import promptfoo from 'promptfoo';
  2. import type { ApiProvider, ProviderOptions, ProviderResponse } from 'promptfoo';
  3. // import promptfoo from '../../dist/src/index.js';
  4. // import type { ApiProvider, ProviderOptions, ProviderResponse } from '../../src/types/providers';
  5. export default class CustomApiProvider implements ApiProvider {
  6. protected providerId: string;
  7. public config: any;
  8. constructor(options: ProviderOptions) {
  9. // The caller may override Provider ID (e.g. when using multiple instances of the same provider)
  10. this.providerId = options.id || 'custom provider';
  11. // The config object contains any options passed to the provider in the config file.
  12. this.config = options.config;
  13. }
  14. id(): string {
  15. return this.providerId;
  16. }
  17. async callApi(prompt: string): Promise<ProviderResponse> {
  18. const body = {
  19. model: 'gpt-4o-mini',
  20. messages: [
  21. {
  22. role: 'user',
  23. content: prompt,
  24. },
  25. ],
  26. max_tokens: Number.parseInt(this.config?.max_tokens, 10) || 1024,
  27. temperature: Number.parseFloat(this.config?.temperature) || 0,
  28. };
  29. // Fetch the data from the API using promptfoo's cache. You can use your own fetch implementation if preferred.
  30. const { data, cached } = await promptfoo.cache.fetchWithCache(
  31. 'https://api.openai.com/v1/chat/completions',
  32. {
  33. method: 'POST',
  34. headers: {
  35. 'Content-Type': 'application/json',
  36. Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
  37. },
  38. body: JSON.stringify(body),
  39. },
  40. 10_000 /* 10 second timeout */,
  41. );
  42. const ret: ProviderResponse = {
  43. output: data.choices[0].message.content,
  44. tokenUsage: {
  45. total: data.usage.total_tokens,
  46. prompt: data.usage.prompt_tokens,
  47. completion: data.usage.completion_tokens,
  48. },
  49. };
  50. return ret;
  51. }
  52. }
Tip!

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

Comments

Loading...