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 846 B

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
  1. const fetch = require('node-fetch');
  2. class CustomApiProvider {
  3. id() {
  4. return 'my-custom-api';
  5. }
  6. async callApi(prompt) {
  7. const body = {
  8. model: 'text-davinci-002',
  9. prompt,
  10. max_tokens: 1024,
  11. temperature: 0,
  12. };
  13. const response = await fetch('https://api.openai.com/v1/completions', {
  14. method: 'POST',
  15. headers: {
  16. 'Content-Type': 'application/json',
  17. Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
  18. },
  19. body: JSON.stringify(body),
  20. });
  21. const data = await response.json();
  22. const ret = {
  23. output: data.choices[0].text,
  24. tokenUsage: {
  25. total: data.usage.total_tokens,
  26. prompt: data.usage.prompt_tokens,
  27. completion: data.usage.completion_tokens,
  28. },
  29. };
  30. return ret;
  31. }
  32. }
  33. module.exports = CustomApiProvider;
Tip!

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

Comments

Loading...