diff --git a/ai_functions.py b/ai_functions.py index 50f2f43..9c6cbc4 100644 --- a/ai_functions.py +++ b/ai_functions.py @@ -54,7 +54,7 @@ async def _openai_call(messages, model, temperature=0.2): input=messages, ) # SDK zapewnia output_text dla zwykłych odpowiedzi - return (getattr(resp, "output_text", None) or "").strip() or str(resp) + return (getattr(resp.output_text, "output_text", None) or "").strip() or str(resp.output_text) def create_vector_store(): @@ -248,15 +248,11 @@ async def handle_response( # ...przygotowanie messages/system prompt/itp. jak masz... # retry/backoff + deadline (zachowuje Twoją semantykę logowania) timeout_sec = 120 - max_retries = 3 deadline = time.time() + timeout_sec - for _ in range(max_retries): - if time.time() > deadline: - break - response = await asyncio.wait_for( - _openai_call(messages=history, model=algorithm), - timeout=max(0.1, deadline - time.time()) - ) + response = await asyncio.wait_for( + _openai_call(messages=history, model=algorithm), + timeout=max(0.1, deadline - time.time()) + ) except openai.APITimeoutError as e: # Handle timeout error, e.g. retry or log @@ -302,12 +298,7 @@ async def handle_response( logger.info("Historia wysłana:") logger.info(history) await asyncio.sleep(15) - result = "" - logger.debug("Odpowiedzi") - logger.info(response) - logger.info("Sformatowane odpowiedzi") - logger.info(result) - temp = {"role": "assistant", "content": result} + temp = {"role": "assistant", "content": response} history.append(temp) if request_type == "MUSIC": with open(MEMORY_FIVE_MUZYKA, "r+", encoding=ENCODING) as file_music_memory: @@ -318,7 +309,7 @@ async def handle_response( file_music_memory.seek(0) # convert back to json. json.dump(file_data, file_music_memory, indent=4) - return result, MESSAGE_TABLE_MUZYKA + return response, MESSAGE_TABLE_MUZYKA elif request_type == "RANDOM": with open(MEMORY_FIVE_SIARA, "r+", encoding=ENCODING) as file_memory: # First we load existing data into a dict. @@ -328,7 +319,7 @@ async def handle_response( file_memory.seek(0) # convert back to json. json.dump(file_data, file_memory, indent=4) - return result, MESSAGE_TABLE + return response, MESSAGE_TABLE elif request_type == "GENERAL": with open(MEMORY_FIVE_SIARA, "r+", encoding=ENCODING) as file_memory: # First we load existing data into a dict. @@ -338,9 +329,9 @@ async def handle_response( file_memory.seek(0) # convert back to json. json.dump(file_data, file_memory, indent=4) - return result, MESSAGE_TABLE + return response, MESSAGE_TABLE else: - return result, [] + return response, [] async def get_random_cyclic_message(client): """