Spaces:
Sleeping
Sleeping
whymath
commited on
Commit
·
53867fd
1
Parent(s):
e04643d
Set RAG instructions on PDF upload based on current mode
Browse files
app.py
CHANGED
|
@@ -14,7 +14,7 @@ Assume you are a student and that the user is your teacher. Your goal is to ensu
|
|
| 14 |
You should always first let the user know if they are correct or not, and then ask them questions to help them learn by teaching rather than explaining things to them.
|
| 15 |
If they ask for feedback, you should provide constructive feedback on the whole conversation instead of asking another question.
|
| 16 |
"""
|
| 17 |
-
|
| 18 |
Pretend you are a student and that the user is your teacher. Your goal is to get the user to teach you about a topic or concept, and you can ask clarifying questions to help them teach better.
|
| 19 |
"""
|
| 20 |
openai_chat_model = ChatOpenAI(model="gpt-3.5-turbo")
|
|
@@ -71,6 +71,7 @@ async def main(message: cl.Message):
|
|
| 71 |
@cl.action_callback("upload_pdf")
|
| 72 |
async def upload_pdf_fn(action: cl.Action):
|
| 73 |
print("\nRunning PDF upload and RAG chain creation")
|
|
|
|
| 74 |
|
| 75 |
# Wait for the user to upload a file
|
| 76 |
files = None
|
|
@@ -83,10 +84,14 @@ async def upload_pdf_fn(action: cl.Action):
|
|
| 83 |
).send()
|
| 84 |
file_uploaded = files[0]
|
| 85 |
# print("\nUploaded file:", file_uploaded, "\n")
|
|
|
|
| 86 |
|
| 87 |
# Create the RAG chain and store it in the user session
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
| 90 |
settings["rag_chain"] = rag_chain
|
| 91 |
settings["current_mode"] = "rag_chain"
|
| 92 |
cl.user_session.set("settings", settings)
|
|
@@ -98,8 +103,8 @@ async def upload_pdf_fn(action: cl.Action):
|
|
| 98 |
@cl.action_callback("switch_default")
|
| 99 |
async def switch_default_fn(action: cl.Action):
|
| 100 |
print("\nSwitching back to default base chain")
|
| 101 |
-
|
| 102 |
settings = cl.user_session.get("settings")
|
|
|
|
| 103 |
settings["rag_chain_available"] = False
|
| 104 |
cl.user_session.set("settings", settings)
|
| 105 |
|
|
@@ -110,9 +115,9 @@ async def switch_default_fn(action: cl.Action):
|
|
| 110 |
@cl.action_callback("switch_ai_student")
|
| 111 |
async def switch_ai_student_fn(action: cl.Action):
|
| 112 |
print("\nSwitching to AI student mode")
|
| 113 |
-
|
| 114 |
settings = cl.user_session.get("settings")
|
| 115 |
-
|
|
|
|
| 116 |
settings["ai_student_chain"] = ai_student_chain
|
| 117 |
settings["current_mode"] = "ai_student_chain"
|
| 118 |
cl.user_session.set("settings", settings)
|
|
|
|
| 14 |
You should always first let the user know if they are correct or not, and then ask them questions to help them learn by teaching rather than explaining things to them.
|
| 15 |
If they ask for feedback, you should provide constructive feedback on the whole conversation instead of asking another question.
|
| 16 |
"""
|
| 17 |
+
aistudent_instructions = """
|
| 18 |
Pretend you are a student and that the user is your teacher. Your goal is to get the user to teach you about a topic or concept, and you can ask clarifying questions to help them teach better.
|
| 19 |
"""
|
| 20 |
openai_chat_model = ChatOpenAI(model="gpt-3.5-turbo")
|
|
|
|
| 71 |
@cl.action_callback("upload_pdf")
|
| 72 |
async def upload_pdf_fn(action: cl.Action):
|
| 73 |
print("\nRunning PDF upload and RAG chain creation")
|
| 74 |
+
settings = cl.user_session.get("settings")
|
| 75 |
|
| 76 |
# Wait for the user to upload a file
|
| 77 |
files = None
|
|
|
|
| 84 |
).send()
|
| 85 |
file_uploaded = files[0]
|
| 86 |
# print("\nUploaded file:", file_uploaded, "\n")
|
| 87 |
+
print("file_uploaded.name =", file_uploaded.name, "; file_uploaded.path =", file_uploaded.path)
|
| 88 |
|
| 89 |
# Create the RAG chain and store it in the user session
|
| 90 |
+
if settings["current_mode"] == "ai_student_chain":
|
| 91 |
+
rag_instructions = aistudent_instructions
|
| 92 |
+
else:
|
| 93 |
+
rag_instructions = base_instructions
|
| 94 |
+
rag_chain = utils.create_rag_chain_from_file(openai_chat_model, rag_instructions, file_uploaded.path, file_uploaded.name)
|
| 95 |
settings["rag_chain"] = rag_chain
|
| 96 |
settings["current_mode"] = "rag_chain"
|
| 97 |
cl.user_session.set("settings", settings)
|
|
|
|
| 103 |
@cl.action_callback("switch_default")
|
| 104 |
async def switch_default_fn(action: cl.Action):
|
| 105 |
print("\nSwitching back to default base chain")
|
|
|
|
| 106 |
settings = cl.user_session.get("settings")
|
| 107 |
+
|
| 108 |
settings["rag_chain_available"] = False
|
| 109 |
cl.user_session.set("settings", settings)
|
| 110 |
|
|
|
|
| 115 |
@cl.action_callback("switch_ai_student")
|
| 116 |
async def switch_ai_student_fn(action: cl.Action):
|
| 117 |
print("\nSwitching to AI student mode")
|
|
|
|
| 118 |
settings = cl.user_session.get("settings")
|
| 119 |
+
|
| 120 |
+
ai_student_chain = utils.create_base_chain(openai_chat_model, aistudent_instructions)
|
| 121 |
settings["ai_student_chain"] = ai_student_chain
|
| 122 |
settings["current_mode"] = "ai_student_chain"
|
| 123 |
cl.user_session.set("settings", settings)
|