subashpoudel commited on
Commit
93d50e5
Β·
1 Parent(s): 53ca471

created backup for analytics

Browse files
api/routers/analytics_chatbot.py CHANGED
@@ -19,11 +19,11 @@ graph = agent.chatbot_graph()
19
  def get_analytics(msg:str):
20
  # user_query=process_query(msg)
21
  config={"configurable": {"thread_id": "analytics-chatbot-thread"},"run_name":"analytics-chatbot"}
22
- try:
23
- result=graph.invoke({'messages':msg},config=config)
 
 
24
  return {
25
  'response': result['response'],
26
  'endpoint': result['endpoint']
27
  }
28
- except Exception as e:
29
- print(e)
 
19
  def get_analytics(msg:str):
20
  # user_query=process_query(msg)
21
  config={"configurable": {"thread_id": "analytics-chatbot-thread"},"run_name":"analytics-chatbot"}
22
+ result=graph.invoke({'messages':msg},config=config)
23
+ if result.get('backup_data') is not None:
24
+ return {'backup_response': result['backup_data']}
25
+ else:
26
  return {
27
  'response': result['response'],
28
  'endpoint': result['endpoint']
29
  }
 
 
logs/access.log CHANGED
@@ -1467,3 +1467,45 @@
1467
  2025-10-16 13:51:59,731 | INFO | access_logger | app.py:21 | Response status: 200
1468
  2025-10-16 13:59:15,639 | INFO | access_logger | app.py:19 | Request: GET http://127.0.0.1:8000/api/analytics-chatbot?msg=I%20want%20demographics%20analytics%20of%20munachiya
1469
  2025-10-16 13:59:21,151 | INFO | access_logger | app.py:21 | Response status: 200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1467
  2025-10-16 13:51:59,731 | INFO | access_logger | app.py:21 | Response status: 200
1468
  2025-10-16 13:59:15,639 | INFO | access_logger | app.py:19 | Request: GET http://127.0.0.1:8000/api/analytics-chatbot?msg=I%20want%20demographics%20analytics%20of%20munachiya
1469
  2025-10-16 13:59:21,151 | INFO | access_logger | app.py:21 | Response status: 200
