File size: 1,875 Bytes
583f6dd
5c271a3
583f6dd
 
38cf703
583f6dd
3002e1b
583f6dd
5c271a3
 
 
583f6dd
38cf703
 
583f6dd
 
3002e1b
a6a0614
3002e1b
583f6dd
 
 
3002e1b
583f6dd
 
 
5c271a3
11bd168
9f72bcf
3002e1b
 
 
5c271a3
3002e1b
 
 
 
5c271a3
583f6dd
5c271a3
583f6dd
8039e4b
5c271a3
 
3002e1b
5c271a3
8039e4b
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
import ast
import logging
from fastapi import APIRouter
from api.stored_data import stored_data
from src.genai.ideation_agent.agent import IdeationAgent
from langgraph.errors import GraphRecursionError
from api.schemas.ideation import IdeationMessage

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

router = APIRouter()
agent = IdeationAgent()
idea_graph = agent.ideation_graph()

@router.post("/ideation")
def ideation_endpoint(msg:IdeationMessage):
    config={"recursion_limit":25, "configurable": {"thread_id": "ideation_thread123"},"run_name":"ideation"}
    scores=msg.scores.model_dump() 
    try:
        result = idea_graph.invoke(
        {
            'scores': [scores],
            'business_details': [stored_data['business_details']],
            'image_caption': [stored_data['image_caption'] if 'image_caption' in stored_data else ""]
        },
        config=config)
        stored_data['final_ideation'] = result['unique_selected_ideas'][-1]
        # stored_data['final_ideation']=ast.literal_eval(stored_data['final_ideation'])
        top_ideas = [{k: v for k, v in idea.items() if k != "scores"} for idea in result['unique_selected_ideas'][-1]]
        more_ideas = [idea for idea in result['normalizer_response'][-1]['ideas'] if idea not in top_ideas]

        app_logger.info('Executed the ideation pipeline.')
        return {'response':
                    {'top_ideas': top_ideas,
                     'more_ideas': more_ideas}
                }
    
    except GraphRecursionError:
        error_logger.error('Ideation loop ran more than specified.')
        result = idea_graph.get_state({"configurable": {"thread_id": "ideation_thread123"}})
        return {'response': result[0]}
    
    except Exception as e:
        error_logger.error(f'Unable to execute the ideation: {e}')
        raise