Skip to content
This repository was archived by the owner on Feb 15, 2025. It is now read-only.

Commit 989a3e6

Browse files
committed
update guide with vector store section
1 parent af3d474 commit 989a3e6

File tree

1 file changed

+79
-8
lines changed

1 file changed

+79
-8
lines changed

website/content/en/docs/dev-with-lfai-guide/dev_guide.md

Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ In order to utilize the LeapfrogAI API outside of the User Interface, you'll nee
2323
#### Via the UI
2424

2525
To create a LeapfrogAI API key via the user interface, perform the following in the UI (reference the [Quick Start](https://docs.leapfrog.ai/docs/local-deploy-guide/quick_start/#checking-deployment) guide for where the UI is deployed):
26+
2627
- Select the **Settings** icon ⚙️ in the top-right corner
2728
- Select **API Keys**
2829
- Select **Create New**
@@ -72,21 +73,62 @@ This is just a basic example; check out the [chat completion reference](https://
7273

7374
### Building a RAG Pipeline using Assistants
7475

75-
Now that we've seen a basic example, let's leverage OpenAI assistants using LeapfrogAI to handle a more complex task: Retrieval Augmented Generation (RAG)
76+
Now that we've seen a basic example, let's leverage OpenAI assistants using LeapfrogAI to handle a more complex task: **Retrieval Augmented Generation (RAG)**.
7677

77-
We'll break this example down into a few step:
78+
We'll break this example down into a few steps:
7879

7980
#### Create a Vector Store
8081

82+
A [vector database](https://www.pinecone.io/learn/vector-database/) is a fundamental piece of RAG-enabled systems. Vector databases store vectorized representations of and creating one is the first step to building a RAG pipeline.
83+
84+
Assuming you've created an OpenAI client as detailed above, create a vector store:
85+
86+
```python
87+
vector_store = client.beta.vector_stores.create(
88+
name="RAG Demo Vector Store",
89+
file_ids=[],
90+
expires_after={"anchor": "last_active_at", "days": 5},
91+
metadata={"project": "RAG Demo", "version": "0.1"},
92+
)
93+
```
94+
8195
#### Upload a file
8296

97+
Now that you have a vector store, let's add some documents. For a simple example, let's assume you have two text files with the following contents:
98+
99+
**doc_1.txt**
100+
101+
```text
102+
Joseph has a pet frog named Milo.
103+
```
104+
105+
**doc_2.txt**
106+
107+
```text
108+
Milo the frog's birthday is on October 7th.
109+
```
110+
111+
You can add these documents to the vector store:
112+
113+
```python
114+
# upload some documents
115+
documents = ['doc_1.txt','doc_2.txt']
116+
for doc in documents:
117+
with open(doc, "rb") as file: # read these files in binary mode
118+
_ = client.beta.vector_stores.files.upload(
119+
vector_store_id=vector_store.id, file=file
120+
)
121+
```
122+
123+
When you upload files to a vector store, this creates a `vector_store_file` object. You can record these to reference later, but it's not necessary to track these when chatting with your documents.
124+
83125
#### Create an Assistant
84126

85-
Assuming you've created an OpenAI as detailed above, create an assistant:
127+
[OpenAI Assistants](https://platform.openai.com/docs/assistants/overview) carry specific instructions and can reference specific tools to add functionality to your workflows. In this case, we'll add the ability for this assistant to search files in our vector store:
86128

87129
```python
88-
# these instructions are for example only, your use case may require more explicit directions
89-
instructions = """
130+
# these instructions are for example only, your use case may require different directions
131+
INSTRUCTIONS = """
90132
You are a helpful, frog-themed AI bot that answers questions for a user. Keep your response short and direct.
91133
You may receive a set of context and a question that will relate to the context.
92134
Do not give information outside the document or repeat your findings.
@@ -95,36 +137,65 @@ instructions = """
95137
# create an assistant
96138
assistant = client.beta.assistants.create(
97139
name="Frog Buddy",
98-
instructions=instructions,
140+
instructions=INSTRUCTIONS,
99141
model="vllm",
100142
tools=[{"type": "file_search"}],
101143
tool_resources={
102-
"file_search": {"vector_store_ids": [self.vector_store.id]}
144+
"file_search": {"vector_store_ids": [vector_store.id]}
103145
},
104146
)
105147
```
148+
106149
#### Create a Thread and Get Messages
107150

151+
Now that we have an assistant that is able to pull context from our vector store, let's query the assistant. This is done with the assistance of threads and runs (see the [assistants overview](https://platform.openai.com/docs/assistants/overview) for more info).
152+
153+
We'll make a query specific to the information in the documents we've uploaded:
154+
108155
```python
156+
# this query can only be answered using the uploaded documents
157+
query = "When is the birthday of Joseph's pet frog?"
158+
109159
# create thread
110160
thread = client.beta.threads.create()
111161
client.beta.threads.messages.create(
112162
thread_id=thread.id,
113163
role="user",
114-
content=message_prompt,
164+
content=query,
115165
)
116166

117167
# create run
118168
run = client.beta.threads.runs.create_and_poll(
119169
assistant_id=assistant.id, thread_id=thread.id
120170
)
171+
```
172+
173+
You'll notice that both documents are needed in order to answer this question. One contains the actual birthday date, while the other contains the relationship information between Joseph and Milo the frog. This is one of the reasons LLMs are utilized when extracting information from documents; they can integrate specific pieces of information across multiple sources.
121174

175+
#### View the Response
176+
177+
With the run executed, you can now list the messages associated with that run to get the response to our query
178+
179+
```python
122180
# get messages
123181
messages = self.client.beta.threads.messages.list(
124182
thread_id=thread.id, run_id=run.id
125183
).data
126184

185+
# print messages
186+
print(messages)
187+
```
188+
189+
The output of this `print(messages)` command will look something like this:
190+
191+
```text
192+
INSERT OUTPUT
127193
```
128194

195+
You'll see that our Frog Buddy assistant was able to recieve the contextual information it needed in order to know how to answer the query.
196+
197+
And this just scratches the surface of what you can create with the OpenAI SDK leveraging LeapfrogAI. This may just be a simple example that doesn't necessarily require the added overhead of RAG, but when you need to search for information hidden in hundreds or thousands of documents, you may not be able to hand your LLM all the data at once, which is where RAG really comes in handy.
129198

130199
## Questions/Feedback
200+
201+
If you have any questions, feedback, or specific update requests on this development guide, please open an issue on the [LeapfrogAI Github Repository](https://github.com/defenseunicorns/leapfrogai)

0 commit comments

Comments
 (0)