-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[examples] Improve the S3 Source example (#706)
- Loading branch information
Showing
12 changed files
with
250 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Preprocessing Text | ||
|
||
This sample application shows how to use some common NLP techniques to preprocess text data and then write the results to a Vector Database. | ||
|
||
We have two pipelines: | ||
|
||
The extract-text.yaml file defines a pipeline that will: | ||
|
||
- Extract text from document files (PDF, Word...) | ||
- Detect the language and filter out non-English documents | ||
- Normalize the text | ||
- Split the text into chunks | ||
- Write the chunks to a Vector Database, in this case DataStax Astra DB | ||
|
||
## Prerequisites | ||
|
||
Prepare some PDF files and upload them to a bucket in S3. | ||
|
||
## Deploy the LangStream application | ||
|
||
``` | ||
./bin/langstream docker run test -app examples/applications/s3-source -s examples/secrets/secrets.yaml --docker-args="-p9900:9000" | ||
``` | ||
|
||
Please note that here we are adding --docker-args="-p9900:9000" to expose the S3 API on port 9900. | ||
|
||
|
||
## Write some documents in the S3 bucket | ||
|
||
``` | ||
# Upload a document to the S3 bucket | ||
dev/s3_upload.sh localhost http://localhost:9900 documents README.md | ||
dev/s3_upload.sh localhost http://localhost:9900 documents examples/applications/s3-source/simple.pdf | ||
``` | ||
|
||
## Interact with the Chatbot | ||
|
||
Now you can use the developer UI to ask questions to the chatbot about your documents. | ||
|
||
If you have uploaded the README file then you should be able to ask "what is LangStream ?" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# | ||
# Copyright DataStax, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
topics: | ||
- name: "questions-topic" | ||
creation-mode: create-if-not-exists | ||
- name: "answers-topic" | ||
creation-mode: create-if-not-exists | ||
- name: "log-topic" | ||
creation-mode: create-if-not-exists | ||
errors: | ||
on-failure: "skip" | ||
pipeline: | ||
- name: "convert-to-structure" | ||
type: "document-to-json" | ||
input: "questions-topic" | ||
configuration: | ||
text-field: "question" | ||
- name: "compute-embeddings" | ||
type: "compute-ai-embeddings" | ||
configuration: | ||
model: "${secrets.open-ai.embeddings-model}" # This needs to match the name of the model deployment, not the base model | ||
embeddings-field: "value.question_embeddings" | ||
text: "{{ value.question }}" | ||
flush-interval: 0 | ||
- name: "lookup-related-documents" | ||
type: "query-vector-db" | ||
configuration: | ||
datasource: "JdbcDatasource" | ||
query: "SELECT text,embeddings_vector FROM documents ORDER BY cosine_similarity(embeddings_vector, CAST(? as FLOAT ARRAY)) DESC LIMIT 20" | ||
fields: | ||
- "value.question_embeddings" | ||
output-field: "value.related_documents" | ||
- name: "re-rank documents with MMR" | ||
type: "re-rank" | ||
configuration: | ||
max: 5 # keep only the top 5 documents, because we have an hard limit on the prompt size | ||
field: "value.related_documents" | ||
query-text: "value.question" | ||
query-embeddings: "value.question_embeddings" | ||
output-field: "value.related_documents" | ||
text-field: "record.text" | ||
embeddings-field: "record.embeddings_vector" | ||
algorithm: "MMR" | ||
lambda: 0.5 | ||
k1: 1.2 | ||
b: 0.75 | ||
- name: "ai-chat-completions" | ||
type: "ai-chat-completions" | ||
|
||
configuration: | ||
model: "${secrets.open-ai.chat-completions-model}" # This needs to be set to the model deployment name, not the base name | ||
# on the log-topic we add a field with the answer | ||
completion-field: "value.answer" | ||
# we are also logging the prompt we sent to the LLM | ||
log-field: "value.prompt" | ||
# here we configure the streaming behavior | ||
# as soon as the LLM answers with a chunk we send it to the answers-topic | ||
stream-to-topic: "answers-topic" | ||
# on the streaming answer we send the answer as whole message | ||
# the 'value' syntax is used to refer to the whole value of the message | ||
stream-response-completion-field: "value" | ||
# we want to stream the answer as soon as we have 20 chunks | ||
# in order to reduce latency for the first message the agent sends the first message | ||
# with 1 chunk, then with 2 chunks....up to the min-chunks-per-message value | ||
# eventually we want to send bigger messages to reduce the overhead of each message on the topic | ||
min-chunks-per-message: 20 | ||
messages: | ||
- role: system | ||
content: | | ||
An user is going to perform a questions, The documents below may help you in answering to their questions. | ||
Please try to leverage them in your answer as much as possible. | ||
If you provide code or YAML snippets, please explicitly state that they are examples. | ||
Do not provide information that is not related to the documents provided below. | ||
Documents: | ||
{{# value.related_documents}} | ||
{{ text}} | ||
{{/ value.related_documents}} | ||
- role: user | ||
content: "{{ value.question}}" | ||
- name: "cleanup-response" | ||
type: "drop-fields" | ||
output: "log-topic" | ||
configuration: | ||
fields: | ||
- "question_embeddings" | ||
- "related_documents" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# | ||
# | ||
# Copyright DataStax, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
configuration: | ||
resources: | ||
- type: "datasource" | ||
name: "JdbcDatasource" | ||
configuration: | ||
service: "jdbc" | ||
driverClass: "herddb.jdbc.Driver" | ||
url: "${secrets.herddb.url}" | ||
user: "${secrets.herddb.user}" | ||
password: "${secrets.herddb.password}" | ||
- type: "open-ai-configuration" | ||
name: "OpenAI Azure configuration" | ||
configuration: | ||
url: "${secrets.open-ai.url}" | ||
access-key: "${secrets.open-ai.access-key}" | ||
provider: "${secrets.open-ai.provider}" | ||
dependencies: | ||
- name: "HerdDB.org JDBC Driver" | ||
url: "https://repo1.maven.org/maven2/org/herddb/herddb-jdbc/0.28.0/herddb-jdbc-0.28.0-thin.jar" | ||
sha512sum: "d8ea8fbb12eada8f860ed660cbc63d66659ab3506bc165c85c420889aa8a1dac53dab7906ef61c4415a038c5a034f0d75900543dd0013bdae50feafd46f51c8e" | ||
type: "java-library" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.