multy-stub/server/routers/dry-wash/image.js
ilnaz 6794b01ac8
Some checks failed
ms-devops/pipeline/head There was a failure building this commit
feat: add fetch image
2025-02-23 12:32:53 +03:00

92 lines
3.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const API_URL = "https://gigachat.devices.sberbank.ru/api/v1"
const router = require('express').Router()
const { v4: uuidv4 } = require("uuid");
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"
const token = 'MTQwMmNmZjgtZjA5OC00OGMxLWI0OTUtNWU3ZTU4YzMzZjdjOmU5OGFiYmNiLThmMDItNGVmOC1hNjhhLTA4Y2QxYjVmOGRmMA=='
const getToken = async (req, res) => {
const rqUID = uuidv4()
const body = new URLSearchParams({
scope: "GIGACHAT_API_PERS",
})
const response = await fetch("https://ngw.devices.sberbank.ru:9443/api/v2/oauth", {
method: "POST",
headers: {
Authorization: `Basic ${token}`,
"Content-Type": "application/x-www-form-urlencoded",
Accept: "application/json",
RqUID: rqUID,
},
body,
})
if (!response.ok) {
const errorData = await response.json();
console.error(" Ошибка запроса:", errorData);
return res.status(response.status).json(errorData);
}
return await response.json()
}
async function analyzeImage(fileId,token) {
const response = await fetch(`${API_URL}/chat/completions`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${token}`,
},
body: JSON.stringify({
model: "GigaChat-Max",
stream: false,
update_interval: 0,
messages: [
{
role: "system",
content:
"Ты эксперт по оценке чистоты автомобилей. Твоя задача — анализировать фотографии машин и определять степень их чистоты по 10-балльной шкале, где 1 — очень грязная, 10 — полностью чистая. Отвечай только числом от 1 до 10, без пояснений и дополнительных слов.",
},
{
role: "user",
content: "Что с чистотой машины? Отвечай на основе приложенного документа",
attachments: [fileId],
},
],
}),
});
const data = await response.json()
console.log(" Результат анализа:", data)
return data
}
router.post("/upload", async (req, res) => {
const {access_token} = await getToken(req, res)
const response = await fetch(`${API_URL}/files`, {
method: "POST",
headers: {
Authorization: `Bearer ${access_token}`,
contentType: "multipart/form-data",
},
body: req.body,
})
const data = await response.json()
const analysisResponse = await analyzeImage(data.id,access_token)
res.json({ fileId: data.id, analysis: analysisResponse })
})
module.exports = router