fix getting giga token

This commit is contained in:
Дмитриев Максим Сергеевич
2025-06-14 18:26:13 +03:00
parent 45cafbee91
commit f37f34d803
4 changed files with 20 additions and 13 deletions

View File

@@ -67,7 +67,8 @@ const getRagSupabaseUrl = async () => {
module.exports = { module.exports = {
getSupabaseUrl, getSupabaseUrl,
getSupabaseKey, getSupabaseKey,
getSupabaseServiceKey getSupabaseServiceKey,
getGigaAuth
}; };
// IIFE для установки переменных окружения // IIFE для установки переменных окружения

View File

@@ -1,18 +1,20 @@
import { Agent } from 'node:https'; import { Agent } from 'node:https';
import { GigaChat } from 'langchain-gigachat'; import { GigaChat } from 'langchain-gigachat';
import { getGigaAuth } from '../get-constants';
const httpsAgent = new Agent({ const httpsAgent = new Agent({
rejectUnauthorized: false, rejectUnauthorized: false,
}); });
// Получаем GIGA_AUTH из переменной окружения (устанавливается в get-constants.js) // Получаем GIGA_AUTH из переменной окружения (устанавливается в get-constants.js)
export const gigachat = new GigaChat({ export const gigachat = (GIGA_AUTH) =>
new GigaChat({
model: 'GigaChat-2', model: 'GigaChat-2',
temperature: 0.7, temperature: 0.7,
scope: 'GIGACHAT_API_PERS', scope: 'GIGACHAT_API_PERS',
streaming: false, streaming: false,
credentials: process.env.GIGA_AUTH, credentials: GIGA_AUTH,
httpsAgent httpsAgent
}); });
export default gigachat; export default gigachat;

View File

@@ -10,6 +10,7 @@ import { CreateTicketTool } from './create-ticket-tool';
export interface SupportAgentConfig { export interface SupportAgentConfig {
temperature?: number; temperature?: number;
threadId?: string; threadId?: string;
GIGA_AUTH?: string;
} }
export interface SupportResponse { export interface SupportResponse {
@@ -34,7 +35,7 @@ export class SupportAgent {
this.memorySaver = new MemorySaver(); this.memorySaver = new MemorySaver();
this.isFirstMessage = true; this.isFirstMessage = true;
this.llm = gigachat; this.llm = gigachat(config.GIGA_AUTH);
if (config.temperature !== undefined) { if (config.temperature !== undefined) {
this.llm.temperature = config.temperature; this.llm.temperature = config.temperature;
} }

View File

@@ -1,5 +1,6 @@
const router = require('express').Router(); const router = require('express').Router();
const { getSupabaseClient } = require('./supabaseClient'); const { getSupabaseClient } = require('./supabaseClient');
const { getGigaAuth } = require('./get-constants');
const { SupportAgent } = require('./support-ai-agent/support-agent'); const { SupportAgent } = require('./support-ai-agent/support-agent');
// Хранилище агентов для разных пользователей // Хранилище агентов для разных пользователей
@@ -8,11 +9,13 @@ const userAgents = new Map();
/** /**
* Получить или создать агента для пользователя * Получить или создать агента для пользователя
*/ */
function getUserAgent(userId) { async function getUserAgent(userId) {
if (!userAgents.has(userId)) { if (!userAgents.has(userId)) {
const GIGA_AUTH = await getGigaAuth();
const config = { const config = {
threadId: userId, threadId: userId,
temperature: 0.7 temperature: 0.7,
GIGA_AUTH
}; };
userAgents.set(userId, new SupportAgent(config)); userAgents.set(userId, new SupportAgent(config));
} }
@@ -74,7 +77,7 @@ router.post('/support', async (req, res) => {
} }
// Получаем агента для пользователя // Получаем агента для пользователя
const agent = getUserAgent(user_id); const agent = await getUserAgent(user_id);
// Получаем ответ от AI-агента, передавая apartment_id // Получаем ответ от AI-агента, передавая apartment_id
const aiResponse = await agent.processMessage(message, apartment_id); const aiResponse = await agent.processMessage(message, apartment_id);