siddheshrj commited on
Commit
8f58ae6
·
verified ·
1 Parent(s): a92ad9b

Implement Custom DeepSeek Wrapper (Source: ISH_harry_potter_rag)

Browse files
Files changed (1) hide show
  1. main.py +53 -21
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
- # Hugging Face Inference API (Serverless & Free)
42
- if not os.getenv("HUGGINGFACEHUB_API_TOKEN"):
43
- print("CRITICAL WARNING: HUGGINGFACEHUB_API_TOKEN not found. API calls will fail.")
44
-
45
- repo_id = "deepseek-ai/DeepSeek-V3.2"
46
-
47
- try:
48
- print(f"Connecting to Hugging Face API ({repo_id})...")
49
- llm = HuggingFaceEndpoint(
50
- repo_id=repo_id,
51
- task="conversational",
52
- max_new_tokens=512,
53
- do_sample=True,
54
- temperature=0.7,
55
- top_p=0.9,
56
- )
57
- print("Hugging Face API Client connected!")
58
- except Exception as e:
59
- print(f"FAILED to connect to HF API: {e}")
60
- llm = None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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: