Skip to content

Commit

Permalink
More on Ollama
Browse files Browse the repository at this point in the history
  • Loading branch information
eolivelli committed Nov 9, 2023
1 parent d4293ad commit 51a85fa
Show file tree
Hide file tree
Showing 11 changed files with 498 additions and 2 deletions.
1 change: 1 addition & 0 deletions examples/applications/ollama-chatbot/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
java/lib/*
96 changes: 96 additions & 0 deletions examples/applications/ollama-chatbot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Running your own Chat bot using docker

This sample application shows how to build a chat bot over the content of a website.
In this case you are going to crawl the LangStream.ai documentation website.

The Chat bot will be able to help you with LangStream.

In this example we are using [HerdDB](ps://github.com/diennea/herddb) as a vector database using the JDBC driver,
but you can use any Vector databases.


## Configure you OpenAI API Key

Export to the ENV the access key to OpenAI

```
export OPEN_AI_ACCESS_KEY=...
```

The default [secrets file](../../secrets/secrets.yaml) reads from the ENV. Check out the file to learn more about
the default settings, you can change them by exporting other ENV variables.

## Deploy the LangStream application in docker

The default docker runner starts Minio, Kafka and HerdDB, so you can run the application locally.

```
./bin/langstream docker run test -app examples/applications/docker-chatbot -s examples/secrets/secrets.yaml
```


## Talk with the Chat bot using the CLI
Since the application opens a gateway, we can use the gateway API to send and consume messages.

```
./bin/langstream gateway chat test -cg bot-output -pg user-input -p sessionId=$(uuidgen)
```

Responses are streamed to the output-topic. If you want to inspect the history of the raw answers you can
consume from the log-topic using the llm-debug gateway:

```
./bin/langstream gateway consume test llm-debug
```

## Application flow chart

```mermaid
flowchart TB
subgraph JdbcDatasource["<b>⛁ JdbcDatasource</b>"]
documents
end
subgraph streaming-cluster["<b>✉️ streaming cluster</b>"]
questions-topic
answers-topic
log-topic
chunks-topic
end
subgraph gateways["<b>gateways</b>"]
user-input --> questions-topic
answers-topic --> bot-output
log-topic --> llm-debug
end
subgraph chatbot["<b>chatbot</b>"]
A("convert-to-structure<br><i>document-to-json</i>") --> B
B("compute-embeddings<br><i>compute-ai-embeddings</i>") --> C
C("lookup-related-documents<br><i>query-vector-db</i>") --> D
D("re-rank documents with MMR<br><i>re-rank</i>") --> E
E("ai-chat-completions<br><i>ai-chat-completions</i>") --> F
F("cleanup-response<br><i>drop-fields</i>")
end
questions-topic --> A
JdbcDatasource --> C
E --> answers-topic
F --> log-topic
subgraph crawler["<b>crawler</b>"]
G("Crawl the WebSite<br><i>webcrawler-source</i>") --> H
H("Extract text<br><i>text-extractor</i>") --> I
I("Normalise text<br><i>text-normaliser</i>") --> J
J("Detect language<br><i>language-detector</i>") --> K
K("Split into chunks<br><i>text-splitter</i>") --> L
L("Convert to structured data<br><i>document-to-json</i>") --> M
M("prepare-structure<br><i>compute</i>") --> N
N("compute-embeddings<br><i>compute-ai-embeddings</i>")
O("Write<br><i>vector-db-sink</i>")
end
P["🌐 web site"] --> G
N --> chunks-topic
chunks-topic --> O
O --> documents
```
32 changes: 32 additions & 0 deletions examples/applications/ollama-chatbot/assets.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#
# 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.
#
assets:
- name: "documents-table"
asset-type: "jdbc-table"
creation-mode: create-if-not-exists
config:
table-name: "documents"
datasource: "JdbcDatasource"
create-statements:
- |
CREATE TABLE documents (
filename TEXT,
chunk_id int,
num_tokens int,
lang TEXT,
text TEXT,
embeddings_vector FLOATA,
PRIMARY KEY (filename, chunk_id));
109 changes: 109 additions & 0 deletions examples/applications/ollama-chatbot/chatbot.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#
# 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"
resources:
disk:
size: 256M
enabled: true
configuration:
model: "multilingual-e5-small"
model-url: "djl://ai.djl.huggingface.pytorch/intfloat/multilingual-e5-small"
ai-service: "huggingface"
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:
ai-service: "ollama"
model: "${secrets.ollama.model}"
# 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.
Take into consideration that the user is always asking questions about the LangStream project.
If you provide code or YAML snippets, please explicitly state that they are examples.
Do not provide information that is not related to the LangStream project.
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"
40 changes: 40 additions & 0 deletions examples/applications/ollama-chatbot/configuration.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#
#
# 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: "ollama-configuration"
name: "ollama"
configuration:
url: "${secrets.ollama.url}"
- type: "hugging-face-configuration"
name: "huggingface"
configuration:
provider: "local"
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"
Loading

0 comments on commit 51a85fa

Please sign in to comment.