Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[compute-ai-embeddings] Rework support for Hugging Face local models for embeddings #584

Merged
merged 5 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions examples/applications/compute-hugging-face-embeddings/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Computing text embeddings with Hugging Face - local execution

This sample application shows how to use Hugging Face to compute text embeddings without calling the API.

## Configuring the model

In order to use an open source model, you need to configure the model in the pipeline.yaml file.
You can do it using environment variables or by editing the file directly.

```
export HUGGING_FACE_PROVIDER=local
export HUGGING_FACE_EMBEDDINGS_MODEL=multilingual-e5-small
export HUGGING_FACE_EMBEDDINGS_MODEL_URL=djl://ai.djl.huggingface.pytorch/intfloat/multilingual-e5-small
```

if you want to the API set HUGGING_FACE_PROVIDER to "api" and configure your API access key.
```
export HUGGING_FACE_PROVIDER=api
export HUGGING_FACE_ACCESS_KEY=your_access_key
export HUGGING_FACE_EMBEDDINGS_MODEL=multilingual-e5-small
```

## Deploy the LangStream application

```
./bin/langstream apps deploy test -app examples/applications/compute-hugging-face-embeddings -i examples/instances/kafka-kubernetes.yaml -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)
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#
#
# 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: "hugging-face-configuration"
name: "Hugging Face AI configuration"
configuration:
access-key: "${ secrets.hugging-face.access-key }"
provider: "${ secrets.hugging-face.provider }"
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.
#

gateways:
- id: produce-input
type: produce
topic: input-topic
parameters:
- sessionId
produce-options:
headers:
- key: langstream-client-session-id
value-from-parameters: sessionId

- id: consume-output
type: consume
topic: output-topic
parameters:
- sessionId
consume-options:
filters:
headers:
- key: langstream-client-session-id
value-from-parameters: sessionId
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#
# 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: "input-topic"
creation-mode: create-if-not-exists
- name: "output-topic"
creation-mode: create-if-not-exists
errors:
on-failure: "skip"
pipeline:
- name: "convert-to-structure"
type: "document-to-json"
input: "input-topic"
configuration:
text-field: "question"
- name: "compute-embeddings"
type: "compute-ai-embeddings"
output: "output-topic"
configuration:
model: "${secrets.hugging-face.embeddings-model}" # This is the id of the model
model-url: "${secrets.hugging-face.embeddings-model-url}" # This is the URL of the repository containing the model
embeddings-field: "value.embeddings"
text: "{{ value.question }}"
batch-size: 10
# this is in milliseconds. It is important to take this value into consideration when using this agent in the chat response pipeline
# in fact this value impacts the latency of the response
# for latency sensitive applications, consider to set batch-size to 1 or flush-interval to 0
flush-interval: 500
3 changes: 3 additions & 0 deletions examples/secrets/secrets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ secrets:
- id: hugging-face
data:
access-key: ${HUGGING_FACE_ACCESS_KEY:-}
provider: ${HUGGING_FACE_PROVIDER:-api}
embeddings-model: ${HUGGING_FACE_EMBEDDINGS_MODEL:-multilingual-e5-small}
embeddings-model-url: ${HUGGING_FACE_EMBEDDINGS_MODEL_URL:-djl://ai.djl.huggingface.pytorch/intfloat/multilingual-e5-small}
- id: astra
data:
clientId: ${ASTRA_CLIENT_ID:-}
Expand Down
3 changes: 2 additions & 1 deletion langstream-agents/langstream-ai-agents/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@
<groupId>ai.djl.pytorch</groupId>
<artifactId>pytorch-native-cpu</artifactId>
<!--classifier>osx-aarch64</classifier-->
<classifier>osx-x86_64</classifier>
<!--<classifier>osx-x86_64</classifier>-->
<classifier>linux-x86_64</classifier>
</dependency>
<dependency>
<groupId>ai.djl.pytorch</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ public CompletionsService getCompletionsService(
public EmbeddingsService getEmbeddingsService(Map<String, Object> additionalConfiguration)
throws Exception {
String provider =
additionalConfiguration
providerConfiguration
.getOrDefault("provider", ComputeProvider.API.name())
.toString()
.toUpperCase();
String modelUrl = (String) additionalConfiguration.get("modelUrl");
String modelUrl = (String) additionalConfiguration.get("model-url");
String model = (String) additionalConfiguration.get("model");
Map<String, String> options = (Map) additionalConfiguration.get("options");
Map<String, String> arguments = (Map) additionalConfiguration.get("arguments");
Expand All @@ -98,7 +98,7 @@ public EmbeddingsService getEmbeddingsService(Map<String, Object> additionalConf
if (model != null && !model.isEmpty()) {
builder.modelName(model);
if (modelUrl == null || modelUrl.isEmpty()) {
modelUrl = "djl://ai.djl.huggingface.pytorch" + model;
modelUrl = "djl://ai.djl.huggingface.pytorch/" + model;
log.info("Automatically computed model URL {}", modelUrl);
}
}
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ public void generateSteps(
aiServiceConfigurationGenerator);
aiServiceConfigurationGenerator.generateAIServiceConfiguration(
(String) step.remove("ai-service"));

// in the user config we use the pascal but the downstream impl requires snake
final Object modelUrl = step.remove("modelUrl");
if (modelUrl != null) {
step.put("model-url", modelUrl);
}
}
};

Expand Down Expand Up @@ -153,7 +147,8 @@ public void generateSteps(
@ConfigProperty(
description =
"""
URL of the model to use. (HuggingFace only). The default is computed from the model: "djl://ai.djl.huggingface.pytorch{model}"
URL of the model to use. (HuggingFace only). The default is computed from the model: "djl://ai.djl.huggingface.pytorch/{model}"
""")
@JsonProperty("model-url")
private String modelUrl;
}
Original file line number Diff line number Diff line change
Expand Up @@ -472,8 +472,8 @@ public void testStepsDoc() {
"type" : "string",
"defaultValue" : "text-embedding-ada-002"
},
"modelUrl" : {
"description" : "URL of the model to use. (HuggingFace only). The default is computed from the model: \\"djl://ai.djl.huggingface.pytorch{model}\\"",
"model-url" : {
"description" : "URL of the model to use. (HuggingFace only). The default is computed from the model: \\"djl://ai.djl.huggingface.pytorch/{model}\\"",
"required" : false,
"type" : "string"
},
Expand Down
Loading
Loading