Spaces:
Sleeping
Sleeping
Commit ·
57efd84
1
Parent(s): dd6a9a1
restore
Browse files
app.py
CHANGED
|
@@ -1,61 +1,40 @@
|
|
| 1 |
from langchain.chat_models import ChatOpenAI
|
| 2 |
import gradio as gr
|
| 3 |
-
|
| 4 |
import os
|
|
|
|
| 5 |
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 6 |
from langchain.vectorstores import DeepLake
|
| 7 |
-
from langchain.text_splitter import CharacterTextSplitter
|
| 8 |
-
from langchain.document_loaders import SeleniumURLLoader
|
| 9 |
from langchain import PromptTemplate
|
| 10 |
from langchain import OpenAI
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def predict(query,history):
|
| 13 |
-
|
| 14 |
-
os.environ['OPENAI_API_KEY'] = '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'
|
| 15 |
-
os.environ["ACTIVELOOP_TOKEN"] = "eyJhbGciOiJIUzUxMiIsImlhdCI6MTY5NTE5MTAyNiwiZXhwIjoxNzU4MzQ5NDA3fQ.eyJpZCI6InV0a2Fyc2h0aXdhcmkifQ.PK_iz7uybeSmgqFvOYrICw-CQDbDY1aOjYhkMu-0Jle6gU33dCwxah7bmy39O0hPN4jYLu_RfLuU-XejyNvXrw"
|
| 16 |
-
|
| 17 |
-
llm = ChatOpenAI(temperature=1.0, model='gpt-3.5-turbo-0613')
|
| 18 |
-
|
| 19 |
-
# URLs of articles to scrape
|
| 20 |
-
urls = ["https://modelwise.ai/product/","https://modelwise.ai/category/blog-articles/","https://modelwise.ai/company/"]
|
| 21 |
-
|
| 22 |
-
# Load documents using Selenium
|
| 23 |
-
loader = SeleniumURLLoader(urls=urls)
|
| 24 |
-
docs_not_splitted = loader.load()
|
| 25 |
-
|
| 26 |
-
# Split documents into smaller chunks
|
| 27 |
-
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
|
| 28 |
-
docs = text_splitter.split_documents(docs_not_splitted)
|
| 29 |
-
|
| 30 |
-
# Create OpenAIEmbeddings instance
|
| 31 |
-
embeddings = OpenAIEmbeddings(model="text-embedding-ada-002")
|
| 32 |
-
|
| 33 |
-
# Specify your ActiveLoop organization ID
|
| 34 |
-
my_activeloop_org_id = "utkarshtiwari"
|
| 35 |
-
my_activeloop_dataset_name = "chatbot_modelwise"
|
| 36 |
-
dataset_path = f"hub://{my_activeloop_org_id}/{my_activeloop_dataset_name}"
|
| 37 |
-
db = DeepLake(dataset_path=dataset_path, embedding_function=embeddings)
|
| 38 |
-
|
| 39 |
-
# Add documents to the Deep Lake dataset
|
| 40 |
-
db.add_documents(docs)
|
| 41 |
-
|
| 42 |
template = """You are an exceptional customer support chatbot for the company Modelwise that gently answers questions related to the company.
|
| 43 |
-
|
| 44 |
You know the following context information.
|
| 45 |
-
|
| 46 |
{chunks_formatted}
|
| 47 |
-
|
| 48 |
-
Answer the following question from a customer. Use only information from the context. If you don't know the answer just ask the customer to contact Arnold and provide
|
| 49 |
-
|
| 50 |
Question: {query}
|
| 51 |
-
|
| 52 |
Answer:"""
|
| 53 |
-
|
| 54 |
# Create a PromptTemplate instance
|
| 55 |
prompt = PromptTemplate(
|
| 56 |
input_variables=["chunks_formatted", "query"],
|
| 57 |
template=template,
|
| 58 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
# Retrieve relevant chunks from the Knowledge Base
|
| 60 |
docs = db.similarity_search(query)
|
| 61 |
retrieved_chunks = [doc.page_content for doc in docs]
|
|
|
|
| 1 |
from langchain.chat_models import ChatOpenAI
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
import os
|
| 4 |
+
|
| 5 |
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 6 |
from langchain.vectorstores import DeepLake
|
|
|
|
|
|
|
| 7 |
from langchain import PromptTemplate
|
| 8 |
from langchain import OpenAI
|
| 9 |
+
from langchain.vectorstores import DeepLake
|
| 10 |
+
|
| 11 |
+
os.environ['OPENAI_API_KEY'] = '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'
|
| 12 |
+
os.environ["ACTIVELOOP_TOKEN"] = "eyJhbGciOiJIUzUxMiIsImlhdCI6MTY5NTE5MTAyNiwiZXhwIjoxNzU4MzQ5NDA3fQ.eyJpZCI6InV0a2Fyc2h0aXdhcmkifQ.PK_iz7uybeSmgqFvOYrICw-CQDbDY1aOjYhkMu-0Jle6gU33dCwxah7bmy39O0hPN4jYLu_RfLuU-XejyNvXrw"
|
| 13 |
|
| 14 |
def predict(query,history):
|
| 15 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
template = """You are an exceptional customer support chatbot for the company Modelwise that gently answers questions related to the company.
|
| 17 |
+
|
| 18 |
You know the following context information.
|
| 19 |
+
|
| 20 |
{chunks_formatted}
|
| 21 |
+
|
| 22 |
+
Answer the following question from a customer. Use only information from the context. If you don't know the answer just ask the customer to contact Arnold and provide his contact details. Do not make up any answer.
|
| 23 |
+
|
| 24 |
Question: {query}
|
| 25 |
+
|
| 26 |
Answer:"""
|
| 27 |
+
|
| 28 |
# Create a PromptTemplate instance
|
| 29 |
prompt = PromptTemplate(
|
| 30 |
input_variables=["chunks_formatted", "query"],
|
| 31 |
template=template,
|
| 32 |
)
|
| 33 |
+
|
| 34 |
+
my_activeloop_org_id = "utkarshtiwari"
|
| 35 |
+
my_activeloop_dataset_name = "chatbot_modelwise"
|
| 36 |
+
dataset_path = f"hub://{my_activeloop_org_id}/{my_activeloop_dataset_name}"
|
| 37 |
+
db = DeepLake(dataset_path=dataset_path, read_only=True)
|
| 38 |
# Retrieve relevant chunks from the Knowledge Base
|
| 39 |
docs = db.similarity_search(query)
|
| 40 |
retrieved_chunks = [doc.page_content for doc in docs]
|