Skip to content
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
63 changes: 39 additions & 24 deletions repli
Original file line number Diff line number Diff line change
Expand Up @@ -815,10 +815,24 @@ filter_templates_dir_exists() {
get_example_from_replicate() {
json=$(get_model_info "$model") || return 1

jq --arg model "$model" \
'{model: $model, input: .default_example.input}' \
<<<"$json" |
yq -P -
# Determine whether the model is official
is_official=$(jq -r '.is_official // false' <<<"$json")

if [[ "$is_official" == "true" ]]; then
log debug "model status: $(blue official)"
# Official image
jq --arg model "$model" \
'{model: $model, input: .default_example.input}' \
<<<"$json" | yq -P -
else
# Non-official image: use version instead of model
log debug "model status: $(blue unofficial)"
version=$(jq -r '.latest_version.id' <<<"$json")

jq --arg model "$model" --arg version "$version" \
'{version: ($model + ":" + $version), input: .default_example.input}' \
<<<"$json" | yq -P -
fi
}

# src/lib/get_file_url.sh
Expand Down Expand Up @@ -1165,40 +1179,41 @@ verify_success_json() {
# src/lib/yurl.sh
yurl() {
local yaml_file="$1"
local model json
local model version json payload url

# YAML validation
if ! yq -e '.model' "$yaml_file" >/dev/null 2>&1; then
log error "no model field in $(blue "$yaml_file")"
return 1
fi

if ! yq -e '.input' "$yaml_file" >/dev/null 2>&1; then
log error "no input field in $(blue "$yaml_file")"
return 1
fi

# Get the model
model=$(yq -r '.model' "$yaml_file")
model=$(yq -r '.model // ""' "$yaml_file")
version=$(yq -r '.version // ""' "$yaml_file")

# Convert .input YAML → JSON
json=$(yq -o=json '.input' "$yaml_file")
if [[ -z "$model" && -z "$version" ]]; then
log error "no model or version field in $(blue "$yaml_file")"
return 1
fi

# Replace any "<filename>" placeholders
json=$(replace_file_placeholders "$json")
if [[ -n "$model" ]]; then
url="$replicate_host/v1/models/${model}/predictions"
else
url="$replicate_host/v1/predictions"
fi

# Replace any embedded "@filename" markers
json=$(replace_embed_markers "$json")
# Convert .input YAML → JSON and replace <filename> and ~filename.txt
payload=$(yq -o=json '.' "$yaml_file")
payload=$(replace_file_placeholders "$payload")
payload=$(replace_embed_markers "$payload")

# Pipe the evaluated JSON to curl which uses it (@-) as its data.
echo "$json" |
jq '{input: .}' |
echo "$payload" |
curl -sS -X POST \
-H "Authorization: Bearer $REPLICATE_API_TOKEN" \
-H "Content-Type: application/json" \
-H "Prefer: wait" \
-d @- \
"$replicate_host/v1/models/${model}/predictions"
"$url"
}

# :command.command_functions
Expand Down Expand Up @@ -1326,9 +1341,9 @@ repli_info_command() {
repli_template_new_command() {

# src/commands/template/new.sh
model="${args[model]}"
name="${args[--name]}"
force="${args[--force]}"
local model="${args[model]}"
local name="${args[--name]}"
local force="${args[--force]}"

[[ "$name" == "auto" ]] && name="${model#*/}"

Expand Down
6 changes: 3 additions & 3 deletions src/commands/template/new.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
model="${args[model]}"
name="${args[--name]}"
force="${args[--force]}"
local model="${args[model]}"
local name="${args[--name]}"
local force="${args[--force]}"

[[ "$name" == "auto" ]] && name="${model#*/}"

Expand Down
22 changes: 18 additions & 4 deletions src/lib/get_example_from_replicate.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
get_example_from_replicate() {
json=$(get_model_info "$model") || return 1

jq --arg model "$model" \
'{model: $model, input: .default_example.input}' \
<<<"$json" |
yq -P -
# Determine whether the model is official
is_official=$(jq -r '.is_official // false' <<<"$json")

if [[ "$is_official" == "true" ]]; then
log debug "model status: $(blue official)"
# Official image
jq --arg model "$model" \
'{model: $model, input: .default_example.input}' \
<<<"$json" | yq -P -
else
# Non-official image: use version instead of model
log debug "model status: $(blue unofficial)"
version=$(jq -r '.latest_version.id' <<<"$json")

jq --arg model "$model" --arg version "$version" \
'{version: ($model + ":" + $version), input: .default_example.input}' \
<<<"$json" | yq -P -
fi
}
35 changes: 18 additions & 17 deletions src/lib/yurl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,39 @@
## usage: yurl request.yaml
yurl() {
local yaml_file="$1"
local model json
local model version json payload url

# YAML validation
if ! yq -e '.model' "$yaml_file" >/dev/null 2>&1; then
log error "no model field in $(blue "$yaml_file")"
return 1
fi

if ! yq -e '.input' "$yaml_file" >/dev/null 2>&1; then
log error "no input field in $(blue "$yaml_file")"
return 1
fi

# Get the model
model=$(yq -r '.model' "$yaml_file")
model=$(yq -r '.model // ""' "$yaml_file")
version=$(yq -r '.version // ""' "$yaml_file")

# Convert .input YAML → JSON
json=$(yq -o=json '.input' "$yaml_file")
if [[ -z "$model" && -z "$version" ]]; then
log error "no model or version field in $(blue "$yaml_file")"
return 1
fi

# Replace any "<filename>" placeholders
json=$(replace_file_placeholders "$json")
if [[ -n "$model" ]]; then
url="$replicate_host/v1/models/${model}/predictions"
else
url="$replicate_host/v1/predictions"
fi

# Replace any embedded "@filename" markers
json=$(replace_embed_markers "$json")
# Convert .input YAML → JSON and replace <filename> and ~filename.txt
payload=$(yq -o=json '.' "$yaml_file")
payload=$(replace_file_placeholders "$payload")
payload=$(replace_embed_markers "$payload")

# Pipe the evaluated JSON to curl which uses it (@-) as its data.
echo "$json" |
jq '{input: .}' |
echo "$payload" |
curl -sS -X POST \
-H "Authorization: Bearer $REPLICATE_API_TOKEN" \
-H "Content-Type: application/json" \
-H "Prefer: wait" \
-d @- \
"$replicate_host/v1/models/${model}/predictions"
"$url"
}
1 change: 1 addition & 0 deletions test/approvals/echo_2_repli_new
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ Templates in tmp/templates:
1. another
2. model1
3. model2
4. nested/upscaler

• info • repli_new_command → copying tmp/templates/model1.yaml → tmp/repli.yaml
1 change: 1 addition & 0 deletions test/approvals/repli_info_google_nano_banana
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ default_example:
input:
prompt: an image of a JSON file
output_format: jpg
is_official: true
latest_version:
openapi_schema:
components:
Expand Down
1 change: 1 addition & 0 deletions test/approvals/repli_info_google_nano_banana_json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"output_format": "jpg"
}
},
"is_official": true,
"latest_version": {
"openapi_schema": {
"components": {
Expand Down
1 change: 1 addition & 0 deletions test/approvals/repli_template_list
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Templates in tmp/templates:
1. another
2. model1
3. model2
4. nested/upscaler
1 change: 1 addition & 0 deletions test/approvals/repli_template_new_google_nano_banana
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
• debug • repli_template_new_command → fetching example for google/nano-banana
• debug • get_model_info → calling replicate API
• debug • get_example_from_replicate → model status: official
• info • repli_template_new_command → saving to tmp/templates/nano-banana.yaml
1 change: 1 addition & 0 deletions test/approvals/repli_template_new_google_nano_banana_force
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
• debug • repli_template_new_command → fetching example for google/nano-banana
• debug • get_model_info → calling replicate API
• debug • get_example_from_replicate → model status: official
• info • repli_template_new_command → saving to tmp/templates/nano-banana.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
• debug • repli_template_new_command → fetching example for google/nano-banana
• debug • get_model_info → calling replicate API
• debug • get_example_from_replicate → model status: official
• info • repli_template_new_command → saving to tmp/templates/banana.yaml
4 changes: 4 additions & 0 deletions test/approvals/repli_template_new_unofficial_mymodel
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
• debug • repli_template_new_command → fetching example for unofficial/mymodel
• debug • get_model_info → calling replicate API
• debug • get_example_from_replicate → model status: unofficial
• info • repli_template_new_command → saving to tmp/templates/mymodel.yaml
3 changes: 2 additions & 1 deletion test/helper.bash
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ reset_state() {
# add dummy templates to the templates dir
add_templates() {
blue_bold " adding dummy templates"
mkdir -p "$REPLI_TEMPLATES_DIR"
mkdir -p "$REPLI_TEMPLATES_DIR/nested"

cp fixtures/templates/basic.yaml "$REPLI_TEMPLATES_DIR/model1.yaml"
cp fixtures/templates/basic.yaml "$REPLI_TEMPLATES_DIR/model2.yaml"
cp fixtures/templates/basic.yaml "$REPLI_TEMPLATES_DIR/another.yaml"
cp fixtures/templates/basic.yaml "$REPLI_TEMPLATES_DIR/nested/upscaler.yaml"
}

add_repli_yaml() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"output_format": "jpg"
}
},
"is_official": true,
"latest_version": {
"openapi_schema": {
"components": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"id": "123",
"model": "google/nano-banana",
"model": "unofficial/mymodel",
"input":
{
"output_format": "jpg",
Expand Down
26 changes: 26 additions & 0 deletions test/mockserver/mocks/v1/models/unofficial/mymodel/get.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"default_example": {
"input": {
"prompt": "an image of a JSON file",
"output_format": "jpg"
}
},
"is_official": false,
"latest_version": {
"id": "660d922d33153019e8c263a3bba265de882e7f4f70396546b6c9c8f9d47a021a",
"openapi_schema": {
"components": {
"schemas": {
"Input": {
"type": "object",
"title": "Input",
"required": [
"prompt"
],
"properties": "... trimmed ..."
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"id": "123",
"model": "google/nano-banana",
"input":
{
"output_format": "jpg",
"prompt": "an image of a JSON file"
},
"output":
[
"http://localhost:3000/assets/out-0.jpg"
],
"error": null,
"status": "succeeded"
}
2 changes: 2 additions & 0 deletions test/spec/template/new.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ describe "templates new"
approve "cat $REPLI_TEMPLATES_DIR/banana.yaml" "cat_banana_yaml"
approve "ls $REPLI_TEMPLATES_DIR"

context "when adding unofficial models"
approve "repli template new unofficial/mymodel"