Create an Assistant to Discuss a Specific Document
Last updated
Was this helpful?
Last updated
Was this helpful?
Today, weâre going to create an AI that helps users engage with the content of a particular document. This assistant can answer questions about the text, explain specific sections, find relevant parts, and even participate in discussions â for example, by offering arguments, clarifying ambiguous points, or helping formulate conclusions. It's especially useful when working with technical documentation, legal texts, research papers, or project documents.
The following features need to be implemented:
Core assistant functionality (ability to communicate with the user and respond accurately to questions using capability).
Document upload (TXT).
Streaming mode.
You can read the step-by-step explanation below or jump straight to the at the bottom of this page. Make sure you have !
As input, we used a .txt
file with the following content and placed it in the same directory as our Python script. For testing, we created a simple file with recipes for three different dishes.
Since we want to immediately start discussing the file contents with the Assistant upon launch, we need to pass it to the Assistant in advance, directly in the code. First, we will open our .txt file using Pythonâs built-in mechanism and pass the file ID in the first user message created directly from the code. The text of this initial message will be set as follows: "Here's my .txt file â extract the text, read through it, and let me know when you're ready to start answering my questions about this document."
In the next section, you will find the ready-made code for creating an Assistant, passing it an input file, and interacting with it in streaming mode. At the end of the page, you will also find the listing of our conversation with this Assistant.
Copy the code, insert your AIMLAPI key, specify the path to your document in the code, and give it a try yourself!
Assistants are a highly advanced way of working with chat models. If you have never worked with OpenAI Assistants before, we recommend reviewing the key concepts and structure of how Assistants operate in the .
Below, in the expandable sections, you can see a still quite basic example of creating a working Assistant, and a little further down, an example of a conversation with it in the console. To exit, you need to type exit
or quit
. Please note: this example is written without using the , which means the Assistant does not provide an answer word by word, but first forms it completely, and then the entire response appears in the console at once.
For a more dynamic interaction, the established practice when communicating with online AI chats is now , where the model's response appears on the user's screen word by word as it is being formed. Let's add this feature to our Assistant as well.