new changes
This commit is contained in:
@@ -22,4 +22,19 @@ class SupaService:
|
||||
"url": url,
|
||||
"user_id": user_id
|
||||
}).execute()
|
||||
return result
|
||||
return result
|
||||
|
||||
|
||||
|
||||
def find_website(self, id: str, user_id: str):
|
||||
result = self.supabase.table("Website").select("*").eq("id", id).eq("user_id", user_id).execute()
|
||||
return result
|
||||
|
||||
|
||||
def get_user(self, jwt: str):
|
||||
try:
|
||||
result = self.supabase.auth.get_user(jwt)
|
||||
return result
|
||||
except:
|
||||
return None
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from models import ChatBody
|
||||
from models import ChatBody, ChatAppBody
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
from langchain.docstore.document import Document as LDocument
|
||||
@@ -14,21 +14,43 @@ from langchain.prompts.chat import (
|
||||
)
|
||||
from langchain.vectorstores import Chroma
|
||||
|
||||
from db.supa import SupaService
|
||||
|
||||
async def chat_extension_handler(body: ChatBody):
|
||||
|
||||
supabase = SupaService()
|
||||
|
||||
|
||||
|
||||
async def chat_app_handler(body: ChatAppBody, jwt: str):
|
||||
try:
|
||||
soup = BeautifulSoup(body.html, 'lxml')
|
||||
|
||||
iframe = soup.find('iframe', id='pageassist-iframe')
|
||||
if iframe:
|
||||
iframe.decompose()
|
||||
div = soup.find('div', id='pageassist-icon')
|
||||
if div:
|
||||
div.decompose()
|
||||
div = soup.find('div', id='__plasmo-loading__')
|
||||
if div:
|
||||
div.decompose()
|
||||
text = soup.get_text()
|
||||
|
||||
user = supabase.get_user(jwt)
|
||||
|
||||
if not user:
|
||||
return {
|
||||
"bot_response": "You are not logged in",
|
||||
"human_message": body.user_message,
|
||||
}
|
||||
|
||||
|
||||
user_id = user.user.id
|
||||
|
||||
|
||||
website_response = supabase.find_website(body.id, user_id)
|
||||
|
||||
website = website_response.data
|
||||
|
||||
if len(website) == 0:
|
||||
return {
|
||||
"bot_response": "Website not found",
|
||||
"human_message": body.user_message,
|
||||
}
|
||||
|
||||
website = website[0]
|
||||
|
||||
|
||||
text = website["html"]
|
||||
|
||||
result = [LDocument(page_content=text, metadata={"source": "test"})]
|
||||
token_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
|
||||
@@ -43,7 +65,76 @@ async def chat_extension_handler(body: ChatBody):
|
||||
messages = [
|
||||
SystemMessagePromptTemplate.from_template("""You are PageAssist bot. Use the following pieces of context from this webpage to answer the question from the user.
|
||||
If you don't know the answer, just say you don't know. DO NOT try to make up an answer.
|
||||
If user want recommendation, helping based on the context then help the user.
|
||||
If user want recommendation, help from the context, or any other information, please provide it.
|
||||
If the question is not related to the context, politely respond that you are tuned to only answer questions that are related to the context. Helpful answer in markdown:
|
||||
-----------------
|
||||
{context}
|
||||
"""),
|
||||
HumanMessagePromptTemplate.from_template("{question}")
|
||||
]
|
||||
|
||||
prompt = ChatPromptTemplate.from_messages(messages)
|
||||
|
||||
|
||||
chat = ConversationalRetrievalChain.from_llm(OpenAI(temperature=0, model_name="gpt-3.5-turbo"), vectorstore.as_retriever(search_kwargs={"k": 1}), return_source_documents=True, qa_prompt=prompt,)
|
||||
|
||||
history = [(d["human_message"], d["bot_response"]) for d in body.history]
|
||||
|
||||
response = chat({
|
||||
"question": body.user_message,
|
||||
"chat_history": history
|
||||
})
|
||||
|
||||
|
||||
answer = response["answer"]
|
||||
answer = answer[answer.find(":")+1:].strip()
|
||||
|
||||
|
||||
return {
|
||||
"bot_response": answer,
|
||||
"human_message": body.user_message,
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
print(e)
|
||||
return {
|
||||
"bot_response": "Something went wrong please try again later",
|
||||
"human_message": body.user_message,
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
async def chat_extension_handler(body: ChatBody):
|
||||
try:
|
||||
soup = BeautifulSoup(body.html, 'lxml')
|
||||
|
||||
iframe = soup.find('iframe', id='pageassist-iframe')
|
||||
if iframe:
|
||||
iframe.decompose()
|
||||
div = soup.find('div', id='pageassist-icon')
|
||||
if div:
|
||||
div.decompose()
|
||||
div = soup.find('div', id='__plasmo-loading__')
|
||||
if div:
|
||||
div.decompose()
|
||||
text = soup.get_text()
|
||||
|
||||
result = [LDocument(page_content=text, metadata={"source": "test"})]
|
||||
token_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
|
||||
doc = token_splitter.split_documents(result)
|
||||
|
||||
print(f'Number of documents: {len(doc)}')
|
||||
|
||||
|
||||
vectorstore = Chroma.from_documents(doc, OpenAIEmbeddings())
|
||||
|
||||
|
||||
messages = [
|
||||
SystemMessagePromptTemplate.from_template("""You are PageAssist bot. Use the following pieces of context from this webpage to answer the question from the user.
|
||||
If you don't know the answer, just say you don't know. DO NOT try to make up an answer.
|
||||
If user want recommendation, help from the context, or any other information, please provide it.
|
||||
If the question is not related to the context, politely respond that you are tuned to only answer questions that are related to the context. Helpful answer in markdown:
|
||||
-----------------
|
||||
{context}
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
from .chat import ChatBody
|
||||
from .chat import ChatBody, ChatAppBody
|
||||
from .user import UserValidation, SaveChatToApp
|
||||
@@ -5,3 +5,9 @@ class ChatBody(BaseModel):
|
||||
html: str
|
||||
history: list
|
||||
# url: str
|
||||
|
||||
class ChatAppBody(BaseModel):
|
||||
id: str
|
||||
user_message: str
|
||||
url: str
|
||||
history: list
|
||||
@@ -1,9 +1,14 @@
|
||||
from fastapi import APIRouter
|
||||
from models import ChatBody
|
||||
from handlers.chat import chat_extension_handler
|
||||
from fastapi import APIRouter, Header
|
||||
from models import ChatBody, ChatAppBody
|
||||
from handlers.chat import chat_extension_handler, chat_app_handler
|
||||
|
||||
router = APIRouter(prefix="/api/v1")
|
||||
|
||||
@router.post("/chat/chrome", tags=["chat"])
|
||||
async def chat_extension(body: ChatBody):
|
||||
return await chat_extension_handler(body)
|
||||
return await chat_extension_handler(body)
|
||||
|
||||
|
||||
@router.post("/chat/app", tags=["chat"])
|
||||
async def chat_app(body: ChatAppBody, x_auth_token: str = Header()):
|
||||
return await chat_app_handler(body, x_auth_token)
|
||||
Reference in New Issue
Block a user