subashpoudel commited on
Commit
f054586
·
1 Parent(s): 8839e20

Made changes in ideation

Browse files
api/routers/__pycache__/context_analysis.cpython-312.pyc CHANGED
Binary files a/api/routers/__pycache__/context_analysis.cpython-312.pyc and b/api/routers/__pycache__/context_analysis.cpython-312.pyc differ
 
api/routers/__pycache__/ideation.cpython-312.pyc CHANGED
Binary files a/api/routers/__pycache__/ideation.cpython-312.pyc and b/api/routers/__pycache__/ideation.cpython-312.pyc differ
 
api/routers/context_analysis.py CHANGED
@@ -54,4 +54,4 @@ def context_analysis(msg: UserMessage):
54
 
55
  yield json.dumps(final_payload) + "\n"
56
 
57
- return StreamingResponse(event_generator(), media_type="event-stream")
 
54
 
55
  yield json.dumps(final_payload) + "\n"
56
 
57
+ return StreamingResponse(event_generator(), media_type="text/event-stream")
api/routers/ideation.py CHANGED
@@ -20,10 +20,10 @@ def ideation_endpoint():
20
  },
21
  config=config,
22
  )
23
- stored_data['final_ideation'] = result['improver_response'][-1]
24
  stored_data['final_ideation']=ast.literal_eval(stored_data['final_ideation'])
25
 
26
- return {'response':ast.literal_eval(result['improver_response'][-1])}
27
  except GraphRecursionError:
28
  result = idea_graph.get_state({"configurable": {"thread_id": "ideation_thread123"}})
29
  return {'response': result[0]}
 
20
  },
21
  config=config,
22
  )
23
+ stored_data['final_ideation'] = result['formatted_response'][-1]
24
  stored_data['final_ideation']=ast.literal_eval(stored_data['final_ideation'])
25
 
26
+ return {'response':ast.literal_eval(result['formatted_response'][-1])}
27
  except GraphRecursionError:
28
  result = idea_graph.get_state({"configurable": {"thread_id": "ideation_thread123"}})
29
  return {'response': result[0]}
logs/access.log CHANGED
@@ -189,3 +189,20 @@
189
  2025-08-10 14:33:24,030 | INFO | access_logger | Response status: 200
190
  2025-08-10 15:14:49,865 | INFO | access_logger | Request: POST http://localhost:8000/api/brainstorm
191
  2025-08-10 15:14:49,880 | INFO | access_logger | Response status: 200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
  2025-08-10 14:33:24,030 | INFO | access_logger | Response status: 200
190
  2025-08-10 15:14:49,865 | INFO | access_logger | Request: POST http://localhost:8000/api/brainstorm
191
  2025-08-10 15:14:49,880 | INFO | access_logger | Response status: 200
192
+ 2025-08-11 13:42:55,059 | INFO | access_logger | Request: GET http://127.0.0.1:8000/docs
193
+ 2025-08-11 13:42:55,076 | INFO | access_logger | Response status: 200
194
+ 2025-08-11 13:42:55,453 | INFO | access_logger | Request: GET http://127.0.0.1:8000/openapi.json
195
+ 2025-08-11 13:42:55,505 | INFO | access_logger | Response status: 200
196
+ 2025-08-11 13:46:16,491 | INFO | access_logger | Request: POST http://127.0.0.1:8000/api/ideation
197
+ 2025-08-11 13:47:27,569 | INFO | access_logger | Response status: 200
198
+ 2025-08-11 13:53:19,240 | INFO | access_logger | Request: POST http://127.0.0.1:8000/api/context-analysis
199
+ 2025-08-11 13:53:19,315 | INFO | access_logger | Response status: 200
200
+ 2025-08-11 13:53:39,072 | INFO | access_logger | Request: POST http://127.0.0.1:8000/api/context-analysis
201
+ 2025-08-11 13:53:39,074 | INFO | access_logger | Response status: 200
202
+ 2025-08-11 14:24:10,065 | INFO | access_logger | Request: POST http://127.0.0.1:8000/api/ideation
203
+ 2025-08-11 14:25:02,611 | INFO | access_logger | Response status: 200
204
+ 2025-08-11 14:25:29,940 | INFO | access_logger | Request: POST http://127.0.0.1:8000/api/ideation
205
+ 2025-08-11 14:26:51,558 | INFO | access_logger | Response status: 200
206
+ 2025-08-11 14:37:03,225 | INFO | access_logger | Request: POST http://127.0.0.1:8000/api/ideation
207
+ 2025-08-11 14:39:51,118 | INFO | access_logger | Request: POST http://127.0.0.1:8000/api/ideation
208
+ 2025-08-11 14:40:35,783 | INFO | access_logger | Response status: 200
src/genai/ideation_agent/__pycache__/agent.cpython-312.pyc CHANGED
Binary files a/src/genai/ideation_agent/__pycache__/agent.cpython-312.pyc and b/src/genai/ideation_agent/__pycache__/agent.cpython-312.pyc differ
 
