rahulrb99 commited on
Commit
fde3424
·
1 Parent(s): 292ce41

Use OpenAI client for HF router (fix dict in string error)

Browse files
Files changed (2) hide show
  1. backend/rag_service.py +8 -9
  2. requirements.txt +1 -0
backend/rag_service.py CHANGED
@@ -3,7 +3,7 @@
3
  import os
4
  import re
5
 
6
- from huggingface_hub import InferenceClient
7
 
8
  from backend.chat_service import save_message, load_chat
9
  from backend.retrieval_service import retrieve_chunks
@@ -12,17 +12,16 @@ MAX_HISTORY_MESSAGES = 20
12
  DEFAULT_MODEL = "mistralai/Mistral-7B-Instruct-v0.2"
13
  TOP_K = 5
14
 
15
- _client: InferenceClient | None = None
16
 
17
 
18
- def _get_client() -> InferenceClient:
19
  global _client
20
  if _client is None:
21
  token = os.getenv("HF_TOKEN")
22
- # Use new HF router (api-inference deprecated)
23
- _client = InferenceClient(
24
  base_url="https://router.huggingface.co/v1",
25
- token=token,
26
  )
27
  return _client
28
 
@@ -78,12 +77,12 @@ def rag_chat(notebook_id: str, query: str, chat_history: list) -> tuple[str, lis
78
 
79
  try:
80
  client = _get_client()
81
- response = client.chat_completion(
82
- messages=messages,
83
  model=DEFAULT_MODEL,
 
84
  max_tokens=512,
85
  )
86
- raw_answer = response.choices[0].message.content
87
  answer = _validate_citations(raw_answer, len(chunks))
88
  except Exception as e:
89
  answer = f"Error calling model: {e}"
 
3
  import os
4
  import re
5
 
6
+ from openai import OpenAI
7
 
8
  from backend.chat_service import save_message, load_chat
9
  from backend.retrieval_service import retrieve_chunks
 
12
  DEFAULT_MODEL = "mistralai/Mistral-7B-Instruct-v0.2"
13
  TOP_K = 5
14
 
15
+ _client: OpenAI | None = None
16
 
17
 
18
+ def _get_client() -> OpenAI:
19
  global _client
20
  if _client is None:
21
  token = os.getenv("HF_TOKEN")
22
+ _client = OpenAI(
 
23
  base_url="https://router.huggingface.co/v1",
24
+ api_key=token,
25
  )
26
  return _client
27
 
 
77
 
78
  try:
79
  client = _get_client()
80
+ response = client.chat.completions.create(
 
81
  model=DEFAULT_MODEL,
82
+ messages=messages,
83
  max_tokens=512,
84
  )
85
+ raw_answer = response.choices[0].message.content or ""
86
  answer = _validate_citations(raw_answer, len(chunks))
87
  except Exception as e:
88
  answer = f"Error calling model: {e}"
requirements.txt CHANGED
@@ -1,5 +1,6 @@
1
  gradio[oauth]==4.44.1
2
  huggingface_hub==0.24.7
 
3
  supabase>=2.0.0
4
  python-dotenv>=1.0.0
5
  realtime==2.3.0
 
1
  gradio[oauth]==4.44.1
2
  huggingface_hub==0.24.7
3
+ openai>=1.0.0
4
  supabase>=2.0.0
5
  python-dotenv>=1.0.0
6
  realtime==2.3.0