Hello - i am using a simple assistant code - see attached
For some reason this code is not working with any API-Key.
(i have API-keys from different customers - with one it is working without problems - but with the other API-key it seems that no result is provided - but without any error-message or anything)
This is the output with the API-Key which is NOT working:
File "D:\DEV\Fiverr2024\TRY\larssegelke\checkHTML2.py", line 62, in <module>
resultAnswer = results.data[0].content[0].text.value
^^^^^^^^^^^^
File "D:\DEV\.venv\openaiplus\Lib\site-packages\pydantic\main.py", line 856, in __getattr__
raise AttributeError(f'{type(self).__name__!r} object has no attribute {item!r}')
AttributeError: 'Message' object has no attribute 'data'
(openaiplus)
Why ist that not working with any API-Key?
What is the problem with this API-Key?
import os
import sys
from dotenv import load_dotenv
from openai import OpenAI
path = os.path.abspath(os.path.dirname(sys.argv[0]))
fn = os.path.join(path, ".env")
load_dotenv(fn)
CHATGPT_API_KEY = os.environ.get("CHATGPT_API_KEY")
client = OpenAI(api_key = CHATGPT_API_KEY)
fn = os.path.join(path, "workText.txt")
vector_store = client.beta.vector_stores.create(name="HTML-File")
file_paths = [fn]
file_streams = [open(path, "rb") for path in file_paths]
file_batch = client.beta.vector_stores.file_batches.upload_and_poll(
vector_store_id=vector_store.id, files=file_streams
)
print(file_batch.status)
print(file_batch.file_counts)
# exit()
print(f"Preparing assistant")
assistant = client.beta.assistants.create(
name="HTML Analyse Assistant",
instructions="You are a machine learning researcher, answer questions about the provided text-file",
model="gpt-4o",
tools=[{"type": "file_search"}],
)
assistant = client.beta.assistants.update(
assistant_id=assistant.id,
tool_resources={"file_search": {"vector_store_ids": [vector_store.id]}},
)
question = """
What is the name of the location?
"""
print(f"Preparing thread")
thread = client.beta.threads.create()
print(f"Preparing question")
results = client.beta.threads.messages.create(
thread_id = thread.id,
role = "user",
content = question
)
print(f"Running for answer")
run = client.beta.threads.runs.create (
thread_id = thread.id,
assistant_id = assistant.id
)
while run.status not in ["completed", "failed"]:
run = client.beta.threads.runs.retrieve (
thread_id = thread.id,
run_id = run.id
)
if run.status == "completed":
results = client.beta.threads.messages.list(
thread_id=thread.id
)
resultAnswer = results.data[0].content[0].text.value
fn = os.path.join(path, "result.txt")
# with open(fn, "w", encoding="utf-8", errors="ignore") as f:
# lines = f.writelines(resultAnswer)
with open(fn, "w", encoding="utf-8", errors="ignore") as f:
lines = f.writelines(str(results))