Spaces:
Sleeping
Sleeping
Update chatbot_rag.py
Browse files- chatbot_rag.py +33 -11
chatbot_rag.py
CHANGED
|
@@ -6,8 +6,9 @@ from langchain.prompts import PromptTemplate
|
|
| 6 |
from langchain_core.runnables import RunnablePassthrough
|
| 7 |
from langchain_core.output_parsers import StrOutputParser
|
| 8 |
import traceback
|
| 9 |
-
|
| 10 |
import os
|
|
|
|
| 11 |
from huggingface_hub import login
|
| 12 |
token = os.getenv("HF_TOKEN")
|
| 13 |
print("🔑 HF_TOKEN available?", token is not None)
|
|
@@ -48,14 +49,18 @@ def build_qa():
|
|
| 48 |
)
|
| 49 |
|
| 50 |
pipe = pipeline(
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
| 58 |
)
|
|
|
|
| 59 |
|
| 60 |
llm = HuggingFacePipeline(pipeline=pipe)
|
| 61 |
|
|
@@ -81,11 +86,28 @@ def build_qa():
|
|
| 81 |
def format_docs(docs):
|
| 82 |
return "\n".join(doc.page_content for doc in docs)
|
| 83 |
|
|
|
|
| 84 |
def hf_to_str(x):
|
| 85 |
-
"""Convert Hugging Face pipeline output to plain string"""
|
| 86 |
if isinstance(x, list) and "generated_text" in x[0]:
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
# 7. RAG chain
|
| 91 |
rag_chain = (
|
|
|
|
| 6 |
from langchain_core.runnables import RunnablePassthrough
|
| 7 |
from langchain_core.output_parsers import StrOutputParser
|
| 8 |
import traceback
|
| 9 |
+
import re
|
| 10 |
import os
|
| 11 |
+
|
| 12 |
from huggingface_hub import login
|
| 13 |
token = os.getenv("HF_TOKEN")
|
| 14 |
print("🔑 HF_TOKEN available?", token is not None)
|
|
|
|
| 49 |
)
|
| 50 |
|
| 51 |
pipe = pipeline(
|
| 52 |
+
"text-generation",
|
| 53 |
+
model=model,
|
| 54 |
+
tokenizer=tokenizer,
|
| 55 |
+
max_new_tokens=128,
|
| 56 |
+
temperature=0.2, # keeps answers deterministic but less rigid than 0
|
| 57 |
+
do_sample=True, # allow some randomness
|
| 58 |
+
top_p=0.9, # nucleus sampling to avoid loops
|
| 59 |
+
repetition_penalty=1.2, # 🚀 penalize repeats
|
| 60 |
+
eos_token_id=tokenizer.eos_token_id, # stop at EOS
|
| 61 |
+
return_full_text=False
|
| 62 |
)
|
| 63 |
+
|
| 64 |
|
| 65 |
llm = HuggingFacePipeline(pipeline=pipe)
|
| 66 |
|
|
|
|
| 86 |
def format_docs(docs):
|
| 87 |
return "\n".join(doc.page_content for doc in docs)
|
| 88 |
|
| 89 |
+
|
| 90 |
def hf_to_str(x):
|
| 91 |
+
"""Convert Hugging Face pipeline output to plain string (clean + generalized)."""
|
| 92 |
if isinstance(x, list) and "generated_text" in x[0]:
|
| 93 |
+
text = x[0]["generated_text"]
|
| 94 |
+
else:
|
| 95 |
+
text = str(x)
|
| 96 |
+
|
| 97 |
+
# 1. Remove markdown/code artifacts
|
| 98 |
+
text = text.replace("```", "").replace("#", "").strip()
|
| 99 |
+
|
| 100 |
+
# 2. Normalize whitespace & line breaks
|
| 101 |
+
text = re.sub(r"\s+", " ", text)
|
| 102 |
+
|
| 103 |
+
# 3. Remove duplicated consecutive phrases (up to ~5 words repeated)
|
| 104 |
+
text = re.sub(r"\b(\w+\s+){1,5}(\1){2,}", r"\1", text)
|
| 105 |
+
|
| 106 |
+
# 4. Trim leading/trailing junk
|
| 107 |
+
text = text.strip(" .,-\n\t")
|
| 108 |
+
|
| 109 |
+
return text
|
| 110 |
+
|
| 111 |
|
| 112 |
# 7. RAG chain
|
| 113 |
rag_chain = (
|