You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: promptEngr.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -99,7 +99,7 @@ icon: fa-edit
99
99
100
100
[localRAGchatbot.html](/promptEngr/localRAGchatbot.html) contains a modified tutorial on building a chatbot (html output).
101
101
102
-
[localRAGchatbot.py](/promptEngr/localRAGchatbot.py) contains just the Python code for the above tutorial, suitable for running in a Python environment to check for errors.
102
+
[localRAGchatbot3.py](/promptEngr/localRAGchatbot3.py) contains just the Python code for the above tutorial, suitable for running in a Python environment to check for errors.
103
103
104
104
[Xiao2025.pdf](/promptEngr/Xiao2025.pdf) contains the input file for the above tutorial.
Copy file name to clipboardExpand all lines: promptEngr/localRAGchatbot.qmd
+35-13Lines changed: 35 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,15 @@
1
1
---
2
2
title: "localRAGchatbot"
3
3
jupyter: python3
4
+
format:
5
+
html:
6
+
embed-resources: true
7
+
toc: true
8
+
monofont: JetBrainsMono Nerd Font
9
+
mainfont: "TeX Gyre Schola"
4
10
---
5
11
6
-
You should *not* try to render this file until you have run the code in the week 10 lecture slides. You must have Ollama and DeepSeek-R1 installed on your machine, which is described in the lecture slides. Then you must be running Ollama via `ollama serve` in a separate terminal window.
12
+
You should *not* try to render this file until you have run the code in the accompanying slides. You must have Ollama and DeepSeek-R1 installed on your machine, which is described in the lecture slides. Then you must be running Ollama via `ollama serve` in a separate terminal window.
7
13
8
14
We will follow a modification of the tutorial by
9
15
Aashi Dutt at
@@ -31,19 +37,31 @@ You must import the following packages but be careful to just run this chunk fir
31
37
32
38
```{python}
33
39
import ollama
34
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
40
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
35
41
from langchain_community.document_loaders import PyMuPDFLoader
36
42
from langchain_community.embeddings import OllamaEmbeddings
43
+
from langchain_ollama import OllamaEmbeddings
37
44
from chromadb.config import Settings
38
45
from chromadb import Client
39
-
from langchain.vectorstores import Chroma
46
+
from langchain_chroma import Chroma
40
47
import gradio as gr
41
48
import re
42
49
from concurrent.futures import ThreadPoolExecutor
43
50
```
44
51
45
52
# The document
46
-
The whole point of this tutorial is create a chatbot that can answer questions about the book *Foundations of LLMs* by Xiao, et al. The following chunk loads the document. It assumes you have downloaded the document and saved it in the same directory as this file. You then split it into smaller chunks, use DeepSeek-R1 to generate embeddings, and store the embeddings in a vector store.
53
+
The whole point of this tutorial is create a chatbot that can answer questions about the book *Foundations of LLMs* by Xiao and Zhu. The following chunk loads the document. It assumes you have downloaded the document and saved it in the same directory as this file. You then split it into smaller chunks, use DeepSeek-R1 to generate embeddings, and store the embeddings in a vector store.
54
+
55
+
# Packages
56
+
You will use a number of packages to accomplish this. First is `PyMuPDFLoader`, which allows you to load a PDF document. You can read more about it at [https://pymupdf.readthedocs.io/en/latest/rag.html](https://pymupdf.readthedocs.io/en/latest/rag.html). It has many more capabilities we won't use and handles many other filetypes. We're just going to load a well-behaved PDF.
57
+
58
+
Second, we split the texts into chunks. The `RecursiveCharacterTextSplitter` is a very popular tool provided by `LangChain` for, as you might guess, splitting large amounts of text, such as our 231 page book, into small chunks. `LangChain` is a popular framework for building LLM applications. It *orchestrates* the various tools you use. You can read more about it at [https://python.langchain.com/docs/](https://python.langchain.com/docs/).
59
+
60
+
Next we generate embeddings for each chunk, using DeepSeek R1. The actual generation process is quite time-consuming on a typical laptop. You can see in the comments that it takes seven minutes on my machine and that you can tell when it's done by watching the window running `ollama serve`.
61
+
62
+
What are we going to do with these embeddings, which constitute a vector representation of the text? We will use them to create a vector database, which we will use to answer questions about the book. For this, we'll use ChromaDB, a popular vector database. You can read more about it at [https://docs.trychroma.com/](https://docs.trychroma.com/).
context = "\n\n".join([doc.page_content for doc in results])
105
124
return context
106
125
107
126
```
108
127
128
+
# Create the prompt
109
129
Next, create the prompt, send it to DeepSeek-R1 using Ollama, and obtain a response. If this doesn't work, it may mean that you are not running Ollama via `ollama serve` in a separate terminal window.
Next, define the RAG (retrieval augmented generation) pipeline.
131
152
132
153
```{python}
@@ -136,7 +157,7 @@ def rag_pipeline(question):
136
157
context = retrieve_context(question)
137
158
138
159
# Generate an answer using DeepSeek-R1
139
-
answer = query_deepseek(question, context)
160
+
answer = query_qwen(question, context)
140
161
return answer
141
162
```
142
163
@@ -148,7 +169,8 @@ def ask_question(question):
148
169
return rag_pipeline(question)
149
170
```
150
171
151
-
Finally, create a Gradio interface for the chatbot. This will appear as a web page in your browser. The user can enter as many prompts as they like.
172
+
# Gradio interface
173
+
Finally, create a Gradio interface for the chatbot. This will appear as a web page in your browser. The user can enter as many prompts as they like. Gradio is a popular library for creating web interfaces for machine learning models. You can read more about it at [https://gradio.app/](https://gradio.app/).
152
174
153
175
```{python}
154
176
#. Create a Gradio interface
@@ -157,11 +179,11 @@ interface = gr.Interface(
157
179
inputs="text",
158
180
outputs="text",
159
181
title="RAG Chatbot: Foundations of LLMs",
160
-
description="Ask any question about the Foundations of LLMs book. Powered by DeepSeek-R1.",
182
+
description="Ask any question about the Foundations of LLMs book. Powered by Qwen 3.5.",
161
183
)
162
184
```
163
185
164
-
Note that I have commented out the following line that actually launches the Gradio interface. This is only for rendering purposes. When you want to actually run the code, you should uncomment it. The interface will then appear in your browser if you point to `http://localhost:7860`.
186
+
Note that I have commented out the following line that actually launches the Gradio interface. This is only for rendering purposes. It should be commented out when you render the document. When you want to actually run the code, you should uncomment it. The interface will then appear in your browser if you point to `http://localhost:7860`.
0 commit comments