Spaces:
Running
Running
Add detailed logging and improve system prompt
Browse files
app.py
CHANGED
|
@@ -13,7 +13,14 @@ from aimakerspace.openai_utils.chatmodel import ChatOpenAI
|
|
| 13 |
import chainlit as cl
|
| 14 |
|
| 15 |
system_template = """\
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
system_role_prompt = SystemRolePrompt(system_template)
|
| 18 |
|
| 19 |
user_prompt_template = """\
|
|
@@ -33,6 +40,10 @@ class RetrievalAugmentedQAPipeline:
|
|
| 33 |
async def arun_pipeline(self, user_query: str):
|
| 34 |
# Get more contexts but limit the total length
|
| 35 |
context_list = self.vector_db_retriever.search_by_text(user_query, k=3) # Reduced from 6 to 3
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# Limit total context length to approximately 3000 tokens (12000 characters)
|
| 38 |
context_prompt = ""
|
|
@@ -45,11 +56,17 @@ class RetrievalAugmentedQAPipeline:
|
|
| 45 |
context_prompt += context[0] + "\n"
|
| 46 |
total_length += len(context[0])
|
| 47 |
|
| 48 |
-
print(f"
|
| 49 |
|
| 50 |
formatted_system_prompt = system_role_prompt.create_message()
|
| 51 |
formatted_user_prompt = user_role_prompt.create_message(question=user_query, context=context_prompt)
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
async def generate_response():
|
| 54 |
async for chunk in self.llm.astream([formatted_system_prompt, formatted_user_prompt]):
|
| 55 |
yield chunk
|
|
|
|
| 13 |
import chainlit as cl
|
| 14 |
|
| 15 |
system_template = """\
|
| 16 |
+
You are a helpful AI assistant that answers questions based on the provided context.
|
| 17 |
+
Your task is to:
|
| 18 |
+
1. Carefully read and understand the context
|
| 19 |
+
2. Answer the user's question using ONLY the information from the context
|
| 20 |
+
3. If the answer cannot be found in the context, say "I cannot find the answer in the provided context"
|
| 21 |
+
4. If you find partial information, share what you found and indicate if more information might be needed
|
| 22 |
+
|
| 23 |
+
Remember: Only use information from the provided context to answer questions."""
|
| 24 |
system_role_prompt = SystemRolePrompt(system_template)
|
| 25 |
|
| 26 |
user_prompt_template = """\
|
|
|
|
| 40 |
async def arun_pipeline(self, user_query: str):
|
| 41 |
# Get more contexts but limit the total length
|
| 42 |
context_list = self.vector_db_retriever.search_by_text(user_query, k=3) # Reduced from 6 to 3
|
| 43 |
+
print("\nRetrieved contexts:")
|
| 44 |
+
for i, (context, score) in enumerate(context_list):
|
| 45 |
+
print(f"\nContext {i+1} (score: {score:.3f}):")
|
| 46 |
+
print(context[:200] + "..." if len(context) > 200 else context)
|
| 47 |
|
| 48 |
# Limit total context length to approximately 3000 tokens (12000 characters)
|
| 49 |
context_prompt = ""
|
|
|
|
| 56 |
context_prompt += context[0] + "\n"
|
| 57 |
total_length += len(context[0])
|
| 58 |
|
| 59 |
+
print(f"\nUsing {len(context_prompt.split())} words of context")
|
| 60 |
|
| 61 |
formatted_system_prompt = system_role_prompt.create_message()
|
| 62 |
formatted_user_prompt = user_role_prompt.create_message(question=user_query, context=context_prompt)
|
| 63 |
|
| 64 |
+
print("\nFinal messages being sent to the model:")
|
| 65 |
+
print("\nSystem prompt:")
|
| 66 |
+
print(formatted_system_prompt)
|
| 67 |
+
print("\nUser prompt:")
|
| 68 |
+
print(formatted_user_prompt)
|
| 69 |
+
|
| 70 |
async def generate_response():
|
| 71 |
async for chunk in self.llm.astream([formatted_system_prompt, formatted_user_prompt]):
|
| 72 |
yield chunk
|