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.2 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
  1. const fetch = require('node-fetch');
  2. class CustomApiProvider {
  3. constructor(options) {
  4. // The caller may override Provider ID (e.g. when using multiple instances of the same provider)
  5. this.providerId = options.id || 'custom provider';
  6. // The config object contains any options passed to the provider in the config file.
  7. this.config = options.config;
  8. }
  9. id() {
  10. return this.providerId;
  11. }
  12. async callApi(prompt) {
  13. return null;
  14. }
  15. async callEmbeddingApi(prompt) {
  16. const body = {
  17. model: 'text-embedding-3-large',
  18. input: prompt,
  19. };
  20. const response = await fetch('https://api.openai.com/v1/embeddings', {
  21. method: 'POST',
  22. headers: {
  23. 'Content-Type': 'application/json',
  24. Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
  25. },
  26. body: JSON.stringify(body),
  27. });
  28. const data = await response.json();
  29. if (!data) {
  30. return {
  31. error: 'Unknown error',
  32. };
  33. }
  34. const ret = {
  35. embedding: data.data[0].embedding,
  36. tokenUsage: {
  37. total: data.usage.total_tokens,
  38. prompt: data.usage.prompt_tokens,
  39. completion: 0,
  40. },
  41. };
  42. return ret;
  43. }
  44. }
  45. module.exports = CustomApiProvider;
Tip!

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

Comments

Loading...