File size: 1,453 Bytes
4f22252
 
cbf9f33
4f22252
4c20612
4f22252
96b76f8
0adaacb
96b76f8
4f22252
96b76f8
0adaacb
4f22252
 
 
 
 
 
 
 
 
 
 
 
3e19edc
96b76f8
3d99097
4f22252
 
 
3e19edc
 
4f22252
3e19edc
034cea3
96b76f8
 
034cea3
 
 
 
 
 
3d99097
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
# hf_client.py (REVISED)

import os
from typing import Optional

from huggingface_hub import InferenceClient
from tavily import TavilyClient

# HF Inference Client
# This is your token, used as a fallback for local testing or when a user token isn't available.
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.
    """
    # Determine which token to use for the API call
    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"
    
    # Initialize the client with the selected token.
    # The Hugging Face Hub automatically bills the account associated with the token.
    # We do NOT use the 'bill_to' parameter for this.
    return InferenceClient(
        provider=provider,
        api_key=token_to_use
    )

# Tavily Search Client
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