Spaces:
Sleeping
Sleeping
initial commit
Browse files
app.py
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import random
|
| 3 |
+
import time
|
| 4 |
+
import openai
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
openai.api_type = os.environ['OPENAI_API_TYPE']
|
| 9 |
+
openai.api_key = os.environ['OPENAI_API_KEY']
|
| 10 |
+
openai.api_base = os.environ['OPENAI_API_BASE']
|
| 11 |
+
openai.api_version = os.environ['OPENAI_API_VERSION']
|
| 12 |
+
|
| 13 |
+
######################## Input TASK 1A ########################
|
| 14 |
+
from langchain.document_loaders import PyPDFLoader, OnlinePDFLoader
|
| 15 |
+
from langchain.embeddings.sentence_transformer import SentenceTransformerEmbeddings
|
| 16 |
+
from langchain.text_splitter import CharacterTextSplitter
|
| 17 |
+
|
| 18 |
+
linkToPDF = os.environ['ONLINE_PDF_URL']
|
| 19 |
+
|
| 20 |
+
loader = OnlinePDFLoader(linkToPDF)
|
| 21 |
+
documents = loader.load()
|
| 22 |
+
|
| 23 |
+
chuck_size = 1000
|
| 24 |
+
chuck_overlap = 200
|
| 25 |
+
text_splitter = CharacterTextSplitter(chunk_size=chuck_size, chunk_overlap=chuck_overlap)
|
| 26 |
+
docs = text_splitter.split_documents(documents)
|
| 27 |
+
###########################END OF TASK 1A ##################################
|
| 28 |
+
|
| 29 |
+
######################## Input TASK 1B ######3##############################
|
| 30 |
+
from langchain.vectorstores import Chroma
|
| 31 |
+
|
| 32 |
+
# create the open-source embedding function
|
| 33 |
+
embedding_function = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 34 |
+
|
| 35 |
+
# create simple ids - Index
|
| 36 |
+
ids = [str(i) for i in range(1, len(docs) + 1)]
|
| 37 |
+
|
| 38 |
+
# load it into Chroma
|
| 39 |
+
db = Chroma.from_documents(docs, embedding_function, ids=ids, collection_metadata={"hnsw:space": "cosine"} )
|
| 40 |
+
###########################END OF TASK 1B ##################################
|
| 41 |
+
|
| 42 |
+
###########################INPUT OF TASK 3B ##################################
|
| 43 |
+
# We try to limit the number of characters for the context
|
| 44 |
+
contextCharsLimit = 3072
|
| 45 |
+
promptStart = "Answer questions truthfully based on the information in sources provided below. \n If you cannot find the answer to a question based on the sources below, respond by saying “I apologize, but I am unable to provide an answer to your question, which is out of the scope of the document uploaded. Thank you! \n Sources:\n"
|
| 46 |
+
|
| 47 |
+
# We try to construct a function which can return the system prompt based on user query and fit in context into system prompt
|
| 48 |
+
def construct_system_prompt_chromadb (userQuery):
|
| 49 |
+
### find the number of relevant documents
|
| 50 |
+
docs = db.similarity_search(userQuery)
|
| 51 |
+
context = []
|
| 52 |
+
|
| 53 |
+
### append all relevant documents pagecontent into the context array
|
| 54 |
+
for match in docs:
|
| 55 |
+
context.append(match.page_content)
|
| 56 |
+
|
| 57 |
+
### loop for the context, check if the chars of context > limit, if not insert the pagecontent into the prompt with "-" or "\n" separator
|
| 58 |
+
for i in range(1, len(context)):
|
| 59 |
+
if len("-".join(context[:i])) >= contextCharsLimit:
|
| 60 |
+
responsePrompt = promptStart + "-".join(context[:i-1])
|
| 61 |
+
elif i == len(context)-1:
|
| 62 |
+
responsePrompt = promptStart + "-".join(context)
|
| 63 |
+
|
| 64 |
+
## return the response rpompt
|
| 65 |
+
return responsePrompt
|
| 66 |
+
########################### END OF TASK 3B ##################################
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
systemMessageContent = "" # System prompt we talked before - e.g., "You are a teaching assistant of a programming course CS1117. Try to answer student's question on python only"
|
| 71 |
+
systemMessage = {"role": "system", "content": systemMessageContent}
|
| 72 |
+
|
| 73 |
+
userMessageContent = "" # place holder
|
| 74 |
+
chatbotMessageContent = "" # place holder
|
| 75 |
+
|
| 76 |
+
temperature = 0.8
|
| 77 |
+
top_p = 0.95
|
| 78 |
+
max_tokens = 800
|
| 79 |
+
numOfHistory = 5 # Add in the number history windows here
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
with gr.Blocks() as simpleChatDemo:
|
| 84 |
+
inputMessages = gr.State([systemMessage])
|
| 85 |
+
# inputMessages.append(systemMessage)
|
| 86 |
+
|
| 87 |
+
# Chatbot interface
|
| 88 |
+
chatbot = gr.Chatbot()
|
| 89 |
+
|
| 90 |
+
# Message is a Text Box
|
| 91 |
+
msg = gr.Textbox()
|
| 92 |
+
|
| 93 |
+
# Clear Button on to clear up the msg and chatbot
|
| 94 |
+
clear = gr.ClearButton([msg, chatbot])
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
def respond(userMessageInput, inputMessagesHistory, chatbot_history):
|
| 98 |
+
|
| 99 |
+
## Construct the system message content based on the prompt function -- i.e., the input messages [0] will change based on system message now
|
| 100 |
+
systemMessageContent = construct_system_prompt_chromadb(userMessageInput) # Change the system content to the function
|
| 101 |
+
systemMessage = {"role": "system", "content": systemMessageContent}
|
| 102 |
+
inputMessagesHistory[0] = systemMessage
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
userMessageContent = userMessageInput
|
| 106 |
+
userMessage = {"role": "user", "content": userMessageContent}
|
| 107 |
+
|
| 108 |
+
inputMessagesHistory.append(userMessage)
|
| 109 |
+
|
| 110 |
+
if len(inputMessagesHistory) > numOfHistory + 1:
|
| 111 |
+
numOutstandingMessages = len(inputMessagesHistory) - (numOfHistory + 1)
|
| 112 |
+
inputMessagesHistory = [inputMessagesHistory[0], *inputMessagesHistory[1+numOutstandingMessages :]]
|
| 113 |
+
print(inputMessages)
|
| 114 |
+
completion = openai.ChatCompletion.create(engine="chatgpt", messages=inputMessagesHistory, temperature=temperature, top_p = top_p, max_tokens = max_tokens)
|
| 115 |
+
|
| 116 |
+
chatbotMessageContent = completion.choices[0].message.content
|
| 117 |
+
chatbotMessage = {"role": "assistant", "content": chatbotMessageContent }
|
| 118 |
+
inputMessagesHistory.append(chatbotMessage)
|
| 119 |
+
|
| 120 |
+
# chat history is main list of [(user message string, bot message string)]
|
| 121 |
+
chatbot_history.append((userMessageContent, chatbotMessageContent))
|
| 122 |
+
|
| 123 |
+
time.sleep(2)
|
| 124 |
+
# return with clear up the message box, and put the new messages into the chat_history
|
| 125 |
+
return "", inputMessagesHistory, chatbot_history,
|
| 126 |
+
|
| 127 |
+
# when the textbox click submit, i.e., enter, the function will be called (function, [input parameters], [output response])
|
| 128 |
+
msg.submit(respond, [msg, inputMessages, chatbot], [msg, inputMessages, chatbot])
|
| 129 |
+
|
| 130 |
+
simpleChatDemo.launch(share=True)
|