Spaces:
Sleeping
Sleeping
Commit
·
872d043
1
Parent(s):
3002e1b
Implemented analytics chatbot
Browse files- api/routers/analytics_chatbot.py +6 -3
- check.py +28 -13
- influencers_names.csv +0 -0
- logs/access.log +94 -0
- src/genai/analytics_chatbot/agent.py +6 -2
- src/genai/analytics_chatbot/utils/name_variations.json +80 -0
- src/genai/analytics_chatbot/utils/nodes.py +28 -2
- src/genai/analytics_chatbot/utils/state.py +3 -1
- src/genai/analytics_chatbot/utils/{api_knowledge.py → utils.py} +43 -1
- src/genai/utils/__pycache__/models_loader.cpython-313.pyc +0 -0
- src/genai/utils/models_loader.py +1 -1
api/routers/analytics_chatbot.py
CHANGED
|
@@ -5,6 +5,7 @@ from fastapi import APIRouter
|
|
| 5 |
from fastapi.responses import StreamingResponse
|
| 6 |
from api.stored_data import stored_data
|
| 7 |
from src.genai.analytics_chatbot.agent import ChatbotAgent
|
|
|
|
| 8 |
from api.schemas.analytics_chatbot import UserMessage
|
| 9 |
|
| 10 |
app_logger = logging.getLogger("app_logger")
|
|
@@ -16,12 +17,14 @@ graph = agent.chatbot_graph()
|
|
| 16 |
|
| 17 |
@router.post("/analytics-chatbot")
|
| 18 |
def context_analysis(msg: UserMessage):
|
|
|
|
| 19 |
config={"configurable": {"thread_id": "analytics-chatbot-thread"}}
|
| 20 |
try:
|
| 21 |
-
result=graph.invoke({'messages':
|
| 22 |
return {
|
| 23 |
'endpoint': result['endpoint'] ,
|
| 24 |
-
'parameters': result['parameters']
|
|
|
|
| 25 |
}
|
| 26 |
except Exception as e:
|
| 27 |
-
|
|
|
|
| 5 |
from fastapi.responses import StreamingResponse
|
| 6 |
from api.stored_data import stored_data
|
| 7 |
from src.genai.analytics_chatbot.agent import ChatbotAgent
|
| 8 |
+
from src.genai.analytics_chatbot.utils.utils import process_query
|
| 9 |
from api.schemas.analytics_chatbot import UserMessage
|
| 10 |
|
| 11 |
app_logger = logging.getLogger("app_logger")
|
|
|
|
| 17 |
|
| 18 |
@router.post("/analytics-chatbot")
|
| 19 |
def context_analysis(msg: UserMessage):
|
| 20 |
+
user_query=process_query(msg.message)
|
| 21 |
config={"configurable": {"thread_id": "analytics-chatbot-thread"}}
|
| 22 |
try:
|
| 23 |
+
result=graph.invoke({'messages':user_query},config=config)
|
| 24 |
return {
|
| 25 |
'endpoint': result['endpoint'] ,
|
| 26 |
+
'parameters': result['parameters'],
|
| 27 |
+
'response': result['response'],
|
| 28 |
}
|
| 29 |
except Exception as e:
|
| 30 |
+
print(e)
|
check.py
CHANGED
|
@@ -1,13 +1,28 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
|
| 3 |
+
# Define the endpoint URL
|
| 4 |
+
url = "https://reveltrends.vercel.app/api/v1/overview/buzz_trend"
|
| 5 |
+
|
| 6 |
+
# Set the parameters
|
| 7 |
+
params = {
|
| 8 |
+
"influencer_username": "divyadhakal_",
|
| 9 |
+
"period": "weekly"
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
# Optional: headers if your API requires authentication
|
| 13 |
+
headers = {
|
| 14 |
+
"Authorization": "Bearer YOUR_API_KEY", # replace with your API key if needed
|
| 15 |
+
"Content-Type": "application/json"
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
# Make the GET request
|
| 19 |
+
response = requests.get(url, params=params, headers=headers)
|
| 20 |
+
|
| 21 |
+
# Check if the request was successful
|
| 22 |
+
if response.status_code == 200:
|
| 23 |
+
data = response.json()
|
| 24 |
+
print("Response data:", data)
|
| 25 |
+
else:
|
| 26 |
+
print("Request failed with status code:", response.status_code)
|
| 27 |
+
print("Response:", response.text)
|
| 28 |
+
|
influencers_names.csv
ADDED
|
File without changes
|
logs/access.log
CHANGED
|
@@ -858,3 +858,97 @@
|
|
| 858 |
2025-09-14 13:46:03,906 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 859 |
2025-09-14 13:48:22,944 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/ideation
|
| 860 |
2025-09-14 13:48:33,764 | INFO | access_logger | api/main.py:21 | Response status: 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 858 |
2025-09-14 13:46:03,906 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 859 |
2025-09-14 13:48:22,944 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/ideation
|
| 860 |
2025-09-14 13:48:33,764 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 861 |
+
2025-09-14 14:06:55,860 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 862 |
+
2025-09-14 14:07:02,506 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 863 |
+
2025-09-14 14:10:18,161 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 864 |
+
2025-09-14 14:10:19,706 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 865 |
+
2025-09-14 14:11:33,825 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 866 |
+
2025-09-14 14:12:57,320 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 867 |
+
2025-09-14 14:12:58,439 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 868 |
+
2025-09-14 14:13:32,016 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 869 |
+
2025-09-14 14:13:32,991 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 870 |
+
2025-09-14 14:14:55,545 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 871 |
+
2025-09-14 14:14:56,921 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 872 |
+
2025-09-14 14:15:46,695 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 873 |
+
2025-09-14 14:15:47,643 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 874 |
+
2025-09-14 14:16:34,927 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 875 |
+
2025-09-14 14:16:34,928 | INFO | access_logger | api/main.py:21 | Response status: 422
|
| 876 |
+
2025-09-14 14:16:53,658 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 877 |
+
2025-09-14 14:17:14,866 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 878 |
+
2025-09-14 14:17:15,866 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 879 |
+
2025-09-14 14:24:37,744 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 880 |
+
2025-09-14 14:24:57,166 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 881 |
+
2025-09-14 14:24:58,134 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 882 |
+
2025-09-14 14:48:07,497 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 883 |
+
2025-09-14 14:49:44,510 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 884 |
+
2025-09-14 14:50:03,540 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 885 |
+
2025-09-14 14:51:54,804 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 886 |
+
2025-09-14 14:51:56,724 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 887 |
+
2025-09-14 14:58:39,208 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 888 |
+
2025-09-14 14:58:50,893 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 889 |
+
2025-09-14 14:58:52,755 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 890 |
+
2025-09-14 15:02:23,989 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 891 |
+
2025-09-14 15:02:25,886 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 892 |
+
2025-09-14 16:12:20,650 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 893 |
+
2025-09-14 16:13:22,526 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 894 |
+
2025-09-14 16:13:24,397 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 895 |
+
2025-09-14 16:16:11,623 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 896 |
+
2025-09-14 16:16:13,615 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 897 |
+
2025-09-14 16:16:46,129 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 898 |
+
2025-09-14 16:16:47,902 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 899 |
+
2025-09-14 16:17:18,515 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 900 |
+
2025-09-14 16:17:20,217 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 901 |
+
2025-09-14 16:20:03,427 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 902 |
+
2025-09-14 16:20:05,403 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 903 |
+
2025-09-14 16:23:53,831 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 904 |
+
2025-09-14 16:24:11,407 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 905 |
+
2025-09-14 16:24:27,087 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 906 |
+
2025-09-14 16:24:28,806 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 907 |
+
2025-09-14 16:25:22,806 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 908 |
+
2025-09-14 16:25:31,353 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 909 |
+
2025-09-14 16:25:33,073 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 910 |
+
2025-09-14 16:25:38,124 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 911 |
+
2025-09-14 16:25:39,820 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 912 |
+
2025-09-14 16:25:43,024 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 913 |
+
2025-09-14 16:25:44,718 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 914 |
+
2025-09-14 16:25:52,387 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 915 |
+
2025-09-14 16:25:54,177 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 916 |
+
2025-09-14 16:25:57,607 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 917 |
+
2025-09-14 16:26:01,471 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 918 |
+
2025-09-14 16:26:05,007 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 919 |
+
2025-09-14 16:26:25,134 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 920 |
+
2025-09-14 16:26:31,508 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 921 |
+
2025-09-14 16:26:52,092 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 922 |
+
2025-09-14 16:26:55,507 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 923 |
+
2025-09-14 16:56:39,667 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 924 |
+
2025-09-14 16:56:46,464 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 925 |
+
2025-09-14 16:57:34,563 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 926 |
+
2025-09-14 16:57:36,571 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 927 |
+
2025-09-14 16:58:05,986 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 928 |
+
2025-09-14 16:58:07,727 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 929 |
+
2025-09-14 17:13:54,511 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 930 |
+
2025-09-14 17:16:56,160 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 931 |
+
2025-09-14 17:21:39,591 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 932 |
+
2025-09-14 17:23:08,674 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 933 |
+
2025-09-14 17:23:10,620 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 934 |
+
2025-09-14 17:25:59,934 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 935 |
+
2025-09-14 17:26:01,106 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 936 |
+
2025-09-14 17:26:21,335 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 937 |
+
2025-09-14 17:26:22,375 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 938 |
+
2025-09-14 17:31:42,930 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 939 |
+
2025-09-14 17:31:49,516 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 940 |
+
2025-09-14 17:34:56,409 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 941 |
+
2025-09-14 17:35:03,207 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 942 |
+
2025-09-14 17:36:06,569 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 943 |
+
2025-09-14 17:36:07,559 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 944 |
+
2025-09-14 17:36:22,964 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 945 |
+
2025-09-14 17:36:23,926 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 946 |
+
2025-09-14 17:37:09,384 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 947 |
+
2025-09-14 17:37:11,240 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 948 |
+
2025-09-14 17:37:51,364 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 949 |
+
2025-09-14 17:37:52,412 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 950 |
+
2025-09-14 17:37:56,650 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 951 |
+
2025-09-14 17:37:58,406 | INFO | access_logger | api/main.py:21 | Response status: 200
|
| 952 |
+
2025-09-14 17:44:12,219 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 953 |
+
2025-09-14 17:45:22,641 | INFO | access_logger | api/main.py:19 | Request: POST http://127.0.0.1:8000/api/analytics-chatbot
|
| 954 |
+
2025-09-14 17:45:29,855 | INFO | access_logger | api/main.py:21 | Response status: 200
|
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
|
| 5 |
|
| 6 |
class ChatbotAgent:
|
| 7 |
def __init__(self):
|
|
@@ -10,7 +10,11 @@ class ChatbotAgent:
|
|
| 10 |
def chatbot_graph(self):
|
| 11 |
graph_builder= StateGraph(State)
|
| 12 |
graph_builder.add_node("chatbot", ChatbotNode().run)
|
|
|
|
|
|
|
| 13 |
graph_builder.add_edge(START, "chatbot")
|
| 14 |
-
graph_builder.add_edge("chatbot",
|
|
|
|
|
|
|
| 15 |
|
| 16 |
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 ChatbotNode, FetchDataNode
|
| 5 |
|
| 6 |
class ChatbotAgent:
|
| 7 |
def __init__(self):
|
|
|
|
| 10 |
def chatbot_graph(self):
|
| 11 |
graph_builder= StateGraph(State)
|
| 12 |
graph_builder.add_node("chatbot", ChatbotNode().run)
|
| 13 |
+
graph_builder.add_node("fetch_data", FetchDataNode().run)
|
| 14 |
+
|
| 15 |
graph_builder.add_edge(START, "chatbot")
|
| 16 |
+
graph_builder.add_edge("chatbot", 'fetch_data')
|
| 17 |
+
graph_builder.add_edge("fetch_data", END)
|
| 18 |
+
|
| 19 |
|
| 20 |
return graph_builder.compile(checkpointer=self.memory)
|
src/genai/analytics_chatbot/utils/name_variations.json
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"divyadhakal_": [
|
| 3 |
+
"divya dhakal",
|
| 4 |
+
"divya",
|
| 5 |
+
"dhakal divya",
|
| 6 |
+
"dibya dhakal"
|
| 7 |
+
],
|
| 8 |
+
"ishtukarkee": [
|
| 9 |
+
"istu karki",
|
| 10 |
+
"istu karkee",
|
| 11 |
+
"ishtu karki",
|
| 12 |
+
"ishtu karkee",
|
| 13 |
+
"ishtoo karkee",
|
| 14 |
+
"ishtu",
|
| 15 |
+
"istu"
|
| 16 |
+
],
|
| 17 |
+
"kano_mama": [
|
| 18 |
+
"kano mama",
|
| 19 |
+
"kano",
|
| 20 |
+
"mama kano"
|
| 21 |
+
],
|
| 22 |
+
"munachiya": [
|
| 23 |
+
"muna chiya",
|
| 24 |
+
"muna",
|
| 25 |
+
"chiya muna"
|
| 26 |
+
],
|
| 27 |
+
"nepal_food": [
|
| 28 |
+
"nepal food",
|
| 29 |
+
"nepalfood",
|
| 30 |
+
"food nepal"
|
| 31 |
+
],
|
| 32 |
+
"ggkaam610": [
|
| 33 |
+
"ggkaam",
|
| 34 |
+
"ggkaam 610",
|
| 35 |
+
"gg kaam",
|
| 36 |
+
"gg_kaam"
|
| 37 |
+
],
|
| 38 |
+
"adars_fpv": [
|
| 39 |
+
"adarsh",
|
| 40 |
+
"adarsh fpv",
|
| 41 |
+
"adarsh_fpv"
|
| 42 |
+
],
|
| 43 |
+
"ajaytm43": [
|
| 44 |
+
"ajay tamang",
|
| 45 |
+
"ajay",
|
| 46 |
+
"tamang ajay"
|
| 47 |
+
],
|
| 48 |
+
"anishakafle": [
|
| 49 |
+
"anisha kafle",
|
| 50 |
+
"anisha",
|
| 51 |
+
"kafle anisha"
|
| 52 |
+
],
|
| 53 |
+
"diwasg": [
|
| 54 |
+
"diwash gurung",
|
| 55 |
+
"diwash",
|
| 56 |
+
"gurung diwash"
|
| 57 |
+
],
|
| 58 |
+
"eatgrubfood": [
|
| 59 |
+
"eat grub food",
|
| 60 |
+
"eatgrub",
|
| 61 |
+
"grubfood",
|
| 62 |
+
"grub food"
|
| 63 |
+
],
|
| 64 |
+
"imsurakshyakc": [
|
| 65 |
+
"surakshya kc",
|
| 66 |
+
"surakshya",
|
| 67 |
+
"kc surakshya"
|
| 68 |
+
],
|
| 69 |
+
"jholeyism": [
|
| 70 |
+
"jholey",
|
| 71 |
+
"jhule",
|
| 72 |
+
"jholay",
|
| 73 |
+
"jhuley"
|
| 74 |
+
],
|
| 75 |
+
"mrbvlog2": [
|
| 76 |
+
"mrb vlogs",
|
| 77 |
+
"mrb",
|
| 78 |
+
"mr b vlog"
|
| 79 |
+
]
|
| 80 |
+
}
|
src/genai/analytics_chatbot/utils/nodes.py
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
|
|
| 1 |
from langchain_core.messages import SystemMessage , HumanMessage , FunctionMessage
|
| 2 |
from .state import State
|
| 3 |
from .schemas import ResponseFormatter
|
| 4 |
from .prompts import chatbot_prompt
|
| 5 |
-
from .
|
| 6 |
from src.genai.utils.models_loader import llm_groq
|
| 7 |
|
| 8 |
class ChatbotNode:
|
|
@@ -10,12 +11,37 @@ class ChatbotNode:
|
|
| 10 |
self.llm = llm_groq
|
| 11 |
|
| 12 |
def run(self, state:State):
|
|
|
|
|
|
|
| 13 |
template = chatbot_prompt()
|
| 14 |
knowledge_base = generate_api_knowledge('https://reveltrends.vercel.app')
|
| 15 |
-
messages = [SystemMessage(content=template),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
result = self.llm.with_structured_output(ResponseFormatter).invoke(messages)
|
|
|
|
| 17 |
return {
|
| 18 |
"messages": [{"role": "assistant", "content": f'''The endpoint is: {result.endpoint}. The parameters are: {result.parameters}'''}],
|
| 19 |
"endpoint": result.endpoint,
|
| 20 |
"parameters": result.parameters,
|
| 21 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
from langchain_core.messages import SystemMessage , HumanMessage , FunctionMessage
|
| 3 |
from .state import State
|
| 4 |
from .schemas import ResponseFormatter
|
| 5 |
from .prompts import chatbot_prompt
|
| 6 |
+
from .utils import generate_api_knowledge
|
| 7 |
from src.genai.utils.models_loader import llm_groq
|
| 8 |
|
| 9 |
class ChatbotNode:
|
|
|
|
| 11 |
self.llm = llm_groq
|
| 12 |
|
| 13 |
def run(self, state:State):
|
| 14 |
+
print('Message:',state['messages'])
|
| 15 |
+
# state['messages'][-1].content = process_query(state['messages'][-1].content)
|
| 16 |
template = chatbot_prompt()
|
| 17 |
knowledge_base = generate_api_knowledge('https://reveltrends.vercel.app')
|
| 18 |
+
messages = [SystemMessage(content=template),
|
| 19 |
+
FunctionMessage(name='analytics_chatbot',content=str(knowledge_base)),
|
| 20 |
+
] + state["messages"]
|
| 21 |
+
if len(state['messages'])>11:
|
| 22 |
+
state["messages"] = state["messages"][-9:]
|
| 23 |
+
print('Messages:', state['messages'])
|
| 24 |
+
print(len(state['messages']))
|
| 25 |
result = self.llm.with_structured_output(ResponseFormatter).invoke(messages)
|
| 26 |
+
print(result)
|
| 27 |
return {
|
| 28 |
"messages": [{"role": "assistant", "content": f'''The endpoint is: {result.endpoint}. The parameters are: {result.parameters}'''}],
|
| 29 |
"endpoint": result.endpoint,
|
| 30 |
"parameters": result.parameters,
|
| 31 |
}
|
| 32 |
+
|
| 33 |
+
class FetchDataNode:
|
| 34 |
+
def __init__(self):
|
| 35 |
+
self.llm = llm_groq
|
| 36 |
+
self.base_url = 'https://reveltrends.vercel.app'
|
| 37 |
+
self.headers = {
|
| 38 |
+
"Authorization": "Bearer YOUR_API_KEY", # replace with your API key if needed
|
| 39 |
+
"Content-Type": "application/json"
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
def run(self, state:State):
|
| 43 |
+
url = f'''{self.base_url}{state['endpoint']}'''
|
| 44 |
+
response = requests.get(url, params=state['parameters'],headers=self.headers)
|
| 45 |
+
return {'response':response.json()}
|
| 46 |
+
|
| 47 |
+
|
src/genai/analytics_chatbot/utils/state.py
CHANGED
|
@@ -5,4 +5,6 @@ from langgraph.graph.message import add_messages
|
|
| 5 |
class State(TypedDict):
|
| 6 |
messages: Annotated[list, add_messages]
|
| 7 |
endpoint: str
|
| 8 |
-
parameters: dict
|
|
|
|
|
|
|
|
|
| 5 |
class State(TypedDict):
|
| 6 |
messages: Annotated[list, add_messages]
|
| 7 |
endpoint: str
|
| 8 |
+
parameters: dict
|
| 9 |
+
response:dict
|
| 10 |
+
error_message:str
|
src/genai/analytics_chatbot/utils/{api_knowledge.py → utils.py}
RENAMED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import requests
|
| 2 |
import json
|
|
|
|
| 3 |
|
| 4 |
def generate_api_knowledge(base_url: str):
|
| 5 |
"""
|
|
@@ -42,4 +43,45 @@ def generate_api_knowledge(base_url: str):
|
|
| 42 |
"description": description
|
| 43 |
})
|
| 44 |
|
| 45 |
-
return api_knowledge
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import requests
|
| 2 |
import json
|
| 3 |
+
import re
|
| 4 |
|
| 5 |
def generate_api_knowledge(base_url: str):
|
| 6 |
"""
|
|
|
|
| 43 |
"description": description
|
| 44 |
})
|
| 45 |
|
| 46 |
+
return api_knowledge
|
| 47 |
+
|
| 48 |
+
def influencers_name():
|
| 49 |
+
return {
|
| 50 |
+
"information_of_influencers_usernames": "From the names_dictionary, the key represents their original names and the value represents their usernames (influencer_username).",
|
| 51 |
+
"names_dictionary": {
|
| 52 |
+
"divya dhakal": "divyadhakal_",
|
| 53 |
+
"istu karki": "ishtukarkee",
|
| 54 |
+
"kano mama": "kano_mama",
|
| 55 |
+
"muna chiya": "munachiya",
|
| 56 |
+
"nepal food": "nepal_food",
|
| 57 |
+
"ggkaam": "ggkaam610",
|
| 58 |
+
"adarsh": "adars_fpv",
|
| 59 |
+
"ajay tamang": "ajaytm43",
|
| 60 |
+
"anisha kafle": "anishakafle",
|
| 61 |
+
"diwash gurung": "diwasg",
|
| 62 |
+
"eat grub food": "eatgrubfood",
|
| 63 |
+
"surakshya kc": "imsurakshyakc",
|
| 64 |
+
"jholey": "jholeyism",
|
| 65 |
+
"mrb vlogs": "mrbvlog2"
|
| 66 |
+
}
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
def process_query(user_query: str) -> str:
|
| 70 |
+
# load mapping from json file
|
| 71 |
+
with open("src/genai/analytics_chatbot/utils/name_variations.json", "r") as f:
|
| 72 |
+
variations = json.load(f)
|
| 73 |
+
|
| 74 |
+
processed = user_query
|
| 75 |
+
|
| 76 |
+
# Build reverse mapping: variation → username
|
| 77 |
+
expanded = {}
|
| 78 |
+
for username, names in variations.items():
|
| 79 |
+
for n in names:
|
| 80 |
+
expanded[n.lower()] = username
|
| 81 |
+
|
| 82 |
+
# Replace names in query
|
| 83 |
+
for name in sorted(expanded.keys(), key=len, reverse=True):
|
| 84 |
+
pattern = r"\b" + re.escape(name) + r"\b"
|
| 85 |
+
processed = re.sub(pattern, expanded[name], processed, flags=re.IGNORECASE)
|
| 86 |
+
|
| 87 |
+
return processed
|
src/genai/utils/__pycache__/models_loader.cpython-313.pyc
CHANGED
|
Binary files a/src/genai/utils/__pycache__/models_loader.cpython-313.pyc and b/src/genai/utils/__pycache__/models_loader.cpython-313.pyc differ
|
|
|
src/genai/utils/models_loader.py
CHANGED
|
@@ -17,7 +17,7 @@ embedding_model = OpenAIEmbeddings(model="text-embedding-3-small", dimensions=15
|
|
| 17 |
llm_anthropic = ChatAnthropic(model='claude-3-7-sonnet-latest', temperature=1)
|
| 18 |
llm_gemini = ChatGoogleGenerativeAI(model="gemini-1.5-flash")
|
| 19 |
llm_groq_openai = ChatGroq(model="openai/gpt-oss-120b",temperature=0.7)
|
| 20 |
-
llm_groq = ChatGroq(model="llama-3.
|
| 21 |
|
| 22 |
llm_gpt = ChatOpenAI(model="gpt-4o-mini",temperature=0.3)
|
| 23 |
llm_gpt_high = ChatOpenAI(model="gpt-4o-mini",temperature=0.5)
|
|
|
|
| 17 |
llm_anthropic = ChatAnthropic(model='claude-3-7-sonnet-latest', temperature=1)
|
| 18 |
llm_gemini = ChatGoogleGenerativeAI(model="gemini-1.5-flash")
|
| 19 |
llm_groq_openai = ChatGroq(model="openai/gpt-oss-120b",temperature=0.7)
|
| 20 |
+
llm_groq = ChatGroq(model="llama-3.1-8b-instant",temperature=0.7)
|
| 21 |
|
| 22 |
llm_gpt = ChatOpenAI(model="gpt-4o-mini",temperature=0.3)
|
| 23 |
llm_gpt_high = ChatOpenAI(model="gpt-4o-mini",temperature=0.5)
|