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
{{ message }}
This repository was archived by the owner on Feb 15, 2025. It is now read-only.
Copy file name to clipboardExpand all lines: website/content/en/docs/dev-with-lfai-guide/dev_guide.md
+79-8Lines changed: 79 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -23,6 +23,7 @@ In order to utilize the LeapfrogAI API outside of the User Interface, you'll nee
23
23
#### Via the UI
24
24
25
25
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
+
26
27
- Select the **Settings** icon ⚙️ in the top-right corner
27
28
- Select **API Keys**
28
29
- Select **Create New**
@@ -72,21 +73,62 @@ This is just a basic example; check out the [chat completion reference](https://
72
73
73
74
### Building a RAG Pipeline using Assistants
74
75
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)**.
76
77
77
-
We'll break this example down into a few step:
78
+
We'll break this example down into a few steps:
78
79
79
80
#### Create a Vector Store
80
81
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:
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
+
withopen(doc, "rb") asfile: # 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
+
83
125
#### Create an Assistant
84
126
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:
86
128
87
129
```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="""
90
132
You are a helpful, frog-themed AI bot that answers questions for a user. Keep your response short and direct.
91
133
You may receive a set of context and a question that will relate to the context.
92
134
Do not give information outside the document or repeat your findings.
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
+
108
155
```python
156
+
# this query can only be answered using the uploaded documents
157
+
query ="When is the birthday of Joseph's pet frog?"
158
+
109
159
# create thread
110
160
thread = client.beta.threads.create()
111
161
client.beta.threads.messages.create(
112
162
thread_id=thread.id,
113
163
role="user",
114
-
content=message_prompt,
164
+
content=query,
115
165
)
116
166
117
167
# create run
118
168
run = client.beta.threads.runs.create_and_poll(
119
169
assistant_id=assistant.id, thread_id=thread.id
120
170
)
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.
121
174
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
122
180
# get messages
123
181
messages =self.client.beta.threads.messages.list(
124
182
thread_id=thread.id, run_id=run.id
125
183
).data
126
184
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
127
193
```
128
194
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.
129
198
130
199
## 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