Spaces:
Paused
Paused
Update api_handler.py
Browse files87 unmodified lines8889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361 unmodified line13813914014114214314414514614714814915015115215315415515615715887 unmodified linesprint(f"B艂膮d pobierania modeli: {e}")return ["gpt-4o", "gpt-4", "gpt-3.5-turbo"]def _get_model_parameters(self, model, temperature, max_tokens, top_p):"""Przygotowuje parametry API dla danego modeluArgs:model: Nazwa modelutemperature: Temperatura (0.0-2.0)max_tokens: Maksymalna d艂ugo艣膰 odpowiedzitop_p: Nucleus sampling parameterReturns:dict: S艂ownik z parametrami gotowymi do przekazania do API"""# Modele o1 wymagaj膮 innych parametr贸wis_o1_model = model.startswith('o1')params = {"model": model,"messages": [] # B臋dzie wype艂nione w generate_response}if is_o1_model:# Modele o1 u偶ywaj膮 max_completion_tokens zamiast max_tokens# i nie akceptuj膮 temperature/top_pparams["max_completion_tokens"] = max_tokenselse:# Standardowe modele (gpt-4, gpt-3.5, fine-tuned)params["max_tokens"] = max_tokensparams["temperature"] = temperatureparams["top_p"] = top_preturn paramsdef generate_response(self, prompt, model="gpt-4o", temperature=0.1, max_tokens=2000, top_p=1.0):"""Generuje odpowied藕 z OpenAI APIArgs:prompt: Tekst promptu systemowegomodel: Model OpenAItemperature: Temperatura (0.0-2.0) - ignorowane dla modeli o1max_tokens: Maksymalna d艂ugo艣膰 odpowiedzitop_p: Nucleus sampling parameter - ignorowane dla modeli o1Returns:str: Wygenerowana odpowied藕 lub komunikat b艂臋du1 unmodified lineif not self.client:return "ERROR: Brak po艂膮czenia z API (nieprawid艂owy klucz)"try:# Przygotuj parametry dostosowane do modeluparams = self._get_model_parameters(model, temperature, max_tokens, top_p)# Dodaj wiadomo艣ciparams["messages"] = [{"role": "system", "content": prompt},{"role": "user", "content": "Please provide your response based on the system prompt."}]# Wywo艂aj API z odpowiednimi parametramiresponse = self.client.chat.completions.create(**params)return response.choices[0].message.contentexcept Exception as e:error_msg = str(e)if "429" in error_msg:return f"ERROR: Rate limit exceeded - poczekaj chwil臋"elif "insufficient_quota" in error_msg:87 unmodified lines8889909192939495969798991001011021031 unmodified line10510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814987 unmodified linesprint(f"B艂膮d pobierania modeli: {e}")return ["gpt-4o", "gpt-4", "gpt-3.5-turbo"]def generate_response(self, prompt, model="gpt-4o", temperature=0.1, max_tokens=2000, top_p=1.0):"""Generuje odpowied藕 z OpenAI API z automatycznym fallback dla nowych modeliArgs:prompt: Tekst promptu systemowegomodel: Model OpenAItemperature: Temperatura (0.0-2.0)max_tokens: Maksymalna d艂ugo艣膰 odpowiedzitop_p: Nucleus sampling parameterReturns:str: Wygenerowana odpowied藕 lub komunikat b艂臋du1 unmodified lineif not self.client:return "ERROR: Brak po艂膮czenia z API (nieprawid艂owy klucz)"# Przygotuj parametry API - najpierw spr贸buj ze starym API (max_tokens)api_params = {"model": model,"messages": [{"role": "system", "content": prompt},{"role": "user", "content": "Please provide your response based on the system prompt."}],"temperature": temperature,"max_tokens": max_tokens, # Starsze modele (gpt-4, gpt-3.5, fine-tuned)"top_p": top_p}try:# Pierwsza pr贸ba: u偶yj max_tokens (kompatybilno艣膰 ze starszymi modelami)response = self.client.chat.completions.create(**api_params)return response.choices[0].message.contentexcept Exception as e:error_msg = str(e)# Automatyczny fallback: je艣li b艂膮d dotyczy max_tokens, prze艂膮cz na max_completion_tokensif "max_tokens" in error_msg and "max_completion_tokens" in error_msg:try:# Usu艅 stary parametr i dodaj nowyapi_params.pop("max_tokens")api_params["max_completion_tokens"] = max_tokens# Usu艅 r贸wnie偶 temperature i top_p (nowe modele ich nie akceptuj膮)api_params.pop("temperature", None)api_params.pop("top_p", None)# Pon贸w zapytanie z nowymi parametramiresponse = self.client.chat.completions.create(**api_params)return response.choices[0].message.contentexcept Exception as retry_error:return f"ERROR: Retry failed: {str(retry_error)[:200]}"# Standardowa obs艂uga b艂臋d贸wif "429" in error_msg:return f"ERROR: Rate limit exceeded - poczekaj chwil臋"elif "insufficient_quota" in error_msg:
api_handler.py+36-45