nicoladisabato commited on
Commit
848e3eb
·
1 Parent(s): 90dc313

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +79 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from urllib.parse import urlparse
3
+ from langchain.chat_models import ChatOpenAI
4
+ from langchain.embeddings import OpenAIEmbeddings
5
+ from langchain.vectorstores import FAISS
6
+ from langchain.chains import RetrievalQA
7
+ from langchain.prompts import PromptTemplate
8
+ from langchain.document_loaders import WebBaseLoader, AsyncHtmlLoader
9
+ from langchain.document_transformers import Html2TextTransformer
10
+ from langchain.callbacks import get_openai_callback
11
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
12
+
13
+ import asyncio
14
+ from langchain.docstore.document import Document
15
+
16
+ import os
17
+ from dotenv import load_dotenv
18
+
19
+
20
+ #loading openai api keys
21
+ load_dotenv()
22
+
23
+
24
+ st.title("🤖 Chat with your website 🤖")
25
+
26
+ input_url = st.text_input("Inserisci url:")
27
+
28
+ question = st.text_area("Chiedi pure:")
29
+
30
+ #generate the main prompt
31
+ prompt_template = """Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.
32
+
33
+
34
+ {context}
35
+
36
+ Question: {question}
37
+ Answer:
38
+ """
39
+
40
+ PROMPT = PromptTemplate(
41
+ template=prompt_template, input_variables=["context", "question"]
42
+ )
43
+
44
+ if st.button("Invia", type="primary"):
45
+
46
+ loader = AsyncHtmlLoader(input_url)
47
+ data = loader.load()
48
+ html2text = Html2TextTransformer()
49
+ docs_transformed = html2text.transform_documents(data)
50
+
51
+ text_splitter = RecursiveCharacterTextSplitter(
52
+ chunk_size = 2000,
53
+ chunk_overlap=200,
54
+ separators="\n"
55
+ )
56
+
57
+ docs = text_splitter.split_documents(docs_transformed)
58
+
59
+ openai_embeddings = OpenAIEmbeddings(model_name="gpt-3.5-turbo")
60
+
61
+ vectordb = FAISS.from_documents(
62
+ documents=docs,
63
+ embedding=openai_embeddings)
64
+
65
+ retriever = vectordb.as_retriever(search_kwargs={"k": 3})
66
+ llm = ChatOpenAI(model_name = "gpt-3.5-turbo", temperature=0)
67
+ relevant_docs = retriever.get_relevant_documents(question)
68
+ qa = RetrievalQA.from_chain_type(llm=llm, chain_type="stuff", retriever=retriever, chain_type_kwargs={"prompt": PROMPT})
69
+
70
+ # Write answer and sources
71
+ answer = st.empty()
72
+
73
+ with get_openai_callback() as cb:
74
+ #run the chain and generate response
75
+ response = qa(question)
76
+ print(cb)
77
+
78
+ answer.write(response['result'])
79
+ st.write(relevant_docs)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ langchain
2
+ openai
3
+ streamlit
4
+ python-dotenv