openai / openai-quickstart-python

Python example app from the OpenAI API quickstart tutorial
https://platform.openai.com/docs/quickstart?context=python
MIT License
1.69k stars 1.31k forks source link

Add a argument to ordering #77

Open ide127 opened 7 months ago

ide127 commented 7 months ago

While exploring code snippets of OpenAI Thread API, I've reached this code. In my amateur opinion, it seems forgot to add the ordering argument to ensure 'newest' message.

xxtrakyle commented 2 months ago

To be honest, B# and Binary are the +

xxtrakyle commented 2 months ago
americadoodles commented 2 weeks ago

You're absolutely right in your observation! Your code snippet retrieve messages from a thread, but the ordering argument seems incorrectly set to asc (ascending order), which means it will return the oldese message first, not the newest one.

I think the order should be set to desc(descending).

def get_newest_message(thread_id): thread_messages = client.beta.threads.messages.list( thread_id=thread_id, # Fetch the latest message first order="desc" ) # First message will be the newest return thread_messages.data[0]