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.
[cli] Add Commands to create an application from an archetype (LangSt…
- Loading branch information
Showing
8 changed files
with
253 additions
and
27 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
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
125 changes: 125 additions & 0 deletions
125
...am-cli/src/main/java/ai/langstream/cli/commands/archetypes/CreateAppFromArchetypeCmd.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,125 @@ | ||
/* | ||
* 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. | ||
*/ | ||
package ai.langstream.cli.commands.archetypes; | ||
|
||
import ai.langstream.admin.client.AdminClient; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import lombok.SneakyThrows; | ||
import picocli.CommandLine; | ||
|
||
@CommandLine.Command( | ||
name = "create-application", | ||
header = "Create an application from an archetype") | ||
public class CreateAppFromArchetypeCmd extends BaseArchetypeCmd { | ||
|
||
private static final ObjectMapper mapper = new ObjectMapper(); | ||
|
||
@CommandLine.Parameters(description = "Name of the application") | ||
private String name; | ||
|
||
@CommandLine.Option( | ||
names = {"-a", "--archetype"}, | ||
description = "Id of the archetype to use") | ||
private String archetypeId; | ||
|
||
@CommandLine.Option( | ||
names = {"-pf", "--parameters-file"}, | ||
description = "File that contains the parameters to use for the application") | ||
private String parametersFile; | ||
|
||
@CommandLine.Option( | ||
names = {"-p", "--parameters"}, | ||
description = "Parameters, in JSON format") | ||
private String parameters; | ||
|
||
@CommandLine.Option( | ||
names = {"--dry-run"}, | ||
description = | ||
"Dry-run mode. Do not deploy the application but only resolves placeholders and display the result.") | ||
private boolean dryRun; | ||
|
||
@CommandLine.Option( | ||
names = {"-o"}, | ||
description = | ||
"Output format for dry-run mode. Formats are: yaml, json. Default value is yaml.") | ||
private Formats format = Formats.yaml; | ||
|
||
Formats format() { | ||
ensureFormatIn(format, Formats.json, Formats.yaml); | ||
return format; | ||
} | ||
|
||
@Override | ||
@SneakyThrows | ||
public void run() { | ||
Map<String, Object> parametersFromFile = readParametersFromFile(parametersFile); | ||
Map<String, Object> parametersFromCommandLine = readParameters(parameters); | ||
Map<String, Object> finalParameters = new HashMap<>(); | ||
finalParameters.putAll(parametersFromFile); | ||
finalParameters.putAll(parametersFromCommandLine); | ||
|
||
AdminClient client = getClient(); | ||
// verify archetype exists | ||
client.archetypes().get(archetypeId); | ||
|
||
final String response = | ||
client.applications() | ||
.deployFromArchetype(name, archetypeId, finalParameters, dryRun); | ||
if (dryRun) { | ||
final Formats format = format(); | ||
print(format == Formats.raw ? Formats.yaml : format, response, null, null); | ||
} else { | ||
log(String.format("application %s deployed", name)); | ||
} | ||
} | ||
|
||
private Map<String, Object> readParametersFromFile(String parametersFile) { | ||
if (parametersFile == null || parametersFile.isEmpty()) { | ||
return Map.of(); | ||
} | ||
final File file = new File(parametersFile); | ||
if (!file.exists()) { | ||
throw new IllegalArgumentException("File " + parametersFile + " does not exist"); | ||
} | ||
try { | ||
return mapper.readValue(file, new TypeReference<Map<String, Object>>() {}); | ||
} catch (IOException err) { | ||
throw new IllegalArgumentException( | ||
"Cannot parse parameters file " | ||
+ file.getAbsolutePath() | ||
+ ": " | ||
+ err.getMessage(), | ||
err); | ||
} | ||
} | ||
|
||
private Map<String, Object> readParameters(String parameters) { | ||
if (parameters == null || parameters.isEmpty()) { | ||
return Map.of(); | ||
} | ||
try { | ||
return mapper.readValue(parameters, new TypeReference<Map<String, Object>>() {}); | ||
} catch (IOException err) { | ||
throw new IllegalArgumentException( | ||
"Cannot parse parameters " + parameters + ": " + err.getMessage(), err); | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
langstream-cli/src/main/java/ai/langstream/cli/commands/archetypes/GetArchetypeCmd.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,35 @@ | ||
/* | ||
* 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. | ||
*/ | ||
package ai.langstream.cli.commands.archetypes; | ||
|
||
import lombok.SneakyThrows; | ||
import picocli.CommandLine; | ||
|
||
@CommandLine.Command(name = "get", header = "Get the metadata about an archetype") | ||
public class GetArchetypeCmd extends BaseArchetypeCmd { | ||
|
||
@CommandLine.Option( | ||
names = {"-a", "--archetype"}, | ||
description = "Id of the archetype to get") | ||
private String archetypeId; | ||
|
||
@Override | ||
@SneakyThrows | ||
public void run() { | ||
final String body = getClient().archetypes().get(archetypeId); | ||
log(body); | ||
} | ||
} |
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
langstream-webservice/src/test/website-qa-chatbot-parameters-example.json
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 @@ | ||
{ | ||
"website-url": ["https://docs.langstream.ai"], | ||
"website-crawler-depth": 2, | ||
"embeddings-model": "some-model", | ||
"access-key": "sdfsf", | ||
"chat-completion-model":"chat-completion-model", | ||
"llm-prompt": [ | ||
{ "role": "system", | ||
"content": "bla bla bla"}, | ||
{ "role": "user", | ||
"content": "{{ value.question }}"} | ||
], | ||
"astra-database": "some database", | ||
"astra-client-id": "dsfdf", | ||
"astra-client-secret": "fsfsdsfs", | ||
"astra-token": "sdfsdfs", | ||
"s3-bucket": "busdfsdfsf", | ||
"s3-access-key": "sdfsdf", | ||
"kafka-bootstrap-servers": "localhost:9092", | ||
"kafka-username": "sdfsdf", | ||
"kafka-password": "kafsdfffdf" | ||
} |