mirror of
https://github.com/Shiva108/ai-llm-red-team-handbook.git
synced 2026-08-27 05:12:34 +02:00
feat: Add webhook notifications and rate limit retry mechanism to LLM client.
This commit is contained in:
@@ -1,51 +0,0 @@
|
||||
# file: client.py
|
||||
|
||||
import json
|
||||
from typing import List, Dict, Any, Optional
|
||||
|
||||
import requests
|
||||
|
||||
from config import LLMConfig, DEFAULT_LLM_CONFIG
|
||||
|
||||
|
||||
class LLMClient:
|
||||
"""Simple generic client for chat-style LLM APIs."""
|
||||
|
||||
def __init__(self, config: Optional[LLMConfig] = None):
|
||||
self.config = config or DEFAULT_LLM_CONFIG
|
||||
|
||||
def chat(
|
||||
self,
|
||||
messages: List[Dict[str, str]],
|
||||
max_tokens: int = 512,
|
||||
temperature: float = 0.2,
|
||||
extra_params: Optional[Dict[str, Any]] = None,
|
||||
) -> str:
|
||||
url = f"{self.config.api_base}/chat/completions"
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
if self.config.api_key:
|
||||
headers["Authorization"] = f"Bearer {self.config.api_key}"
|
||||
if self.config.extra_headers:
|
||||
headers.update(self.config.extra_headers)
|
||||
|
||||
payload: Dict[str, Any] = {
|
||||
"model": self.config.model,
|
||||
"messages": messages,
|
||||
"max_tokens": max_tokens,
|
||||
"temperature": temperature,
|
||||
}
|
||||
if self.config.extra_params:
|
||||
payload.update(self.config.extra_params)
|
||||
if extra_params:
|
||||
payload.update(extra_params)
|
||||
|
||||
resp = requests.post(url, headers=headers, data=json.dumps(payload), timeout=self.config.timeout)
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
|
||||
try:
|
||||
return data["choices"][0]["message"]["content"]
|
||||
except (KeyError, IndexError) as exc:
|
||||
raise RuntimeError(f"Unexpected LLM response format: {data}") from exc
|
||||
@@ -1,43 +0,0 @@
|
||||
# file: config.py
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional, Dict, Any
|
||||
|
||||
|
||||
@dataclass
|
||||
class LLMConfig:
|
||||
api_base: str # e.g. "https://api.openai.com/v1"
|
||||
api_key: str # or a local token; can be empty for local deployments
|
||||
model: str # e.g. "gpt-4.1" or "local-llm"
|
||||
timeout: int = 60
|
||||
extra_headers: Optional[Dict[str, str]] = None
|
||||
extra_params: Optional[Dict[str, Any]] = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class TestConfig:
|
||||
max_tokens: int = 1024
|
||||
temperature: float = 0.2
|
||||
output_dir: str = "reports"
|
||||
# synthetic “sensitive” markers used in tests – replace with your own
|
||||
synthetic_identifiers: Dict[str, str] = None
|
||||
|
||||
|
||||
DEFAULT_LLM_CONFIG = LLMConfig(
|
||||
api_base="http://localhost:11434/v1", # example: local LLM endpoint
|
||||
api_key="",
|
||||
model="local-llm",
|
||||
extra_headers=None,
|
||||
extra_params=None,
|
||||
)
|
||||
|
||||
DEFAULT_TEST_CONFIG = TestConfig(
|
||||
max_tokens=1024,
|
||||
temperature=0.2,
|
||||
output_dir="reports",
|
||||
synthetic_identifiers={
|
||||
"employee_id": "EMP-999999",
|
||||
"customer_id": "CUST-123456",
|
||||
"secret_project": "PROJECT-DRAGONFIRE",
|
||||
},
|
||||
)
|
||||
Reference in New Issue
Block a user