#323: Add the LLM to the Python Friday RAG
Last week we turned blog posts written in Markdown into embeddings in Chroma. That gives us the data that we need for our Python Friday RAG (Retrieval Augmented Generation). In this post we reuse the LangChain configuration that helped us in various posts to connect our script to a local LLM running inside LM Studio.
Installation
If you do not already have these packages installed, now is a good time to do so:
Build and augment the prompt
A main part of our RAG is to augment the prompt we send to the LLM with data from our Chroma vector store. For this task we create a class that handles all the work for us. In the constructor we wire everything up, while the _format_prompt() method fetches the data and augments the prompt. While get_chain() builds the LangChain chain, handle_query() is the connector between our prompt building method and LangChain:
Wire it up and loop through the questions
The final part is where we initialise our class, build the chain and add our well-known loop that allows us to ask multiple questions in one session:
Ask about Python Friday on the command line
We can now run our script and ask questions about Python. If we have posts matching the question, we get an answer augmented with examples from the blog. If the question cannot be answered, our RAG says so:
--- Python Friday RAG Chatbot Started (Type 'exit' to stop) ---
You: What is PEP?
🧾 Answer:
A Python Enhancement Proposal (PEP) is a design document that provides
information to the Python community or describes a new feature for Python,
its processes, or environment. It offers a concise technical specification
of the feature and explains the rationale behind it. PEPs serve as the
primary mechanism for proposing major new features, gathering community
input on issues, and documenting the design decisions made in Python. The
author of a PEP is responsible for building consensus within the community
and recording any dissenting opinions.
**References:**
- #17: What is PEP? / PEP?
------------------------------
You: How can we read a file?
🧾 Answer:
To read a file in Python you use the built‑in `open()` function to obtain
a file object and then call one of its read methods.
1. **Open the file**
```python
with open("filename.txt", "r") as f:
...
```
The `"r"` mode tells Python that you want to read the file (the default).
Using a `with` statement ensures the file is closed automatically, even
if an error occurs.
2. **Read the whole contents at once**
```python
content = f.read()
print(content)
```
This returns the entire file as a single string.
3. **Read line by line**
*Using `readline()` in a loop*
```python
while True:
line = f.readline()
if not line: # empty string signals end of file
break
print(line)
```
*Or using `readlines()` to get a list of all lines*
```python
lines = f.readlines()
for line in lines:
print(line)
```
4. **Handle different encodings**
If the file isn’t encoded in UTF‑8, specify the correct encoding when
opening it:
```python
with open("umlaute.txt", "r", encoding="iso-8859-15") as f:
content = f.read()
print(content)
```
These techniques cover reading text files safely and flexibly in Python.
---
**References:**
- #16: Working With Files / Reading and writing text files
- #16: Working With Files / Reading a file line by line
- #16: Working With Files / Fixing encoding problems
------------------------------
You: what is claude?
🧾 Answer:
I do not have enough information in the provided context.
References:
- #17: What is PEP?
------------------------------
Next
With Chroma and LangChain we can build a nice little RAG application that answers questions based on posts on PythonFriday.dev. That is a great example on how we can build such a system for ourselves. Next week we add a user interface to get more of a ChatGPT experience.