src/genai/ideation_agent/agent.py CHANGED
@@ -1,6 +1,6 @@
1
  from langgraph.graph import StateGraph, START, END , MessagesState
2
  from .utils.state import State
3
- from .utils.nodes import ideator , critic , improver , validator1 , validator2 , validator3 , validator4 , route1_after_validation , route2_after_validation , route3_after_validation , route4_after_validation
4
  from langgraph.checkpoint.memory import MemorySaver
5
  memory = MemorySaver()
6
 
@@ -9,27 +9,22 @@ def ideation_graph():
9
  graph_builder= StateGraph(State)
10
  graph_builder.add_node(ideator)
11
  graph_builder.add_node(critic)
12
- graph_builder.add_node(improver)
13
  graph_builder.add_node(validator1)
14
  graph_builder.add_node(validator2)
15
- graph_builder.add_node(validator3)
16
- graph_builder.add_node(validator4)
17
 
18
 
19
  graph_builder.add_edge(START, "ideator") # Start the graph with node_1
20
  graph_builder.add_edge("ideator", "critic")
21
- graph_builder.add_edge("critic", "improver")
22
- graph_builder.add_edge("improver", "validator1")
23
  graph_builder.add_edge("validator1", "validator2")
24
- graph_builder.add_edge("validator2", "validator3")
25
- graph_builder.add_edge("validator3", "validator4")
26
- graph_builder.add_edge("validator4", END)
27
 
28
  # Use conditional routing from validator
29
- graph_builder.add_conditional_edges("validator1", route1_after_validation,{False:'improver',True:'validator2'})
30
- graph_builder.add_conditional_edges("validator2", route2_after_validation,{False:'improver',True:'validator3'})
31
- graph_builder.add_conditional_edges("validator3", route3_after_validation,{False:'improver',True:'validator4'})
32
- graph_builder.add_conditional_edges("validator4", route4_after_validation,{False:'improver',True:END})
33
 
34
  return graph_builder.compile(checkpointer=memory)
35
 
 
1
  from langgraph.graph import StateGraph, START, END , MessagesState
2
  from .utils.state import State
3
+ from .utils.nodes import ideator , critic , format_response , validator1 , validator2 , route1_after_validation , route2_after_validation
4
  from langgraph.checkpoint.memory import MemorySaver
5
  memory = MemorySaver()
6
 
 
9
  graph_builder= StateGraph(State)
10
  graph_builder.add_node(ideator)
11
  graph_builder.add_node(critic)
12
+ graph_builder.add_node(format_response)
13
  graph_builder.add_node(validator1)
14
  graph_builder.add_node(validator2)
 
 
15
 
16
 
17
  graph_builder.add_edge(START, "ideator") # Start the graph with node_1
18
  graph_builder.add_edge("ideator", "critic")
19
+ graph_builder.add_edge("critic", "format_response")
20
+ graph_builder.add_edge("format_response", "validator1")
21
  graph_builder.add_edge("validator1", "validator2")
22
+ graph_builder.add_edge("validator2", END)
23
+
 
24
 
25
  # Use conditional routing from validator
