Skip to content

Commit 4f7f6a2

Browse files
authored
Langflow demonstration project tutorial (#311)
1 parent c34b138 commit 4f7f6a2

20 files changed

+210
-0
lines changed

examplecode/tools/langflow.mdx

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
---
2+
title: Langflow
3+
---
4+
5+
[Langflow](https://www.langflow.org/) is a visual framework for building multi-agent and RAG applications.
6+
It is open-source, fully customizable, and works with most LLMs and many vector stores out of the box.
7+
8+
![Langflow designer](/img/langflow/designer.png)
9+
10+
This no-code, hands-on demonstration walks you through creating a Langflow project that enables you to use GPT-4o-mini to chat
11+
in real time with a PDF document that is processed by Unstructured and has its processed data stored in an
12+
[Astra DB](https://www.datastax.com/products/datastax-astra) vector database.
13+
14+
## Prerequisites
15+
16+
import AstraDBShared from '/snippets/general-shared-text/astradb.mdx';
17+
18+
<AstraDBShared />
19+
20+
Also:
21+
22+
- [Sign up for an OpenAI account](https://platform.openai.com/signup), and [get your OpenAI API key](https://help.openai.com/en/articles/4936850-where-do-i-find-my-openai-api-key).
23+
- [Sign up for a free Langflow account](https://astra.datastax.com/signup?type=langflow).
24+
- [Get your Unstructured Serverless API key](/api-reference/api-services/saas-api-development-guide#get-started).
25+
26+
## Create and run the demonstration project
27+
28+
<Steps>
29+
<Step title="Create the Langflow project">
30+
1. Sign in to your Langflow dashboard.
31+
2. From your dashboard, click **New Project**.
32+
3. Click **Blank Flow**.
33+
</Step>
34+
<Step title="Add the Unstructured component">
35+
In this step, you add a component that instructs Unstructured Serverless API services to process a local file or a local directory of files that you specify.
36+
37+
1. On the sidebar, expand **Experimental (Beta)**, and then expand **Loaders**.
38+
2. Drag the **Unstructured** component onto the designer area.
39+
3. In the **Unstructured** component, click the box or icon next to **File**, and then select a file or a directory of files for Unstructured to process. This component works only with the file extensions `.pdf`, `.docx`, and `.txt`.
40+
41+
You can select any files that you want. This demonstration uses [the text of the United States Constitution in PDF format](https://constitutioncenter.org/media/files/constitution.pdf),
42+
saved to your local development machine.
43+
44+
4. For **Unstructured.io Serverless API Key**, enter your Unstructured API key value.
45+
46+
![Unstructured component](/img/langflow/unstructured-component.png)
47+
48+
5. Wait until **Saved** appears in the top navigation bar.
49+
50+
![Saved message](/img/langflow/saved.png)
51+
52+
</Step>
53+
<Step title="Add the OpenAI Embeddings component">
54+
In this step, you add a component that generates vector embeddings for the processed data that Unstructured outputs.
55+
56+
1. On the sidebar, expand **Embeddings**, and then drag the **OpenAI Embeddings** component onto the designer area.
57+
2. In the **OpenAI Embeddings** component, for **Model**, select `text-embedding-3-large`.
58+
3. For **OpenAI API Key**, enter your OpenAI API key's value.
59+
60+
![OpenAI Embeddings component](/img/langflow/openai-embeddings-component.png)
61+
62+
4. Wait until **Saved** appears in the top navigation bar.
63+
</Step>
64+
<Step title="Add the Astra DB components">
65+
In this step, you add two components. The first component instructs Astra DB to ingest into the specified Astra DB collection the processed data that Unstructured outputs along
66+
with the associated generated vector embeddings. The second component instructs Astra DB to take user-supplied chat messages and perform contextual
67+
searches over the ingested data in the specified Astra DB collection, outputting its search results.
68+
69+
1. On the sidebar, expand **Vector Stores**, and then drag the **Astra DB** component onto the designer area.
70+
2. Double-click the **Astra DB** component's title bar, and rename the component to `Astra DB Ingest`.
71+
3. Repeat these previous two actions to add a second **Astra DB** component, renaming it to `Astra DB RAG`.
72+
4. In both of these **Astra DB** components, in the **Database** list, select the name of your Astra DB database. Make sure this is the same database name in both components.
73+
5. In the **Collection** list in both components, select the name of the collection in the database. Make sure this is the same collection name in both components.
74+
6. In the **Astra DB Application Token** box in both components, enter your Astra DB application token's value. Make sure this is the same application token value in both components.
75+
7. Connect the **Data** output from the **Unstructured** component to the **Ingest Data** input in the **Astra DB Ingest** component.
76+
77+
To make the connection, click and hold your mouse pointer inside of the circle next to **Data** in the **Unstructured** component.
78+
While holding your mouse pointer, drag it over into the circle next to **Ingest Data** in the **Astra DB Ingest** component. Then
79+
release your mouse pointer. A line appears between these two circles.
80+
81+
8. Connect the **Embeddings** output from the **OpenAI Embeddings** component to the **Embedding or Astra Vectorize** input in the **Astra DB Ingest** component.
82+
83+
![Astra DB component](/img/langflow/astra-db-component.png)
84+
85+
9. Wait until **Saved** appears in the top navigation bar.
86+
10. In the title bar of the **Astra DB Ingest** component, click the play icon. This ingests the processed data
87+
from Unstructured and the associated generated vector embeddings into the specified Astra DB collection.
88+
89+
![Play icon](/img/langflow/build.png)
90+
91+
11. Wait until **Building** disppears from the top navigation bar, and a green check mark appears next to this play icon. This could take several minutes.
92+
93+
<Note>
94+
Each time you click the play icon in the **Astra DB Ingest** component, Unstructured reprocesses the specified local
95+
file or a local directory. If neither the specified file names or directories nor the specified collection name change, this could result in multiple duplicate records
96+
being inserted into the specified Astra DB collection. You should only click the play icon in the **Astra DB Ingest** component when you want to insert new processed data into
97+
the specified Astra DB collection.
98+
</Note>
99+
</Step>
100+
<Step title="Add the Chat Input component">
101+
In this step, you add a component that takes user-supplied chat messages and sends them as input to Astra DB for contextual searching.
102+
103+
1. On the sidebar, expand **Inputs**, and then drag the **Chat Input** component onto the designer area.
104+
2. Connect the **Message** output from the **Chat Input** component to the **Search Input** input in the **Astra DB RAG** component.
105+
106+
![Chat Input component](/img/langflow/chat-input-component.png)
107+
108+
3. Wait until **Saved** appears in the top navigation bar.
109+
</Step>
110+
<Step title="Add the Parse Data component">
111+
In this step, you add a component that takes the Astra DB search results and converts them into plain text, suitable for inclusion in
112+
a prompt to a text-based LLM.
113+
114+
1. On the sidebar, expand **Helpers**, and then drag the **Parse Data** component onto the designer area.
115+
2. Connect the **Search Results** output from the **Astra DB RAG** component to the **Data** input in the **Parse Data** component.
116+
117+
![Parse Data component](/img/langflow/parse-data-component.png)
118+
119+
3. Wait until **Saved** appears in the top navigation bar.
120+
</Step>
121+
<Step title="Add the Prompt component">
122+
In this step, you add a component that builds a prompt and then sends it to a text-based LLM.
123+
124+
1. On the sidebar, expand **Prompts**, and then drag the **Prompt** component onto the designer area.
125+
2. In the **Prompt** component, next to **Template**, click the box or arrow icon.
126+
3. In the **Edit Prompt** window, enter the following prompt:
127+
128+
```text
129+
{context}
130+
131+
---
132+
133+
Given the context above, answer the question as best as possible.
134+
135+
Question: {question}
136+
137+
Answer:
138+
```
139+
140+
4. Click **Check & Save**.
141+
142+
![Prompt component](/img/langflow/edit-prompt.png)
143+
144+
5. Connect the **Text** output from the **Parse Data** component to the **context** input in the **Prompt** component.
145+
146+
![Connect Prompt component](/img/langflow/connect-prompt-component.png)
147+
148+
149+
6. Connect the **Message** output from the **Chat Input** component to the **question** input in the **Prompt** component.
150+
151+
<Note>
152+
You will now have two connections from the **Message** output in the **Chat Input** component:
153+
154+
- One connection was already made to the **Search Input** input in the **Astra DB RAG** component.
155+
- Another connection has just now been made to the **question** input in the **Prompt** component.
156+
</Note>
157+
158+
7. Wait until **Saved** appears in the top navigation bar.
159+
</Step>
160+
<Step title="Add the OpenAI component">
161+
In this step, you create a component that sends a prompt to a text-based LLM and outputs the LLM's response.
162+
163+
1. On the sidebar, expand **Models**, and then drag the **OpenAI** component onto the designer area.
164+
2. In the **Model Name** list, select **gpt-4o-mini**.
165+
3. For **OpenAI API Key**, enter your OpenAI API key's value.
166+
4. For **Temperature**, enter `0.1`.
167+
5. Connect the **Prompt Message** output from the **Prompt** component to the **Input** input in the **OpenAI** component.
168+
169+
![OpenAI component](/img/langflow/openai-component.png)
170+
171+
6. Wait until **Saved** appears in the top navigation bar.
172+
</Step>
173+
<Step title="Add the Chat Output component">
174+
In this step, you create a component that returns the answer to the user's original chat message.
175+
176+
1. On the sidebar, expand **Outputs**, and then drag the **Chat Output** component onto the designer area.
177+
2. Connect the **Text** output from the **OpenAI** component to the **Text** input in the **Chat Output** component.
178+
179+
![Chat Output component](/img/langflow/chat-output-component.png)
180+
181+
3. Wait until **Saved** appears in the top navigation bar.
182+
183+
The final project should look like this:
184+
185+
![Final project results](/img/langflow/final-project.png)
186+
187+
</Step>
188+
<Step title="Run the project">
189+
1. In the designer area, click **Playground**.
190+
191+
![Open Playground button](/img/langflow/open-playground.png)
192+
193+
2. Enter a question into the chat box, for example, `What rights does the fifth amendment guarantee?` Then press the send button.
194+
195+
![Playground window](/img/langflow/playground.png)
196+
197+
3. Wait until the answer appears.
198+
4. Ask as many additional questions as you want to.
199+
</Step>
200+
</Steps>
201+
202+
## Learn more
203+
204+
See the [Langflow documentation](https://docs.langflow.org/).
92.1 KB
Loading

img/langflow/build.png

23.6 KB
Loading
64.8 KB
Loading
40.6 KB
Loading
38.8 KB
Loading

img/langflow/delete-connector.png

93.4 KB
Loading

img/langflow/designer.png

160 KB
Loading

img/langflow/edit-prompt.png

56.3 KB
Loading

img/langflow/final-project.png

196 KB
Loading

0 commit comments

Comments
 (0)