update server/routers/back-new/features/image/image.controller.js
This commit is contained in:
@@ -1,33 +1,41 @@
|
|||||||
const axios = require('axios');
|
const axios = require('axios');
|
||||||
const makeLinks = require('../../shared/hateoas');
|
const makeLinks = require('../../shared/hateoas');
|
||||||
const path = require('path');
|
|
||||||
const qs = require('qs');
|
|
||||||
const { v4: uuidv4 } = require('uuid');
|
const { v4: uuidv4 } = require('uuid');
|
||||||
require('dotenv').config({ path: path.resolve(__dirname, '../../.env') });
|
|
||||||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
|
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
|
||||||
|
|
||||||
exports.generate = async (req, res) => {
|
// 获取access_token
|
||||||
const { prompt } = req.query;
|
async function fetchAccessToken(apiKey) {
|
||||||
if (!prompt) {
|
|
||||||
return res.status(400).json({ error: 'Prompt parameter is required' });
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
const apiKey = process.env.GIGACHAT_API_KEY;
|
|
||||||
const tokenResp = await axios.post(
|
const tokenResp = await axios.post(
|
||||||
'https://ngw.devices.sberbank.ru:9443/api/v2/oauth',
|
'https://ngw.devices.sberbank.ru:9443/api/v2/oauth',
|
||||||
{
|
{ scope: 'GIGACHAT_API_PERS' },
|
||||||
'scope':' GIGACHAT_API_PERS',
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/x-www-form-urlencoded',
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
'Accept': 'application/json',
|
'Accept': 'application/json',
|
||||||
'Authorization': `Basic ${apiKey}`,
|
'Authorization': `Basic ${apiKey}`,
|
||||||
'RqUID':'6f0b1291-c7f3-43c6-bb2e-9f3efb2dc98e'
|
'RqUID': uuidv4()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
const accessToken = tokenResp.data.access_token;
|
return tokenResp.data.access_token;
|
||||||
|
} catch (err) {
|
||||||
|
console.error('AI生成图片出错: 获取access_token失败');
|
||||||
|
if (err.response) {
|
||||||
|
console.error('status:', err.response.status);
|
||||||
|
console.error('headers:', err.response.headers);
|
||||||
|
console.error('data:', err.response.data);
|
||||||
|
console.error('config:', err.config);
|
||||||
|
} else {
|
||||||
|
console.error('AI生成图片出错:', err.message);
|
||||||
|
}
|
||||||
|
throw new Error('获取access_token失败: ' + err.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 调用chat生成图片描述
|
||||||
|
async function fetchChatContent(accessToken, prompt) {
|
||||||
|
try {
|
||||||
const chatResp = await axios.post(
|
const chatResp = await axios.post(
|
||||||
'https://gigachat.devices.sberbank.ru/api/v1/chat/completions',
|
'https://gigachat.devices.sberbank.ru/api/v1/chat/completions',
|
||||||
{
|
{
|
||||||
@@ -48,11 +56,25 @@ exports.generate = async (req, res) => {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
const content = chatResp.data.choices[0].message.content;
|
const content = chatResp.data.choices[0].message.content;
|
||||||
const match = content.match(/<img src=\"(.*?)\"/);
|
console.log('GigaChat返回内容:', content);
|
||||||
if (!match) {
|
return content;
|
||||||
return res.status(500).json({ error: 'No image generated' });
|
} catch (err) {
|
||||||
|
console.error('AI生成图片出错: chat接口失败');
|
||||||
|
if (err.response) {
|
||||||
|
console.error('status:', err.response.status);
|
||||||
|
console.error('headers:', err.response.headers);
|
||||||
|
console.error('data:', err.response.data);
|
||||||
|
console.error('config:', err.config);
|
||||||
|
} else {
|
||||||
|
console.error('AI生成图片出错:', err.message);
|
||||||
}
|
}
|
||||||
const imageId = match[1];
|
throw new Error('chat接口失败: ' + err.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取图片内容
|
||||||
|
async function fetchImageContent(accessToken, imageId) {
|
||||||
|
try {
|
||||||
const imageResp = await axios.get(
|
const imageResp = await axios.get(
|
||||||
`https://gigachat.devices.sberbank.ru/api/v1/files/${imageId}/content`,
|
`https://gigachat.devices.sberbank.ru/api/v1/files/${imageId}/content`,
|
||||||
{
|
{
|
||||||
@@ -63,12 +85,10 @@ exports.generate = async (req, res) => {
|
|||||||
responseType: 'arraybuffer'
|
responseType: 'arraybuffer'
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
res.set('Content-Type', 'image/jpeg');
|
return imageResp.data;
|
||||||
res.set('X-HATEOAS', JSON.stringify(makeLinks('/gigachat', { self: '/prompt' })));
|
|
||||||
res.send(imageResp.data);
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
console.error('AI生成图片出错: 获取图片内容失败');
|
||||||
if (err.response) {
|
if (err.response) {
|
||||||
console.error('AI生成图片出错:');
|
|
||||||
console.error('status:', err.response.status);
|
console.error('status:', err.response.status);
|
||||||
console.error('headers:', err.response.headers);
|
console.error('headers:', err.response.headers);
|
||||||
console.error('data:', err.response.data);
|
console.error('data:', err.response.data);
|
||||||
@@ -76,6 +96,31 @@ exports.generate = async (req, res) => {
|
|||||||
} else {
|
} else {
|
||||||
console.error('AI生成图片出错:', err.message);
|
console.error('AI生成图片出错:', err.message);
|
||||||
}
|
}
|
||||||
|
throw new Error('获取图片内容失败: ' + err.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.generate = async (req, res) => {
|
||||||
|
const { prompt } = req.query;
|
||||||
|
if (!prompt) {
|
||||||
|
return res.status(400).json({ error: 'Prompt parameter is required' });
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const apiKey = process.env.GIGACHAT_API_KEY;
|
||||||
|
const accessToken = await fetchAccessToken(apiKey);
|
||||||
|
const content = await fetchChatContent(accessToken, prompt);
|
||||||
|
const match = content.match(/<img src=\"(.*?)\"/);
|
||||||
|
if (!match) {
|
||||||
|
console.error('AI生成图片出错: GigaChat未返回图片标签');
|
||||||
|
return res.status(500).json({ error: 'No image generated' });
|
||||||
|
}
|
||||||
|
const imageId = match[1];
|
||||||
|
const imageData = await fetchImageContent(accessToken, imageId);
|
||||||
|
res.set('Content-Type', 'image/jpeg');
|
||||||
|
res.set('X-HATEOAS', JSON.stringify(makeLinks('/gigachat', { self: '/prompt' })));
|
||||||
|
res.send(imageData);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('AI生成图片出错: 未知错误', err);
|
||||||
res.status(500).json({ error: err.message });
|
res.status(500).json({ error: err.message });
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user