whymath commited on
Commit
7291483
·
1 Parent(s): c6af435

Updating prompt and RAG pipeline for default virtual student behavior

Browse files
Files changed (1) hide show
  1. utils.py +22 -22
utils.py CHANGED
@@ -30,40 +30,40 @@ def chunk_documents(docs, tiktoken_len):
30
 
31
  def create_raqa_chain_from_docs():
32
 
33
- # Load the documents from a PDF file using PyMuPDFLoader
34
- docs = PyMuPDFLoader("https://d18rn0p25nwr6d.cloudfront.net/CIK-0001326801/c7318154-f6ae-4866-89fa-f0c589f2ee3d.pdf").load() # TODO: Update this to enable user to upload PDF
35
- print("Loaded", len(docs), "documents")
36
- print(docs[0])
37
 
38
- # Create a Qdrant vector store from the split chunks and embedding model, and obtain its retriever
39
- split_chunks = chunk_documents(docs, tiktoken_len)
40
- embedding_model = OpenAIEmbeddings(model="text-embedding-3-small")
41
- qdrant_vectorstore = Qdrant.from_documents(
42
- split_chunks,
43
- embedding_model,
44
- location=":memory:",
45
- collection_name="LoadedPDF",
46
- )
47
- qdrant_retriever = qdrant_vectorstore.as_retriever()
48
 
49
  # Define the RAG prompt template
50
  RAG_PROMPT = """
51
- CONTEXT:
52
- {context}
53
 
54
- QUERY:
55
  {question}
56
-
57
- Use the provided context to answer the provided user query. Only use the provided context to answer the query. If you do not know the answer, respond with "I don't know".
58
  """
59
  rag_prompt = ChatPromptTemplate.from_template(RAG_PROMPT)
60
 
61
  # Create the retrieval augmented QA chain using the Qdrant retriever, RAG prompt, and OpenAI chat model
62
  openai_chat_model = ChatOpenAI(model="gpt-3.5-turbo")
 
 
 
 
 
63
  retrieval_augmented_qa_chain = (
64
- {"context": itemgetter("question") | qdrant_retriever, "question": itemgetter("question")}
65
- | RunnablePassthrough.assign(context=itemgetter("context"))
66
- | {"response": rag_prompt | openai_chat_model, "context": itemgetter("context")}
67
  )
68
 
69
  return retrieval_augmented_qa_chain
 
30
 
31
  def create_raqa_chain_from_docs():
32
 
33
+ # # Load the documents from a PDF file using PyMuPDFLoader
34
+ # docs = PyMuPDFLoader("https://d18rn0p25nwr6d.cloudfront.net/CIK-0001326801/c7318154-f6ae-4866-89fa-f0c589f2ee3d.pdf").load() # TODO: Update this to enable user to upload PDF
35
+ # print("Loaded", len(docs), "documents")
36
+ # print(docs[0])
37
 
38
+ # # Create a Qdrant vector store from the split chunks and embedding model, and obtain its retriever
39
+ # split_chunks = chunk_documents(docs, tiktoken_len)
40
+ # embedding_model = OpenAIEmbeddings(model="text-embedding-3-small")
41
+ # qdrant_vectorstore = Qdrant.from_documents(
42
+ # split_chunks,
43
+ # embedding_model,
44
+ # location=":memory:",
45
+ # collection_name="LoadedPDF",
46
+ # )
47
+ # qdrant_retriever = qdrant_vectorstore.as_retriever()
48
 
49
  # Define the RAG prompt template
50
  RAG_PROMPT = """
51
+ Assume you are a virtual student being taught by the user. You can ask clarifying questions to better understand the user's explanation. Your goal is to ensure that the user understands the concept they are explaining. You can also ask questions to help the user elaborate on their explanation. You can ask questions like "Can you explain that in simpler terms?" or "Can you provide an example?".
 
52
 
53
+ USER MESSAGE:
54
  {question}
 
 
55
  """
56
  rag_prompt = ChatPromptTemplate.from_template(RAG_PROMPT)
57
 
58
  # Create the retrieval augmented QA chain using the Qdrant retriever, RAG prompt, and OpenAI chat model
59
  openai_chat_model = ChatOpenAI(model="gpt-3.5-turbo")
60
+ # retrieval_augmented_qa_chain = (
61
+ # {"context": itemgetter("question") | qdrant_retriever, "question": itemgetter("question")}
62
+ # | RunnablePassthrough.assign(context=itemgetter("context"))
63
+ # | {"response": rag_prompt | openai_chat_model, "context": itemgetter("context")}
64
+ # )
65
  retrieval_augmented_qa_chain = (
66
+ {"response": rag_prompt | openai_chat_model}
 
 
67
  )
68
 
69
  return retrieval_augmented_qa_chain