File size: 2,579 Bytes
583f6dd
8039e4b
5c271a3
ef9fa4b
8039e4b
583f6dd
 
ef9fa4b
583f6dd
5c271a3
 
 
ef9fa4b
583f6dd
 
 
 
8039e4b
5c271a3
 
 
 
 
 
 
 
 
 
 
9f72bcf
5c271a3
9f72bcf
5c271a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8039e4b
f054586
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
import ast
import json
import logging
from fastapi import APIRouter
from fastapi.responses import StreamingResponse
from api.stored_data import stored_data
from src.genai.context_analysis_agent.agent import IntroductionChatbot
from api.schemas.context_analysis import UserMessage

app_logger = logging.getLogger("app_logger")
error_logger = logging.getLogger("error_logger")

router = APIRouter()
context_analysis_graph = IntroductionChatbot()   

@router.post("/context-analysis")
def context_analysis(msg: UserMessage):
    def event_generator():
        try:
            accumulated_response = ""
            for chunk in context_analysis_graph.chat(msg.message):
                accumulated_response += chunk
                payload = {
                    "streamed_response": chunk,
                }
                yield json.dumps(payload) + "\n"  
            app_logger.info('Executed context analysis agent')

            last_response = context_analysis_graph.messages[-1]["content"]
            print('Last-Response:', last_response)

            if context_analysis_graph.is_complete(last_response) == 'complete':
                app_logger.info('Context analysis completed.')
                try:
                    details = context_analysis_graph.extract_details()
                    app_logger.info('Details extracted after context analysis.')
                except Exception as e:
                    error_logger.error(f'Unable to extract details: {e}')

                if type(details) != dict:details = details.model_dump()
                if isinstance(details, str):details = ast.literal_eval(details)
                    
                stored_data["business_details"] = details
                try:
                    context_analysis_graph.reset()
                    app_logger.info('Context analysis memory reset after details extraction.')
                except Exception as e:
                    error_logger.error('Unable to reset memory:', e)

                final_payload = {
                    "response": accumulated_response,
                    "complete": True,
                    "business_details": details
                }
            else:
                final_payload = {
                    "response": accumulated_response,
                    "complete": False
                }

            yield json.dumps(final_payload) + "\n"
        except Exception as e:
            error_logger.error('Unable to run context analysis agent:', e)

    return StreamingResponse(event_generator(), media_type="text/event-stream")