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

prompt.js 3.0 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  1. /**
  2. * Image Analysis Example
  3. *
  4. * This example demonstrates how to format prompts for different AI providers
  5. * when performing image analysis tasks.
  6. *
  7. * Note: For simplicity and to avoid dependencies, this example uses the built-in
  8. * 'https' module. In a production environment, use more robust libraries like
  9. * axios for better error handling and features.
  10. */
  11. const https = require('https');
  12. // The system prompt that instructs the AI model
  13. const systemPrompt = 'Describe the image in a few words';
  14. /**
  15. * Fetches an image from a URL and converts it to a base64 string.
  16. * This is required for many providers like Anthropic and llava (via ollama, llama.cpp, etc.)
  17. *
  18. * @param {string} imageUrl - The URL of the image to fetch
  19. * @returns {Promise<string>} A promise that resolves with the base64-encoded image data
  20. */
  21. function getImageBase64(imageUrl) {
  22. return new Promise((resolve, reject) => {
  23. https
  24. .get(imageUrl, (response) => {
  25. const data = [];
  26. response.on('data', (chunk) => {
  27. data.push(chunk);
  28. });
  29. response.on('end', () => {
  30. const buffer = Buffer.concat(data);
  31. resolve(buffer.toString('base64'));
  32. });
  33. })
  34. .on('error', (err) => {
  35. reject(err);
  36. });
  37. });
  38. }
  39. /**
  40. * Formats the prompt based on the AI provider.
  41. *
  42. * @param {Object} context - The context object containing provider information and variables
  43. * @param {Object} context.provider - Information about the AI provider
  44. * @param {string} context.provider.id - The ID of the AI provider
  45. * @param {string} context.provider.label - The label of the AI provider
  46. * @param {Object} context.vars - Variables passed to the function
  47. * @param {string} context.vars.image_url - The URL of the image to analyze
  48. * @returns {Promise<Array>} A promise that resolves with the formatted prompt
  49. * @throws {Error} If an unsupported provider is specified
  50. */
  51. async function formatImagePrompt(context) {
  52. // The ID always exists
  53. if (
  54. context.provider.id.startsWith('bedrock:anthropic') ||
  55. context.provider.id === 'anthropic:claude-3-5-sonnet-20241022'
  56. ) {
  57. return [
  58. { role: 'system', content: systemPrompt },
  59. {
  60. role: 'user',
  61. content: [
  62. {
  63. type: 'image',
  64. source: {
  65. type: 'base64',
  66. media_type: 'image/jpeg',
  67. data: await getImageBase64(context.vars.image_url),
  68. },
  69. },
  70. ],
  71. },
  72. ];
  73. }
  74. // We can use the label if provided in the config.
  75. if (context.provider.label === 'custom label for gpt-4.1') {
  76. return [
  77. {
  78. role: 'system',
  79. content: [{ type: 'text', text: systemPrompt }],
  80. },
  81. {
  82. role: 'user',
  83. content: [
  84. {
  85. type: 'image_url',
  86. image_url: {
  87. url: context.vars.image_url,
  88. },
  89. },
  90. ],
  91. },
  92. ];
  93. }
  94. throw new Error(`Unsupported provider: ${JSON.stringify(context.provider)}`);
  95. }
  96. module.exports = { formatImagePrompt };
Tip!

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

Comments

Loading...