26
+ graph_builder.add_conditional_edges("validator1", route1_after_validation,{False:'critic',True:'validator2'})
27
+ graph_builder.add_conditional_edges("validator2", route2_after_validation,{False:'critic',True:END})
 
 
28
 
29
  return graph_builder.compile(checkpointer=memory)
30
 
src/genai/ideation_agent/utils/__pycache__/nodes.cpython-312.pyc CHANGED
Binary files a/src/genai/ideation_agent/utils/__pycache__/nodes.cpython-312.pyc and b/src/genai/ideation_agent/utils/__pycache__/nodes.cpython-312.pyc differ
 
src/genai/ideation_agent/utils/__pycache__/prompts.cpython-312.pyc CHANGED
Binary files a/src/genai/ideation_agent/utils/__pycache__/prompts.cpython-312.pyc and b/src/genai/ideation_agent/utils/__pycache__/prompts.cpython-312.pyc differ
 
src/genai/ideation_agent/utils/__pycache__/state.cpython-312.pyc CHANGED
Binary files a/src/genai/ideation_agent/utils/__pycache__/state.cpython-312.pyc and b/src/genai/ideation_agent/utils/__pycache__/state.cpython-312.pyc differ
 
src/genai/ideation_agent/utils/__pycache__/tools.cpython-312.pyc CHANGED
Binary files a/src/genai/ideation_agent/utils/__pycache__/tools.cpython-312.pyc and b/src/genai/ideation_agent/utils/__pycache__/tools.cpython-312.pyc differ
 
src/genai/ideation_agent/utils/nodes.py CHANGED
@@ -1,8 +1,8 @@
1
- from .state import State , ValidationFormatter , ImproverResponseFormatter
2
  from .tools import retrieve_tool
3
  from langgraph.prebuilt import create_react_agent
4
  from src.genai.utils.models_loader import ideator_llm, critic_llm , improver_llm , validator_llm, llm
5
- from langchain_core.messages import SystemMessage , HumanMessage, ToolMessage
6
  from .prompts import ideator_prompt , critic_prompt , improver_prompt , validator_prompt, idea_refinement_prompt
7
 
8
 
@@ -22,11 +22,13 @@ improver_agent = create_react_agent(
22
  )
23
 
24
 
 
25
  def ideator(state:State):
26
  template = ideator_prompt(state)
 
27
  messages = [SystemMessage(content=template),
28
- HumanMessage(content=f'''The business_details is:\n{state.business_details[-1]}\n
29
- The information of the image is:\n{state.image_caption[-1]}''')]
30
  try:
31
  response = ideator_agent.invoke({'messages':messages})
32
  print('Ideator Response:',response)
@@ -43,8 +45,10 @@ def ideator(state:State):
43
 
44
  def critic(state:State):
45
  template = critic_prompt(state)
 
46
  messages = [SystemMessage(content=template),
47
- HumanMessage(content=f'''The business_details is\n{state.business_details[-1]}\n The information of the image is:\n{state.image_caption[-1]}''')]
 
48
 
49
  try:
50
  response = critic_agent.invoke({'messages':messages})
@@ -59,24 +63,39 @@ def critic(state:State):
59
  print('Critic backup Response:',response.content)
60
  state.critic_response.append(response.content)
61
  return state
62
-
63
-
64
- def improver(state:State):
65
  response_list = []
66
- template = improver_prompt(state)
67
- messages = [SystemMessage(content=template),
68
- HumanMessage(content=f'''The business_details is:\n{state.business_details[-1]}\n The information of the image is:\n{state.image_caption[-1]}''')]
69
- print('Improver Prompt:',messages)
70
- response = improver_llm.with_structured_output(ImproverResponseFormatter).invoke(messages)
71
  response_list.append(response.improved_idea1)
72
  response_list.append(response.improved_idea2)
73
  response_list.append(response.improved_idea3)
74
  response_list.append(response.improved_idea4)
75
-
76
- state.improver_response.append(str(response_list))
77
- state.critic_fault.append(response.faults)
78
- print('Improver response:',response_list)
79
  return state
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
 
