How to force Assistants to use knowledge retrieval?

When using the following code to ask a question about an individual listed in my file, the Assistants API only returns a generic answer “As of my latest update in April 2023…” instead of an answer based on the knowledge clearly spelled out in this short document. Any suggestion on how to force the Assistant to look more closely at the file ?

import os
import time
from openai import OpenAI

file_id = os.environ.get("OPENAI_FILE_ABOUT_K", "")
print("File ID", file_id)
openai_client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY", ""))
assistant = openai_client.beta.assistants.create(name="Assistant",
                                                 description="You are a personal assistant chatbot. When asked a question, always refer to the uploaded "
                                                             "file in order to provide the best answer."
                                                             "queries.", model="gpt-4-1106-preview",
                                                 tools=[{"type": "retrieval"}], file_ids=[file_id], )

print("Assistant ID", assistant.id)
thread = openai_client.beta.threads.create()
thread_id = thread.id
print("Thread ID", thread_id)

message = "Who is [name of person]?"

openai_client.beta.threads.messages.create(thread_id, role="user", content=message)

run = openai_client.beta.threads.runs.create(thread_id=thread_id, assistant_id=assistant.id,
                                             instructions="Use the uploaded file to answer questions.", )
run_in_progress = True
while run_in_progress:
    run = openai_client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run.id)
    if run.status == "completed":
        run_in_progress = False
    else:
        time.sleep(3)
messages = openai_client.beta.threads.messages.list(thread_id=thread_id, order="desc")
print(messages.data)
new_messages = []
for message_object in messages.data:
    try:
        if message_object.role == "assistant":
            new_messages.append(message_object.content[0].text.value)
        else:
            break
    except Exception as e:
        print("Error interpreting message", e)
        print(message_object)
        new_messages.append("Error interpreting message")
new_messages.reverse()
prediction = ""
for message_string in new_messages:
    prediction = prediction + message_string + "\n"
print(prediction)
1 Like

Hi,

When you create a message you can also specify the file id’s it should use maybe that will help you getting an answer from the files. See https://platform.openai.com/docs/api-reference/messages/createMessage

Regards,

Taner.

1 Like

As this topic has an accepted solution, closing topic.