Spaces:
Sleeping
Sleeping
Delete unified_llm_sender2.py
Browse files- unified_llm_sender2.py +0 -235
unified_llm_sender2.py
DELETED
|
@@ -1,235 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
ماژول مدیریت یکپارچه LLM ها
|
| 3 |
-
پشتیبانی از: ChatGPT, Grok, Gemini, DeepSeek
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
import os
|
| 7 |
-
import requests
|
| 8 |
-
import logging
|
| 9 |
-
from typing import Optional
|
| 10 |
-
|
| 11 |
-
logger = logging.getLogger(__name__)
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
class UnifiedLLMSender:
|
| 15 |
-
"""کلاس یکپارچه برای ارسال به LLM های مختلف"""
|
| 16 |
-
|
| 17 |
-
MODELS = {
|
| 18 |
-
'gpt-4o-mini': {
|
| 19 |
-
'provider': 'openai',
|
| 20 |
-
'api_base': 'https://api.openai.com/v1/chat/completions',
|
| 21 |
-
'env_key': 'OPENAI_API_KEY'
|
| 22 |
-
},
|
| 23 |
-
'gpt-4o': {
|
| 24 |
-
'provider': 'openai',
|
| 25 |
-
'api_base': 'https://api.openai.com/v1/chat/completions',
|
| 26 |
-
'env_key': 'OPENAI_API_KEY'
|
| 27 |
-
},
|
| 28 |
-
'o1': {
|
| 29 |
-
'provider': 'openai',
|
| 30 |
-
'api_base': 'https://api.openai.com/v1/chat/completions',
|
| 31 |
-
'env_key': 'OPENAI_API_KEY'
|
| 32 |
-
},
|
| 33 |
-
'o1-mini': {
|
| 34 |
-
'provider': 'openai',
|
| 35 |
-
'api_base': 'https://api.openai.com/v1/chat/completions',
|
| 36 |
-
'env_key': 'OPENAI_API_KEY'
|
| 37 |
-
},
|
| 38 |
-
'grok-beta': {
|
| 39 |
-
'provider': 'xai',
|
| 40 |
-
'api_base': 'https://api.x.ai/v1/chat/completions',
|
| 41 |
-
'env_key': 'XAI_API_KEY'
|
| 42 |
-
},
|
| 43 |
-
'grok-2-latest': {
|
| 44 |
-
'provider': 'xai',
|
| 45 |
-
'api_base': 'https://api.x.ai/v1/chat/completions',
|
| 46 |
-
'env_key': 'XAI_API_KEY'
|
| 47 |
-
},
|
| 48 |
-
'gemini-2.0-flash-exp': {
|
| 49 |
-
'provider': 'google',
|
| 50 |
-
'api_base': 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-exp:generateContent',
|
| 51 |
-
'env_key': 'GOOGLE_API_KEY'
|
| 52 |
-
},
|
| 53 |
-
'gemini-1.5-pro': {
|
| 54 |
-
'provider': 'google',
|
| 55 |
-
'api_base': 'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-pro:generateContent',
|
| 56 |
-
'env_key': 'GOOGLE_API_KEY'
|
| 57 |
-
},
|
| 58 |
-
'deepseek-chat': {
|
| 59 |
-
'provider': 'deepseek',
|
| 60 |
-
'api_base': 'https://api.deepseek.com/v1/chat/completions',
|
| 61 |
-
'env_key': 'DEEPSEEK_API_KEY'
|
| 62 |
-
},
|
| 63 |
-
'deepseek-reasoner': {
|
| 64 |
-
'provider': 'deepseek',
|
| 65 |
-
'api_base': 'https://api.deepseek.com/v1/chat/completions',
|
| 66 |
-
'env_key': 'DEEPSEEK_API_KEY'
|
| 67 |
-
}
|
| 68 |
-
}
|
| 69 |
-
|
| 70 |
-
def __init__(self, model: str = 'gpt-4o-mini'):
|
| 71 |
-
"""
|
| 72 |
-
مقداردهی اولیه
|
| 73 |
-
|
| 74 |
-
Args:
|
| 75 |
-
model: نام مدل (مثلاً 'gpt-4o-mini', 'grok-beta', 'gemini-2.0-flash-exp', 'deepseek-chat')
|
| 76 |
-
"""
|
| 77 |
-
self.model = model
|
| 78 |
-
|
| 79 |
-
if model not in self.MODELS:
|
| 80 |
-
raise ValueError(f"مدل {model} پشتیبانی نمیشود. مدلهای موجود: {list(self.MODELS.keys())}")
|
| 81 |
-
|
| 82 |
-
self.config = self.MODELS[model]
|
| 83 |
-
self.provider = self.config['provider']
|
| 84 |
-
self.api_key = os.getenv(self.config['env_key'])
|
| 85 |
-
|
| 86 |
-
if not self.api_key:
|
| 87 |
-
logger.warning(f"⚠️ API Key برای {model} تنظیم نشده: {self.config['env_key']}")
|
| 88 |
-
|
| 89 |
-
def send(self,
|
| 90 |
-
text: str,
|
| 91 |
-
system_msg: Optional[str] = None,
|
| 92 |
-
max_tokens: int = 4096,
|
| 93 |
-
temperature: float = 0.3,
|
| 94 |
-
lang: str = 'fa') -> str:
|
| 95 |
-
"""
|
| 96 |
-
ارسال پیام به LLM
|
| 97 |
-
|
| 98 |
-
Args:
|
| 99 |
-
text: متن ورودی
|
| 100 |
-
system_msg: پیام سیستم
|
| 101 |
-
max_tokens: حداکثر توکن خروجی
|
| 102 |
-
temperature: دما
|
| 103 |
-
lang: زبان
|
| 104 |
-
|
| 105 |
-
Returns:
|
| 106 |
-
پاسخ مدل
|
| 107 |
-
"""
|
| 108 |
-
if not self.api_key:
|
| 109 |
-
return f"❌ API Key برای {self.model} موجود نیست. لطفاً {self.config['env_key']} را تنظیم کنید."
|
| 110 |
-
|
| 111 |
-
try:
|
| 112 |
-
if self.provider == 'google':
|
| 113 |
-
return self._send_google(text, system_msg, max_tokens, temperature)
|
| 114 |
-
else:
|
| 115 |
-
# OpenAI-compatible APIs (OpenAI, xAI, DeepSeek)
|
| 116 |
-
return self._send_openai_compatible(text, system_msg, max_tokens, temperature)
|
| 117 |
-
|
| 118 |
-
except Exception as e:
|
| 119 |
-
logger.error(f"❌ خطا در ارسال به {self.model}: {e}")
|
| 120 |
-
return f"❌ خطا: {str(e)}"
|
| 121 |
-
|
| 122 |
-
def _send_openai_compatible(self, text: str, system_msg: Optional[str],
|
| 123 |
-
max_tokens: int, temperature: float) -> str:
|
| 124 |
-
"""ارسال به API های سازگار با OpenAI"""
|
| 125 |
-
|
| 126 |
-
messages = []
|
| 127 |
-
if system_msg:
|
| 128 |
-
messages.append({"role": "system", "content": system_msg})
|
| 129 |
-
messages.append({"role": "user", "content": text})
|
| 130 |
-
|
| 131 |
-
headers = {
|
| 132 |
-
"Authorization": f"Bearer {self.api_key}",
|
| 133 |
-
"Content-Type": "application/json"
|
| 134 |
-
}
|
| 135 |
-
|
| 136 |
-
payload = {
|
| 137 |
-
"model": self.model,
|
| 138 |
-
"messages": messages,
|
| 139 |
-
"max_tokens": max_tokens,
|
| 140 |
-
"temperature": temperature
|
| 141 |
-
}
|
| 142 |
-
|
| 143 |
-
logger.info(f"📤 ارسال به {self.model}...")
|
| 144 |
-
|
| 145 |
-
response = requests.post(
|
| 146 |
-
self.config['api_base'],
|
| 147 |
-
headers=headers,
|
| 148 |
-
json=payload,
|
| 149 |
-
timeout=120
|
| 150 |
-
)
|
| 151 |
-
|
| 152 |
-
if response.status_code == 200:
|
| 153 |
-
result = response.json()
|
| 154 |
-
content = result['choices'][0]['message']['content']
|
| 155 |
-
logger.info(f"✅ پاسخ از {self.model} دریافت شد ({len(content)} کاراکتر)")
|
| 156 |
-
return content
|
| 157 |
-
else:
|
| 158 |
-
error_msg = f"خطای API {response.status_code}: {response.text}"
|
| 159 |
-
logger.error(f"❌ {error_msg}")
|
| 160 |
-
return f"❌ {error_msg}"
|
| 161 |
-
|
| 162 |
-
def _send_google(self, text: str, system_msg: Optional[str],
|
| 163 |
-
max_tokens: int, temperature: float) -> str:
|
| 164 |
-
"""ارسال به Google Gemini API"""
|
| 165 |
-
|
| 166 |
-
# ترکیب system message با user message
|
| 167 |
-
full_text = text
|
| 168 |
-
if system_msg:
|
| 169 |
-
full_text = f"{system_msg}\n\n{text}"
|
| 170 |
-
|
| 171 |
-
url = f"{self.config['api_base']}?key={self.api_key}"
|
| 172 |
-
|
| 173 |
-
payload = {
|
| 174 |
-
"contents": [{
|
| 175 |
-
"parts": [{
|
| 176 |
-
"text": full_text
|
| 177 |
-
}]
|
| 178 |
-
}],
|
| 179 |
-
"generationConfig": {
|
| 180 |
-
"maxOutputTokens": max_tokens,
|
| 181 |
-
"temperature": temperature
|
| 182 |
-
}
|
| 183 |
-
}
|
| 184 |
-
|
| 185 |
-
logger.info(f"📤 ارسال به {self.model}...")
|
| 186 |
-
|
| 187 |
-
response = requests.post(
|
| 188 |
-
url,
|
| 189 |
-
headers={"Content-Type": "application/json"},
|
| 190 |
-
json=payload,
|
| 191 |
-
timeout=120
|
| 192 |
-
)
|
| 193 |
-
|
| 194 |
-
if response.status_code == 200:
|
| 195 |
-
result = response.json()
|
| 196 |
-
content = result['candidates'][0]['content']['parts'][0]['text']
|
| 197 |
-
logger.info(f"✅ پاسخ از {self.model} دریافت شد ({len(content)} کاراکتر)")
|
| 198 |
-
return content
|
| 199 |
-
else:
|
| 200 |
-
error_msg = f"خطای API {response.status_code}: {response.text}"
|
| 201 |
-
logger.error(f"❌ {error_msg}")
|
| 202 |
-
return f"❌ {error_msg}"
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
# تابع کمکی برای گرفتن لیست مدلهای موجود
|
| 206 |
-
def get_available_models() -> dict:
|
| 207 |
-
"""دریافت لیست مدلهای موجود و وضعیت API key های آنها"""
|
| 208 |
-
available = {}
|
| 209 |
-
|
| 210 |
-
for model_name, config in UnifiedLLMSender.MODELS.items():
|
| 211 |
-
env_key = config['env_key']
|
| 212 |
-
has_key = bool(os.getenv(env_key))
|
| 213 |
-
available[model_name] = {
|
| 214 |
-
'provider': config['provider'],
|
| 215 |
-
'has_key': has_key,
|
| 216 |
-
'env_key': env_key
|
| 217 |
-
}
|
| 218 |
-
|
| 219 |
-
return available
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
def get_model_display_names() -> dict:
|
| 223 |
-
"""دریافت نامهای نمایشی مدلها"""
|
| 224 |
-
return {
|
| 225 |
-
'gpt-4o-mini': '🤖 ChatGPT 4o-mini',
|
| 226 |
-
'gpt-4o': '🤖 ChatGPT 4o',
|
| 227 |
-
'o1': '🤖 ChatGPT o1',
|
| 228 |
-
'o1-mini': '🤖 ChatGPT o1-mini',
|
| 229 |
-
'grok-beta': '🚀 Grok Beta',
|
| 230 |
-
'grok-2-latest': '🚀 Grok 2',
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