vip11017 commited on
Commit
52974f8
·
1 Parent(s): 9ba0e98

sovled problem about demo rag being deployed

Browse files
app/chatbot/demo_rag.py CHANGED
@@ -239,7 +239,6 @@ async def get_response(query, session_id, name, email, rag_config, config) -> di
239
  result['response'] = f"Error in processing chat: {e}"
240
 
241
  print(f"Response: {result['response']}")
242
-
243
  """log_chat(
244
  session_id=session_id,
245
  company_id=rag_config.get('company_id'),
 
239
  result['response'] = f"Error in processing chat: {e}"
240
 
241
  print(f"Response: {result['response']}")
 
242
  """log_chat(
243
  session_id=session_id,
244
  company_id=rag_config.get('company_id'),
app/chatbot/demo_routes.py CHANGED
@@ -2,6 +2,7 @@ from fastapi import APIRouter, HTTPException
2
  from pydantic import BaseModel
3
  from app.chatbot.demo_rag import get_response
4
  from app.config import demo_chatbot_configs, demo_form_submissions
 
5
 
6
  router = APIRouter()
7
 
@@ -11,8 +12,15 @@ class ChatInput(BaseModel):
11
  name: str
12
  email: str
13
 
 
 
 
 
 
 
 
14
 
15
- @router.post("/demo/{submission_id}")
16
  async def demo_chat(submission_id: str, input: ChatInput):
17
  # Lazy-load chatbot config by submission_id
18
  print(f"Got question: {input.question} for submission_id: {submission_id} and session_id: {input.session_id}")
@@ -40,7 +48,7 @@ async def demo_chat(submission_id: str, input: ChatInput):
40
  return {"answer": response.get("response", "")}
41
 
42
 
43
- @router.get("/demo/status/{submission_id}")
44
  async def demo_chatbot_status(submission_id: str):
45
  """
46
  Returns the per-stage progress of a demo chatbot based on the submission_id.
@@ -56,7 +64,7 @@ async def demo_chatbot_status(submission_id: str):
56
 
57
 
58
 
59
- @router.get("/demo/info/{submission_id}")
60
  async def demo_chatbot_info(submission_id: str):
61
  """
62
  Returns basic info about the demo chatbot.
@@ -72,4 +80,40 @@ async def demo_chatbot_info(submission_id: str):
72
  "chatbot_purpose": config.get("chatbot_purpose"),
73
  "sensitive_topics": config.get("sensitive_topics"),
74
 
75
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  from pydantic import BaseModel
3
  from app.chatbot.demo_rag import get_response
4
  from app.config import demo_chatbot_configs, demo_form_submissions
5
+ from app.chatbot.demo_rag import session_histories, HumanMessage, AIMessage, log_chat
6
 
7
  router = APIRouter()
8
 
 
12
  name: str
13
  email: str
14
 
15
+ #FAQ logging endpoint
16
+ class FAQInput(BaseModel):
17
+ session_id: str
18
+ question: str
19
+ answer: str
20
+ name: str
21
+ email: str
22
 
23
+ @router.post("/{submission_id}")
24
  async def demo_chat(submission_id: str, input: ChatInput):
25
  # Lazy-load chatbot config by submission_id
26
  print(f"Got question: {input.question} for submission_id: {submission_id} and session_id: {input.session_id}")
 
48
  return {"answer": response.get("response", "")}
49
 
50
 
51
+ @router.get("/status/{submission_id}")
52
  async def demo_chatbot_status(submission_id: str):
53
  """
54
  Returns the per-stage progress of a demo chatbot based on the submission_id.
 
64
 
65
 
66
 
67
+ @router.get("/info/{submission_id}")
68
  async def demo_chatbot_info(submission_id: str):
69
  """
70
  Returns basic info about the demo chatbot.
 
80
  "chatbot_purpose": config.get("chatbot_purpose"),
81
  "sensitive_topics": config.get("sensitive_topics"),
82
 
83
+ }
84
+
85
+ @router.post("/faq/{submission_id}")
86
+ async def log_faq(chatbot_id: str, faq: FAQInput):
87
+ print(f'{chatbot_id}')
88
+ session_id = faq.session_id
89
+ question = faq.question
90
+ answer = faq.answer
91
+ name = faq.name
92
+ email = faq.email
93
+
94
+ # Load RAG config
95
+ rag_config = demo_chatbot_configs.find_one({"chatbot_id": chatbot_id})
96
+ if not rag_config:
97
+ raise HTTPException(status_code=404, detail="Chatbot not found")
98
+
99
+ # Ensure session exists
100
+ if session_id not in session_histories:
101
+ session_histories[session_id] = []
102
+
103
+ # Append FAQ to session history
104
+ session_histories[session_id].append(HumanMessage(content=question))
105
+ session_histories[session_id].append(AIMessage(content=answer))
106
+
107
+ # Log to DB
108
+ log_chat(
109
+ session_id=session_id,
110
+ company_id=rag_config.get('company_id'),
111
+ chatbot_id=rag_config.get('chatbot_id'),
112
+ name=name,
113
+ email=email,
114
+ query=question,
115
+ answer=answer,
116
+ metadata={"source": "FAQ"}
117
+ )
118
+
119
+ return {"status": "success"}
app/chatbot/mongodb.py CHANGED
@@ -14,6 +14,7 @@ def log_chat(session_id: str, name: str, email: str, query: str, answer: str, la
14
  """
15
  Logs a chat interaction to the MongoDB 'ChatLogs' collection.
16
  """
 
17
  data = {
18
  "company_id": company_id,
19
  "chatbot_id": chatbot_id,
 
14
  """
15
  Logs a chat interaction to the MongoDB 'ChatLogs' collection.
16
  """
17
+ print("test")
18
  data = {
19
  "company_id": company_id,
20
  "chatbot_id": chatbot_id,
app/chatbot/prod_rag.py CHANGED
@@ -2,7 +2,6 @@
2
  from typing import List, TypedDict
3
 
4
  from langchain_core.messages import BaseMessage, HumanMessage, AIMessage
5
- from langchain_core.tools import tool
6
  from langchain_core.runnables import RunnableLambda
7
 
8
  from langchain_qdrant import QdrantVectorStore
@@ -234,8 +233,7 @@ async def get_response(query, session_id, name, email, rag_config, config) -> di
234
  result = {}
235
  result['response'] = f"Error in processing chat: {e}"
236
 
237
- print(f"Response: {result['response']}")
238
-
239
  log_chat(
240
  session_id=session_id,
241
  company_id=rag_config.get('company_id'),
 
2
  from typing import List, TypedDict
3
 
4
  from langchain_core.messages import BaseMessage, HumanMessage, AIMessage
 
5
  from langchain_core.runnables import RunnableLambda
6
 
7
  from langchain_qdrant import QdrantVectorStore
 
233
  result = {}
234
  result['response'] = f"Error in processing chat: {e}"
235
 
236
+ print(f"Responsjh: {result['response']}")
 
237
  log_chat(
238
  session_id=session_id,
239
  company_id=rag_config.get('company_id'),
app/chatbot/{prod_route.py → prod_routes.py} RENAMED
@@ -1,6 +1,6 @@
1
  from fastapi import APIRouter, HTTPException
2
  from pydantic import BaseModel
3
- from app.chatbot.demo_rag import get_response
4
  from app.config import prod_chatbot_configs
5
  from app.chatbot.prod_rag import session_histories, HumanMessage, AIMessage, log_chat
6
 
@@ -22,9 +22,8 @@ class FAQInput(BaseModel):
22
  email: str
23
 
24
 
25
- @router.post("/prod/{chatbot_id}")
26
  async def prod_chat(chatbot_id: str, input: ChatInput):
27
- # Lazy-load chatbot config
28
  print(f"got question: {input.question} for chatbot_id: {chatbot_id} and session_id: {input.session_id}")
29
 
30
  rag_config = prod_chatbot_configs.find_one({"chatbot_id": chatbot_id})
@@ -58,12 +57,9 @@ async def log_faq(chatbot_id: str, faq: FAQInput):
58
  email = faq.email
59
 
60
  # Load RAG config
61
- if chatbot_id not in chatbot_sessions:
62
- rag_config = prod_chatbot_configs.find_one({"chatbot_id": chatbot_id})
63
- if not rag_config:
64
- raise HTTPException(status_code=404, detail="Chatbot not found")
65
- chatbot_sessions[chatbot_id] = rag_config
66
- rag_config = chatbot_sessions[chatbot_id]
67
 
68
  # Ensure session exists
69
  if session_id not in session_histories:
@@ -72,7 +68,6 @@ async def log_faq(chatbot_id: str, faq: FAQInput):
72
  # Append FAQ to session history
73
  session_histories[session_id].append(HumanMessage(content=question))
74
  session_histories[session_id].append(AIMessage(content=answer))
75
-
76
  # Log to DB
77
  log_chat(
78
  session_id=session_id,
 
1
  from fastapi import APIRouter, HTTPException
2
  from pydantic import BaseModel
3
+ from app.chatbot.prod_rag import get_response
4
  from app.config import prod_chatbot_configs
5
  from app.chatbot.prod_rag import session_histories, HumanMessage, AIMessage, log_chat
6
 
 
22
  email: str
23
 
24
 
25
+ @router.post("/{chatbot_id}")
26
  async def prod_chat(chatbot_id: str, input: ChatInput):
 
27
  print(f"got question: {input.question} for chatbot_id: {chatbot_id} and session_id: {input.session_id}")
28
 
29
  rag_config = prod_chatbot_configs.find_one({"chatbot_id": chatbot_id})
 
57
  email = faq.email
58
 
59
  # Load RAG config
60
+ rag_config = prod_chatbot_configs.find_one({"chatbot_id": chatbot_id})
61
+ if not rag_config:
62
+ raise HTTPException(status_code=404, detail="Chatbot not found")
 
 
 
63
 
64
  # Ensure session exists
65
  if session_id not in session_histories:
 
68
  # Append FAQ to session history
69
  session_histories[session_id].append(HumanMessage(content=question))
70
  session_histories[session_id].append(AIMessage(content=answer))
 
71
  # Log to DB
72
  log_chat(
73
  session_id=session_id,
app/main.py CHANGED
@@ -2,7 +2,7 @@ from fastapi import FastAPI
2
  from fastapi.middleware.cors import CORSMiddleware
3
  from app.ingestion.routes import router as ingestion_router
4
  from app.chatbot.demo_routes import router as demo_router
5
- from app.chatbot.prod_route import router as prod_router
6
 
7
  app = FastAPI(
8
  title="Chatbot Platform - Demo Ingestion",
@@ -23,5 +23,5 @@ app.add_middleware(
23
  )
24
 
25
  app.include_router(ingestion_router, prefix="/ingestion", tags=["ingestion"])
26
- app.include_router(demo_router, prefix="/chatbot", tags=["demochatbot"])
27
- app.include_router(prod_router, prefix="/prodchatbot", tags=['production'])
 
2
  from fastapi.middleware.cors import CORSMiddleware
3
  from app.ingestion.routes import router as ingestion_router
4
  from app.chatbot.demo_routes import router as demo_router
5
+ from app.chatbot.prod_routes import router as prod_router
6
 
7
  app = FastAPI(
8
  title="Chatbot Platform - Demo Ingestion",
 
23
  )
24
 
25
  app.include_router(ingestion_router, prefix="/ingestion", tags=["ingestion"])
26
+ app.include_router(demo_router, prefix="/demo", tags=["demochatbot"])
27
+ app.include_router(prod_router, prefix="/prod", tags=['production'])
app/mongodb_functions.ipynb CHANGED
@@ -35,7 +35,7 @@
35
  },
36
  {
37
  "cell_type": "code",
38
- "execution_count": null,
39
  "id": "6392c44a",
40
  "metadata": {},
41
  "outputs": [],
@@ -56,7 +56,7 @@
56
  },
57
  {
58
  "cell_type": "code",
59
- "execution_count": null,
60
  "id": "2ac76531",
61
  "metadata": {},
62
  "outputs": [],
@@ -76,7 +76,7 @@
76
  },
77
  {
78
  "cell_type": "code",
79
- "execution_count": null,
80
  "id": "fbd14ade",
81
  "metadata": {},
82
  "outputs": [],
@@ -90,15 +90,85 @@
90
  },
91
  {
92
  "cell_type": "code",
93
- "execution_count": null,
94
  "id": "54d0d26b",
95
  "metadata": {},
96
- "outputs": [],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
  "source": [
98
  "demo = get_demo_config(\"vGZzRy8\")\n",
99
  "demo"
100
  ]
101
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  {
103
  "cell_type": "code",
104
  "execution_count": null,
@@ -157,6 +227,34 @@
157
  "override_demo_prompt(\"vGZzRy8\", template)"
158
  ]
159
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
  {
161
  "cell_type": "code",
162
  "execution_count": null,
 
35
  },
36
  {
37
  "cell_type": "code",
38
+ "execution_count": 3,
39
  "id": "6392c44a",
40
  "metadata": {},
41
  "outputs": [],
 
56
  },
57
  {
58
  "cell_type": "code",
59
+ "execution_count": 4,
60
  "id": "2ac76531",
61
  "metadata": {},
62
  "outputs": [],
 
76
  },
77
  {
78
  "cell_type": "code",
79
+ "execution_count": 5,
80
  "id": "fbd14ade",
81
  "metadata": {},
82
  "outputs": [],
 
90
  },
91
  {
92
  "cell_type": "code",
93
+ "execution_count": 6,
94
  "id": "54d0d26b",
95
  "metadata": {},
96
+ "outputs": [
97
+ {
98
+ "data": {
99
+ "text/plain": [
100
+ "{'_id': ObjectId('697689e6c8ece1ab25a744c3'),\n",
101
+ " 'submission_id': 'vGZzRy8',\n",
102
+ " 'chatbot_id': 'a3a86959-0f92-5124-8eb8-d3012f8b1ea3',\n",
103
+ " 'company_id': '80bd8dd8-1e20-5271-927a-02beab69abdd',\n",
104
+ " 'company_name': 'Auro Pharmaceuticals Inc.',\n",
105
+ " 'prompt_template': 'You are Auro-Chat, a wellness and skincare assistant for Auro Wellness and Skincare, here to answer any questions anyone has on wellness and skincare using the provided context from Auro Wellness\\'s websites which contained company-approved documents, blogs, and FAQs. Whenever they say \"you\" or refer to the chatbot always assume that means they are referring to the company, Auro.\\nSTRICT RULES YOU MUST FOLLOW:\\n1. If the Contextual Knowledge section is empty, say: \\n \"At the moment, I cannot answer this question. Please try rephrasing it or contact us via email at [info@aurowellness.com](mailto:info@aurowellness.com) or call us at [(562)-352-9630](tel:5623529630)\"\\n2. Do NOT use your own general knowledge. Only use information from the Contextual Knowledge.\\n3. You may ONLY reference product names, ingredients, routines, kits, bundles, benefits, or usage instructions that appear explicitly and word-for-word in the Contextual Knowledge.\\n4. If the user mentions a product, ingredient, routine step, or kit that does NOT appear in the Contextual Knowledge, you must NOT assume it exists and must respond with:\\n \"The provided information does not specify this specific product. Could you please try rephrasing the question\"\\n5. Conversation History may contain mistakes or unsupported claims. You MUST re-validate all information from history against the Contextual Knowledge before using it.\\n6. Do NOT include any special tokens such as <s>, </s>, [OUT], or any code-like answers\\n7. Do NOT include any links, URLs, references to external websites, or promotional content\\n8. Keep your answer in a **single paragraph or concise list**; do NOT add extra paragraphs\\n9. Keep your answers clear, human-readable, and concise (1-3 sentences)\\n10. You are NOT allowed to say transdermal; replace all instances of transdermal with topical\\n11. You cannot take actions such as signing users up, storing emails, notifying customers, modifying orders, or accessing accounts. \\n12. Any questions regarding retinol, scalp treatments, or the combination of our products with retinol must be directed to call or email us.\\n13. Any questions that mention children and its use of our products must be directed to call or email us.',\n",
106
+ " 'retrievers': [{'name': 'all',\n",
107
+ " 'collection': 'chatbot_e9ca4ef5-2c41-487b-bfe3-d2d0193250c5',\n",
108
+ " 'top_k': 25,\n",
109
+ " 'filter_score': 0.7}]}"
110
+ ]
111
+ },
112
+ "execution_count": 6,
113
+ "metadata": {},
114
+ "output_type": "execute_result"
115
+ }
116
+ ],
117
  "source": [
118
  "demo = get_demo_config(\"vGZzRy8\")\n",
119
  "demo"
120
  ]
121
  },
122
+ {
123
+ "cell_type": "code",
124
+ "execution_count": 8,
125
+ "id": "d84f9ee2",
126
+ "metadata": {},
127
+ "outputs": [
128
+ {
129
+ "data": {
130
+ "text/plain": [
131
+ "{'_id': ObjectId('697689e6c8ece1ab25a744c3'),\n",
132
+ " 'chatbot_id': 'a3a86959-0f92-5124-8eb8-d3012f8b1ea3',\n",
133
+ " 'company_id': '80bd8dd8-1e20-5271-927a-02beab69abdd',\n",
134
+ " 'company_name': 'Auro Pharmaceuticals Inc.',\n",
135
+ " 'prompt_template': '\\nYou are Auro-Chat, an assistant for Auro Pharmaceuticals.\\nAnswer ONLY using the provided context from Auro Pharmaceuticals\\'s approved content.\\n\\nSTRICT RULES:\\n1. If the Contextual Knowledge section is empty, say: \"Sorry, I cannot answer that question. Please call or email for further assistance. Information can be found on the website\"\\n2. Do NOT use your own general knowledge. Only reference the Contextual Knowledge.\\n3. Only reference topics explicitly allowed: Answer Customer Questions, Recommend Products or Services, Capture Leads (email, phone).\\n4. Do NOT discuss banned topics: Can not give medical advice.\\n5. Keep responses Friendly.\\n5. Keep the answers clear and concise in 1-3 sentences\\n',\n",
136
+ " 'retrievers': [{'name': 'retrieve_products',\n",
137
+ " 'collection': 'auro_product',\n",
138
+ " 'top_k': 5,\n",
139
+ " 'filter_score': 0.8},\n",
140
+ " {'name': 'retrieve_support',\n",
141
+ " 'collection': 'auro_support',\n",
142
+ " 'top_k': 5,\n",
143
+ " 'filter_score': 0.8},\n",
144
+ " {'name': 'retrieve_faqs',\n",
145
+ " 'collection': 'auro_faqs',\n",
146
+ " 'top_k': 5,\n",
147
+ " 'filter_score': 0.8},\n",
148
+ " {'name': 'retrieve_blogs',\n",
149
+ " 'collection': 'auro_blogs',\n",
150
+ " 'top_k': 5,\n",
151
+ " 'filter_score': 0.8},\n",
152
+ " {'name': 'retrieve_technology',\n",
153
+ " 'collection': 'auro_technology',\n",
154
+ " 'top_k': 5,\n",
155
+ " 'filter_score': 0.8},\n",
156
+ " {'name': 'retrieve_revolution',\n",
157
+ " 'collection': 'auro_revolution',\n",
158
+ " 'top_k': 5,\n",
159
+ " 'filter_score': 0.8}]}"
160
+ ]
161
+ },
162
+ "execution_count": 8,
163
+ "metadata": {},
164
+ "output_type": "execute_result"
165
+ }
166
+ ],
167
+ "source": [
168
+ "prod = get_prod_config(\"a3a86959-0f92-5124-8eb8-d3012f8b1ea3\")\n",
169
+ "prod"
170
+ ]
171
+ },
172
  {
173
  "cell_type": "code",
174
  "execution_count": null,
 
227
  "override_demo_prompt(\"vGZzRy8\", template)"
228
  ]
229
  },
230
+ {
231
+ "cell_type": "code",
232
+ "execution_count": 9,
233
+ "id": "07aee8cc",
234
+ "metadata": {},
235
+ "outputs": [],
236
+ "source": [
237
+ "template = \"\"\"You are Auro-Chat, a wellness and skincare assistant for Auro Wellness and Skincare, here to answer any questions anyone has on wellness and skincare using the provided context from Auro Wellness's websites which contained company-approved documents, blogs, and FAQs. Whenever they say \"you\" or refer to the chatbot always assume that means they are referring to the company, Auro.\n",
238
+ "STRICT RULES YOU MUST FOLLOW:\n",
239
+ "1. If the Contextual Knowledge section is empty, say: \n",
240
+ " \"At the moment, I cannot answer this question. Please try rephrasing it or contact us via email at [info@aurowellness.com](mailto:info@aurowellness.com) or call us at [(562)-352-9630](tel:5623529630)\"\n",
241
+ "2. Do NOT use your own general knowledge. Only use information from the Contextual Knowledge.\n",
242
+ "3. You may ONLY reference product names, ingredients, routines, kits, bundles, benefits, or usage instructions that appear explicitly and word-for-word in the Contextual Knowledge.\n",
243
+ "4. If the user mentions a product, ingredient, routine step, or kit that does NOT appear in the Contextual Knowledge, you must NOT assume it exists and must respond with:\n",
244
+ " \"The provided information does not specify this specific product. Could you please try rephrasing the question\"\n",
245
+ "5. Conversation History may contain mistakes or unsupported claims. You MUST re-validate all information from history against the Contextual Knowledge before using it.\n",
246
+ "6. Do NOT include any special tokens such as <s>, </s>, [OUT], or any code-like answers\n",
247
+ "7. Do NOT include any links, URLs, references to external websites, or promotional content\n",
248
+ "8. Keep your answer in a **single paragraph or concise list**; do NOT add extra paragraphs\n",
249
+ "9. Keep your answers clear, human-readable, and concise (1-3 sentences)\n",
250
+ "10. You are NOT allowed to say transdermal; replace all instances of transdermal with topical\n",
251
+ "11. You cannot take actions such as signing users up, storing emails, notifying customers, modifying orders, or accessing accounts. \n",
252
+ "12. Any questions regarding retinol, scalp treatments, or the combination of our products with retinol must be directed to call or email us.\n",
253
+ "13. Any questions that mention children and its use of our products must be directed to call or email us.\"\"\"\n",
254
+ "\n",
255
+ "update_prod_prompt(\"a3a86959-0f92-5124-8eb8-d3012f8b1ea3\", template)"
256
+ ]
257
+ },
258
  {
259
  "cell_type": "code",
260
  "execution_count": null,