Spaces:
Sleeping
Sleeping
Commit
·
975d352
1
Parent(s):
2e1d36a
Add detailed logging to chatbot_utils and update app
Browse files- app.py +10 -0
- chatbot_utils.py +71 -0
- requirements.txt +1 -0
- src/frontend/temp_uploaded_image.jpg +0 -0
app.py
CHANGED
|
@@ -14,6 +14,7 @@ import re
|
|
| 14 |
from openai import OpenAI
|
| 15 |
import os
|
| 16 |
from dotenv import load_dotenv
|
|
|
|
| 17 |
|
| 18 |
# Configure logging
|
| 19 |
logging.basicConfig(level=logging.INFO)
|
|
@@ -188,5 +189,14 @@ def main():
|
|
| 188 |
logger.error(f"Error processing document: {str(e)}")
|
| 189 |
st.error(f"Error processing document: {str(e)}")
|
| 190 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
if __name__ == "__main__":
|
| 192 |
main()
|
|
|
|
| 14 |
from openai import OpenAI
|
| 15 |
import os
|
| 16 |
from dotenv import load_dotenv
|
| 17 |
+
from chatbot_utils import ask_receipt_chatbot
|
| 18 |
|
| 19 |
# Configure logging
|
| 20 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 189 |
logger.error(f"Error processing document: {str(e)}")
|
| 190 |
st.error(f"Error processing document: {str(e)}")
|
| 191 |
|
| 192 |
+
st.markdown("---")
|
| 193 |
+
st.header("💬 Receipt Chatbot")
|
| 194 |
+
st.write("Ask questions about your receipts stored in DynamoDB.")
|
| 195 |
+
user_question = st.text_input("Enter your question:", "What is the total amount paid?")
|
| 196 |
+
if st.button("Ask Chatbot"):
|
| 197 |
+
with st.spinner("Getting answer from Perplexity LLM..."):
|
| 198 |
+
answer = ask_receipt_chatbot(user_question)
|
| 199 |
+
st.success(answer)
|
| 200 |
+
|
| 201 |
if __name__ == "__main__":
|
| 202 |
main()
|
chatbot_utils.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import boto3
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
import logging
|
| 5 |
+
|
| 6 |
+
logger = logging.getLogger(__name__)
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def ask_receipt_chatbot(question, region_name='us-east-1', table_name='Receipts'):
|
| 10 |
+
"""
|
| 11 |
+
Given a user question, fetch all receipts from DynamoDB, format them as context, and query Perplexity LLM.
|
| 12 |
+
Returns the LLM's answer or an error message.
|
| 13 |
+
"""
|
| 14 |
+
logger.info(f"[chatbot] Received chatbot question: {question}")
|
| 15 |
+
# Initialize OpenAI client for Perplexity
|
| 16 |
+
api_key = os.environ.get('PERPLEXITY_API_KEY') or os.environ.get('OPENAI_API_KEY')
|
| 17 |
+
if api_key:
|
| 18 |
+
logger.info("[chatbot] Using Perplexity/OpenAI API key from environment.")
|
| 19 |
+
else:
|
| 20 |
+
logger.warning("[chatbot] No Perplexity/OpenAI API key found in environment!")
|
| 21 |
+
client = OpenAI(
|
| 22 |
+
api_key=api_key,
|
| 23 |
+
base_url="https://api.perplexity.ai"
|
| 24 |
+
)
|
| 25 |
+
try:
|
| 26 |
+
logger.info(f"[chatbot] Connecting to DynamoDB in region: {region_name}")
|
| 27 |
+
dynamodb = boto3.resource('dynamodb', region_name=region_name)
|
| 28 |
+
logger.info(f"[chatbot] Getting table: {table_name}")
|
| 29 |
+
table = dynamodb.Table(table_name)
|
| 30 |
+
logger.info(f"[chatbot] Scanning DynamoDB table: {table_name}")
|
| 31 |
+
response = table.scan()
|
| 32 |
+
items = response.get('Items', [])
|
| 33 |
+
logger.info(f"[chatbot] Fetched {len(items)} items from DynamoDB. Response: {response}")
|
| 34 |
+
# Format items for context
|
| 35 |
+
context = "\n".join([
|
| 36 |
+
f"Receipt {item.get('receipt_no', '')}:\n"
|
| 37 |
+
f" Name: {item.get('name', '')}\n"
|
| 38 |
+
f" Date: {item.get('date', '')}\n"
|
| 39 |
+
f" Product: {item.get('product', '')}\n"
|
| 40 |
+
f" Amount Paid: {item.get('amount_paid', '')}\n"
|
| 41 |
+
for item in items
|
| 42 |
+
])
|
| 43 |
+
logger.info(f"[chatbot] Context for LLM prompt created. Length: {len(context)} characters. Context: {context}")
|
| 44 |
+
prompt = f"Based on these receipts:\n{context}\n\nQuestion: {question}\nPlease provide a 2-3 line answer."
|
| 45 |
+
logger.info(f"[chatbot] Prompt for LLM: {prompt}")
|
| 46 |
+
messages = [
|
| 47 |
+
{
|
| 48 |
+
"role": "system",
|
| 49 |
+
"content": (
|
| 50 |
+
"You are an artificial intelligence assistant and you need to "
|
| 51 |
+
"engage in a helpful, detailed, polite conversation with a user. "
|
| 52 |
+
"Give a 2-3 line answer."
|
| 53 |
+
)
|
| 54 |
+
},
|
| 55 |
+
{
|
| 56 |
+
"role": "user",
|
| 57 |
+
"content": prompt
|
| 58 |
+
}
|
| 59 |
+
]
|
| 60 |
+
logger.info("[chatbot] Sending request to Perplexity LLM...")
|
| 61 |
+
response = client.chat.completions.create(
|
| 62 |
+
model="sonar",
|
| 63 |
+
messages=messages
|
| 64 |
+
)
|
| 65 |
+
logger.info(f"[chatbot] Received response from Perplexity LLM: {response}")
|
| 66 |
+
answer = response.choices[0].message.content
|
| 67 |
+
logger.info(f"[chatbot] LLM answer: {answer}")
|
| 68 |
+
return answer
|
| 69 |
+
except Exception as e:
|
| 70 |
+
logger.error(f"[chatbot] Error in ask_receipt_chatbot: {str(e)}", exc_info=True)
|
| 71 |
+
return f"Error from LLM or DynamoDB: {str(e)}"
|
requirements.txt
CHANGED
|
@@ -31,3 +31,4 @@ pydantic>=2.0.0
|
|
| 31 |
openai
|
| 32 |
streamlit
|
| 33 |
plotly==5.18.0
|
|
|
|
|
|
| 31 |
openai
|
| 32 |
streamlit
|
| 33 |
plotly==5.18.0
|
| 34 |
+
tesseract-ocr
|
src/frontend/temp_uploaded_image.jpg
CHANGED
|
|
Git LFS Details
|