Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -11,8 +11,13 @@ from langchain_core.prompts import PromptTemplate
|
|
11 |
from langchain.schema.output_parser import StrOutputParser
|
12 |
from langchain.schema.runnable import RunnablePassthrough
|
13 |
from langchain.schema.runnable.config import RunnableConfig
|
|
|
14 |
|
15 |
-
#
|
|
|
|
|
|
|
|
|
16 |
load_dotenv()
|
17 |
|
18 |
HF_LLM_ENDPOINT = os.environ["HF_LLM_ENDPOINT"]
|
@@ -27,13 +32,17 @@ hf_embeddings = HuggingFaceEndpointEmbeddings(
|
|
27 |
huggingfacehub_api_token=HF_TOKEN,
|
28 |
)
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
37 |
|
38 |
RAG_PROMPT_TEMPLATE = """\
|
39 |
system
|
@@ -48,15 +57,20 @@ assistant
|
|
48 |
|
49 |
rag_prompt = PromptTemplate.from_template(RAG_PROMPT_TEMPLATE)
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
@cl.author_rename
|
62 |
def rename(original_author: str):
|
@@ -73,14 +87,16 @@ async def start_chat():
|
|
73 |
| rag_prompt | hf_llm
|
74 |
)
|
75 |
cl.user_session.set("lcel_rag_chain", lcel_rag_chain)
|
76 |
-
|
77 |
-
|
|
|
78 |
|
79 |
@cl.on_message
|
80 |
async def main(message: cl.Message):
|
81 |
try:
|
82 |
lcel_rag_chain = cl.user_session.get("lcel_rag_chain")
|
83 |
if lcel_rag_chain is None:
|
|
|
84 |
await cl.Message(content="Session has expired. Please restart the chat.").send()
|
85 |
return
|
86 |
|
@@ -91,6 +107,10 @@ async def main(message: cl.Message):
|
|
91 |
):
|
92 |
await msg.stream_token(chunk)
|
93 |
await msg.send()
|
|
|
94 |
except KeyError as e:
|
|
|
95 |
await cl.Message(content="An error occurred. Please restart the chat.").send()
|
96 |
-
|
|
|
|
|
|
11 |
from langchain.schema.output_parser import StrOutputParser
|
12 |
from langchain.schema.runnable import RunnablePassthrough
|
13 |
from langchain.schema.runnable.config import RunnableConfig
|
14 |
+
import logging
|
15 |
|
16 |
+
# Set up logging
|
17 |
+
logging.basicConfig(level=logging.INFO)
|
18 |
+
logger = logging.getLogger(__name__)
|
19 |
+
|
20 |
+
# Load environment variables
|
21 |
load_dotenv()
|
22 |
|
23 |
HF_LLM_ENDPOINT = os.environ["HF_LLM_ENDPOINT"]
|
|
|
32 |
huggingfacehub_api_token=HF_TOKEN,
|
33 |
)
|
34 |
|
35 |
+
try:
|
36 |
+
vectorstore = FAISS.load_local(
|
37 |
+
vectorstore_path,
|
38 |
+
hf_embeddings,
|
39 |
+
allow_dangerous_deserialization=True
|
40 |
+
)
|
41 |
+
hf_retriever = vectorstore.as_retriever()
|
42 |
+
logger.info("Loaded Vectorstore")
|
43 |
+
except Exception as e:
|
44 |
+
logger.error(f"Error loading Vectorstore: {e}")
|
45 |
+
raise
|
46 |
|
47 |
RAG_PROMPT_TEMPLATE = """\
|
48 |
system
|
|
|
57 |
|
58 |
rag_prompt = PromptTemplate.from_template(RAG_PROMPT_TEMPLATE)
|
59 |
|
60 |
+
try:
|
61 |
+
hf_llm = HuggingFaceEndpoint(
|
62 |
+
endpoint_url=HF_LLM_ENDPOINT,
|
63 |
+
max_new_tokens=512,
|
64 |
+
top_k=10,
|
65 |
+
top_p=0.95,
|
66 |
+
temperature=0.1,
|
67 |
+
repetition_penalty=1.0,
|
68 |
+
huggingfacehub_api_token=HF_TOKEN,
|
69 |
+
)
|
70 |
+
logger.info("Initialized HuggingFace LLM endpoint")
|
71 |
+
except Exception as e:
|
72 |
+
logger.error(f"Error initializing HuggingFace LLM endpoint: {e}")
|
73 |
+
raise
|
74 |
|
75 |
@cl.author_rename
|
76 |
def rename(original_author: str):
|
|
|
87 |
| rag_prompt | hf_llm
|
88 |
)
|
89 |
cl.user_session.set("lcel_rag_chain", lcel_rag_chain)
|
90 |
+
logger.info("Started chat and set LCEL RAG chain in user session")
|
91 |
+
except Exception as e:
|
92 |
+
logger.error(f"Error during chat start: {e}")
|
93 |
|
94 |
@cl.on_message
|
95 |
async def main(message: cl.Message):
|
96 |
try:
|
97 |
lcel_rag_chain = cl.user_session.get("lcel_rag_chain")
|
98 |
if lcel_rag_chain is None:
|
99 |
+
logger.warning("Session has expired. Asking user to restart the chat.")
|
100 |
await cl.Message(content="Session has expired. Please restart the chat.").send()
|
101 |
return
|
102 |
|
|
|
107 |
):
|
108 |
await msg.stream_token(chunk)
|
109 |
await msg.send()
|
110 |
+
logger.info("Message processed successfully")
|
111 |
except KeyError as e:
|
112 |
+
logger.error(f"Session error: {e}")
|
113 |
await cl.Message(content="An error occurred. Please restart the chat.").send()
|
114 |
+
except Exception as e:
|
115 |
+
logger.error(f"Unexpected error: {e}")
|
116 |
+
await cl.Message(content="An unexpected error occurred. Please try again.").send()
|