Spaces:
Sleeping
Sleeping
Update api_handler.py
Browse files- api_handler.py +51 -14
api_handler.py
CHANGED
|
@@ -88,6 +88,39 @@ class APIHandler:
|
|
| 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
|
|
@@ -95,9 +128,9 @@ class APIHandler:
|
|
| 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
|
|
@@ -106,16 +139,17 @@ class APIHandler:
|
|
| 106 |
return "ERROR: Brak po艂膮czenia z API (nieprawid艂owy klucz)"
|
| 107 |
|
| 108 |
try:
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
| 119 |
return response.choices[0].message.content
|
| 120 |
except Exception as e:
|
| 121 |
error_msg = str(e)
|
|
@@ -141,12 +175,15 @@ class APIHandler:
|
|
| 141 |
Returns:
|
| 142 |
float: Szacunkowy koszt w USD
|
| 143 |
"""
|
| 144 |
-
# Ceny za 1M token贸w (stan na
|
| 145 |
pricing = {
|
| 146 |
"gpt-4o": {"input": 2.50, "output": 10.00},
|
| 147 |
"gpt-4-turbo": {"input": 10.00, "output": 30.00},
|
| 148 |
"gpt-4": {"input": 30.00, "output": 60.00},
|
| 149 |
-
"gpt-3.5-turbo": {"input": 0.50, "output": 1.50}
|
|
|
|
|
|
|
|
|
|
| 150 |
}
|
| 151 |
|
| 152 |
# Znajd藕 odpowiedni cennik
|
|
|
|
| 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
|
|
|
|
| 128 |
Args:
|
| 129 |
prompt: Tekst promptu systemowego
|
| 130 |
model: Model OpenAI
|
| 131 |
+
temperature: Temperatura (0.0-2.0) - ignorowane dla modeli o1
|
| 132 |
max_tokens: Maksymalna d艂ugo艣膰 odpowiedzi
|
| 133 |
+
top_p: Nucleus sampling parameter - ignorowane dla modeli o1
|
| 134 |
|
| 135 |
Returns:
|
| 136 |
str: Wygenerowana odpowied藕 lub komunikat b艂臋du
|
|
|
|
| 139 |
return "ERROR: Brak po艂膮czenia z API (nieprawid艂owy klucz)"
|
| 140 |
|
| 141 |
try:
|
| 142 |
+
# Przygotuj parametry dostosowane do modelu
|
| 143 |
+
params = self._get_model_parameters(model, temperature, max_tokens, top_p)
|
| 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 |
+
# Wywo艂aj API z odpowiednimi parametrami
|
| 152 |
+
response = self.client.chat.completions.create(**params)
|
| 153 |
return response.choices[0].message.content
|
| 154 |
except Exception as e:
|
| 155 |
error_msg = str(e)
|
|
|
|
| 175 |
Returns:
|
| 176 |
float: Szacunkowy koszt w USD
|
| 177 |
"""
|
| 178 |
+
# Ceny za 1M token贸w (stan na grudzie艅 2024)
|
| 179 |
pricing = {
|
| 180 |
"gpt-4o": {"input": 2.50, "output": 10.00},
|
| 181 |
"gpt-4-turbo": {"input": 10.00, "output": 30.00},
|
| 182 |
"gpt-4": {"input": 30.00, "output": 60.00},
|
| 183 |
+
"gpt-3.5-turbo": {"input": 0.50, "output": 1.50},
|
| 184 |
+
"o1-preview": {"input": 15.00, "output": 60.00},
|
| 185 |
+
"o1-mini": {"input": 3.00, "output": 12.00},
|
| 186 |
+
"o1": {"input": 15.00, "output": 60.00}
|
| 187 |
}
|
| 188 |
|
| 189 |
# Znajd藕 odpowiedni cennik
|