starfriend commited on
Commit
92ac28d
·
verified ·
1 Parent(s): 812b993

Upload 2 files

Browse files
llm_services/llm_client.py CHANGED
@@ -7,9 +7,12 @@ from .llm_usage import usage_summary
7
  def generate(messages, *, provider='azure', model=None, max_completion_tokens=1200):
8
  if os.getenv("ENABLE_EXTERNAL_LLM", "false").lower() not in {"1", "true", "yes"}:
9
  raise RuntimeError("External LLM calls are disabled (ENABLE_EXTERNAL_LLM=false)")
10
- if provider != "azure":
11
- raise ValueError(f"Unsupported provider: {provider}. Azure is implemented in Stage 1.")
12
- from .providers.azure import complete
 
 
 
13
  started = time.monotonic()
14
  result = complete(messages, model=model, max_completion_tokens=max_completion_tokens)
15
  result['elapsed_s'] = round(time.monotonic() - started, 3)
 
7
  def generate(messages, *, provider='azure', model=None, max_completion_tokens=1200):
8
  if os.getenv("ENABLE_EXTERNAL_LLM", "false").lower() not in {"1", "true", "yes"}:
9
  raise RuntimeError("External LLM calls are disabled (ENABLE_EXTERNAL_LLM=false)")
10
+ if provider == "azure":
11
+ from .providers.azure import complete
12
+ elif provider == "deepseek":
13
+ from .providers.deepseek import complete
14
+ else:
15
+ raise ValueError(f"Unsupported provider: {provider}")
16
  started = time.monotonic()
17
  result = complete(messages, model=model, max_completion_tokens=max_completion_tokens)
18
  result['elapsed_s'] = round(time.monotonic() - started, 3)
llm_services/providers/deepseek.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """DeepSeek OpenAI-compatible Chat Completions adapter for research-only calls.
2
+
3
+ Secrets live in HF Space environment, never in code/client-facing responses.
4
+ Non-thinking mode is explicit by default, and can be configured with the
5
+ DEEPSEEK_THINKING HF Space variable. Record it for experimental provenance.
6
+ """
7
+ from __future__ import annotations
8
+ import os
9
+ from openai import OpenAI
10
+
11
+ _ALLOWED_THINKING = {"enabled", "disabled"}
12
+
13
+
14
+ def _config():
15
+ mode = os.getenv("DEEPSEEK_THINKING", "disabled").strip().lower()
16
+ if mode not in _ALLOWED_THINKING:
17
+ raise ValueError("DEEPSEEK_THINKING must be enabled or disabled")
18
+ return mode
19
+
20
+
21
+ def create_client():
22
+ key = os.environ["DEEPSEEK_API_KEY"]
23
+ return OpenAI(
24
+ api_key=key,
25
+ base_url=os.getenv("DEEPSEEK_BASE_URL", "https://api.deepseek.com"),
26
+ timeout=float(os.getenv("EXTERNAL_LLM_TIMEOUT_S", "240")),
27
+ max_retries=1,
28
+ )
29
+
30
+
31
+ def complete(messages, *, model=None, max_completion_tokens=1200):
32
+ requested = model or os.getenv("DEEPSEEK_MODEL", "deepseek-flash")
33
+ mode = _config()
34
+ # DeepSeek's Chat Completions interface uses max_tokens, not Azure's
35
+ # max_completion_tokens. With thinking disabled, it budgets final text.
36
+ resp = create_client().chat.completions.create(
37
+ model=requested,
38
+ messages=messages,
39
+ stream=False,
40
+ max_tokens=int(max_completion_tokens),
41
+ extra_body={"thinking": {"type": mode}},
42
+ )
43
+ if not resp.choices:
44
+ raise RuntimeError("DeepSeek returned no choices")
45
+ choice = resp.choices[0]
46
+ usage = resp.usage
47
+ prompt_details = getattr(usage, "prompt_tokens_details", None) if usage else None
48
+ cached = (getattr(usage, "prompt_cache_hit_tokens", None) if usage else None)
49
+ if cached is None and prompt_details is not None:
50
+ cached = getattr(prompt_details, "cached_tokens", None)
51
+ return {
52
+ "answer": choice.message.content or "",
53
+ "provider": "deepseek",
54
+ "model_requested": requested,
55
+ "model_returned": resp.model,
56
+ "finish_reason": choice.finish_reason,
57
+ "thinking_mode": mode,
58
+ "usage": {
59
+ "input_tokens": usage.prompt_tokens if usage else None,
60
+ "output_tokens": usage.completion_tokens if usage else None,
61
+ "total_tokens": usage.total_tokens if usage else None,
62
+ "cached_input_tokens": cached,
63
+ },
64
+ }