Spaces:
Sleeping
Sleeping
OpenEnv Contributor commited on
Commit ·
5ffc5cb
1
Parent(s): 0ed031d
fix: make HF_TOKEN mandatory at startup per hackathon requirements
Browse files- inference.py +30 -31
inference.py
CHANGED
|
@@ -39,20 +39,42 @@ from cloud_soc_env import CloudSOCEnv, CloudState, SCENARIOS
|
|
| 39 |
# =============================================================================
|
| 40 |
|
| 41 |
# Environment variables with defaults
|
| 42 |
-
# Using Together AI API for Qwen2.5-3B-Instruct
|
| 43 |
# Together AI: https://www.together.ai (free tier available, no CC required)
|
| 44 |
API_BASE_URL = os.getenv("API_BASE_URL", "https://api.together.xyz/v1")
|
| 45 |
MODEL_NAME = os.getenv("MODEL_NAME", "meta-llama/Qwen2.5-3B-Instruct")
|
| 46 |
-
HF_TOKEN = os.getenv("HF_TOKEN"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
# Initialize OpenAI-compatible client
|
| 49 |
client = OpenAI(
|
| 50 |
base_url=API_BASE_URL,
|
| 51 |
-
api_key=HF_TOKEN
|
| 52 |
)
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
# Memory pressure settings (Mechanic #6)
|
| 58 |
# Optimized for 2vCPU/8GB RAM - reduce context window
|
|
@@ -174,37 +196,14 @@ def emit_end(success: bool, steps: int, rewards: List[float]):
|
|
| 174 |
|
| 175 |
def call_llm(messages: List[Dict[str, str]], temperature: float = 0.5, retry_count: int = 0) -> str:
|
| 176 |
"""
|
| 177 |
-
Call the LLM with given messages via
|
| 178 |
|
| 179 |
Optimized for Qwen2.5-3B-Instruct on 2vCPU/8GB RAM.
|
| 180 |
Implements adaptive temperature: increases on retries for diversity.
|
| 181 |
Returns the raw response content.
|
| 182 |
-
"""
|
| 183 |
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
# Validate HF_TOKEN at runtime (required per hackathon guidelines)
|
| 187 |
-
if not HF_TOKEN:
|
| 188 |
-
# Print setup instructions only once
|
| 189 |
-
if not _hf_token_warning_printed:
|
| 190 |
-
error_msg = (
|
| 191 |
-
"ERROR: API token (HF_TOKEN) is required for inference.\n"
|
| 192 |
-
" Setup Instructions:\n"
|
| 193 |
-
" 1. Go to https://www.together.ai (sign up free, no CC required)\n"
|
| 194 |
-
" 2. Get your API key from https://www.together.ai/settings/api-keys\n"
|
| 195 |
-
" 3. In the HF Space settings, add HF_TOKEN as a Secret with your Together AI key\n"
|
| 196 |
-
" 4. Restart the Space\n"
|
| 197 |
-
)
|
| 198 |
-
print(error_msg, file=sys.stderr)
|
| 199 |
-
sys.stderr.flush()
|
| 200 |
-
_hf_token_warning_printed = True
|
| 201 |
-
|
| 202 |
-
# Return error response instead of crashing
|
| 203 |
-
return json.dumps({
|
| 204 |
-
"thought": "Cannot call LLM: API token not configured. See stderr for setup instructions.",
|
| 205 |
-
"tool": "aws.soc.get_alerts",
|
| 206 |
-
"args": {}
|
| 207 |
-
})
|
| 208 |
|
| 209 |
# Adaptive temperature: increase slightly on retries to get different outputs
|
| 210 |
# Lower baseline temp (0.5) for 3B model to be more deterministic
|
|
|
|
| 39 |
# =============================================================================
|
| 40 |
|
| 41 |
# Environment variables with defaults
|
| 42 |
+
# Using Together AI API for Qwen2.5-3B-Instruct
|
| 43 |
# Together AI: https://www.together.ai (free tier available, no CC required)
|
| 44 |
API_BASE_URL = os.getenv("API_BASE_URL", "https://api.together.xyz/v1")
|
| 45 |
MODEL_NAME = os.getenv("MODEL_NAME", "meta-llama/Qwen2.5-3B-Instruct")
|
| 46 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 47 |
+
|
| 48 |
+
# Validate HF_TOKEN is provided (required per hackathon guidelines)
|
| 49 |
+
if HF_TOKEN is None:
|
| 50 |
+
raise ValueError(
|
| 51 |
+
"HF_TOKEN environment variable is required.\n"
|
| 52 |
+
"To set it:\n"
|
| 53 |
+
" 1. Sign up at https://www.together.ai (free, no CC)\n"
|
| 54 |
+
" 2. Get your API key: https://www.together.ai/settings/api-keys\n"
|
| 55 |
+
" 3. In HF Space Settings → Secrets, add: HF_TOKEN=<your_key>\n"
|
| 56 |
+
" 4. Restart the Space"
|
| 57 |
+
)
|
| 58 |
|
| 59 |
# Initialize OpenAI-compatible client
|
| 60 |
client = OpenAI(
|
| 61 |
base_url=API_BASE_URL,
|
| 62 |
+
api_key=HF_TOKEN
|
| 63 |
)
|
| 64 |
|
| 65 |
+
|
| 66 |
+
class AgentState(Enum):
|
| 67 |
+
"""Track agent's cognitive state for adaptive prompting"""
|
| 68 |
+
EXPLORING = "exploring"
|
| 69 |
+
INVESTIGATING = "investigating"
|
| 70 |
+
CONTAINING = "containing"
|
| 71 |
+
RECOVERING = "recovering"
|
| 72 |
+
CLOSING = "closing"
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
# =============================================================================
|
| 76 |
+
# MEMORY PRESSURE MANAGER (Mechanic #6)
|
| 77 |
+
# =============================================================================
|
| 78 |
|
| 79 |
# Memory pressure settings (Mechanic #6)
|
| 80 |
# Optimized for 2vCPU/8GB RAM - reduce context window
|
|
|
|
| 196 |
|
| 197 |
def call_llm(messages: List[Dict[str, str]], temperature: float = 0.5, retry_count: int = 0) -> str:
|
| 198 |
"""
|
| 199 |
+
Call the LLM with given messages via Together AI API.
|
| 200 |
|
| 201 |
Optimized for Qwen2.5-3B-Instruct on 2vCPU/8GB RAM.
|
| 202 |
Implements adaptive temperature: increases on retries for diversity.
|
| 203 |
Returns the raw response content.
|
|
|
|
| 204 |
|
| 205 |
+
Note: HF_TOKEN is validated at startup, so guaranteed to be set here.
|
| 206 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
|
| 208 |
# Adaptive temperature: increase slightly on retries to get different outputs
|
| 209 |
# Lower baseline temp (0.5) for 3B model to be more deterministic
|