-
Notifications
You must be signed in to change notification settings - Fork 0
/
27.py
164 lines (140 loc) · 6.03 KB
/
27.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import os
import streamlit as st
from langchain import LLMChain
from langchain.chat_models import ChatOpenAI
from langchain.prompts.chat import (ChatPromptTemplate,
HumanMessagePromptTemplate,
SystemMessagePromptTemplate)
from langchain.document_loaders import *
from langchain.chains.summarize import load_summarize_chain
import tempfile
from langchain.docstore.document import Document
import time
from langchain.memory import ConversationBufferMemory
from langchain.chains.question_answering import load_qa_chain
from langchain.prompts import PromptTemplate
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
openai_api_key = st.sidebar.text_input(
"OpenAI API Key",
placeholder="sk-...",
value=os.getenv("OPENAI_API_KEY", ""),
type="password",
)
st.title('MarketinGPT')
#Get the Google Drive folder path from the user
Google_Drive_folder = st.text_input("Enter your Google Drive folder path")
#Load the contents of the Google Drive folder
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
def load_folder_contents(folder_id):
creds = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('drive', 'v3', credentials=creds)
results = service.files().list(
pageSize=10, fields="nextPageToken, files(id, name)", q="'{}' in parents".format(folder_id)).execute()
items = results.get('files', [])
if not items:
print('No files found.')
else:
print('Files:')
for item in items:
print(u'{0} ({1})'.format(item['name'], item['id']))
if Google_Drive_folder:
knowledge_base_doc = load_folder_contents(Google_Drive_folder)
else:
knowledge_base_doc = ''
#Convert the Document to string content
knowledge_base_str = "".join([doc.page_content for doc in knowledge_base_doc])
#Index the contents into a vector database
# Importing required libraries
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
def index_contents(knowledge_base_str):
# Initialize the TfidfVectorizer
vectorizer = TfidfVectorizer()
# Fit and transform the data
vector_database = vectorizer.fit_transform([knowledge_base_str])
return vector_database
# Invoke the function using the provided arguments
vector_database = index_contents(knowledge_base_str)
#Get the OpenAI API key from the user
OpenAI_API_key = st.text_input("Enter your OpenAI API key")
#Get the message from the user
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
if message := st.chat_input("Type your message"):
with st.chat_message("user"):
st.markdown(message)
st.session_state.messages.append({"role": "user", "content": message})
#Search the vector database for relevant information related to the message
# Importing required libraries
from sklearn.feature_extraction.text import CountVectorizer
def search_database(message, vector_database):
# Initialize the CountVectorizer
count_vectorizer = CountVectorizer()
# Fit and transform the data
message_vector = count_vectorizer.fit_transform([message])
# Calculate the cosine similarity
similarity_scores = cosine_similarity(message_vector, vector_database)
# Get the index of the most similar document
most_similar_index = np.argmax(similarity_scores)
# Return the most similar document
return vector_database[most_similar_index]
# Invoke the function using the provided arguments
relevant_info = search_database(message, vector_database)
#Send the relevant information along with the message to the LLM
def send_info_to_LLM(message,relevant_info,OpenAI_API_key):
prompt = PromptTemplate(
input_variables=['chat_history', 'message', 'relevant_info', 'OpenAI_API_key'], template='''You are a chatbot having a conversation with a human. You are supposed to send the relevant information along with the message to the LLM.
Relevant Information: {relevant_info}
{chat_history}
Human: {message}
Chatbot:'''
)
memory = ConversationBufferMemory(memory_key="chat_history", input_key="message")
llm = ChatOpenAI(model_name="gpt-3.5-turbo-16k", openai_api_key=openai_api_key, temperature=0)
chat_llm_chain = LLMChain(
llm=llm,
prompt=prompt,
verbose=False,
memory=memory,
)
return chat_llm_chain
if not openai_api_key.startswith('sk-'):
st.warning('Please enter your OpenAI API key!', icon='⚠')
response = ""
elif message and relevant_info and OpenAI_API_key:
if 'chat_llm_chain' not in st.session_state:
st.session_state.chat_llm_chain = send_info_to_LLM(message,relevant_info,OpenAI_API_key)
response = st.session_state.chat_llm_chain.run(message=message, relevant_info=relevant_info, OpenAI_API_key=OpenAI_API_key)
else:
response = ""
#Display the response to the user with chat interface
with st.chat_message("assistant"):
message_placeholder = st.empty()
full_response = ""
# Simulate stream of response with milliseconds delay
for chunk in response.split():
full_response += chunk + " "
time.sleep(0.05)
# Add a blinking cursor to simulate typing
message_placeholder.markdown(full_response + "▌")
message_placeholder.markdown(full_response)
# Add assistant response to chat history
if full_response:
st.session_state.messages.append({"role": "assistant", "content": full_response})