1470
+ 2025-10-16 16:15:38,202 | INFO | access_logger | app.py:19 | Request: GET http://127.0.0.1:8000/api/analytics-chatbot?msg=is%20muna%20having%20strong%20engagement%20than%20divya%3F
1471
+ 2025-10-16 16:15:44,816 | INFO | access_logger | app.py:21 | Response status: 200
1472
+ 2025-10-16 16:16:19,370 | INFO | access_logger | app.py:19 | Request: GET http://127.0.0.1:8000/api/analytics-chatbot?msg=who%20is%20better%20for%20my%20business%3F%20muna%20or%20divya%3F
1473
+ 2025-10-16 16:16:25,888 | INFO | access_logger | app.py:21 | Response status: 200
1474
+ 2025-10-16 16:32:41,530 | INFO | access_logger | app.py:19 | Request: GET http://127.0.0.1:8000/api/analytics-chatbot?msg=iam%20subash
1475
+ 2025-10-16 17:01:38,064 | INFO | access_logger | app.py:19 | Request: GET http://127.0.0.1:8000/api/analytics-chatbot?msg=iam%20subash
1476
+ 2025-10-16 17:01:52,561 | INFO | access_logger | app.py:21 | Response status: 200
1477
+ 2025-10-17 12:07:55,874 | INFO | access_logger | app.py:19 | Request: GET http://127.0.0.1:8000/
1478
+ 2025-10-17 12:07:55,875 | INFO | access_logger | app.py:21 | Response status: 200
1479
+ 2025-10-17 12:07:56,420 | INFO | access_logger | app.py:19 | Request: GET http://127.0.0.1:8000/favicon.ico
1480
+ 2025-10-17 12:07:56,421 | INFO | access_logger | app.py:21 | Response status: 404
1481
+ 2025-10-17 12:07:58,630 | INFO | access_logger | app.py:19 | Request: GET http://127.0.0.1:8000/docs
1482
+ 2025-10-17 12:07:58,631 | INFO | access_logger | app.py:21 | Response status: 200
1483
+ 2025-10-17 12:07:58,788 | INFO | access_logger | app.py:19 | Request: GET http://127.0.0.1:8000/openapi.json
1484
+ 2025-10-17 12:07:58,795 | INFO | access_logger | app.py:21 | Response status: 200
1485
+ 2025-10-17 12:08:09,324 | INFO | access_logger | app.py:19 | Request: GET http://127.0.0.1:8000/api/analytics-chatbot?msg=I%20am%20subash
1486
+ 2025-10-17 12:12:39,346 | INFO | access_logger | app.py:19 | Request: GET http://127.0.0.1:8000/api/analytics-chatbot?msg=I%20am%20subash
1487
+ 2025-10-17 12:14:10,002 | INFO | access_logger | app.py:19 | Request: GET http://127.0.0.1:8000/api/analytics-chatbot?msg=I%20am%20subash
1488
+ 2025-10-17 12:14:13,701 | INFO | access_logger | app.py:21 | Response status: 200
1489
+ 2025-10-17 12:15:09,470 | INFO | access_logger | app.py:19 | Request: GET http://127.0.0.1:8000/api/analytics-chatbot?msg=how%20are%20you%3F
1490
+ 2025-10-17 12:15:14,264 | INFO | access_logger | app.py:21 | Response status: 200
1491
+ 2025-10-17 12:16:44,106 | INFO | access_logger | app.py:19 | Request: GET http://127.0.0.1:8000/api/analytics-chatbot?msg=I%20want%20analytics%20of%20muna%20chiya%20of%20last%2010%20days
1492
+ 2025-10-17 12:16:48,707 | INFO | access_logger | app.py:21 | Response status: 200
1493
+ 2025-10-17 12:18:43,858 | INFO | access_logger | app.py:19 | Request: GET http://127.0.0.1:8000/api/analytics-chatbot?msg=I%20want%20analytics%20of%20muna%20chiya%20of%20last%2010%20days
1494
+ 2025-10-17 12:18:48,714 | INFO | access_logger | app.py:21 | Response status: 200
1495
+ 2025-10-17 12:27:38,479 | INFO | access_logger | app.py:19 | Request: GET http://127.0.0.1:8000/api/show-analytics
1496
+ 2025-10-17 12:27:39,454 | INFO | access_logger | app.py:21 | Response status: 200
1497
+ 2025-10-17 13:13:22,253 | INFO | access_logger | app.py:19 | Request: GET http://127.0.0.1:8000/api/analytics-chatbot?msg=I%20want%20analytics%20of%20muna%20chiya%20of%20last%2010%20days
1498
+ 2025-10-17 13:18:41,884 | INFO | access_logger | app.py:19 | Request: GET http://127.0.0.1:8000/api/analytics-chatbot?msg=I%20want%20analytics%20of%20muna%20chiya%20of%20last%2010%20days
1499
+ 2025-10-17 13:18:51,310 | INFO | access_logger | app.py:21 | Response status: 200
1500
+ 2025-10-17 13:42:23,977 | INFO | access_logger | app.py:19 | Request: GET http://127.0.0.1:8000/api/analytics-chatbot?msg=I%20want%20analytics%20of%20muna%20chiya%20of%20last%2010%20days
1501
+ 2025-10-17 13:42:33,362 | INFO | access_logger | app.py:21 | Response status: 200
1502
+ 2025-10-17 13:46:31,111 | INFO | access_logger | app.py:19 | Request: GET http://127.0.0.1:8000/api/analytics-chatbot?msg=I%20want%20analytics%20of%20muna%20chiya%20of%20last%2010%20days
1503
+ 2025-10-17 13:46:38,070 | INFO | access_logger | app.py:21 | Response status: 200
1504
+ 2025-10-17 13:53:58,981 | INFO | access_logger | app.py:19 | Request: GET http://127.0.0.1:8000/
1505
+ 2025-10-17 13:53:58,982 | INFO | access_logger | app.py:21 | Response status: 200
1506
+ 2025-10-17 13:54:02,244 | INFO | access_logger | app.py:19 | Request: GET http://127.0.0.1:8000/docs
1507
+ 2025-10-17 13:54:02,245 | INFO | access_logger | app.py:21 | Response status: 200
1508
+ 2025-10-17 13:54:02,375 | INFO | access_logger | app.py:19 | Request: GET http://127.0.0.1:8000/openapi.json
1509
+ 2025-10-17 13:54:02,382 | INFO | access_logger | app.py:21 | Response status: 200
1510
+ 2025-10-17 13:54:26,472 | INFO | access_logger | app.py:19 | Request: GET http://127.0.0.1:8000/api/analytics-chatbot?msg=i%20want%20the%20analytics%20of%20last%2010%20days%20of%20muna%20chiya
1511
+ 2025-10-17 13:54:36,464 | INFO | access_logger | app.py:21 | Response status: 200
logs/app.log CHANGED
@@ -132,3 +132,4 @@
132
  2025-10-14 13:36:55,021 | INFO | app_logger | api/routers/ideation.py:33 | Executed the ideation pipeline.
