Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@
import com.hubspot.jinjava.doc.annotations.JinjavaSnippet;
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
import com.hubspot.jinjava.interpret.TemplateSyntaxException;
import com.hubspot.jinjava.objects.PyWrapper;
import com.hubspot.jinjava.util.ObjectTruthValue;
import java.util.Map;
import java.util.Objects;

@JinjavaDoc(
value = "If the value is undefined it will return the passed default value, otherwise the value of the variable",
Expand Down Expand Up @@ -87,9 +85,7 @@ public Object filter(
return object;
}

return (defaultValue instanceof PyWrapper) || (defaultValue == null)
? defaultValue
: Objects.toString(defaultValue);
return defaultValue;
}

@Override
Expand Down
54 changes: 54 additions & 0 deletions src/test/java/com/hubspot/jinjava/SparkChatTemplateTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.hubspot.jinjava;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;

public class SparkChatTemplateTest {

private static final String PROMPT = "What is the capital of New York, USA?";

@Test
public void matchesPythonSparkDefaultsAndReasoningEfforts() throws IOException {
String template;
try (
InputStream stream = getClass().getResourceAsStream("/spark_chat_template.jinja")
) {
if (stream == null) {
throw new IOException("Missing Spark chat template test resource");
}
template = new String(stream.readAllBytes(), StandardCharsets.UTF_8);
}

Map<String, Object> base = new HashMap<>();
base.put("messages", List.of(Map.of("role", "user", "content", PROMPT)));
base.put("add_generation_prompt", true);

String pythonHigh = "<|start▁of▁sentence|><|System|>\n"
+ "you are a helpful assistant.\n"
+ "`thinking_effort` guides on how much to think in your thinking channel "
+ "(not including the response channel), supported values include `low` and `high`.\n"
+ "Now the system is invoked with `thinking_effort=high`."
+ "<|end▁of▁sentence|><|start▁of▁sentence|><|User|>"
+ PROMPT
+ "<|end▁of▁sentence|><|start▁of▁sentence|><|Bot|><think>";
String pythonLow = pythonHigh.replace("thinking_effort=high", "thinking_effort=low");

Map<String, Object> high = new HashMap<>(base);
high.put("enable_thinking", true);
high.put("reasoning_effort", "high");
Map<String, Object> low = new HashMap<>(base);
low.put("enable_thinking", true);
low.put("reasoning_effort", "low");

assertThat(new Jinjava().render(template, high)).isEqualTo(pythonHigh);
assertThat(new Jinjava().render(template, low)).isEqualTo(pythonLow);
assertThat(new Jinjava().render(template, base)).isEqualTo(pythonHigh);
}
}
1 change: 1 addition & 0 deletions src/test/resources/spark_chat_template.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{%- if not messages %}{{- raise_exception('No messages provided.') }}{%- endif %}{%- set enable_thinking = enable_thinking | default(true) %}{%- set reasoning_instructions = '' %}{%- if enable_thinking is true %}{%- set resolved_reasoning_effort = reasoning_effort|default('high') %}{%- if resolved_reasoning_effort not in ('high', 'low') %}{{- raise_exception('Unexpected reasoning effort ' ~ reasoning_effort ~ '. Supported values are high (default) and low.') }}{%- endif %}{%- if resolved_reasoning_effort == 'high' %}{%- set reasoning_instructions = '\n`thinking_effort` guides on how much to think in your thinking channel (not including the response channel), supported values include `low` and `high`.\nNow the system is invoked with `thinking_effort=high`.' %}{%- elif resolved_reasoning_effort == 'low' %}{%- set reasoning_instructions = '\n`thinking_effort` guides on how much to think in your thinking channel (not including the response channel), supported values include `low` and `high`.\nNow the system is invoked with `thinking_effort=low`.' %}{%- endif %}{%- endif %}{%- set default_system = "you are a helpful assistant." + reasoning_instructions %}{{- '<|start▁of▁sentence|><|System|>' + '\n' + default_system }}{{- '<|end▁of▁sentence|>' }}{%- for message in messages %}{%- if message.role == "user" %}{{- '<|start▁of▁sentence|><|User|>' + message.content + '<|end▁of▁sentence|>' }}{%- endif %}{%- endfor %}{%- if add_generation_prompt %}{{- '<|start▁of▁sentence|><|Bot|>' }}{%- if enable_thinking is defined and enable_thinking %}{{- '<think>' }}{%- endif %}{%- if enable_thinking is defined and not enable_thinking %}{{- '</think>' }}{%- endif %}{%- endif -%}
Loading