-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
100 lines (76 loc) · 3.19 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from dotenv import load_dotenv
import os
import requests
from bs4 import BeautifulSoup
import streamlit as st
from langchain.text_splitter import CharacterTextSplitter
from langchain.embeddings.openai import OpenAIEmbeddings
from langchain import FAISS
from langchain.chains.question_answering import load_qa_chain
from langchain.llms import OpenAI
from langchain.callbacks import get_openai_callback
from streamlit_chat import message
# Load environment variables
load_dotenv()
def get_conversation_string():
conversation_string = ""
for i in range(len(st.session_state['responses']) - 1):
conversation_string += "Human: " + st.session_state['requests'][i] + "\n"
conversation_string += "Bot: " + st.session_state['responses'][i + 1] + "\n"
return conversation_string
def extract_text_from_url(url):
# Fetch the content from the URL
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
# Extract text content from the webpage
text = ' '.join([p.get_text() for p in soup.find_all('p')])
return text
def process_text(text):
# Split the text into chunks using langchain
text_splitter = CharacterTextSplitter(
separator="\n",
chunk_size=1000,
chunk_overlap=200,
length_function=len
)
chunks = text_splitter.split_text(text)
# Convert the chunks of text into embeddings to form a knowledge base
embeddings = OpenAIEmbeddings()
knowledgeBase = FAISS.from_texts(chunks, embeddings)
return knowledgeBase
def main():
st.title("IntelliProbe 🪄")
confluence_link = st.text_input('Paste the URL of the Confluence Page:')
if 'responses' not in st.session_state:
st.session_state['responses'] = ["How can I assist you?"]
if 'requests' not in st.session_state:
st.session_state['requests'] = []
# container for chat history
response_container = st.container()
# container for text box
text_container = st.container()
if confluence_link:
extracted_texts = extract_text_from_url(confluence_link)
# Create the knowledge base object
knowledgeBase = process_text(extracted_texts)
with text_container:
query = st.text_input("Query: ", key="input")
if query:
#FAISS similarity search
docs = knowledgeBase.similarity_search(query)
llm = OpenAI()
chain = load_qa_chain(llm, chain_type='stuff')
with st.spinner("typing..."):
with get_openai_callback() as cost:
response = chain.run(input_documents=docs, question=query)
print(cost)
st.session_state.requests.append(query)
st.session_state.responses.append(response)
with response_container:
if st.session_state['responses']:
for i in range(len(st.session_state['responses'])):
message(st.session_state['responses'][i], key=str(i))
if i < len(st.session_state['requests']):
message(st.session_state["requests"][i], is_user=True, key=str(i) + '_user')
if __name__ == "__main__":
main()