133
  2025-10-14 13:55:41,999 | INFO | app_logger | api/routers/context_analysis.py:27 | Context Analysis Completed.
134
  2025-10-14 14:03:00,252 | INFO | app_logger | api/routers/brainstorm.py:42 | Executed brainstorming agent.
 
 
132
  2025-10-14 13:36:55,021 | INFO | app_logger | api/routers/ideation.py:33 | Executed the ideation pipeline.
133
  2025-10-14 13:55:41,999 | INFO | app_logger | api/routers/context_analysis.py:27 | Context Analysis Completed.
134
  2025-10-14 14:03:00,252 | INFO | app_logger | api/routers/brainstorm.py:42 | Executed brainstorming agent.
135
+ 2025-10-17 12:27:39,451 | INFO | app_logger | api/routers/show_analytics.py:14 | Influencer Analytics returned by orchestrator.
src/genai/analytics_chatbot/agent.py CHANGED
@@ -1,7 +1,7 @@
1
  from langgraph.graph import StateGraph, START, END
2
  from langgraph.checkpoint.memory import MemorySaver
3
  from .utils.state import State
4
- from .utils.nodes import ChatbotNode, FetchDataNode , FetchLastMessage , RetrievePossibleEndpoints , FetchParametersNode , RetrieveExactEndpoint
5
 
6
  class ChatbotAgent:
7
  def __init__(self):
@@ -14,6 +14,7 @@ class ChatbotAgent:
14
  graph_builder.add_node("retrieve_exact_endpoint", RetrieveExactEndpoint().run)
15
  graph_builder.add_node("fetch_parameters", FetchParametersNode().run)
16
  graph_builder.add_node("fetch_data", FetchDataNode().run)
 
17
 
18
  graph_builder.add_edge(START, "fetch_last_message")
19
  graph_builder.add_edge("fetch_last_message", 'retrieve_api_endpoints')
@@ -21,6 +22,9 @@ class ChatbotAgent:
21
  graph_builder.add_edge("retrieve_exact_endpoint", 'fetch_parameters')
22
  graph_builder.add_edge("fetch_parameters", 'fetch_data')
23
  graph_builder.add_edge("fetch_data", END)
 
24
 
 
 
25
 
26
  return graph_builder.compile(checkpointer=self.memory)
 
1
  from langgraph.graph import StateGraph, START, END
2
  from langgraph.checkpoint.memory import MemorySaver
3
  from .utils.state import State
4
+ from .utils.nodes import FetchDataNode , FetchLastMessage , RetrievePossibleEndpoints , FetchParametersNode , RetrieveExactEndpoint, BackupRetrievalNode, RoutingNode
5
 
6
  class ChatbotAgent:
7
  def __init__(self):
 
14
  graph_builder.add_node("retrieve_exact_endpoint", RetrieveExactEndpoint().run)
15
  graph_builder.add_node("fetch_parameters", FetchParametersNode().run)
16
  graph_builder.add_node("fetch_data", FetchDataNode().run)
17
+ graph_builder.add_node("backup_response", BackupRetrievalNode().run)
18
 
19
  graph_builder.add_edge(START, "fetch_last_message")
20
  graph_builder.add_edge("fetch_last_message", 'retrieve_api_endpoints')
 
22
  graph_builder.add_edge("retrieve_exact_endpoint", 'fetch_parameters')
23
  graph_builder.add_edge("fetch_parameters", 'fetch_data')
24
  graph_builder.add_edge("fetch_data", END)
25
+ graph_builder.add_edge("backup_response", END)
26
 
27
+ graph_builder.add_conditional_edges("fetch_parameters", RoutingNode().run,{'execute_backup':'backup_response', 'go_on':"fetch_data"})
28
+ graph_builder.add_conditional_edges("fetch_data", RoutingNode().run,{'execute_backup':'backup_response', 'go_on':END})
29
 
30
  return graph_builder.compile(checkpointer=self.memory)
src/genai/analytics_chatbot/utils/nodes.py CHANGED
@@ -1,14 +1,16 @@
1
  import requests
2
  from langchain_core.messages import SystemMessage , HumanMessage , FunctionMessage
3
  from .state import State
 
4
  from .schemas import ResponseFormatter , CompareBodyFormatter, LatestMessageFormatter, ParameterFormatter, EndpointFormatter
5
- from .prompts import chatbot_prompt , get_body_prompt , fetch_last_message_prompt , fetch_parameters_prompt, fetch_endpoint_prompt
6
  from .utils import generate_api_knowledge , process_query, get_endpoint_info
7
  from src.genai.utils.models_loader import llm_gpt
8
  import numpy as np
9
- from src.genai.utils.data_loader import api_knowledge_df, api_index
10
  from src.genai.utils.models_loader import embedding_model
11
 
 
12
  class FetchLastMessage:
13
  def __init__(self):
14
  self.llm = llm_gpt
@@ -34,7 +36,7 @@ class RetrievePossibleEndpoints:
34
 
35
  def run(self,state:State):
36
  query_embedding = np.array(embedding_model.embed_query(state['latest_message'])).reshape(1, -1).astype('float32')
37
- distances, indices = self.index.search(query_embedding, 5)
38
  for idx in indices[0]:
39
  row = self.df.iloc[idx]
40
  print('Endpoint:',row['endpoint'])
@@ -71,52 +73,33 @@ class FetchParametersNode:
71
  self.llm = llm_gpt
72
 
73
  def run(self , state:State):
74
- print('Entered to fetch parameters')
75
- print(state['method'])
76
- if state['method'] == 'GET':
77
- print('Condition satisfied')
78
- template = fetch_parameters_prompt
79
- messages=[SystemMessage(content=template),
80
- HumanMessage(content=f'''The query is: {state['latest_message']}\n. The needed parameters: {str(state['needed_parameters'])}''')
81
- ]
82
- print('messages:', messages)
83
- result = self.llm.with_structured_output(ParameterFormatter, method='function_calling').invoke(messages)
84
- parameters_values={key: process_query(value) for key, value in result.parameters_values.items()}
85
- print('The parameter values:', parameters_values)
86
- return {
87
- 'parameters_values':parameters_values
88
- }
89
- else:
90
- return{
91
- 'parameters_values': {}
92
- }
93
-
94
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
- class ChatbotNode:
97
- def __init__(self):
98
- self.llm = llm_gpt
99
 
