The code works a few iterations of the following:
1) Creating vector store
2) Upload individual file (to ensure clean and singular context)
3) Call OpenAI's "client.beta.threads.runs.create_and_poll"
4) Output messages in a CSV format.
But the code works randomly a few iterations, and then errored out
------------------
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
-------------------
Under normal working mode the expected output below:
/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
Содержимое аннотаций = []
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
завершено
FileCounts(cancelled=0, Completed=1, error=0, in_progress=0, total=1)
ToolResourcesFileSearch(vector_store_ids=['vs_hCyCMmnh3JHkduKeEGGwsqWf'])
массив сообщений: [Message(id='msg_dI46JfPkfnNdynoCtM1KDGO6', Assistant_id='asst_eRMnT4VbENVujM7uHxBV4MgT', Attachments=[],complete_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')], созданный_at=1719334135, incomplete_at=None, incomplete_details=None, метаданные={}, object='thread.message', role='assistant', run_id='run_IZhhrHJhF9eOtQfdsT36Jf35 ', status=None, thread_id='thread_CL5Kf6ZU680kTVNawUWeAiDK')]
Содержимое сообщения = 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')
Значение сообщения = ```csv
Чжун Ан Интеллектуал Ливинг Сервис Лимитед, Услуги по управлению имуществом, Управление выбросами
Чжун Ан Intelligent Living Service Limited, Услуги по управлению недвижимостью, Использование ресурсов
Чжун Ан Intelligent Living Service Limited, Услуги по управлению недвижимостью, Окружающая среда и природные ресурсы
Чжун Ан Intelligent Living Service Limited, Услуги по управлению недвижимостью, Изменение климата
Чжун Ан Интеллигент Ливинг Сервис Лимитед, Услуги по управлению недвижимостью, Социальная ответственность
Чжун Ан Интеллигент Ливинг Сервис Лимитед, Услуги по управлению недвижимостью, Социальная ответственность
Чжун Ан Интеллигент Ливинг Сервис Лимитед, Услуги по управлению недвижимостью, Изменение климата
Чжун Ан Интеллигент Ливинг Сервис Лимитед, Услуги по управлению недвижимостью, Социальная ответственность
p>
Чжун Ан Intelligent Living Service Limited, Услуги по управлению имуществом, практика трудоустройства и труда
Чжун Ан Intelligent Living Service Limited, Услуги по управлению имуществом, Здоровье и Безопасность< /p>
Чжун Ан Intelligent Living Service Limited, Услуги по управлению недвижимостью, развитие и обучение
Чжун Ан Intelligent Living Service Limited, Услуги по управлению недвижимостью, Управление цепочками поставок< /p>
Чжун Ан Интеллигент Ливинг Сервис Лимитед, Услуги по управлению недвижимостью, Борьба с коррупцией
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="Вы эксперт ESG-аналитик с более чем 20-летним опытом анализа отчетов ESG/CSR, опубликованных мировыми публичными компаниями. Используйте свою базу знаний, чтобы ответить на вопросы о деятельности и направлениях ESG",
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_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 = вектор_store.id,
files = file_stream
)
if IS_DEBUG:
print(file_batch.status)
print(file_batch.file_counts)< /p>
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"),
Цель = " помощники"
)
thread = client.beta.threads.create(
messages=[
{
"role": "user ",
"content": "как называется компания? в какой отрасли это? каковы 10 главных ESG-приоритетов компании? Для ответа выведите только CSV в следующем формате (ни больше, ни меньше): , , . Повторите этот вывод для каждой из наиболее важных ESG-инициатив компании.",
# Прикрепите новый файл к сообщению.
"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))< /p>
print ("массив сообщений: ",messages)
message_content = messages[0].content[0].text
if IS_DEBUG :
print("Содержимое сообщения = ", message_content)
print("Значение сообщения = ", message_content.value)
annotations = message_content.annotations
if IS_DEBUG:
print("Annotations Content = ", annotations)
citations = []
для индекса аннотация в перечислении(аннотации):
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
Случайная «IndexError: проблема с индексом списка вне диапазона» OpenAI «messages = list(client.beta.threads.messages.li ⇐ Python
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение