|
|
|
|
|
import os |
|
from typing import Optional |
|
|
|
from huggingface_hub import InferenceClient |
|
from tavily import TavilyClient |
|
|
|
|
|
|
|
HF_TOKEN = os.getenv('HF_TOKEN') |
|
|
|
def get_inference_client(model_id: str, provider: str = "auto", user_token: Optional[str] = None): |
|
""" |
|
Return an InferenceClient. |
|
Prioritizes the user's token for billing if provided. |
|
Falls back to the environment HF_TOKEN otherwise. |
|
""" |
|
|
|
token_to_use = user_token or HF_TOKEN |
|
|
|
if not token_to_use: |
|
raise ValueError("An API token must be provided either by the user or as an environment variable (HF_TOKEN).") |
|
|
|
if model_id == "moonshotai/Kimi-K2-Instruct": |
|
provider = "groq" |
|
|
|
|
|
|
|
|
|
return InferenceClient( |
|
provider=provider, |
|
api_key=token_to_use |
|
) |
|
|
|
|
|
TAVILY_API_KEY = os.getenv('TAVILY_API_KEY') |
|
tavily_client = None |
|
if TAVILY_API_KEY: |
|
try: |
|
tavily_client = TavilyClient(api_key=TAVILY_API_KEY) |
|
except Exception as e: |
|
print(f"Failed to initialize Tavily client: {e}") |
|
tavily_client = None |