100
- def run(self, state:State):
101
- print('Message:',state['messages'])
102
- template = chatbot_prompt()
103
- knowledge_base = generate_api_knowledge('https://reveltrends.vercel.app')
104
- print('The knowledge base is:', knowledge_base)
105
- messages = [SystemMessage(content=template),
106
- FunctionMessage(name='analytics_chatbot',content=str(knowledge_base)),
107
- ] + state["messages"]
108
- if len(state['messages'])>11:
109
- state["messages"] = state["messages"][-9:]
110
- print('Messages:', state['messages'])
111
- print(len(state['messages']))
112
- result = self.llm.with_structured_output(ResponseFormatter, method='function_calling').invoke(messages)
113
- print('The result is:',result)
114
- return {
115
- "messages": [{"role": "assistant", "content": f'''The endpoint is: {result.endpoint}. The parameters are: {result.parameters}'''}],
116
- "endpoint": result.endpoint,
117
- "method": result.method,
118
- "parameters": result.parameters,
119
- }
120
 
121
  class FetchDataNode:
122
  def __init__(self):
@@ -128,30 +111,54 @@ class FetchDataNode:
128
  }
129
 
130
  def run(self, state:State):
131
- print('Entered to fetch data')
132
- url = f'''{self.base_url}{state['endpoint']}'''
133
- if state['method'] == 'GET':
134
- response = requests.get(url, params=state['parameters_values'],headers=self.headers)
135
- elif state['endpoint'] == '/api/v1/compare/':
136
- print('Condition satisfied')
137
- messages = [SystemMessage(content=get_body_prompt()),
138
- HumanMessage(content=str(state['messages']))]
139
- response=llm_gpt.with_structured_output(CompareBodyFormatter , method='function_calling').invoke(messages)
140
- print('INF names response:', response)
141
- payload = {
142
- "usernames": list(map(process_query,response.names)),
143
- "freq": response.frequency
144
- }
145
-
146
- print('The payload is:',payload)
147
-
148
- headers = {
149
- "Content-Type": "application/json"
150
- }
 
 
 
 
 
 
 
 
151
 
152
- response = requests.post(url, json=payload, headers=headers)
153
 
154
- return {'response':response.json()}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155
 
156
 
 
157
 
 
1
  import requests
2
  from langchain_core.messages import SystemMessage , HumanMessage , FunctionMessage
3
  from .state import State
4
+ from .tools import RetrieverBackup
5
  from .schemas import ResponseFormatter , CompareBodyFormatter, LatestMessageFormatter, ParameterFormatter, EndpointFormatter
6
+ from .prompts import chatbot_prompt , get_body_prompt , fetch_last_message_prompt , fetch_parameters_prompt, fetch_endpoint_prompt, backup_retrieval_prompt
7
  from .utils import generate_api_knowledge , process_query, get_endpoint_info
8
  from src.genai.utils.models_loader import llm_gpt
9
  import numpy as np
10
+ from src.genai.utils.data_loader import api_knowledge_df, api_index, caption_df , caption_index
11
  from src.genai.utils.models_loader import embedding_model
12
 
13
+
14
  class FetchLastMessage:
15
  def __init__(self):
16
  self.llm = llm_gpt
 
36
 
37
  def run(self,state:State):
38
  query_embedding = np.array(embedding_model.embed_query(state['latest_message'])).reshape(1, -1).astype('float32')
39
+ distances, indices = self.index.search(query_embedding, 3)
40
  for idx in indices[0]:
41
  row = self.df.iloc[idx]
42
  print('Endpoint:',row['endpoint'])
 
73
  self.llm = llm_gpt
74
 
75
  def run(self , state:State):
