Spaces:
Running
Running
Update api_handler.py
Browse files- api_handler.py +36 -45
api_handler.py
CHANGED
|
@@ -88,49 +88,16 @@ class APIHandler:
|
|
| 88 |
print(f"B艂膮d pobierania modeli: {e}")
|
| 89 |
return ["gpt-4o", "gpt-4", "gpt-3.5-turbo"]
|
| 90 |
|
| 91 |
-
def _get_model_parameters(self, model, temperature, max_tokens, top_p):
|
| 92 |
-
"""
|
| 93 |
-
Przygotowuje parametry API dla danego modelu
|
| 94 |
-
|
| 95 |
-
Args:
|
| 96 |
-
model: Nazwa modelu
|
| 97 |
-
temperature: Temperatura (0.0-2.0)
|
| 98 |
-
max_tokens: Maksymalna d艂ugo艣膰 odpowiedzi
|
| 99 |
-
top_p: Nucleus sampling parameter
|
| 100 |
-
|
| 101 |
-
Returns:
|
| 102 |
-
dict: S艂ownik z parametrami gotowymi do przekazania do API
|
| 103 |
-
"""
|
| 104 |
-
# Modele o1 wymagaj膮 innych parametr贸w
|
| 105 |
-
is_o1_model = model.startswith('o1')
|
| 106 |
-
|
| 107 |
-
params = {
|
| 108 |
-
"model": model,
|
| 109 |
-
"messages": [] # B臋dzie wype艂nione w generate_response
|
| 110 |
-
}
|
| 111 |
-
|
| 112 |
-
if is_o1_model:
|
| 113 |
-
# Modele o1 u偶ywaj膮 max_completion_tokens zamiast max_tokens
|
| 114 |
-
# i nie akceptuj膮 temperature/top_p
|
| 115 |
-
params["max_completion_tokens"] = max_tokens
|
| 116 |
-
else:
|
| 117 |
-
# Standardowe modele (gpt-4, gpt-3.5, fine-tuned)
|
| 118 |
-
params["max_tokens"] = max_tokens
|
| 119 |
-
params["temperature"] = temperature
|
| 120 |
-
params["top_p"] = top_p
|
| 121 |
-
|
| 122 |
-
return params
|
| 123 |
-
|
| 124 |
def generate_response(self, prompt, model="gpt-4o", temperature=0.1, max_tokens=2000, top_p=1.0):
|
| 125 |
"""
|
| 126 |
-
Generuje odpowied藕 z OpenAI API
|
| 127 |
|
| 128 |
Args:
|
| 129 |
prompt: Tekst promptu systemowego
|
| 130 |
model: Model OpenAI
|
| 131 |
-
temperature: Temperatura (0.0-2.0)
|
| 132 |
max_tokens: Maksymalna d艂ugo艣膰 odpowiedzi
|
| 133 |
-
top_p: Nucleus sampling parameter
|
| 134 |
|
| 135 |
Returns:
|
| 136 |
str: Wygenerowana odpowied藕 lub komunikat b艂臋du
|
|
@@ -138,21 +105,45 @@ class APIHandler:
|
|
| 138 |
if not self.client:
|
| 139 |
return "ERROR: Brak po艂膮czenia z API (nieprawid艂owy klucz)"
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
# Dodaj wiadomo艣ci
|
| 146 |
-
params["messages"] = [
|
| 147 |
{"role": "system", "content": prompt},
|
| 148 |
{"role": "user", "content": "Please provide your response based on the system prompt."}
|
| 149 |
-
]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
-
|
| 152 |
-
|
|
|
|
| 153 |
return response.choices[0].message.content
|
|
|
|
| 154 |
except Exception as e:
|
| 155 |
error_msg = str(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
if "429" in error_msg:
|
| 157 |
return f"ERROR: Rate limit exceeded - poczekaj chwil臋"
|
| 158 |
elif "insufficient_quota" in error_msg:
|
|
|
|
| 88 |
print(f"B艂膮d pobierania modeli: {e}")
|
| 89 |
return ["gpt-4o", "gpt-4", "gpt-3.5-turbo"]
|
| 90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
def generate_response(self, prompt, model="gpt-4o", temperature=0.1, max_tokens=2000, top_p=1.0):
|
| 92 |
"""
|
| 93 |
+
Generuje odpowied藕 z OpenAI API z automatycznym fallback dla nowych modeli
|
| 94 |
|
| 95 |
Args:
|
| 96 |
prompt: Tekst promptu systemowego
|
| 97 |
model: Model OpenAI
|
| 98 |
+
temperature: Temperatura (0.0-2.0)
|
| 99 |
max_tokens: Maksymalna d艂ugo艣膰 odpowiedzi
|
| 100 |
+
top_p: Nucleus sampling parameter
|
| 101 |
|
| 102 |
Returns:
|
| 103 |
str: Wygenerowana odpowied藕 lub komunikat b艂臋du
|
|
|
|
| 105 |
if not self.client:
|
| 106 |
return "ERROR: Brak po艂膮czenia z API (nieprawid艂owy klucz)"
|
| 107 |
|
| 108 |
+
# Przygotuj parametry API - najpierw spr贸buj ze starym API (max_tokens)
|
| 109 |
+
api_params = {
|
| 110 |
+
"model": model,
|
| 111 |
+
"messages": [
|
|
|
|
|
|
|
| 112 |
{"role": "system", "content": prompt},
|
| 113 |
{"role": "user", "content": "Please provide your response based on the system prompt."}
|
| 114 |
+
],
|
| 115 |
+
"temperature": temperature,
|
| 116 |
+
"max_tokens": max_tokens, # Starsze modele (gpt-4, gpt-3.5, fine-tuned)
|
| 117 |
+
"top_p": top_p
|
| 118 |
+
}
|
| 119 |
|
| 120 |
+
try:
|
| 121 |
+
# Pierwsza pr贸ba: u偶yj max_tokens (kompatybilno艣膰 ze starszymi modelami)
|
| 122 |
+
response = self.client.chat.completions.create(**api_params)
|
| 123 |
return response.choices[0].message.content
|
| 124 |
+
|
| 125 |
except Exception as e:
|
| 126 |
error_msg = str(e)
|
| 127 |
+
|
| 128 |
+
# Automatyczny fallback: je艣li b艂膮d dotyczy max_tokens, prze艂膮cz na max_completion_tokens
|
| 129 |
+
if "max_tokens" in error_msg and "max_completion_tokens" in error_msg:
|
| 130 |
+
try:
|
| 131 |
+
# Usu艅 stary parametr i dodaj nowy
|
| 132 |
+
api_params.pop("max_tokens")
|
| 133 |
+
api_params["max_completion_tokens"] = max_tokens
|
| 134 |
+
|
| 135 |
+
# Usu艅 r贸wnie偶 temperature i top_p (nowe modele ich nie akceptuj膮)
|
| 136 |
+
api_params.pop("temperature", None)
|
| 137 |
+
api_params.pop("top_p", None)
|
| 138 |
+
|
| 139 |
+
# Pon贸w zapytanie z nowymi parametrami
|
| 140 |
+
response = self.client.chat.completions.create(**api_params)
|
| 141 |
+
return response.choices[0].message.content
|
| 142 |
+
|
| 143 |
+
except Exception as retry_error:
|
| 144 |
+
return f"ERROR: Retry failed: {str(retry_error)[:200]}"
|
| 145 |
+
|
| 146 |
+
# Standardowa obs艂uga b艂臋d贸w
|
| 147 |
if "429" in error_msg:
|
| 148 |
return f"ERROR: Rate limit exceeded - poczekaj chwil臋"
|
| 149 |
elif "insufficient_quota" in error_msg:
|