Spaces:
Sleeping
Sleeping
Create api/openrouter.py
Browse files- api/openrouter.py +68 -0
api/openrouter.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
from typing import Dict, List, Optional
|
| 5 |
+
|
| 6 |
+
class OpenRouterClient:
|
| 7 |
+
def __init__(self, api_key: str = ""):
|
| 8 |
+
self.base_url = "https://openrouter.ai/api/v1"
|
| 9 |
+
self.api_key = api_key or os.getenv("OPENROUTER_API_KEY", "")
|
| 10 |
+
self.headers = {
|
| 11 |
+
"Authorization": f"Bearer {self.api_key}",
|
| 12 |
+
"Content-Type": "application/json",
|
| 13 |
+
"HTTP-Referer": "https://huggingface.co/spaces", # Required by OpenRouter
|
| 14 |
+
"X-Title": "OpenRouter AI Hub" # Optional
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
def chat_completion(
|
| 18 |
+
self,
|
| 19 |
+
model: str,
|
| 20 |
+
messages: List[Dict[str, str]],
|
| 21 |
+
temperature: Optional[float] = 0.7,
|
| 22 |
+
max_tokens: Optional[int] = 1024,
|
| 23 |
+
**kwargs
|
| 24 |
+
) -> Dict:
|
| 25 |
+
"""
|
| 26 |
+
Get chat completion from OpenRouter
|
| 27 |
+
|
| 28 |
+
Parameters:
|
| 29 |
+
- model: Model identifier (e.g., "openai/gpt-3.5-turbo")
|
| 30 |
+
- messages: List of message dictionaries with role and content
|
| 31 |
+
- temperature: Creativity parameter (0-2)
|
| 32 |
+
- max_tokens: Maximum length of response
|
| 33 |
+
"""
|
| 34 |
+
payload = {
|
| 35 |
+
"model": model,
|
| 36 |
+
"messages": messages,
|
| 37 |
+
"temperature": temperature,
|
| 38 |
+
"max_tokens": max_tokens,
|
| 39 |
+
**kwargs
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
try:
|
| 43 |
+
response = requests.post(
|
| 44 |
+
f"{self.base_url}/chat/completions",
|
| 45 |
+
headers=self.headers,
|
| 46 |
+
data=json.dumps(payload)
|
| 47 |
+
|
| 48 |
+
response.raise_for_status()
|
| 49 |
+
return response.json()
|
| 50 |
+
except requests.exceptions.RequestException as e:
|
| 51 |
+
raise Exception(f"OpenRouter API request failed: {str(e)}")
|
| 52 |
+
|
| 53 |
+
def list_models(self) -> List[Dict]:
|
| 54 |
+
"""
|
| 55 |
+
List all available models from OpenRouter
|
| 56 |
+
"""
|
| 57 |
+
try:
|
| 58 |
+
response = requests.get(
|
| 59 |
+
f"{self.base_url}/models",
|
| 60 |
+
headers=self.headers)
|
| 61 |
+
|
| 62 |
+
response.raise_for_status()
|
| 63 |
+
return response.json().get("data", [])
|
| 64 |
+
except requests.exceptions.RequestException as e:
|
| 65 |
+
raise Exception(f"OpenRouter API request failed: {str(e)}")
|
| 66 |
+
|
| 67 |
+
def get_openrouter_client():
|
| 68 |
+
return OpenRouterClient()
|