Spaces:
Sleeping
Sleeping
Implement Custom DeepSeek Wrapper (Source: ISH_harry_potter_rag)
Browse files
main.py
CHANGED
|
@@ -38,26 +38,59 @@ if os.path.exists(FAISS_PATH):
|
|
| 38 |
retriever = None
|
| 39 |
else:
|
| 40 |
print("WARNING: FAISS index not found at path. Run ingest.py first.")
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
from langchain_core.prompts import PromptTemplate
|
| 63 |
from langchain_core.output_parsers import StrOutputParser
|
|
@@ -92,7 +125,6 @@ if retriever and llm:
|
|
| 92 |
{"context": retriever | format_docs, "question": RunnablePassthrough()}
|
| 93 |
| prompt
|
| 94 |
| llm
|
| 95 |
-
| StrOutputParser()
|
| 96 |
)
|
| 97 |
print("DEBUG: rag_chain constructed successfully.")
|
| 98 |
else:
|
|
|
|
| 38 |
retriever = None
|
| 39 |
else:
|
| 40 |
print("WARNING: FAISS index not found at path. Run ingest.py first.")
|
| 41 |
+
import requests
|
| 42 |
+
import json
|
| 43 |
+
from langchain_core.runnables import RunnableLambda
|
| 44 |
+
|
| 45 |
+
# Custom DeepSeek Connector (Cloned from Reference Repo)
|
| 46 |
+
def call_deepseek_v3(prompt_input):
|
| 47 |
+
# Handle LangChain prompt objects
|
| 48 |
+
if hasattr(prompt_input, "to_string"):
|
| 49 |
+
prompt_text = prompt_input.to_string()
|
| 50 |
+
else:
|
| 51 |
+
prompt_text = str(prompt_input)
|
| 52 |
+
|
| 53 |
+
# Direct Router API used by the reference repo
|
| 54 |
+
api_url = "https://router.huggingface.co/v1/chat/completions"
|
| 55 |
+
token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
| 56 |
+
|
| 57 |
+
if not token:
|
| 58 |
+
return "Error: No API Token found."
|
| 59 |
+
|
| 60 |
+
headers = {
|
| 61 |
+
"Authorization": f"Bearer {token}",
|
| 62 |
+
"Content-Type": "application/json",
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
payload = {
|
| 66 |
+
"model": "deepseek-ai/DeepSeek-V3.2",
|
| 67 |
+
"messages": [
|
| 68 |
+
{
|
| 69 |
+
"role": "system",
|
| 70 |
+
"content": "You are an expert on Stranger Things. Answer clearly and concisely."
|
| 71 |
+
},
|
| 72 |
+
{
|
| 73 |
+
"role": "user",
|
| 74 |
+
"content": prompt_text
|
| 75 |
+
}
|
| 76 |
+
],
|
| 77 |
+
"temperature": 0.3,
|
| 78 |
+
"max_tokens": 512,
|
| 79 |
+
"stream": False
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
try:
|
| 83 |
+
response = requests.post(api_url, headers=headers, json=payload)
|
| 84 |
+
if response.status_code != 200:
|
| 85 |
+
print(f"API Error {response.status_code}: {response.text}")
|
| 86 |
+
return f"DeepSeek Error: {response.text}"
|
| 87 |
+
return response.json()["choices"][0]["message"]["content"]
|
| 88 |
+
except Exception as e:
|
| 89 |
+
print(f"DeepSeek Connection Exception: {e}")
|
| 90 |
+
return f"Error: {e}"
|
| 91 |
+
|
| 92 |
+
llm = RunnableLambda(call_deepseek_v3)
|
| 93 |
+
print("DeepSeek V3.2 Client (Custom Request) initialized!")
|
| 94 |
|
| 95 |
from langchain_core.prompts import PromptTemplate
|
| 96 |
from langchain_core.output_parsers import StrOutputParser
|
|
|
|
| 125 |
{"context": retriever | format_docs, "question": RunnablePassthrough()}
|
| 126 |
| prompt
|
| 127 |
| llm
|
|
|
|
| 128 |
)
|
| 129 |
print("DEBUG: rag_chain constructed successfully.")
|
| 130 |
else:
|