Created
January 21, 2025 18:14
-
-
Save deepanshululla/1611b8584f24b4e8050c0f55b88fc235 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from openai import OpenAI | |
from openai.types.beta.threads.message_create_params import AttachmentToolFileSearch, Attachment | |
def completion(client, filename, prompt, assistant_id): | |
# Create thread | |
thread = client.beta.threads.create() | |
file = client.files.create(file=open(filename, "rb"), purpose="assistants") | |
# Create assistant | |
client.beta.threads.messages.create( | |
thread_id=thread.id, | |
role="user", | |
attachments=[ | |
Attachment( | |
file_id=file.id, tools=[AttachmentToolFileSearch(type="file_search")] | |
) | |
], | |
content=prompt, | |
) | |
# Run thread | |
run = client.beta.threads.runs.create_and_poll( | |
thread_id=thread.id, assistant_id=assistant_id, timeout=1000 | |
) | |
if run.status != "completed": | |
raise Exception("Run failed.", run.status) | |
messages_cursor = client.beta.threads.messages.list(thread_id=thread.id) | |
messages = [message for message in messages_cursor] | |
return messages | |
if __name__ == '__main__': | |
api_key="" | |
os.environ["OPENAI_API_KEY"] = api_key | |
client = OpenAI( | |
api_key=api_key, | |
) | |
assistant = client.beta.assistants.create( | |
model="gpt-4o", | |
description="Line items analyzer", | |
name="Line items analyzer", | |
tools=[{"type":"file_search"}] | |
) | |
assistant_id = assistant.id | |
prompt = """ | |
Your task is to extract line items given as a tabular data in the pdf. | |
A line item is defined as an item in the invoice table that has a Description, Quantity, and Unit Price associated with it. | |
Think before you output your response and make sure it is accurate. | |
Please return the answer in json format. No need to write any code or any explanation | |
""" | |
messages = completion(client, "XinCubeInvoice.pdf", prompt, assistant_id) | |
# Output text | |
res_txt = messages[0].content[0].text.value | |
print(res_txt) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment