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

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

Comments

Loading...