Spaces:
Sleeping
Sleeping
File size: 3,276 Bytes
3002e1b 3f5fe23 3002e1b 3f5fe23 acfddab 3002e1b acfddab 3f5fe23 3002e1b 872d043 acfddab 0b2c9fd acfddab 0b2c9fd 3002e1b 0b2c9fd 3002e1b 3f5fe23 3002e1b acfddab ae75807 acfddab ae75807 da7bd22 ae75807 93d50e5 ae75807 acfddab | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | import ast
import os
import json
import hashlib
import base64
import logging
from fastapi import APIRouter
from typing import Optional
from redis import Redis
from fastapi.responses import StreamingResponse
from api.stored_data import stored_data
from src.genai.analytics_chatbot.agent import ChatbotAgent
from src.genai.analytics_chatbot.utils.utils import process_query
from src.genai.analytics_chatbot.handlers.analytics_description import generate_analytics_description
from api.schemas.analytics_chatbot import AnalyticsChatRequest
from config.redis_config import redis_client
from openai import OpenAI
app_logger = logging.getLogger("app_logger")
error_logger = logging.getLogger("error_logger")
router = APIRouter()
agent=ChatbotAgent()
graph = agent.chatbot_graph()
# @router.post("/analytics-chatbot")
# def get_analytics(request: AnalyticsChatRequest):
# user_query = process_query(request.msg)
# print('Processed user query:', user_query)
# cache_key = f"analytics:{hashlib.md5(user_query.encode()).hexdigest()}"
# cached_response = redis_client.get(cache_key)
# print('cached-response:', cached_response)
# if cached_response:
# response_to_cache = json.loads(cached_response)
# else:
# response_to_cache = {}
# if not response_to_cache.get('response') or not response_to_cache.get('endpoint'):
# config = {"configurable": {"thread_id": "analytics-chatbot-thread"},
# "run_name": "analytics-chatbot"}
# result = graph.invoke({'messages': user_query}, config=config)
# if result.get('backup_data') is not None:
# response_to_cache['backup_response'] = result['backup_data']
# else:
# response_to_cache['response'] = result['response']
# response_to_cache['endpoint'] = result['endpoint']
# if request.image_base64 and not response_to_cache.get('description'):
# description = generate_analytics_description(user_query, request.image_base64)
# if description is not None:
# response_to_cache['description'] = description
# redis_client.set(cache_key, json.dumps(response_to_cache), ex=3000)
# return response_to_cache
@router.post("/analytics-chatbot")
def get_analytics(request: AnalyticsChatRequest):
user_query = process_query(request.msg)
print("Processed user query:", user_query)
response_to_return = {}
# Always invoke the graph (no caching)
config = {
"configurable": {"thread_id": "analytics-chatbot-thread"},
"run_name": "analytics-chatbot"
}
result = graph.invoke({'messages': user_query}, config=config)
# Handle primary vs backup response
if result.get('backup_data') is not None:
response_to_return['backup_response'] = result['backup_data']
else:
response_to_return['response'] = result.get('response')
# response_to_return['endpoint'] = result.get('endpoint')
# # Handle image description if image is provided
# if request.image_base64:
# description = generate_analytics_description(user_query, request.image_base64)
# if description is not None:
# response_to_return['description'] = description
return response_to_return
|