82
 
@@ -106,31 +125,31 @@ def validator2(state:State):
106
  state.disagreement_reason.append(response.reason)
107
  return state
108
 
109
- def validator3(state:State):
110
- template = validator_prompt(state)
111
- messages = [SystemMessage(content=template),
112
- HumanMessage(content=f'''The business_details is:\n{state.business_details[-1]}''')]
113
-
114
- response = critic_llm.with_structured_output(ValidationFormatter).invoke(messages)
115
- print(f'Validator 3 response: {response}')
116
- state.validator3_response.append(response.result)
117
- print('The state check:',state.validator1_response[-1])
118
- if 'not validated' in response.result:
119
- state.disagreement_reason.append(response.reason)
120
- return state
121
-
122
- def validator4(state:State):
123
- template = validator_prompt(state)
124
- messages = [SystemMessage(content=template),
125
- HumanMessage(content=f'''The business_details is:\n{state.business_details[-1]}''')]
126
-
127
- response = improver_llm.with_structured_output(ValidationFormatter).invoke(messages)
128
- print(f'Validator 4 response: {response}')
129
- state.validator4_response.append(response.result)
130
- print('The state check:',state.validator1_response[-1])
131
- if 'not validated' in response.result:
132
- state.disagreement_reason.append(response.reason)
133
- return state
134
 
135
  def route1_after_validation(state:State):
136
  if 'not validated' in state.validator1_response[-1]:
@@ -146,15 +165,15 @@ def route2_after_validation(state:State):
146
  return True
147
 
148
 
149
- def route3_after_validation(state:State):
150
- if 'not validated' in state.validator3_response[-1]:
151
- return False
152
- else:
153
- return True
154
 
155
 
156
- def route4_after_validation(state:State):
157
- if 'not validated' in state.validator4_response[-1]:
158
- return False
159
- else:
160
- return True
 
1
+ from .state import State , ValidationFormatter , ImproverResponseFormatter, CriticResponseFormatter
2
  from .tools import retrieve_tool
3
  from langgraph.prebuilt import create_react_agent
4
  from src.genai.utils.models_loader import ideator_llm, critic_llm , improver_llm , validator_llm, llm
5
+ from langchain_core.messages import SystemMessage , HumanMessage
6
  from .prompts import ideator_prompt , critic_prompt , improver_prompt , validator_prompt, idea_refinement_prompt
7
 
8
 
 
22
  )
23
 
24
 
25
+
26
  def ideator(state:State):
27
  template = ideator_prompt(state)
28
+ # imdb_reference = retrieve_tool(state.business_details[-1])
29
  messages = [SystemMessage(content=template),
30
+ HumanMessage(content=f'''The business_details is\n{state.business_details[-1]}\n
31
+ The information of the image is:\n{state.image_caption[-1]}''')]
32
  try:
33
  response = ideator_agent.invoke({'messages':messages})
34
  print('Ideator Response:',response)
 
45
 
46
  def critic(state:State):
47
  template = critic_prompt(state)
48
+ # imdb_reference= retrieve_tool(state.business_details[-1])
49
  messages = [SystemMessage(content=template),
50
+ HumanMessage(content=f'''The business_details is\n{state.business_details[-1]}\n
51
+ The information of the image is:\n{state.image_caption[-1]}''')]
52
 
53
  try:
54
  response = critic_agent.invoke({'messages':messages})
 
63
  print('Critic backup Response:',response.content)
64
  state.critic_response.append(response.content)
65
  return state
66
+
67
+ def format_response(state:State):
 
68
  response_list = []
69
+ messages = [SystemMessage(content='''Just extract all the 4 improved ideas and the faults of critic as it is from the critic's response'''),
70
+ HumanMessage(content=f'''The critic's response is: \n {state.critic_response[-1]}''')]
71
+ response = critic_llm.with_structured_output(CriticResponseFormatter).invoke(messages)
 
 
72
  response_list.append(response.improved_idea1)
73
  response_list.append(response.improved_idea2)
74
  response_list.append(response.improved_idea3)