76
+ try:
77
+ print('Entered to fetch parameters')
78
+ print(state['method'])
79
+ if state['method'] == 'GET':
80
+ print('Condition satisfied')
81
+ template = fetch_parameters_prompt
82
+ messages=[SystemMessage(content=template),
83
+ HumanMessage(content=f'''The query is: {state['latest_message']}\n. The needed parameters: {str(state['needed_parameters'])}''')
84
+ ]
85
+ print('messages:', messages)
86
+ result = self.llm.with_structured_output(ParameterFormatter, method='function_calling').invoke(messages)
87
+ # parameters_values={key: process_query(value) for key, value in result.parameters_values.items()}
88
+ parameters_values = {k: (process_query(v) if isinstance(v, str) else v) for k, v in result.parameters_values.items()}
 
 
 
 
 
 
 
89
 
90
+ print('The parameter values:', parameters_values)
91
+ return {
92
+ 'parameters_values':parameters_values
93
+ }
94
+ else:
95
+ return{
96
+ 'parameters_values': {}
97
+ }
98
+ except Exception as e:
99
+ print('Error occoured:', e)
100
+ return {'error_message': str(e)}
101
 
 
 
 
102
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
 
104
  class FetchDataNode:
105
  def __init__(self):
 
111
  }
112
 
113
  def run(self, state:State):
114
+ try:
115
+ print('Entered to fetch data')
116
+ url = f'''{self.base_url}{state['endpoint']}'''
117
+ if state['method'] == 'GET':
118
+ response = requests.get(url, params=state['parameters_values'],headers=self.headers)
119
+ elif state['endpoint'] == '/api/v1/compare/':
120
+ print('Condition satisfied')
121
+ messages = [SystemMessage(content=get_body_prompt()),
122
+ HumanMessage(content=str(state['messages']))]
123
+ response=llm_gpt.with_structured_output(CompareBodyFormatter , method='function_calling').invoke(messages)
124
+ print('INF names response:', response)
125
+ payload = {
126
+ "usernames": list(map(process_query,response.names)),
127
+ "freq": response.frequency
128
+ }
129
+
130
+ print('The payload is:',payload)
131
+
132
+ headers = {
133
+ "Content-Type": "application/json"
134
+ }
135
+
136
+ response = requests.post(url, json=payload, headers=headers)
137
+
138
+ return {'response':response.json()}
139
+ except Exception as e:
140
+ print('Error occoured:', e)
141
+ return {'error_message': str(e)}
142
 
 
143
 
144
+ class BackupRetrievalNode:
145
+ def __init__(self):
146
+ self.llm = llm_gpt
147
+
148
+ def run(self, state:State):
149
+ retrieval=RetrieverBackup().retrieve(state['latest_message'])
150
+ return {'backup_data': retrieval}
151
+
152
+ class RoutingNode:
153
+ def __init__(self):
154
+ pass
155
+
156
+ def run(self,state:State):
157
+ if state.get('error_message') is not None:
158
+ return 'execute_backup'
159
+ else:
160
+ return 'go_on'
161
 
162
 
163
+
164
 
src/genai/analytics_chatbot/utils/prompts.py CHANGED
@@ -103,4 +103,9 @@ User Query: I want weekly engagement stats of John
103
  Possible Endpoints: ['/api/v1/overview/buzz_trend', '/api/v1/analytics/engagement', '/api/v1/analytics/followers']
104
  endpoint: /api/v1/analytics/engagement
105
 
 
 
 
 
 
106
  '''
 
103
  Possible Endpoints: ['/api/v1/overview/buzz_trend', '/api/v1/analytics/engagement', '/api/v1/analytics/followers']
104
  endpoint: /api/v1/analytics/engagement
105
 
106
+ '''
107
+
108
+ backup_retrieval_prompt = '''
109
+ You are provided with the retrieved data as a function message and the user query.
110
+ Respond to the user query only through the context of retrieved data. Don't give hallucinated responses.
111
  '''
