Partial + reformatting

This commit is contained in:
2025-08-20 18:24:50 +02:00
parent 9f262e5d2f
commit 2f13853084
2 changed files with 42 additions and 112 deletions
+34 -24
View File
@@ -21,7 +21,7 @@ from constants import (
WORD_REACTIONS,
)
#this do per user
# this do per user
VECTOR_STORE_ID = -1
@@ -47,23 +47,26 @@ async def _openai_call(messages, model, temperature=0.2):
else:
logger.info("4.0+")
# Responses API (zalecane dla 4o/4.1*)
# Responses API (zalecane dla 4o/4.1*)
resp = await OPENAICLIENT.responses.create(
model=model,
temperature=temperature,
input=messages,
)
# SDK zapewnia output_text dla zwykłych odpowiedzi
return (getattr(resp.output_text, "output_text", None) or "").strip() or str(resp.output_text)
return (getattr(resp.output_text, "output_text", None) or "").strip() or str(
resp.output_text
)
def create_vector_store():
# Create a vector store caled "Financial Statements"
return OPENAICLIENT.beta.vector_stores.create_and_poll(name="Hammer Stash")
#expires_after={
#"anchor": "last_active_at",
#"days": 7}
#)
# expires_after={
# "anchor": "last_active_at",
# "days": 7}
# )
def upload_files_to_vector_store(assistant):
@@ -71,15 +74,14 @@ def upload_files_to_vector_store(assistant):
file_paths = ["edgar/goog-10k.pdf", "edgar/brka-10k.txt"]
file_streams = [open(path, "rb") for path in file_paths]
#file = client.beta.vector_stores.files.create_and_poll(
#vector_store_id="vs_abc123",
#file_id="file-abc123"
#)
#batch = client.beta.vector_stores.file_batches.create_and_poll(
#vector_store_id="vs_abc123",
#file_ids=['file_1', 'file_2', 'file_3', 'file_4', 'file_5']
#)
# file = client.beta.vector_stores.files.create_and_poll(
# vector_store_id="vs_abc123",
# file_id="file-abc123"
# )
# batch = client.beta.vector_stores.file_batches.create_and_poll(
# vector_store_id="vs_abc123",
# file_ids=['file_1', 'file_2', 'file_3', 'file_4', 'file_5']
# )
# Use the upload and poll SDK helper to upload the files, add them to the vector store,
# and poll the status of the file batch for completion.
@@ -91,10 +93,11 @@ def upload_files_to_vector_store(assistant):
print(file_batch.status)
print(file_batch.file_counts)
assistant = OPENAICLIENT.beta.assistants.update(
assistant_id=assistant.id,
tool_resources={"file_search": {"vector_store_ids": [VECTOR_STORE_ID]}},
assistant_id=assistant.id,
tool_resources={"file_search": {"vector_store_ids": [VECTOR_STORE_ID]}},
)
def delete_files_from_vector_store(assistant, file_id):
result = OPENAICLIENT.beta.vector_stores.file_batches.delete(
vector_store_id=VECTOR_STORE_ID, files=file_id
@@ -103,8 +106,8 @@ def delete_files_from_vector_store(assistant, file_id):
# You can print the status and the file counts of the batch to see the result of this operation.
print(result)
assistant = OPENAICLIENT.beta.assistants.update(
assistant_id=assistant.id,
tool_resources={"file_search": {"vector_store_ids": [VECTOR_STORE_ID]}},
assistant_id=assistant.id,
tool_resources={"file_search": {"vector_store_ids": [VECTOR_STORE_ID]}},
)
@@ -133,7 +136,14 @@ def num_tokens_from_string(message, model):
async def handle_response(
prompt, vykidailo, bartender, history, username, request_type, algorithm="gpt-4o", none_request=""
prompt,
vykidailo,
bartender,
history,
username,
request_type,
algorithm="gpt-4o",
none_request="",
):
"""
Handle responses by appending them to a history, use OpenAI to
@@ -222,7 +232,6 @@ async def handle_response(
table = []
token_amount = 10000
prompt_gpt_request_size = num_tokens_from_string(
{"role": "user", "content": final_prompt}, "gpt-4"
)
@@ -254,7 +263,7 @@ async def handle_response(
deadline = time.time() + timeout_sec
response = await asyncio.wait_for(
_openai_call(messages=history, model=algorithm),
timeout=max(0.1, deadline - time.time())
timeout=max(0.1, deadline - time.time()),
)
except openai.APITimeoutError as e:
@@ -336,6 +345,7 @@ async def handle_response(
else:
return response, []
async def get_random_cyclic_message(client):
"""
The function `get_random_cyclic_message` returns a random cyclic message from a list of cyclic
@@ -438,7 +448,7 @@ async def chat_with_assistant(message, assistant_name):
logger.info(block.text.value)
chat_response += block.text.value
await discord_friendly_send(message.channel, chat_response)
#await message.channel.send(chat_response)
# await message.channel.send(chat_response)
done = True
elif run.status == "cancelled":
await discord_friendly_send(message.channel, "Cos sie wywaliło")