Spaces:
Build error
Build error
samlonka commited on
Commit ·
c83be6d
1
Parent(s): bc26f8c
'tool_added'
Browse files
app.py
CHANGED
|
@@ -14,12 +14,11 @@ import tiktoken
|
|
| 14 |
from crag import crag_app
|
| 15 |
from datetime import timedelta
|
| 16 |
from sqlalchemy import create_engine
|
| 17 |
-
from cache import (write_to_db,
|
| 18 |
-
current_time)
|
| 19 |
|
| 20 |
|
| 21 |
#load postgres engine
|
| 22 |
-
engine = create_engine("postgresql://postgres:sampath@localhost:5432/postgres")
|
| 23 |
#load keys
|
| 24 |
os.environ['OPENAI_API_KEY'] = st.secrets["OPENAI_API_KEY"]
|
| 25 |
chat_history = ChatMessageHistory()
|
|
@@ -69,8 +68,8 @@ def veda_bot(sidebar: bool = True) -> None:
|
|
| 69 |
if "messages" not in st.session_state.keys():
|
| 70 |
st.session_state.messages = [{"role": "assistant", "content": "Hi. I am an AI Assistant. Ask me a question about Vedas!"}]
|
| 71 |
|
| 72 |
-
if "session_uuid" not in st.session_state:
|
| 73 |
-
|
| 74 |
|
| 75 |
if "feedback" not in st.session_state:
|
| 76 |
st.session_state["feedback"] = None
|
|
|
|
| 14 |
from crag import crag_app
|
| 15 |
from datetime import timedelta
|
| 16 |
from sqlalchemy import create_engine
|
| 17 |
+
#from cache import (write_to_db,current_time)
|
|
|
|
| 18 |
|
| 19 |
|
| 20 |
#load postgres engine
|
| 21 |
+
#engine = create_engine("postgresql://postgres:sampath@localhost:5432/postgres")
|
| 22 |
#load keys
|
| 23 |
os.environ['OPENAI_API_KEY'] = st.secrets["OPENAI_API_KEY"]
|
| 24 |
chat_history = ChatMessageHistory()
|
|
|
|
| 68 |
if "messages" not in st.session_state.keys():
|
| 69 |
st.session_state.messages = [{"role": "assistant", "content": "Hi. I am an AI Assistant. Ask me a question about Vedas!"}]
|
| 70 |
|
| 71 |
+
#if "session_uuid" not in st.session_state:
|
| 72 |
+
# st.session_state["session_uuid"] = f"{current_time()}-{str(uuid.uuid4())}"
|
| 73 |
|
| 74 |
if "feedback" not in st.session_state:
|
| 75 |
st.session_state["feedback"] = None
|
cache.py
DELETED
|
@@ -1,75 +0,0 @@
|
|
| 1 |
-
from langchain_openai import OpenAI
|
| 2 |
-
import os
|
| 3 |
-
import time
|
| 4 |
-
from langchain.cache import SQLAlchemyCache
|
| 5 |
-
from sqlalchemy import Column, Computed, Index, Integer, Sequence, String, create_engine
|
| 6 |
-
from sqlalchemy.orm import sessionmaker, declarative_base
|
| 7 |
-
from datetime import datetime
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
#load postgres engine
|
| 13 |
-
engine = create_engine("postgresql://postgres:sampath@localhost:5432/postgres")
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
Base = declarative_base()
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
class FeedBackCache(Base):
|
| 22 |
-
"""Postgres table for fulltext-indexed LLM Cache"""
|
| 23 |
-
|
| 24 |
-
__tablename__ = "veda_bot_feedback"
|
| 25 |
-
id = Column(Integer, Sequence("cache_id"), primary_key=True)
|
| 26 |
-
user_message = Column(String, nullable=True)
|
| 27 |
-
assistant_message = Column(String, nullable=True)
|
| 28 |
-
feedback_score = Column(String, nullable=True)
|
| 29 |
-
feedback_text = Column(String, nullable=True)
|
| 30 |
-
|
| 31 |
-
# Create the table in the database
|
| 32 |
-
Base.metadata.create_all(engine)
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
def write_to_db(u_message, a_message, f_score, f_text):
|
| 36 |
-
try:
|
| 37 |
-
# Create a sessionmaker bound to the engine
|
| 38 |
-
Session = sessionmaker(bind=engine)
|
| 39 |
-
|
| 40 |
-
# Create a session
|
| 41 |
-
session = Session()
|
| 42 |
-
|
| 43 |
-
message = FeedBackCache(
|
| 44 |
-
user_message=u_message["content"],
|
| 45 |
-
assistant_message=a_message["content"],
|
| 46 |
-
feedback_score=f_score,
|
| 47 |
-
feedback_text=f_text
|
| 48 |
-
)
|
| 49 |
-
|
| 50 |
-
# Add the instance to the session
|
| 51 |
-
session.add(message)
|
| 52 |
-
|
| 53 |
-
# Commit the session to persist the changes to the database
|
| 54 |
-
session.commit()
|
| 55 |
-
print("Feedback written to DB successfully!")
|
| 56 |
-
|
| 57 |
-
except Exception as e:
|
| 58 |
-
# If an error occurs, rollback the session and print the error message
|
| 59 |
-
session.rollback()
|
| 60 |
-
print("Error occurred while writing feedback to DB:", e)
|
| 61 |
-
|
| 62 |
-
finally:
|
| 63 |
-
# Close the session
|
| 64 |
-
session.close()
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
def current_time() -> str:
|
| 68 |
-
"""Return the current time as a string. Used as part of the session UUID."""
|
| 69 |
-
# Get current date and time
|
| 70 |
-
current_datetime = datetime.now()
|
| 71 |
-
|
| 72 |
-
# Convert to a long number format
|
| 73 |
-
datetime_string = current_datetime.strftime("%Y%m%d%H%M%S")
|
| 74 |
-
|
| 75 |
-
return datetime_string
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|