-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OPIK-855] Add type to prompt version (#1179)
- Loading branch information
1 parent
6f65206
commit 4f17d04
Showing
16 changed files
with
168 additions
and
31 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
27 changes: 27 additions & 0 deletions
27
apps/opik-backend/src/main/java/com/comet/opik/api/PromptType.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,27 @@ | ||
package com.comet.opik.api; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonValue; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.util.Arrays; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum PromptType { | ||
|
||
MUSTACHE("mustache"), | ||
JINJA2("jinja2"); | ||
|
||
@JsonValue | ||
private final String value; | ||
|
||
@JsonCreator | ||
public static PromptType fromString(String value) { | ||
return Arrays.stream(values()) | ||
.filter(promptType -> promptType.value.equals(value)) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalArgumentException("Unknown prompt type '%s'".formatted(value))); | ||
} | ||
} |
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
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
16 changes: 16 additions & 0 deletions
16
apps/opik-backend/src/main/java/com/comet/opik/domain/template/Jinja2Parser.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,16 @@ | ||
package com.comet.opik.domain.template; | ||
|
||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
public class Jinja2Parser implements TemplateParser { | ||
@Override | ||
public Set<String> extractVariables(String template) { | ||
return Set.of(); | ||
} | ||
|
||
@Override | ||
public String render(String template, Map<String, ?> context) { | ||
throw new UnsupportedOperationException("Jinja2 template rendering is not supported"); | ||
} | ||
} |
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
11 changes: 11 additions & 0 deletions
11
apps/opik-backend/src/main/java/com/comet/opik/domain/template/TemplateParser.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,11 @@ | ||
package com.comet.opik.domain.template; | ||
|
||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
public interface TemplateParser { | ||
|
||
Set<String> extractVariables(String template); | ||
|
||
String render(String template, Map<String, ?> context); | ||
} |
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
30 changes: 30 additions & 0 deletions
30
apps/opik-backend/src/main/java/com/comet/opik/utils/TemplateParseUtils.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,30 @@ | ||
package com.comet.opik.utils; | ||
|
||
import com.comet.opik.api.PromptType; | ||
import com.comet.opik.domain.template.Jinja2Parser; | ||
import com.comet.opik.domain.template.MustacheParser; | ||
import com.comet.opik.domain.template.TemplateParser; | ||
import lombok.NonNull; | ||
import lombok.experimental.UtilityClass; | ||
|
||
import java.util.EnumMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
@UtilityClass | ||
public class TemplateParseUtils { | ||
private static final Map<PromptType, TemplateParser> parsers = new EnumMap<>(PromptType.class); | ||
|
||
static { | ||
parsers.put(PromptType.MUSTACHE, new MustacheParser()); | ||
parsers.put(PromptType.JINJA2, new Jinja2Parser()); | ||
} | ||
|
||
public static Set<String> extractVariables(@NonNull String template, @NonNull PromptType type) { | ||
return parsers.get(type).extractVariables(template); | ||
} | ||
|
||
public static String render(@NonNull String template, @NonNull Map<String, ?> context, @NonNull PromptType type) { | ||
return parsers.get(type).render(template, context); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...rc/main/resources/liquibase/db-app-state/migrations/000010_add_type_to_prompt_version.sql
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,6 @@ | ||
--liquibase formatted sql | ||
--changeset BorisTkachenko:add_type_to_prompt_version | ||
|
||
ALTER TABLE prompt_versions ADD COLUMN type ENUM('mustache', 'jinja2') NOT NULL DEFAULT 'mustache'; | ||
|
||
--rollback ALTER TABLE prompt_versions DROP COLUMN type; |
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
Oops, something went wrong.