Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,14 @@
|
|
| 1 |
-
#
|
|
|
|
| 2 |
|
| 3 |
# Basic Imports & Setup
|
| 4 |
import os
|
| 5 |
-
from
|
| 6 |
|
| 7 |
# Using Chainlit for our UI
|
| 8 |
import chainlit as cl
|
| 9 |
from chainlit.prompt import Prompt, PromptMessage
|
|
|
|
| 10 |
|
| 11 |
# Getting the API key from the .env file
|
| 12 |
from dotenv import load_dotenv
|
|
@@ -22,12 +24,13 @@ file_id = "1UQnaQjBKKyWAiLdr6UlwSJovOp9zDdxr"
|
|
| 22 |
# file_id = "12cvKg19CJf-wt98q5sPJctjp5fW-nsh6" //Used for MLOps Meetup
|
| 23 |
direct_url = f"https://drive.google.com/uc?export=download&id={file_id}"
|
| 24 |
|
|
|
|
| 25 |
# Now load the document using the direct URL
|
| 26 |
docs = PyMuPDFLoader(direct_url).load()
|
| 27 |
|
| 28 |
import tiktoken
|
| 29 |
def tiktoken_len(text):
|
| 30 |
-
tokens = tiktoken.encoding_for_model("
|
| 31 |
text,
|
| 32 |
)
|
| 33 |
return len(tokens)
|
|
@@ -36,20 +39,20 @@ def tiktoken_len(text):
|
|
| 36 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 37 |
|
| 38 |
text_splitter = RecursiveCharacterTextSplitter(
|
| 39 |
-
chunk_size=500, # 500 tokens per chunk, experiment with this value
|
| 40 |
-
chunk_overlap=50, # 50 tokens overlap between chunks, experiment with this value
|
| 41 |
-
length_function=tiktoken_len,
|
| 42 |
)
|
| 43 |
|
| 44 |
split_chunks = text_splitter.split_documents(docs)
|
| 45 |
|
| 46 |
# Load the embeddings model
|
| 47 |
-
from
|
| 48 |
|
| 49 |
-
embedding_model =
|
| 50 |
|
| 51 |
# Load the vector store and retriever from Qdrant
|
| 52 |
-
from
|
| 53 |
|
| 54 |
qdrant_vectorstore = Qdrant.from_documents(
|
| 55 |
split_chunks,
|
|
@@ -60,12 +63,10 @@ qdrant_vectorstore = Qdrant.from_documents(
|
|
| 60 |
|
| 61 |
qdrant_retriever = qdrant_vectorstore.as_retriever()
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
model = AutoModelForCausalLM.from_pretrained("Upstage/SOLAR-10.7B-v1.0")
|
| 66 |
|
| 67 |
-
|
| 68 |
-
from langchain.prompts import ChatPromptTemplate
|
| 69 |
|
| 70 |
RAG_PROMPT = """
|
| 71 |
SYSTEM:
|
|
@@ -77,15 +78,15 @@ When to talk with the user about conferences, it can be a "transactional convers
|
|
| 77 |
Here is an example of a transactional conversation:
|
| 78 |
User: When is the conference?
|
| 79 |
You: The conference is on June 1st, 2024. What else would you like to know?
|
| 80 |
-
It can also be a chain of questions and answers where you and the user
|
| 81 |
Here is an example of a transactional conversation:
|
| 82 |
User: What sessions should I attend?
|
| 83 |
You: You should attend the keynote session by Bono. Would you like to know more?
|
| 84 |
User: Yes
|
| 85 |
You: The keynote session by Bono is on June 1st, 2024. What else would you like?
|
| 86 |
-
If asked a question about sessions, you can provide detailed information about the session.
|
| 87 |
If there are multiple sessions, you can provide information about each session.
|
| 88 |
-
The format of session
|
| 89 |
Title:
|
| 90 |
Description:
|
| 91 |
Speaker:
|
|
@@ -97,15 +98,15 @@ CONTEXT:
|
|
| 97 |
{context}
|
| 98 |
QUERY:
|
| 99 |
{question}
|
| 100 |
-
ALL
|
| 101 |
Most questions are about the date, location, and purpose of the conference.
|
| 102 |
You may be asked for fine details about the conference regarding the speakers, sponsors, and attendees.
|
| 103 |
You are capable of looking up information and providing detailed responses.
|
| 104 |
When asked a question about a conference, you should provide a detailed response.
|
| 105 |
After completing your response, you should ask the user if they would like to know more about the conference by asking "Hope that helps".
|
| 106 |
-
If the user says "yes", you should provide more information about the conference. If the user says "no", you should say "Goodbye!
|
| 107 |
If you are asked a question about Cher, you should respond with "Rock on With Your Bad Self!".
|
| 108 |
-
You guess if you do not have
|
| 109 |
"""
|
| 110 |
rag_prompt = ChatPromptTemplate.from_template(RAG_PROMPT)
|
| 111 |
|
|
@@ -116,14 +117,14 @@ from langchain.schema.runnable import RunnablePassthrough
|
|
| 116 |
retrieval_augmented_qa_chain = (
|
| 117 |
{"context": itemgetter("question") | qdrant_retriever, "question": itemgetter("question")}
|
| 118 |
| RunnablePassthrough.assign(context=itemgetter("context"))
|
| 119 |
-
| {"response": rag_prompt |
|
| 120 |
)
|
| 121 |
|
| 122 |
# Chainlit App
|
| 123 |
@cl.on_chat_start
|
| 124 |
async def start_chat():
|
| 125 |
settings = {
|
| 126 |
-
"model": "
|
| 127 |
"temperature": 0,
|
| 128 |
"max_tokens": 500,
|
| 129 |
"top_p": 1,
|
|
@@ -135,9 +136,9 @@ async def start_chat():
|
|
| 135 |
@cl.on_message
|
| 136 |
async def main(message: cl.Message):
|
| 137 |
chainlit_question = message.content
|
|
|
|
| 138 |
response = retrieval_augmented_qa_chain.invoke({"question": chainlit_question})
|
| 139 |
chainlit_answer = response["response"].content
|
| 140 |
|
| 141 |
msg = cl.Message(content=chainlit_answer)
|
| 142 |
-
await msg.send()
|
| 143 |
-
|
|
|
|
| 1 |
+
# AI MAKERSPACE PREPR
|
| 2 |
+
# Date: 2024-5-16
|
| 3 |
|
| 4 |
# Basic Imports & Setup
|
| 5 |
import os
|
| 6 |
+
from openai import AsyncOpenAI
|
| 7 |
|
| 8 |
# Using Chainlit for our UI
|
| 9 |
import chainlit as cl
|
| 10 |
from chainlit.prompt import Prompt, PromptMessage
|
| 11 |
+
from chainlit.playground.providers import ChatOpenAI
|
| 12 |
|
| 13 |
# Getting the API key from the .env file
|
| 14 |
from dotenv import load_dotenv
|
|
|
|
| 24 |
# file_id = "12cvKg19CJf-wt98q5sPJctjp5fW-nsh6" //Used for MLOps Meetup
|
| 25 |
direct_url = f"https://drive.google.com/uc?export=download&id={file_id}"
|
| 26 |
|
| 27 |
+
|
| 28 |
# Now load the document using the direct URL
|
| 29 |
docs = PyMuPDFLoader(direct_url).load()
|
| 30 |
|
| 31 |
import tiktoken
|
| 32 |
def tiktoken_len(text):
|
| 33 |
+
tokens = tiktoken.encoding_for_model("gpt-3.5-turbo").encode(
|
| 34 |
text,
|
| 35 |
)
|
| 36 |
return len(tokens)
|
|
|
|
| 39 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 40 |
|
| 41 |
text_splitter = RecursiveCharacterTextSplitter(
|
| 42 |
+
chunk_size = 500, # 500 tokens per chunk, experiment with this value
|
| 43 |
+
chunk_overlap = 50, # 50 tokens overlap between chunks, experiment with this value
|
| 44 |
+
length_function = tiktoken_len,
|
| 45 |
)
|
| 46 |
|
| 47 |
split_chunks = text_splitter.split_documents(docs)
|
| 48 |
|
| 49 |
# Load the embeddings model
|
| 50 |
+
from langchain_openai.embeddings import OpenAIEmbeddings
|
| 51 |
|
| 52 |
+
embedding_model = OpenAIEmbeddings(model="text-embedding-3-small")
|
| 53 |
|
| 54 |
# Load the vector store and retriever from Qdrant
|
| 55 |
+
from langchain_community.vectorstores import Qdrant
|
| 56 |
|
| 57 |
qdrant_vectorstore = Qdrant.from_documents(
|
| 58 |
split_chunks,
|
|
|
|
| 63 |
|
| 64 |
qdrant_retriever = qdrant_vectorstore.as_retriever()
|
| 65 |
|
| 66 |
+
from langchain_openai import ChatOpenAI
|
| 67 |
+
openai_chat_model = ChatOpenAI(model="gpt-3.5-turbo")
|
|
|
|
| 68 |
|
| 69 |
+
from langchain_core.prompts import ChatPromptTemplate
|
|
|
|
| 70 |
|
| 71 |
RAG_PROMPT = """
|
| 72 |
SYSTEM:
|
|
|
|
| 78 |
Here is an example of a transactional conversation:
|
| 79 |
User: When is the conference?
|
| 80 |
You: The conference is on June 1st, 2024. What else would you like to know?
|
| 81 |
+
It can also be a chain of questions and answers where you and the user continues the chain until they say "Got it".
|
| 82 |
Here is an example of a transactional conversation:
|
| 83 |
User: What sessions should I attend?
|
| 84 |
You: You should attend the keynote session by Bono. Would you like to know more?
|
| 85 |
User: Yes
|
| 86 |
You: The keynote session by Bono is on June 1st, 2024. What else would you like?
|
| 87 |
+
If asked a question about a sessions, you can provide detailed information about the session.
|
| 88 |
If there are multiple sessions, you can provide information about each session.
|
| 89 |
+
The format of session related replies is:
|
| 90 |
Title:
|
| 91 |
Description:
|
| 92 |
Speaker:
|
|
|
|
| 98 |
{context}
|
| 99 |
QUERY:
|
| 100 |
{question}
|
| 101 |
+
ALL ANWSERS MUST COME FROM THE INCLUDE DOCUMENTS AND NOT FROM INTERNET OR FROM AI (do not make up an answer). If you can't reply, say: dunno, look it up yourself, bozo.
|
| 102 |
Most questions are about the date, location, and purpose of the conference.
|
| 103 |
You may be asked for fine details about the conference regarding the speakers, sponsors, and attendees.
|
| 104 |
You are capable of looking up information and providing detailed responses.
|
| 105 |
When asked a question about a conference, you should provide a detailed response.
|
| 106 |
After completing your response, you should ask the user if they would like to know more about the conference by asking "Hope that helps".
|
| 107 |
+
If the user says "yes", you should provide more information about the conference. If the user says "no", you should say "Goodbye! or ask if they would like to provide feedback.
|
| 108 |
If you are asked a question about Cher, you should respond with "Rock on With Your Bad Self!".
|
| 109 |
+
You guess if you do not have a answer, but you must preface the repsonse with: "I might be guessing, but ..."
|
| 110 |
"""
|
| 111 |
rag_prompt = ChatPromptTemplate.from_template(RAG_PROMPT)
|
| 112 |
|
|
|
|
| 117 |
retrieval_augmented_qa_chain = (
|
| 118 |
{"context": itemgetter("question") | qdrant_retriever, "question": itemgetter("question")}
|
| 119 |
| RunnablePassthrough.assign(context=itemgetter("context"))
|
| 120 |
+
| {"response": rag_prompt | openai_chat_model, "context": itemgetter("context")}
|
| 121 |
)
|
| 122 |
|
| 123 |
# Chainlit App
|
| 124 |
@cl.on_chat_start
|
| 125 |
async def start_chat():
|
| 126 |
settings = {
|
| 127 |
+
"model": "gpt-3.5-turbo",
|
| 128 |
"temperature": 0,
|
| 129 |
"max_tokens": 500,
|
| 130 |
"top_p": 1,
|
|
|
|
| 136 |
@cl.on_message
|
| 137 |
async def main(message: cl.Message):
|
| 138 |
chainlit_question = message.content
|
| 139 |
+
#chainlit_question = "What was the total value of 'Cash and cash equivalents' as of December 31, 2023?"
|
| 140 |
response = retrieval_augmented_qa_chain.invoke({"question": chainlit_question})
|
| 141 |
chainlit_answer = response["response"].content
|
| 142 |
|
| 143 |
msg = cl.Message(content=chainlit_answer)
|
| 144 |
+
await msg.send()
|
|
|