This repository has been archived by the owner on Aug 25, 2024. It is now read-only.
forked from LangStream/langstream
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GenAI: add support for compute-ai-embeddings function (#14)
- Loading branch information
Showing
9 changed files
with
171 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
configuration: | ||
resources: | ||
- type: "open-ai-configuration" | ||
name: "OpenAI Azure configuration" | ||
id: "openai-configuration" | ||
configuration: | ||
url: "https://put-here-you-api-server" | ||
access-key: "put-here-you-api-key" | ||
provider": "azure" |
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,20 @@ | ||
module: "module-1" | ||
id: "pipeline-1" | ||
topics: | ||
- name: "input-topic" | ||
creation-mode: create-if-not-exists | ||
schema: | ||
type: avro | ||
schema: '{"type":"record","namespace":"examples","name":"Product","fields":[{"name":"id","type":"string"},{"name":"name","type":"string"},{"name":"description","type":"string"}]}}' | ||
- name: "output-topic" | ||
creation-mode: create-if-not-exists | ||
pipeline: | ||
- name: "compute-embeddings" | ||
id: "step1" | ||
type: "compute-ai-embeddings" | ||
input: "input-topic" | ||
output: "output-topic" | ||
configuration: | ||
model: "text-embedding-ada-002" | ||
embeddings-field: "value.embeddings" | ||
text: "{{ value.name }} {{ value.description }}" |
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
22 changes: 22 additions & 0 deletions
22
...r/src/main/java/com/datastax/oss/sga/pulsar/agents/ai/ComputeEmbeddingsAgentProvider.java
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,22 @@ | ||
package com.datastax.oss.sga.pulsar.agents.ai; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class ComputeEmbeddingsAgentProvider extends GenAIToolKitFunctionAgentProvider { | ||
|
||
public ComputeEmbeddingsAgentProvider() { | ||
super("compute-ai-embeddings"); | ||
} | ||
|
||
@Override | ||
protected void generateSteps(Map<String, Object> originalConfiguration, List<Map<String, Object>> steps) { | ||
Map<String, Object> step = Map.of( | ||
"type", "compute-ai-embeddings", | ||
"model", originalConfiguration.getOrDefault("model", "text-embedding-ada-002"), | ||
"embeddings-field", originalConfiguration.getOrDefault("embeddings-field", "embeddings"), | ||
"text", originalConfiguration.getOrDefault("text", "") | ||
); | ||
steps.add(step); | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
...rc/main/java/com/datastax/oss/sga/pulsar/agents/ai/GenAIToolKitFunctionAgentProvider.java
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,75 @@ | ||
package com.datastax.oss.sga.pulsar.agents.ai; | ||
|
||
import com.datastax.oss.sga.api.model.AgentConfiguration; | ||
import com.datastax.oss.sga.api.model.ApplicationInstance; | ||
import com.datastax.oss.sga.api.model.Module; | ||
import com.datastax.oss.sga.api.model.Resource; | ||
import com.datastax.oss.sga.api.runtime.ClusterRuntime; | ||
import com.datastax.oss.sga.api.runtime.ConnectionImplementation; | ||
import com.datastax.oss.sga.api.runtime.PhysicalApplicationInstance; | ||
import com.datastax.oss.sga.pulsar.PulsarClusterRuntime; | ||
import com.datastax.oss.sga.pulsar.PulsarTopic; | ||
import com.datastax.oss.sga.pulsar.agents.AbstractPulsarFunctionAgentProvider; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class GenAIToolKitFunctionAgentProvider extends AbstractPulsarFunctionAgentProvider { | ||
|
||
public GenAIToolKitFunctionAgentProvider(String stepType) { | ||
super(List.of(stepType), List.of(PulsarClusterRuntime.CLUSTER_TYPE)); | ||
} | ||
|
||
@Override | ||
protected String getFunctionType(AgentConfiguration agentConfiguration) { | ||
// https://github.com/datastax/pulsar-transformations/tree/master/pulsar-ai-tools | ||
return "ai-tools"; | ||
} | ||
|
||
@Override | ||
protected String getFunctionClassname(AgentConfiguration agentConfiguration) { | ||
return null; | ||
} | ||
|
||
protected void generateSteps(Map<String, Object> originalConfiguration, List<Map<String, Object>> steps) { | ||
} | ||
|
||
private void generateOpenAIConfiguration(ApplicationInstance applicationInstance, Map<String, Object> configuration) { | ||
Resource resource = applicationInstance.getResources().values().stream() | ||
.filter(r -> r.type().equals("open-ai-configuration")) | ||
.findFirst().orElse(null); | ||
if (resource != null) { | ||
String url = (String) resource.configuration().get("url"); | ||
String accessKey = (String) resource.configuration().get("access-key"); | ||
String provider = (String) resource.configuration().get("provider"); | ||
Map<String, Object> openaiConfiguration = new HashMap<>(); | ||
if (url != null) { | ||
openaiConfiguration.put("url", url); | ||
} | ||
if (accessKey != null) { | ||
openaiConfiguration.put("access-key", accessKey); | ||
} | ||
if (provider != null) { | ||
openaiConfiguration.put("provider", provider); | ||
} | ||
configuration.put("openai", openaiConfiguration); | ||
} | ||
} | ||
|
||
@Override | ||
protected Map<String, Object> computeAgentConfiguration(AgentConfiguration agentConfiguration, Module module, | ||
PhysicalApplicationInstance physicalApplicationInstance, | ||
ClusterRuntime clusterRuntime) { | ||
Map<String, Object> originalConfiguration = super.computeAgentConfiguration(agentConfiguration, module, physicalApplicationInstance, clusterRuntime); | ||
Map<String, Object> configuration = new HashMap<>(); | ||
|
||
generateOpenAIConfiguration(physicalApplicationInstance.getApplicationInstance(), configuration); | ||
|
||
List<Map<String, Object>> steps = new ArrayList<>(); | ||
configuration.put("steps", steps); | ||
generateSteps(originalConfiguration, steps); | ||
return configuration; | ||
} | ||
} |
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