Create an Assistant to Discuss a Specific Document

Idea and Step-by-Step Plan

Today, we’re going to create an AI Assistant 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 Chat Completion capability).

  • Document upload (TXT).

  • Streaming mode.

Step-by-Step Explanation

1. Preparing Input File

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.

Input Text File (recipes.txt)
1. Sun-Dried Tomato & Garlic Pasta

Prep Time: 25 minutes
Servings: 2

Ingredients:
•	200g spaghetti
•	6–8 sun-dried tomatoes in oil
•	2 garlic cloves
•	Olive oil — 2 tbsp
•	Salt — to taste
•	Black pepper — to taste
•	Fresh basil (optional)
Required Kitchen Tools:
•	Large pot
•	Frying pan
•	Strainer
•	Cutting board & knife
•	Wooden spoon
Instructions:
1.	Boil a large pot of salted water and cook the spaghetti according to package instructions.
2.	While the pasta cooks, finely chop the garlic and sun-dried tomatoes.
3.	In a frying pan, heat olive oil over medium heat. Add garlic and cook for 30 seconds until fragrant.
4.	Add sun-dried tomatoes and stir for 2–3 minutes.
5.	Drain the pasta and toss it into the pan with the tomato-garlic mixture.
6.	Mix well, season with salt and pepper, and garnish with fresh basil if desired.
7.	Serve hot.


2. Chickpea & Avocado Salad

Prep Time: 15 minutes
Servings: 2

Ingredients:
•	1 can of chickpeas (400g), drained and rinsed
•	1 ripe avocado, diced
•	1 small red onion, finely chopped
•	Juice of 1 lemon
•	Olive oil — 1 tbsp
•	Salt & pepper — to taste
•	Fresh parsley (optional)
Required Kitchen Tools:
•	Mixing bowl
•	Cutting board & knife
•	Fork or spoon
•	Citrus squeezer (optional)
Instructions:
1.	In a bowl, combine chickpeas, diced avocado, and chopped red onion.
2.	Squeeze in lemon juice and drizzle with olive oil.
3.	Season with salt and pepper.
4.	Toss everything gently to mix, trying not to mash the avocado.
5.	Top with chopped parsley if desired.
6.	Serve immediately or chill for 10 minutes.


3. Quick Oatmeal Banana Cookies
Prep Time: 10 minutes
Bake Time: 15 minutes
Servings: ~12 cookies

Ingredients:
•	2 ripe bananas
•	1 cup rolled oats
•	1/4 cup chocolate chips or chopped nuts (optional)
•	1/2 tsp cinnamon (optional)
Required Kitchen Tools:
•	Mixing bowl
•	Fork or potato masher
•	Baking tray
•	Parchment paper
•	Oven
Instructions:
1.	Preheat oven to 180°C (350°F). Line a baking tray with parchment paper.
2.	In a bowl, mash the bananas until smooth.
3.	Mix in oats and any add-ins like chocolate chips or cinnamon.
4.	Scoop spoonfuls of the mixture onto the tray and flatten slightly.
5.	Bake for 12–15 minutes until edges are golden.
6.	Let cool for a few minutes before serving.

2. Core assistant functionality

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 corresponding section.

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 streaming mode, 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.

Simple Example with the Core Assistant Functionality
Interaction Example

3. Let's Add a File to Discuss!

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."

File uploading
Creating the first message in code with attaching the file

4. Add Streaming Mode

For a more dynamic interaction, the established practice when communicating with online AI chats is now streaming mode, 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.

Explanation

How to handle events

To do this, we will use the pre-built EventHandler class from the AssistantEventHandler library.

Creating the handler:

What events are handled

on_text_created(self, text)

Triggered when the Assistant creates a text response. The code simply prints assistant > to indicate the beginning of the output.

on_text_delta(self, delta, snapshot)

Triggered when new parts of text (tokens) arrive. The code prints each new word or letter to the console without a newline (end=""), creating the effect of the text appearing gradually.

on_tool_call_created(self, tool_call)

Triggered if the Assistant decides to use one of the tools (e.g., Code Interpreter or external APIs). The code simply prints the type of the invoked tool.

on_tool_call_delta(self, delta, snapshot)

Triggered when the assistant sends data to a tool or receives a result from it.


How it works

When the Assistant starts forming a response, it triggers on_text_created.

Then, as tokens are generated, on_text_delta is triggered, updating the text in real time.

If a tool is used in the response, on_tool_call_created is triggered, followed by on_tool_call_delta to show the process of the tool handling the data.

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.

Full Code Example

Code
Conversation Example

Copy the code, insert your AIMLAPI key, specify the path to your document in the code, and give it a try yourself!

Last updated

Was this helpful?