src/genai/analytics_chatbot/utils/state.py CHANGED
@@ -11,4 +11,5 @@ class State(TypedDict):
11
  response:dict
12
  error_message:str
13
  latest_message:str
14
- parameters_values:str
 
 
11
  response:dict
12
  error_message:str
13
  latest_message:str
14
+ parameters_values:str
15
+ backup_data:str
src/genai/analytics_chatbot/utils/tools.py CHANGED
@@ -1,7 +1,11 @@
1
-
2
  import numpy as np
3
- from src.genai.utils.data_loader import api_knowledge_df, api_index
 
 
 
4
  from src.genai.utils.models_loader import embedding_model
 
5
 
6
  class APIKnowledgeRetrieveTool:
7
  def __init__(self):
@@ -17,6 +21,89 @@ class APIKnowledgeRetrieveTool:
17
  'parameters':row['parameters']}
18
  return data
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
 
22
 
 
1
+ import re
2
  import numpy as np
3
+ import faiss
4
+ from src.genai.utils.models_loader import embedding_model , encoding_model
5
+ from src.genai.utils.utils import clean_text
6
+ from src.genai.utils.data_loader import api_knowledge_df, api_index, caption_df , caption_index
7
  from src.genai.utils.models_loader import embedding_model
8
+ import pandas as pd
9
 
10
  class APIKnowledgeRetrieveTool:
11
  def __init__(self):
 
21
  'parameters':row['parameters']}
22
  return data
23
 
24
+ class RetrieverBackup:
25
+ def __init__(self):
26
+ self.df = caption_df
27
+ self.index = caption_index
28
+
29
+ def _filter_dataset(self, query):
30
+ usernames = self.df["username"].dropna().unique()
31
+ matched_users = [u for u in usernames if re.search(rf"\b{re.escape(u)}\b", query)]
32
+ if matched_users:
33
+ filtered_df = self.df[self.df["username"].isin(matched_users)]
34
+ return filtered_df
35
+ else:
36
+ return self.df
37
+
38
+
39
+ def retrieve_old(self, query):
40
+ query_embedding = np.array(embedding_model.embed_query(str(query))).reshape(1, -1).astype('float32')
41
+ print('Embeddings Generated')
42
+ faiss.normalize_L2(query_embedding)
43
+ print('Query embedded')
44
+ filtered_df = self._filter_dataset(query)
45
+ distances, indices = self.index.search(query_embedding, len(filtered_df))
46
+ similarity_threshold = 0.35
47
+ selected = [(idx, sim) for idx, sim in zip(indices[0], distances[0]) if sim >= similarity_threshold]
48
+ if not selected:
49
+ return "No influencers found."
50
+
51
+ outer_list = []
52
+ for rank, (idx, sim) in enumerate(selected, 1):
53
+ row = filtered_df.iloc[idx]
54
+ inner_list = [
55
+ f"[{rank}]. The influencer name is: **{row['username']}** β€” Likes: **{row['likesCount']}**, Comments: **{row['commentCount']}**",
56
+ f"The branding or promotion done is:\n{row['visible_texts_or_brandings']}",
57
+ f"The details of product or service is:\n{row['product_or_service_details']}"
58
+ ]
59
+ outer_list.append(inner_list)
60
+
61
+ cleaned_response = clean_text(str(outer_list))
62
+ print('response cleaned')
63
+ tokens = encoding_model.encode(cleaned_response)[:500]
64
+ print('tokens got')
65
+ return encoding_model.decode(tokens)
66
+
67
+ def retrieve(self, query):
68
+ query_embedding = np.array(embedding_model.embed_query(str(query))).reshape(1, -1).astype('float32')
69
+ faiss.normalize_L2(query_embedding)
70
+
71
+ # Search on full dataset (index is built on full df)
72
+ distances, indices = self.index.search(query_embedding, len(self.df))
73
+ similarity_threshold = 0.35
74
+
75
+ # Prepare matched usernames
76
+ usernames = self.df["username"].dropna().unique()
77
+ matched_users = [u for u in usernames if re.search(rf"\b{re.escape(u)}\b", query)]
78
+
79
+ results = []
80
+ rank = 1
81
+ for idx, sim in zip(indices[0], distances[0]):
82
+ if sim < similarity_threshold:
83
+ continue
84
+
85
+ row = self.df.iloc[idx]
86
+
87
+ # If query mentions usernames, only keep those rows
88
+ if matched_users and row["username"] not in matched_users:
89
+ continue
90
+
91
+ results.append({
92
+ 'url': row['videoUrl'],
93
+ 'username': row['username'],
94
+ 'likesCount': int(row['likesCount']) if pd.notnull(row['likesCount']) else None,
95
+ 'commentCount': int(row['commentCount']) if pd.notnull(row['commentCount']) else None
96
+ })
97
+ results = results[:10] if len(results) > 10 else results
98
+ return results
99
+
100
+
101
+
102
+
103
+
104
+
105
+
106
+
107
 
