Skip to content

Commit

Permalink
added extra checks to constructors (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
langchain4j committed Aug 29, 2023
1 parent bebfc78 commit 1e8c5a2
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.Arrays;
import java.util.List;

import static dev.langchain4j.internal.ValidationUtils.ensureNotNull;

/**
* Represents a dense vector embedding of a text.
* This class encapsulates a float array that captures the "meaning" or semantic information of the text.
Expand All @@ -16,7 +18,7 @@ public class Embedding {
private final float[] vector;

public Embedding(float[] vector) {
this.vector = vector;
this.vector = ensureNotNull(vector, "vector");
}

public float[] vector() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import static dev.langchain4j.data.message.SystemMessage.systemMessage;
import static dev.langchain4j.data.message.UserMessage.userMessage;
import static dev.langchain4j.internal.Utils.quoted;
import static dev.langchain4j.internal.ValidationUtils.ensureNotBlank;

/**
* Represents a prompt (an input text sent to the LLM).
Expand All @@ -21,7 +22,7 @@ public class Prompt {
private final String text;

public Prompt(String text) {
this.text = text;
this.text = ensureNotBlank(text, "text");
}

public String text() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import java.util.HashMap;
import java.util.Map;

import static dev.langchain4j.internal.Exceptions.illegalArgument;
import static dev.langchain4j.internal.Utils.isNullOrBlank;
import static dev.langchain4j.internal.ValidationUtils.ensureNotBlank;
import static dev.langchain4j.internal.ValidationUtils.ensureNotNull;
import static java.util.Collections.singletonMap;

/**
Expand All @@ -32,9 +32,14 @@ public class PromptTemplate {
private final Mustache mustache;
private final Clock clock;

private PromptTemplate(Mustache mustache, Clock clock) {
this.mustache = mustache;
this.clock = clock;
public PromptTemplate(String template) {
this(template, Clock.systemDefaultZone());
}

PromptTemplate(String template, Clock clock) {
StringReader stringReader = new StringReader(ensureNotBlank(template, "template"));
this.mustache = MUSTACHE_FACTORY.compile(stringReader, "template");
this.clock = ensureNotNull(clock, "clock");
}

/**
Expand Down Expand Up @@ -68,16 +73,6 @@ private Map<String, Object> injectDateTimeVariables(Map<String, Object> variable
}

public static PromptTemplate from(String template) {
return from(template, Clock.systemDefaultZone());
}

static PromptTemplate from(String template, Clock clock) {
if (isNullOrBlank(template)) {
throw illegalArgument("Prompt template cannot be null or empty");
}
return new PromptTemplate(
MUSTACHE_FACTORY.compile(new StringReader(template), "template"),
clock
);
return new PromptTemplate(template);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void should_provide_time_automatically() {

Clock clock = Clock.fixed(Instant.now(), ZoneOffset.UTC);

PromptTemplate promptTemplate = PromptTemplate.from("My name is {{it}} and now is {{current_time}}", clock);
PromptTemplate promptTemplate = new PromptTemplate("My name is {{it}} and now is {{current_time}}", clock);

Prompt prompt = promptTemplate.apply("Klaus");

Expand All @@ -63,7 +63,7 @@ void should_provide_date_and_time_automatically() {

Clock clock = Clock.fixed(Instant.now(), ZoneOffset.UTC);

PromptTemplate promptTemplate = PromptTemplate.from("My name is {{it}} and now is {{current_date_time}}", clock);
PromptTemplate promptTemplate = new PromptTemplate("My name is {{it}} and now is {{current_date_time}}", clock);

Prompt prompt = promptTemplate.apply("Klaus");

Expand Down

0 comments on commit 1e8c5a2

Please sign in to comment.