-
Notifications
You must be signed in to change notification settings - Fork 141
Add semantic filters with abstractions for semantic models as a platform #808
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
22b1fd1
implementation of semantic filters in Wayang
mspruc d5ca01d
delete bin
mspruc 4f94f41
delete unused file
mspruc b01802d
spell check and grammar
mspruc 2a83058
Add missing license to guide
mspruc 7346e70
Add semantic operators with platforms per model
juripetersen db219ba
Update pom and fix address for Ollama
juripetersen 4597c11
Mock Ollama API for test
juripetersen 26ba63f
Remove dotfiles
juripetersen c255672
Remove default.properties for flink
juripetersen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,156 @@ | ||
| <!-- | ||
|
|
||
| Licensed to the Apache Software Foundation (ASF) under one or more | ||
| contributor license agreements. See the NOTICE file distributed with | ||
| this work for additional information regarding copyright ownership. | ||
| The ASF licenses this file to You 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. | ||
|
|
||
| --> | ||
|
|
||
| # Developing with Semantic Operators in Apache Wayang | ||
|
|
||
| This guide explains how to define semantic operators, provide executable implementations, register multiple implementations, estimate their costs, and let Apache Wayang select an implementation during optimization. | ||
|
|
||
| The example uses a semantic filter that classifies movie reviews as positive or negative through Ollama. | ||
|
|
||
| ## 1. Semantic operators | ||
|
|
||
| A semantic operator is an operator much like any Wayang operator, however, it takes a prompt as input, | ||
| that describes how it should act. | ||
|
|
||
| For example: | ||
|
|
||
| ```java | ||
| .semanticFilter( | ||
| "Analyze the review after the | and write either " | ||
| + "\"POSITIVE\" if the review has a positive sentiment and " | ||
| + "\"NEGATIVE\" if the review has a negative sentiment." | ||
| ) | ||
| ``` | ||
|
|
||
| The prompt describes the task. A `SemanticAlgorithm` provides one concrete implementation of that task. | ||
| A `SemanticAlgorithm` could theoretically be any UDF you desire, there are no strict requirements on its implementation. | ||
| The implementation only that the requires the UDF is implemented as something that takes an input `Record` and a `prompt` and outputs | ||
| whatever datatype is required by the operator. | ||
|
|
||
| The Ollama local open-source model is used as an example for this guide, but may also be useful for quick development. | ||
|
|
||
| We set up our local model hosting locally using Docker: | ||
|
|
||
| ```yaml | ||
| ollama: | ||
| image: ollama/ollama:latest | ||
| container_name: apache-wayang-ollama | ||
| ports: | ||
| - "11434:11434" | ||
| volumes: | ||
| - ollama-data:/root/.ollama | ||
| - ./docker/ollama-init.sh:/ollama-init.sh | ||
| entrypoint: ["/bin/bash", "/ollama-init.sh"] | ||
| restart: always | ||
| tty: true | ||
| networks: | ||
| - wayang-network | ||
| ``` | ||
|
|
||
| ```sh | ||
| #!/bin/bash | ||
| ollama serve & | ||
| sleep 10 | ||
| ollama pull tinyllama | ||
| wait | ||
| ``` | ||
|
|
||
| We setup the backend call to the model in Wayang: | ||
|
|
||
| ```java | ||
| private static String callOllama(final String prompt) throws IOException, InterruptedException { | ||
| final String requestBody = String.format("{\"model\": \"%s\", \"prompt\": \"%s\", \"stream\": false}", | ||
| MODEL_NAME, escapeJson(prompt)); | ||
|
|
||
| final HttpRequest request = HttpRequest.newBuilder().uri(URI.create(OLLAMA_API_URL)) | ||
| .header("Content-Type", "application/json").POST(HttpRequest.BodyPublishers.ofString(requestBody)) | ||
| .timeout(Duration.ofMinutes(2)).build(); | ||
|
|
||
| final HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); | ||
|
|
||
| if (response.statusCode() != 200) { | ||
| throw new IOException("Ollama API error: " + response.body()); | ||
| } | ||
|
|
||
| return parseOllamaResponse(response.body()); | ||
| } | ||
| ``` | ||
|
|
||
| and the semantic UDF: | ||
|
|
||
| ```java | ||
| public static boolean isPositiveSentiment(final Review review, final String prompt) | ||
| throws IOException, InterruptedException { | ||
| final String response = callOllama(prompt + " | " + review.getReviewText()); | ||
| return response.contains("POSITIVE"); | ||
| } | ||
| ``` | ||
|
|
||
| Now we can define our `SemanticAlgorithm`: | ||
|
|
||
| ```java | ||
| final SemanticAlgorithm ollamaFilter = new SemanticAlgorithm(); | ||
| ollamaFilter.impl = (input, prompt) -> { | ||
| try { | ||
| return isPositiveSentiment((Review) input); | ||
| } catch (IOException | InterruptedException e) { | ||
| throw new RuntimeException("Ollama call failed", e); | ||
| } | ||
| }; | ||
| ``` | ||
|
|
||
| And also provide a UDF load estimator: | ||
|
|
||
| ```java | ||
| ollamaFilter.loadProfileEstimator = | ||
| LoadProfileEstimators.createFromSpecification( | ||
| "wayang.semantic.ollama.model1.load", | ||
| configuration | ||
| ); | ||
| ``` | ||
|
|
||
| Note that you should provide a separate configuration for each semantic operator. | ||
| Now we register these UDFs with the `SemanticPlugin` this automatically constructs a new operator | ||
| per UDF. Please note this may have implications for optimization time depending on your setup. | ||
|
|
||
| ```java | ||
| final SemanticPlugin plugin = Semantic.plugin() | ||
| .withOperatorMapping(SemanticFilterOperator.class, ollamaFilter) | ||
| .withOperatorMapping(SemanticFilterOperator.class, ollamaFilter2) | ||
| .withOperatorMapping(SemanticFilterOperator.class, ollamaFilter3); | ||
|
|
||
| final WayangContext wayangContext = new WayangContext() | ||
| .withPlugin(Java.basicPlugin()) | ||
| .withPlugin(plugin); | ||
| ``` | ||
|
|
||
| Currently, we only have mappings for semantic operators in Java, so you also need the `Java.basicPlugin()`. | ||
| Finally, you can construct your Wayang plan: | ||
|
|
||
| ```java | ||
| final Collection<Long> positiveReviewCnt = planBuilder.loadCollection(loadReviews()) | ||
| .filter(review -> "taken_3".equals(review.getId())) | ||
| .semanticFilter("Analyze the review after the | and write either \"POSITIVE\" if the review has a positive sentiment and \"NEGATIVE\" if the review has a negative sentiment.") | ||
| .withTargetModels(ollamaFilter, ollamaFilter2, ollamaFilter3) | ||
| .count() | ||
| .collect(); | ||
| ``` | ||
|
|
||
| You need to provide semantic operators with their target models, even if you plan to use all models you've constructed. | ||
|
|
This file contains hidden or 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 hidden or 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 hidden or 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
77 changes: 77 additions & 0 deletions
77
.../wayang-basic/src/main/java/org/apache/wayang/basic/operators/SemanticFilterOperator.java
This file contains hidden or 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,77 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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. | ||
| */ | ||
|
|
||
| package org.apache.wayang.basic.operators; | ||
|
|
||
| import java.util.Set; | ||
|
|
||
| import org.apache.wayang.core.plan.wayangplan.UnaryToUnaryOperator; | ||
| import org.apache.wayang.core.types.DataSetType; | ||
|
|
||
| public class SemanticFilterOperator<T> extends UnaryToUnaryOperator<T, T> implements SemanticOperator { | ||
|
|
||
| private final String prompt; | ||
|
|
||
| private final Set<Object> targetModels; | ||
|
|
||
| public SemanticFilterOperator(final DataSetType<T> type, final String prompt) { | ||
| super(type, type, false); | ||
| this.prompt = prompt; | ||
| this.targetModels = null; | ||
| } | ||
|
|
||
| public SemanticFilterOperator(final DataSetType<T> type, final String prompt, final Set<Object> targetModels) { | ||
| super(type, type, false); | ||
| this.prompt = prompt; | ||
| this.targetModels = targetModels; | ||
| } | ||
|
|
||
| public SemanticFilterOperator(final DataSetType<T> type) { | ||
| super(type, type, false); | ||
| this.prompt = ""; | ||
| this.targetModels = null; | ||
| } | ||
|
|
||
| public SemanticFilterOperator(final DataSetType<T> inputType, final DataSetType<T> outputType, | ||
| final boolean isSupportingBroadcastInputs) { | ||
| super(inputType, outputType, isSupportingBroadcastInputs); | ||
| this.prompt = ""; | ||
| this.targetModels = null; | ||
| } | ||
|
|
||
| public SemanticFilterOperator(final UnaryToUnaryOperator<T, T> that) { | ||
| super(that); | ||
| this.prompt = ""; | ||
| this.targetModels = null; | ||
| } | ||
|
|
||
| @Override | ||
| public String getPrompt() { | ||
| return prompt; | ||
| } | ||
|
|
||
| @Override | ||
| public Set<Object> getTargetModels() { | ||
| return targetModels; | ||
| } | ||
|
|
||
| @Override | ||
| public void addTargetModel(final Object model) { | ||
| targetModels.add(model); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.