75
  response_list.append(response.improved_idea4)
76
+ state.formatted_response.append(str(response_list))
77
+ state.ideator_fault.append(response.faults)
78
+ print('Formatted Response:', response_list)
 
79
  return state
80
+
81
+
82
+
83
+ # def improver(state:State):
84
+ # response_list = []
85
+ # template = improver_prompt(state)
86
+ # messages = [SystemMessage(content=template),
87
+ # HumanMessage(content=f'''The business_details is:\n{state.business_details[-1]}\n The information of the image is:\n{state.image_caption[-1]}''')]
88
+ # print('Improver Prompt:',messages)
89
+ # response = improver_llm.with_structured_output(ImproverResponseFormatter).invoke(messages)
90
+ # response_list.append(response.improved_idea1)
91
+ # response_list.append(response.improved_idea2)
92
+ # response_list.append(response.improved_idea3)
93
+ # response_list.append(response.improved_idea4)
94
+
95
+ # state.improver_response.append(str(response_list))
96
+ # state.critic_fault.append(response.faults)
97
+ # print('Improver response:',response_list)
98
+ # return state
99
 
100
 
101
 
 
125
  state.disagreement_reason.append(response.reason)
126
  return state
127
 
128
+ # def validator3(state:State):
129
+ # template = validator_prompt(state)
130
+ # messages = [SystemMessage(content=template),
131
+ # HumanMessage(content=f'''The business_details is:\n{state.business_details[-1]}''')]
132
+
133
+ # response = critic_llm.with_structured_output(ValidationFormatter).invoke(messages)
134
+ # print(f'Validator 3 response: {response}')
135
+ # state.validator3_response.append(response.result)
136
+ # print('The state check:',state.validator1_response[-1])
137
+ # if 'not validated' in response.result:
138
+ # state.disagreement_reason.append(response.reason)
139
+ # return state
140
+
141
+ # def validator4(state:State):
142
+ # template = validator_prompt(state)
143
+ # messages = [SystemMessage(content=template),
144
+ # HumanMessage(content=f'''The business_details is:\n{state.business_details[-1]}''')]
145
+
146
+ # response = improver_llm.with_structured_output(ValidationFormatter).invoke(messages)
147
+ # print(f'Validator 4 response: {response}')
148
+ # state.validator4_response.append(response.result)
149
+ # print('The state check:',state.validator1_response[-1])
150
+ # if 'not validated' in response.result:
151
+ # state.disagreement_reason.append(response.reason)
152
+ # return state
153
 
154
  def route1_after_validation(state:State):
155
  if 'not validated' in state.validator1_response[-1]:
 
165
  return True
166
 
167
 
168
+ # def route3_after_validation(state:State):
169
+ # if 'not validated' in state.validator3_response[-1]:
170
+ # return False
171
+ # else:
172
+ # return True
173
 
174
 
175
+ # def route4_after_validation(state:State):
176
+ # if 'not validated' in state.validator4_response[-1]:
177
+ # return False
178
+ # else:
179
+ # return True
src/genai/ideation_agent/utils/prompts.py CHANGED
@@ -226,7 +226,7 @@ Each idea creatively tells a short story that connects indirectly to the busines
226
  {state.business_details[-1]}
227
 
228
  **Final 4 Ideas from Improver**:
229
- {state.improver_response[-1]}
230
 
231
  ---
232
 
 
226
  {state.business_details[-1]}
227
 
228
  **Final 4 Ideas from Improver**:
229
+ {state.formatted_response[-1]}
230
 
231
  ---
232
 
src/genai/ideation_agent/utils/state.py CHANGED
@@ -7,8 +7,8 @@ class State(BaseModel):
7
  business_details : Annotated[list[dict],operator.add] = []
8
  ideator_response: Annotated[list[str],operator.add] = []
9
  critic_response: Annotated[list[str],operator.add] = []
10
- critic_fault: Annotated[list[str],operator.add] = []
11
- improver_response: Annotated[list[str],operator.add] = []
12
  validator1_response: Annotated[list[str],operator.add] = []
