Utkarsh-Tiwari commited on
Commit
9cd1987
·
1 Parent(s): 777edbe

gradio app chatbot

Browse files
Files changed (1) hide show
  1. app.py +75 -0
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ os.environ['OPENAI_API_KEY'] = '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'
13
+ os.environ["ACTIVELOOP_TOKEN"] = "eyJhbGciOiJIUzUxMiIsImlhdCI6MTY5NTE5MTAyNiwiZXhwIjoxNzU4MzQ5NDA3fQ.eyJpZCI6InV0a2Fyc2h0aXdhcmkifQ.PK_iz7uybeSmgqFvOYrICw-CQDbDY1aOjYhkMu-0Jle6gU33dCwxah7bmy39O0hPN4jYLu_RfLuU-XejyNvXrw"
14
+
15
+ llm = ChatOpenAI(temperature=1.0, model='gpt-3.5-turbo-0613')
16
+
17
+ # URLs of articles to scrape
18
+ urls = ["https://modelwise.ai/product/","https://modelwise.ai/category/blog-articles/","https://modelwise.ai/company/"]
19
+
20
+ # Load documents using Selenium
21
+ loader = SeleniumURLLoader(urls=urls)
22
+ docs_not_splitted = loader.load()
23
+
24
+ # Split documents into smaller chunks
25
+ text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
26
+ docs = text_splitter.split_documents(docs_not_splitted)
27
+
28
+ # Create OpenAIEmbeddings instance
29
+ embeddings = OpenAIEmbeddings(model="text-embedding-ada-002")
30
+
31
+ # Specify your ActiveLoop organization ID
32
+ my_activeloop_org_id = "utkarshtiwari"
33
+ my_activeloop_dataset_name = "chatbot_modelwise"
34
+ dataset_path = f"hub://{my_activeloop_org_id}/{my_activeloop_dataset_name}"
35
+ db = DeepLake(dataset_path=dataset_path, embedding_function=embeddings)
36
+
37
+ # Add documents to the Deep Lake dataset
38
+ db.add_documents(docs)
39
+
40
+ template = """You are an exceptional customer support chatbot for the company Modelwise that gently answers questions related to the company.
41
+
42
+ You know the following context information.
43
+
44
+ {chunks_formatted}
45
+
46
+ 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.
47
+
48
+ Question: {query}
49
+
50
+ Answer:"""
51
+
52
+ # Create a PromptTemplate instance
53
+ prompt = PromptTemplate(
54
+ input_variables=["chunks_formatted", "query"],
55
+ template=template,
56
+ )
57
+
58
+ def predict(query):
59
+
60
+ # Retrieve relevant chunks from the Knowledge Base
61
+ docs = db.similarity_search(query)
62
+ retrieved_chunks = [doc.page_content for doc in docs]
63
+
64
+ # Format the prompt with retrieved chunks and user query
65
+ chunks_formatted = "\n\n".join(retrieved_chunks)
66
+ prompt_formatted = prompt.format(chunks_formatted=chunks_formatted, query=query)
67
+ # Create an OpenAI instance for text generation
68
+ llm = OpenAI(model="text-davinci-003", temperature=0)
69
+
70
+ # Generate the answer using GPT-3
71
+ answer = llm(prompt_formatted)
72
+ print(answer)
73
+ return answer
74
+
75
+ gr.ChatInterface(predict).launch()