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

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

Comments

Loading...