13
  validator2_response: Annotated[list[str],operator.add] = []
14
  validator3_response: Annotated[list[str],operator.add] = []
@@ -30,4 +30,11 @@ class ValidationFormatter(BaseModel):
30
  result: str = Field(description="Returns **validated** if the idea is validated. Returns **not validated** if story is not validated.")
31
  reason: str = Field(description="If the idea is **not validated**, it returns the reason why it is not validated.")
32
 
 
 
 
 
 
 
 
33
 
 
7
  business_details : Annotated[list[dict],operator.add] = []
8
  ideator_response: Annotated[list[str],operator.add] = []
9
  critic_response: Annotated[list[str],operator.add] = []
10
+ ideator_fault: Annotated[list[str],operator.add] = []
11
+ formatted_response: Annotated[list[str],operator.add] = []
12
  validator1_response: Annotated[list[str],operator.add] = []
13
  validator2_response: Annotated[list[str],operator.add] = []
14
  validator3_response: Annotated[list[str],operator.add] = []
 
30
  result: str = Field(description="Returns **validated** if the idea is validated. Returns **not validated** if story is not validated.")
31
  reason: str = Field(description="If the idea is **not validated**, it returns the reason why it is not validated.")
32
 
33
+ class CriticResponseFormatter(BaseModel):
34
+ faults: str= Field(description="Returns the faults in the critic's response if any.")
35
+ improved_idea1: str = Field(description="Returns the first improved idea of exactly 40 words. ")
36
+ improved_idea2: str = Field(description="Returns the second improved idea of exactly 40 words. ")
37
+ improved_idea3: str = Field(description="Returns the third improved idea of exactly 40 words. ")
38
+ improved_idea4: str = Field(description="Returns the fourth improved idea of exactly 40 words. ")
39
+
40
 
src/genai/ideation_agent/utils/tools.py CHANGED
@@ -55,7 +55,7 @@ def retrieve_tool_old(business_details):
55
  return encoding.decode(trimmed_response)
56
 
57
 
58
- @tool("imdb_movies_ideas_retrieval_tool", args_schema=QueryFormatter, return_direct=False,description="Retrieve imdb movies-related idea for a given query.")
59
  def retrieve_tool(business_details):
60
  '''
61
  Always invoke this tool.
@@ -64,7 +64,7 @@ def retrieve_tool(business_details):
64
  query_embedding = np.array(embedding_model.embed_query(str(business_details))).reshape(1, -1).astype('float32')
65
  faiss.normalize_L2(query_embedding)
66
 
67
- top_k = 5
68
  distances, indices = ideas_index.search(query_embedding, top_k)
69
 
70
  outer_list = []
@@ -81,4 +81,13 @@ def retrieve_tool(business_details):
81
  outer_list.append(inner_list)
82
 
83
  cleaned_response = clean_text(str(outer_list))
84
- return str(cleaned_response)
 
 
 
 
 
 
 
 
 
 
55
  return encoding.decode(trimmed_response)
56
 
57
 
58
+ # @tool("imdb_movies_ideas_retrieval_tool", args_schema=QueryFormatter, return_direct=False,description="Retrieve imdb movies-related idea for a given query.")
59
  def retrieve_tool(business_details):
60
  '''
61
  Always invoke this tool.
 
64
  query_embedding = np.array(embedding_model.embed_query(str(business_details))).reshape(1, -1).astype('float32')
65
  faiss.normalize_L2(query_embedding)
66
 
67
+ top_k = 10
68
  distances, indices = ideas_index.search(query_embedding, top_k)
69
 
70
  outer_list = []
 
81
  outer_list.append(inner_list)
82
 
83
  cleaned_response = clean_text(str(outer_list))
84
+ return str(cleaned_response)
85
+
86
+ # business_details= {
87
+ # "business_type": "restaurant",
88
+ # "platform": "instagram",
89
+ # "target_audience": "youths",
90
+ # "business_goals": "to increase sales",
91
+ # "offerings": "nepali local foods",
92
+ # "Challenges_faced": "competition with other restaurants"
93
+ # },