108
 
109
 
src/genai/analytics_chatbot/utils/utils.py CHANGED
@@ -80,26 +80,6 @@ def generate_api_knowledge(base_url: str):
80
 
81
  return api_knowledge
82
 
83
- def influencers_name():
84
- return {
85
- "information_of_influencers_usernames": "From the names_dictionary, the key represents their original names and the value represents their usernames (influencer_username).",
86
- "names_dictionary": {
87
- "divya dhakal": "divyadhakal_",
88
- "istu karki": "ishtukarkee",
89
- "kano mama": "kano_mama",
90
- "muna chiya": "munachiya",
91
- "nepal food": "nepal_food",
92
- "ggkaam": "ggkaam610",
93
- "adarsh": "adars_fpv",
94
- "ajay tamang": "ajaytm43",
95
- "anisha kafle": "anishakafle",
96
- "diwash gurung": "diwasg",
97
- "eat grub food": "eatgrubfood",
98
- "surakshya kc": "imsurakshyakc",
99
- "jholey": "jholeyism",
100
- "mrb vlogs": "mrbvlog2"
101
- }
102
- }
103
 
104
  def process_query(user_query: str) -> str:
105
  # load mapping from json file
 
80
 
81
  return api_knowledge
82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
  def process_query(user_query: str) -> str:
85
  # load mapping from json file
src/genai/ideation_agent/utils/prompts.py CHANGED
@@ -47,6 +47,20 @@ idea_2:
47
  One-line_description: show how one bag works in 3 daily scenarios (work, casual, night out).
48
  Hook: β€œ1 bag β†’ 3 lifestyles.”
49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
  Generate creative and attention-grabbing video ideas that stand out. Each idea must include surprising elements, be far from generic, and be memorable for its originality and catchiness.
51
  """
52
 
 
47
  One-line_description: show how one bag works in 3 daily scenarios (work, casual, night out).
48
  Hook: β€œ1 bag β†’ 3 lifestyles.”
49
 
50
+ The business details is:
51
+ Business Type: Fitness and Gym.
52
+ Platforms: Instagram reels, TikTok
53
+ Target Audience: age 18–50
54
+ Content Shooting Device: Mobile phone
55
+
56
+ The ideas for it are:
57
+ idea 1:
58
+ title: Hydration Hacks for Gym Lovers
59
+ One_liner_description: A short informative video where a trainer shares quick, practical hydration tips.
60
+ hook: Feeling tired mid-workout? You might just be thirsty.
61
+ Unique_selling_proposition: Simple, science-backed advice from trainers that helps members train smarter, not harder.
62
+ niche: People looking for health, wellness, and workout education.
63
+
64
  Generate creative and attention-grabbing video ideas that stand out. Each idea must include surprising elements, be far from generic, and be memorable for its originality and catchiness.
65
  """
66