img2text.js 918 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. const { OpenAI } = require('openai');
  2. const config = require('../config');
  3. const client = new OpenAI({
  4. apiKey: config.API_KEY,
  5. baseURL: config.BASE_URL
  6. });
  7. async function img2text(prompt, imageUrl) {
  8. try {
  9. if (!prompt) {
  10. throw new Error('Missing prompt');
  11. }
  12. if (!imageUrl) {
  13. throw new Error('Missing imageUrl');
  14. }
  15. const response = await client.chat.completions.create({
  16. model: 'gpt-4o',
  17. messages: [
  18. {
  19. role: 'user',
  20. content: [
  21. { type: 'text', text: prompt },
  22. {
  23. type: 'image_url',
  24. image_url: {
  25. url: imageUrl,
  26. detail: 'high'
  27. }
  28. }
  29. ]
  30. }
  31. ],
  32. max_tokens: 300,
  33. stream: false
  34. });
  35. return { success: true, data: response };
  36. } catch (error) {
  37. throw error;
  38. }
  39. }
  40. module.exports = img2text;