img2text.js 1.1 KB

12345678910111213141516171819202122
  1. const path = require('path')
  2. const aiModule = require(path.join(__dirname, '../../../../ai/ai.js'))
  3. /** 入参:prompt, model, imageUrl(参考图地址,如 data URL 或 http URL) */
  4. async function executeImg2text({ prompt, model, imageUrl, folderPath }) {
  5. const p = prompt != null ? String(prompt).trim() : ''
  6. const url = imageUrl != null ? String(imageUrl).trim() : ''
  7. if (!url) return { success: false, error: 'img2text 缺少 imageUrl' }
  8. const m = model != null ? String(model).trim().toLowerCase() : ''
  9. const action = m === 'doubao' ? 'doubao_img2text' : 'img2text'
  10. try {
  11. const result = await aiModule.run(action, p, url)
  12. if (!result.success) return { success: false, error: result.error || 'img2text 失败' }
  13. const data = result.data
  14. const text = data?.choices?.[0]?.message?.content ?? data?.choices?.[0]?.text ?? ''
  15. return { success: true, value: typeof text === 'string' ? text : String(text) }
  16. } catch (e) {
  17. return { success: false, error: (e && (e.message || String(e))) || 'img2text 异常' }
  18. }
  19. }
  20. module.exports = { executeImg2text }