Случайная «IndexError: проблема с индексом списка вне диапазона» OpenAI «messages = list(client.beta.threads.messages.liPython

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Случайная «IndexError: проблема с индексом списка вне диапазона» OpenAI «messages = list(client.beta.threads.messages.li

Сообщение Anonymous »

Код выполняет несколько итераций следующих операций:
  • Создание векторного хранилища
  • Загрузка отдельного файла (в обеспечить чистый и единый контекст)
  • Вызов «client.beta.threads.runs.create_and_poll» OpenAI
    Вывод сообщений в формате CSV.
Но код работает случайно несколько итераций, а потом выдает ошибку
IndexError Traceback (most recent call last)
in ()
54
55 print ("messages array: ",messages)
---> 56 message_content = messages[0].content[0].text
57
58 if IS_DEBUG:

IndexError: list index out of range

В обычном рабочем режиме ожидаемый результат ниже:
/content/drive/MyDrive/ESGFiles/ESGReports-00093-TERMBRAY IND-2024.pdf

completed

FileCounts(cancelled=0, completed=1, failed=0, in_progress=0, total=1)

ToolResourcesFileSearch(vector_store_ids=['vs_bq7LKpJxDxuY7ZBJSJSdBOQy'])

messages array: [Message(id='msg_WxLClmiixcSNz95VndCesBKk', assistant_id='asst_eRMnT4VbENVujM7uHxBV4MgT', attachments=[], completed_at=None, content=[TextContentBlock(text=Text(annotations=[], value='```csv\nTermbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Compliance with applicable environmental laws, policies, and regulations\nTermbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Prioritising the utilization of clean energy, while minimising water and electricity consumption and preventing pollution\nTermbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Proper management of wastewater and municipal solid waste generated during our business operations in accordance with government-mandated guidelines\nTermbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Communicating our environmental goals, and performance outcomes to our employees, fostering a greater sense of environmental awareness\nTermbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Reducing absolute GHG emissions by 15% by 2025 and 20% by 2030, and achieving carbon neutrality by 2050\nTermbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Increasing the utilization of renewable and low-carbon energy sources whenever possible\nTermbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Promoting the utilization of environmentally-friendly vehicles and fuels, such as bio-diesel, as a preferred option over diesel vehicles\nTermbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Substituting regular diesel with ultra-low sulfur diesel for all diesel-powered machinery and facilities\nTermbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Ensuring compliance with all relevant environmental laws and policies and issuing necessary emission licenses\nTermbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Conducting regular maintenance and repairs of machinery and facilities to ensure emissions remain within established limits\n```'), type='text')], created_at=1719334101, incomplete_at=None, incomplete_details=None, metadata={}, object='thread.message', role='assistant', run_id='run_woDYnQAaMIZhZiRp7WkzZI5L', status=None, thread_id='thread_1LIAUWJyO4VSfQHcMW2hNxoB')]

Message Content = Text(annotations=[], value='```csv\nTermbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Compliance with applicable environmental laws, policies, and regulations\nTermbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Prioritising the utilization of clean energy, while minimising water and electricity consumption and preventing pollution\nTermbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Proper management of wastewater and municipal solid waste generated during our business operations in accordance with government-mandated guidelines\nTermbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Communicating our environmental goals, and performance outcomes to our employees, fostering a greater sense of environmental awareness\nTermbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Reducing absolute GHG emissions by 15% by 2025 and 20% by 2030, and achieving carbon neutrality by 2050\nTermbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Increasing the utilization of renewable and low-carbon energy sources whenever possible\nTermbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Promoting the utilization of environmentally-friendly vehicles and fuels, such as bio-diesel, as a preferred option over diesel vehicles\nTermbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Substituting regular diesel with ultra-low sulfur diesel for all diesel-powered machinery and facilities\nTermbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Ensuring compliance with all relevant environmental laws and policies and issuing necessary emission licenses\nTermbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Conducting regular maintenance and repairs of machinery and facilities to ensure emissions remain within established limits\n```')

Message Value = ```csv

Termbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Compliance with applicable environmental laws, policies, and regulations

Termbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Prioritising the utilization of clean energy, while minimising water and electricity consumption and preventing pollution

Termbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Proper management of wastewater and municipal solid waste generated during our business operations in accordance with government-mandated guidelines

Termbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Communicating our environmental goals, and performance outcomes to our employees, fostering a greater sense of environmental awareness

Termbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Reducing absolute GHG emissions by 15% by 2025 and 20% by 2030, and achieving carbon neutrality by 2050

Termbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Increasing the utilization of renewable and low-carbon energy sources whenever possible

Termbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Promoting the utilization of environmentally-friendly vehicles and fuels, such as bio-diesel, as a preferred option over diesel vehicles

Termbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Substituting regular diesel with ultra-low sulfur diesel for all diesel-powered machinery and facilities

Termbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Ensuring compliance with all relevant environmental laws and policies and issuing necessary emission licenses

Termbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Conducting regular maintenance and repairs of machinery and facilities to ensure emissions remain within established limits

```

Annotations Content = []

```csv

Termbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Compliance with applicable environmental laws, policies, and regulations

Termbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Prioritising the utilization of clean energy, while minimising water and electricity consumption and preventing pollution

Termbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Proper management of wastewater and municipal solid waste generated during our business operations in accordance with government-mandated guidelines

Termbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Communicating our environmental goals, and performance outcomes to our employees, fostering a greater sense of environmental awareness

Termbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Reducing absolute GHG emissions by 15% by 2025 and 20% by 2030, and achieving carbon neutrality by 2050

Termbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Increasing the utilization of renewable and low-carbon energy sources whenever possible

Termbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Promoting the utilization of environmentally-friendly vehicles and fuels, such as bio-diesel, as a preferred option over diesel vehicles

Termbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Substituting regular diesel with ultra-low sulfur diesel for all diesel-powered machinery and facilities

Termbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Ensuring compliance with all relevant environmental laws and policies and issuing necessary emission licenses

Termbray Industries International (Holdings) Limited, Property Development and Investment/Money Lending, Conducting regular maintenance and repairs of machinery and facilities to ensure emissions remain within established limits

```

/content/drive/MyDrive/ESGFiles/ESGReports-02271-ZHONGAN SERVICE-2024.pdf

completed

FileCounts(cancelled=0, completed=1, failed=0, in_progress=0, total=1)

ToolResourcesFileSearch(vector_store_ids=['vs_hCyCMmnh3JHkduKeEGGwsqWf'])

messages array: [Message(id='msg_dI46JfPkfnNdynoCtM1KDGO6', assistant_id='asst_eRMnT4VbENVujM7uHxBV4MgT', attachments=[], completed_at=None, content=[TextContentBlock(text=Text(annotations=[], value='```csv\nZhong An Intelligent Living Service Limited, Property Management Services, Emissions Management\nZhong An Intelligent Living Service Limited, Property Management Services, Use of Resources\nZhong An Intelligent Living Service Limited, Property Management Services, The Environment and Natural Resources\nZhong An Intelligent Living Service Limited, Property Management Services, Climate Change\nZhong An Intelligent Living Service Limited, Property Management Services, Social Responsibility\nZhong An Intelligent Living Service Limited, Property Management Services, Employment and Labour Practices\nZhong An Intelligent Living Service Limited, Property Management Services, Health and Safety\nZhong An Intelligent Living Service Limited, Property Management Services, Development and Training\nZhong An Intelligent Living Service Limited, Property Management Services, Supply Chain Management\nZhong An Intelligent Living Service Limited, Property Management Services, Anti-corruption\n```'), type='text')], created_at=1719334135, incomplete_at=None, incomplete_details=None, metadata={}, object='thread.message', role='assistant', run_id='run_IZhhrHJhF9eOtQfdsT36Jf35', status=None, thread_id='thread_CL5Kf6ZU680kTVNawUWeAiDK')]

Message Content = Text(annotations=[], value='```csv\nZhong An Intelligent Living Service Limited, Property Management Services, Emissions Management\nZhong An Intelligent Living Service Limited, Property Management Services, Use of Resources\nZhong An Intelligent Living Service Limited, Property Management Services, The Environment and Natural Resources\nZhong An Intelligent Living Service Limited, Property Management Services, Climate Change\nZhong An Intelligent Living Service Limited, Property Management Services, Social Responsibility\nZhong An Intelligent Living Service Limited, Property Management Services, Employment and Labour Practices\nZhong An Intelligent Living Service Limited, Property Management Services, Health and Safety\nZhong An Intelligent Living Service Limited, Property Management Services, Development and Training\nZhong An Intelligent Living Service Limited, Property Management Services, Supply Chain Management\nZhong An Intelligent Living Service Limited, Property Management Services, Anti-corruption\n```')

Message Value = ```csv

Zhong An Intelligent Living Service Limited, Property Management Services, Emissions Management

Zhong An Intelligent Living Service Limited, Property Management Services, Use of Resources

Zhong An Intelligent Living Service Limited, Property Management Services, The Environment and Natural Resources

Zhong An Intelligent Living Service Limited, Property Management Services, Climate Change

Zhong An Intelligent Living Service Limited, Property Management Services, Social Responsibility

Zhong An Intelligent Living Service Limited, Property Management Services, Employment and Labour Practices

Zhong An Intelligent Living Service Limited, Property Management Services, Health and Safety

Zhong An Intelligent Living Service Limited, Property Management Services, Development and Training

Zhong An Intelligent Living Service Limited, Property Management Services, Supply Chain Management

Zhong An Intelligent Living Service Limited, Property Management Services, Anti-corruption

```

Annotations Content = []

```csv

Zhong An Intelligent Living Service Limited, Property Management Services, Emissions Management

Zhong An Intelligent Living Service Limited, Property Management Services, Use of Resources

Zhong An Intelligent Living Service Limited, Property Management Services, The Environment and Natural Resources

Zhong An Intelligent Living Service Limited, Property Management Services, Climate Change

Zhong An Intelligent Living Service Limited, Property Management Services, Social Responsibility

Zhong An Intelligent Living Service Limited, Property Management Services, Employment and Labour Practices

Zhong An Intelligent Living Service Limited, Property Management Services, Health and Safety

Zhong An Intelligent Living Service Limited, Property Management Services, Development and Training

Zhong An Intelligent Living Service Limited, Property Management Services, Supply Chain Management

Zhong An Intelligent Living Service Limited, Property Management Services, Anti-corruption

Сначала я подумал, что это ограничение скорости, поэтому ввел задержку в 5 секунд между каждым вызовом, но безрезультатно.
Код ниже:
assistant = client.beta.assistants.create(
name="ESG Analyst Assistant",
instructions="You are an expert ESG analyst with over 20 years of analyzing ESG / CSR reports published by global public companies. Use you knowledge base to answer questions about ESG activities and focuses",
model="gpt-4o",
tools=[{"type": "file_search"}],
)

IS_DEBUG = True
#vector_store = client.beta.vector_stores.create(name="ESG_Reports")

#for i in np.arange(0,len(directory_files)):
for i in np.arange(0,10):
vector_store = client.beta.vector_stores.create(name="ESG_Reports")

file_path = directory_path+"/"+directory_files
file_stream = [open(file_path, "rb")]

if IS_DEBUG:
print(file_path)

file_batch = client.beta.vector_stores.file_batches.upload_and_poll(
vector_store_id = vector_store.id,
files = file_stream
)
if IS_DEBUG:
print(file_batch.status)
print(file_batch.file_counts)

assistant = client.beta.assistants.update(
assistant_id=assistant.id,
tool_resources={"file_search": {"vector_store_ids": [vector_store.id]}},
)

message_file = client.files.create(
file = open (file_path,"rb"),
purpose = "assistants"
)

thread = client.beta.threads.create(
messages=[
{
"role": "user",
"content": "what is the name of the company? what industry is it in? what are the top 10 ESG priorities for the company? For the answer, please output only the CSV in the following format (nothing more, and nothing less): , , . Repeat this output for each of the top ESG intiatiives for the company.",
# Attach the new file to the message.
"attachments": [
{ "file_id": message_file.id, "tools": [{"type": "file_search"}] }
],
}
]
)

if IS_DEBUG:
print(thread.tool_resources.file_search)

run = client.beta.threads.runs.create_and_poll(
thread_id = thread.id,
assistant_id = assistant.id
)

messages = list(client.beta.threads.messages.list(thread_id=thread.id, run_id = run.id))

print ("messages array: ",messages)
message_content = messages[0].content[0].text

if IS_DEBUG:
print("Message Content = ", message_content)
print("Message Value = ", message_content.value)

annotations = message_content.annotations

if IS_DEBUG:
print("Annotations Content = ", annotations)

citations = []

for index, annotation in enumerate(annotations):
message_content.value = message_content.value.replace(annotation.text, f"[{index}]")
if file_citation := getattr(annotation, "file_citation",None):
cited_file = client.files.retrieve(file_citation.file_id)
citations.append(f"[{index}] {cited_file.filename}")

print(message_content.value)

if IS_DEBUG:
print("\n".join(citations))

client.beta.vector_stores.delete(vector_store_id=vector_store.id)
sleep(5)


Подробнее здесь: https://stackoverflow.com/questions/786 ... s-listclie
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»