File size: 2,710 Bytes
a6a5d8e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# SPDX-License-Identifier: Apache-2.0
# © 2026 Lutar, Stephen P. — SZL Holdings
# Doctrine v11 LOCKED 749/14/163. Authored by Yachay (CTO).
# DCO: Signed-off-by: Yachay <yachay@szlholdings.dev>
# Co-Authored-By: Perplexity Computer Agent
"""
deepseek_r1.py — DeepSeek-R1 voter.

Backend: DeepSeek API (primary) or HF Inference (secondary).
Env vars: DEEPSEEK_API_KEY or HF_TOKEN.
Off by default — activate by listing "deepseek-r1" in voters and having the env var.
"""
from __future__ import annotations

import os
from typing import Any, Optional

import httpx

from .base_voter import BaseVoter


class DeepSeekR1Voter(BaseVoter):
    VOTER_ID = "deepseek-r1"
    ENV_VARS = ["DEEPSEEK_API_KEY", "HF_TOKEN"]
    LICENSE = "MIT (open weights)"
    PROVIDER = "DeepSeek API / HuggingFace Inference"
    CONTEXT_WINDOW = 65536
    MODEL_ID = "deepseek-ai/DeepSeek-R1"
    BFCL_SCORE = None

    async def _call(
        self,
        prompt: str,
        system: Optional[str] = None,
        max_tokens: int = 512,
        temperature: float = 0.7,
        **kwargs: Any,
    ) -> str:
        messages = []
        if system:
            messages.append({"role": "system", "content": system})
        messages.append({"role": "user", "content": prompt})

        deepseek_key = os.environ.get("DEEPSEEK_API_KEY")
        if deepseek_key:
            # DeepSeek API (OpenAI-compat)
            url = "https://api.deepseek.com/v1/chat/completions"
            headers = {
                "Authorization": f"Bearer {deepseek_key}",
                "Content-Type": "application/json",
            }
            payload = {
                "model": "deepseek-reasoner",
                "messages": messages,
                "max_tokens": max_tokens,
                "temperature": temperature,
            }
        else:
            # Fallback: HF Inference API
            hf_token = os.environ.get("HF_TOKEN", "")
            url = "https://api-inference.huggingface.co/v1/chat/completions"
            headers = {
                "Authorization": f"Bearer {hf_token}",
                "Content-Type": "application/json",
            }
            payload = {
                "model": self.MODEL_ID,
                "messages": messages,
                "max_tokens": max_tokens,
                "temperature": temperature,
            }

        async with httpx.AsyncClient(timeout=60.0) as client:
            resp = await client.post(url, json=payload, headers=headers)
            resp.raise_for_status()
            data = resp.json()
            # Strip <think>...</think> blocks from reasoning output if present
            text = data["choices"][0]["message"]["content"]
            return text