-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhf_client.py
More file actions
86 lines (71 loc) Β· 3.03 KB
/
Copy pathhf_client.py
File metadata and controls
86 lines (71 loc) Β· 3.03 KB
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
82
83
84
85
86
# hf_client.py
# Hugging Face Inference adapter that mimics the Anthropic SDK interface
# Lets us run the agent for free using HF Inference Providers
# Uses an OpenAI-compatible chat completion endpoint
# Haofei Sun - CSE 5360
import os
# default model - Kimi K2 is one of the best free options on HF
DEFAULT_MODEL = "moonshotai/Kimi-K2-Instruct-0905"
class _TextBlock:
def __init__(self, text: str):
self.type = "text"
self.text = text
class _Response:
def __init__(self, text: str):
self.content = [_TextBlock(text)]
class HFAnthropicAdapter:
"""
Drop-in replacement for anthropic.Anthropic() that calls
HF Inference Providers via the OpenAI-compatible router.
The .messages.create() signature matches Anthropic exactly so
the rest of the agent code doesn't need to change.
"""
class _Messages:
def __init__(self, parent):
self.parent = parent
def create(self, model, max_tokens, messages, system="",
thinking=None, **kwargs):
import time
# convert anthropic messages format to openai format
openai_messages = []
if system:
# strengthen JSON contract for open-weight models
system = system + (
"\n\nIMPORTANT: Respond with ONLY a valid JSON object/array. "
"No prose, no markdown fences, no commentary before or after."
)
openai_messages.append({"role": "system", "content": system})
for m in messages:
openai_messages.append({"role": m["role"], "content": m["content"]})
# open-weight models sometimes return empty strings on first try β
# retry up to 3 times, lowering temperature on each retry for stability
last_text = ""
for attempt in range(3):
temp = [0.3, 0.1, 0.0][attempt]
try:
completion = self.parent.client.chat.completions.create(
model=self.parent.model,
messages=openai_messages,
max_tokens=max(max_tokens, 1500),
temperature=temp,
)
text = completion.choices[0].message.content or ""
if text.strip():
return _Response(text)
last_text = text
except Exception:
if attempt == 2:
raise
time.sleep(0.6 * (attempt + 1))
return _Response(last_text)
def __init__(self, api_key: str = None, model: str = DEFAULT_MODEL):
try:
from openai import OpenAI
except ImportError:
raise ImportError("HF backend requires `pip install openai`")
self.client = OpenAI(
base_url="https://router.huggingface.co/v1",
api_key=api_key or os.environ.get("HF_TOKEN"),
)
self.model = model
self.messages = self._Messages(self)