proj_name stringclasses 131 values | relative_path stringlengths 30 228 | class_name stringlengths 1 68 | func_name stringlengths 1 48 | masked_class stringlengths 78 9.82k | func_body stringlengths 46 9.61k | len_input int64 29 2.01k | len_output int64 14 1.94k | total int64 55 2.05k | relevant_context stringlengths 0 38.4k |
|---|---|---|---|---|---|---|---|---|---|
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/data/message/GsonChatMessageAdapter.java | GsonChatMessageAdapter | deserialize | class GsonChatMessageAdapter implements JsonDeserializer<ChatMessage>, JsonSerializer<ChatMessage> {
private static final Gson GSON = new GsonBuilder()
.registerTypeAdapter(Content.class, new GsonContentAdapter())
.registerTypeAdapter(TextContent.class, new GsonContentAdapter())
.registerTypeAdapter(ImageContent.class, new GsonContentAdapter())
.create();
private static final String CHAT_MESSAGE_TYPE = "type"; // do not change, will break backward compatibility!
@Override
public JsonElement serialize(ChatMessage chatMessage, Type ignored, JsonSerializationContext context) {
JsonObject messageJsonObject = GSON.toJsonTree(chatMessage).getAsJsonObject();
messageJsonObject.addProperty(CHAT_MESSAGE_TYPE, chatMessage.type().toString());
return messageJsonObject;
}
@Override
public ChatMessage deserialize(JsonElement messageJsonElement, Type ignored, JsonDeserializationContext context) {<FILL_FUNCTION_BODY>}
} |
String chatMessageTypeString = messageJsonElement.getAsJsonObject().get(CHAT_MESSAGE_TYPE).getAsString();
ChatMessageType chatMessageType = ChatMessageType.valueOf(chatMessageTypeString);
ChatMessage chatMessage = GSON.fromJson(messageJsonElement, chatMessageType.messageClass());
if (chatMessage instanceof UserMessage && ((UserMessage) chatMessage).contents() == null) {
// keeping backward compatibility with old schema TODO remove after a few releases
chatMessage = UserMessage.from(messageJsonElement.getAsJsonObject().get("text").getAsString());
}
return chatMessage;
| 258 | 154 | 412 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/data/message/GsonChatMessageJsonCodec.java | GsonChatMessageJsonCodec | messagesFromJson | class GsonChatMessageJsonCodec implements ChatMessageJsonCodec {
/**
* The {@link Gson} instance used for serialization and deserialization.
*/
static final Gson GSON = new GsonBuilder()
.registerTypeAdapter(ChatMessage.class, new GsonChatMessageAdapter())
.registerTypeAdapter(SystemMessage.class, new GsonChatMessageAdapter())
.registerTypeAdapter(UserMessage.class, new GsonChatMessageAdapter())
.registerTypeAdapter(AiMessage.class, new GsonChatMessageAdapter())
.registerTypeAdapter(ToolExecutionResultMessage.class, new GsonChatMessageAdapter())
.create();
/**
* A {@link Type} object representing a list of {@link ChatMessage} objects.
*/
private static final Type MESSAGE_LIST_TYPE = (new TypeToken<List<ChatMessage>>() {}).getType();
/**
* Constructs a new {@link GsonChatMessageJsonCodec}.
*/
public GsonChatMessageJsonCodec() {}
@Override
public ChatMessage messageFromJson(String json) {
return GSON.fromJson(json, ChatMessage.class);
}
@Override
public List<ChatMessage> messagesFromJson(String json) {<FILL_FUNCTION_BODY>}
@Override
public String messageToJson(ChatMessage message) {
return GSON.toJson(message);
}
@Override
public String messagesToJson(List<ChatMessage> messages) {
return GSON.toJson(messages);
}
} |
List<ChatMessage> messages = GSON.fromJson(json, MESSAGE_LIST_TYPE);
return messages == null ? emptyList() : messages;
| 391 | 41 | 432 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/data/message/GsonContentAdapter.java | GsonContentAdapter | deserialize | class GsonContentAdapter implements JsonDeserializer<Content>, JsonSerializer<Content> {
private static final Gson GSON = new Gson();
private static final String CONTENT_TYPE = "type"; // do not change, will break backward compatibility!
@Override
public JsonElement serialize(Content content, Type ignored, JsonSerializationContext context) {
JsonObject contentJsonObject = GSON.toJsonTree(content).getAsJsonObject();
contentJsonObject.addProperty(CONTENT_TYPE, content.type().toString());
return contentJsonObject;
}
@Override
public Content deserialize(JsonElement contentJsonElement, Type ignored, JsonDeserializationContext context) {<FILL_FUNCTION_BODY>}
} |
String contentTypeString = contentJsonElement.getAsJsonObject().get(CONTENT_TYPE).getAsString();
ContentType contentType = ContentType.valueOf(contentTypeString);
return GSON.fromJson(contentJsonElement, contentType.getContentClass());
| 183 | 67 | 250 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/data/message/SystemMessage.java | SystemMessage | equals | class SystemMessage implements ChatMessage {
private final String text;
/**
* Creates a new system message.
* @param text the message text.
*/
public SystemMessage(String text) {
this.text = ensureNotBlank(text, "text");
}
/**
* Returns the message text.
* @return the message text.
*/
public String text() {
return text;
}
@Override
public ChatMessageType type() {
return SYSTEM;
}
@Override
public boolean equals(Object o) {<FILL_FUNCTION_BODY>}
@Override
public int hashCode() {
return Objects.hash(text);
}
@Override
public String toString() {
return "SystemMessage {" +
" text = " + quoted(text) +
" }";
}
/**
* Creates a new system message.
* @param text the message text.
* @return the system message.
*/
public static SystemMessage from(String text) {
return new SystemMessage(text);
}
/**
* Creates a new system message.
* @param text the message text.
* @return the system message.
*/
public static SystemMessage systemMessage(String text) {
return from(text);
}
} |
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SystemMessage that = (SystemMessage) o;
return Objects.equals(this.text, that.text);
| 349 | 63 | 412 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/data/message/TextContent.java | TextContent | equals | class TextContent implements Content {
private final String text;
/**
* Creates a new text content.
* @param text the text.
*/
public TextContent(String text) {
this.text = ensureNotBlank(text, "text");
}
/**
* Returns the text.
* @return the text.
*/
public String text() {
return text;
}
@Override
public ContentType type() {
return TEXT;
}
@Override
public boolean equals(Object o) {<FILL_FUNCTION_BODY>}
@Override
public int hashCode() {
return Objects.hash(text);
}
@Override
public String toString() {
return "TextContent {" +
" text = " + quoted(text) +
" }";
}
/**
* Creates a new text content.
* @param text the text.
* @return the text content.
*/
public static TextContent from(String text) {
return new TextContent(text);
}
} |
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TextContent that = (TextContent) o;
return Objects.equals(this.text, that.text);
| 284 | 63 | 347 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/data/message/ToolExecutionResultMessage.java | ToolExecutionResultMessage | equals | class ToolExecutionResultMessage implements ChatMessage {
private final String id;
private final String toolName;
private final String text;
/**
* Creates a {@link ToolExecutionResultMessage}.
* @param id the id of the tool.
* @param toolName the name of the tool.
* @param text the result of the tool execution.
*/
public ToolExecutionResultMessage(String id, String toolName, String text) {
this.id = id;
this.toolName = toolName;
this.text = ensureNotBlank(text, "text");
}
/**
* Returns the id of the tool.
* @return the id of the tool.
*/
public String id() {
return id;
}
/**
* Returns the name of the tool.
* @return the name of the tool.
*/
public String toolName() {
return toolName;
}
/**
* Returns the result of the tool execution.
* @return the result of the tool execution.
*/
public String text() {
return text;
}
@Override
public ChatMessageType type() {
return TOOL_EXECUTION_RESULT;
}
@Override
public boolean equals(Object o) {<FILL_FUNCTION_BODY>}
@Override
public int hashCode() {
return Objects.hash(id, toolName, text);
}
@Override
public String toString() {
return "ToolExecutionResultMessage {" +
" id = " + quoted(id) +
" toolName = " + quoted(toolName) +
" text = " + quoted(text) +
" }";
}
/**
* Creates a {@link ToolExecutionResultMessage} from a {@link ToolExecutionRequest} and the result of the tool execution.
* @param request the request.
* @param toolExecutionResult the result of the tool execution.
* @return the {@link ToolExecutionResultMessage}.
*/
public static ToolExecutionResultMessage from(ToolExecutionRequest request, String toolExecutionResult) {
return new ToolExecutionResultMessage(request.id(), request.name(), toolExecutionResult);
}
/**
* Creates a {@link ToolExecutionResultMessage} from a {@link ToolExecutionRequest} and the result of the tool execution.
* @param id the id of the tool.
* @param toolName the name of the tool.
* @param toolExecutionResult the result of the tool execution.
* @return the {@link ToolExecutionResultMessage}.
*/
public static ToolExecutionResultMessage from(String id, String toolName, String toolExecutionResult) {
return new ToolExecutionResultMessage(id, toolName, toolExecutionResult);
}
/**
* Creates a {@link ToolExecutionResultMessage} from a {@link ToolExecutionRequest} and the result of the tool execution.
* @param request the request.
* @param toolExecutionResult the result of the tool execution.
* @return the {@link ToolExecutionResultMessage}.
*/
public static ToolExecutionResultMessage toolExecutionResultMessage(ToolExecutionRequest request, String toolExecutionResult) {
return from(request, toolExecutionResult);
}
/**
* Creates a {@link ToolExecutionResultMessage} from a {@link ToolExecutionRequest} and the result of the tool execution.
* @param id the id of the tool.
* @param toolName the name of the tool.
* @param toolExecutionResult the result of the tool execution.
* @return the {@link ToolExecutionResultMessage}.
*/
public static ToolExecutionResultMessage toolExecutionResultMessage(String id, String toolName, String toolExecutionResult) {
return from(id, toolName, toolExecutionResult);
}
} |
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ToolExecutionResultMessage that = (ToolExecutionResultMessage) o;
return Objects.equals(this.id, that.id)
&& Objects.equals(this.toolName, that.toolName)
&& Objects.equals(this.text, that.text);
| 933 | 101 | 1,034 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/data/segment/TextSegment.java | TextSegment | equals | class TextSegment {
private final String text;
private final Metadata metadata;
/**
* Creates a new text segment.
*
* @param text the text.
* @param metadata the metadata.
*/
public TextSegment(String text, Metadata metadata) {
this.text = ensureNotBlank(text, "text");
this.metadata = ensureNotNull(metadata, "metadata");
}
/**
* Returns the text.
*
* @return the text.
*/
public String text() {
return text;
}
/**
* Returns the metadata.
*
* @return the metadata.
*/
public Metadata metadata() {
return metadata;
}
/**
* Returns the metadata value for the given key.
*
* @param key the key.
* @return the metadata value, or null if not found.
*/
// TODO deprecate once the new experimental API is settled
public String metadata(String key) {
return metadata.get(key);
}
@Override
public boolean equals(Object o) {<FILL_FUNCTION_BODY>}
@Override
public int hashCode() {
return Objects.hash(text, metadata);
}
@Override
public String toString() {
return "TextSegment {" +
" text = " + quoted(text) +
" metadata = " + metadata.asMap() +
" }";
}
/**
* Creates a new text segment.
*
* @param text the text.
* @return the text segment.
*/
public static TextSegment from(String text) {
return new TextSegment(text, new Metadata());
}
/**
* Creates a new text segment.
*
* @param text the text.
* @param metadata the metadata.
* @return the text segment.
*/
public static TextSegment from(String text, Metadata metadata) {
return new TextSegment(text, metadata);
}
/**
* Creates a new text segment.
*
* @param text the text.
* @return the text segment.
*/
public static TextSegment textSegment(String text) {
return from(text);
}
/**
* Creates a new text segment.
*
* @param text the text.
* @param metadata the metadata.
* @return the text segment.
*/
public static TextSegment textSegment(String text, Metadata metadata) {
return from(text, metadata);
}
} |
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TextSegment that = (TextSegment) o;
return Objects.equals(this.text, that.text)
&& Objects.equals(this.metadata, that.metadata);
| 663 | 79 | 742 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/internal/GsonJsonCodec.java | GsonJsonCodec | toInputStream | class GsonJsonCodec implements Json.JsonCodec {
private static final Gson GSON = new GsonBuilder()
.setPrettyPrinting()
.registerTypeAdapter(
LocalDate.class,
(JsonSerializer<LocalDate>) (localDate, type, context) ->
new JsonPrimitive(localDate.format(ISO_LOCAL_DATE))
)
.registerTypeAdapter(
LocalDate.class,
(JsonDeserializer<LocalDate>) (json, type, context) ->
LocalDate.parse(json.getAsString(), ISO_LOCAL_DATE)
)
.registerTypeAdapter(
LocalDateTime.class,
(JsonSerializer<LocalDateTime>) (localDateTime, type, context) ->
new JsonPrimitive(localDateTime.format(ISO_LOCAL_DATE_TIME))
)
.registerTypeAdapter(
LocalDateTime.class,
(JsonDeserializer<LocalDateTime>) (json, type, context) ->
LocalDateTime.parse(json.getAsString(), ISO_LOCAL_DATE_TIME)
)
.create();
@Override
public String toJson(Object o) {
return GSON.toJson(o);
}
/**
* Reads a JSON string and returns an object of the specified type.
*
* <p>As a special case, if the type is {@link Map}, the returned object is
* parsed as a {@code Map<String, String>}.
*
* @param json JSON string to be parsed.
* @param type Type of the object to be returned.
* @return the object represented by the JSON string.
* @param <T> Type of the object to be returned.
*/
@Override
public <T> T fromJson(String json, Class<T> type) {
return GSON.fromJson(json, type);
}
@Override
public InputStream toInputStream(Object o, Class<?> type) throws IOException {<FILL_FUNCTION_BODY>}
} |
try (
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
byteArrayOutputStream, StandardCharsets.UTF_8);
JsonWriter jsonWriter = new JsonWriter(outputStreamWriter)
) {
GSON.toJson(o, type, jsonWriter);
jsonWriter.flush();
return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
}
| 508 | 110 | 618 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/internal/Json.java | Json | loadCodec | class Json {
private Json() {
}
/**
* The abstract JSON codec interface.
*/
public interface JsonCodec {
/**
* Convert the given object to JSON.
*
* @param o the object to convert.
* @return the JSON string.
*/
String toJson(Object o);
/**
* Convert the given JSON string to an object of the given type.
*
* @param json the JSON string.
* @param type the type of the object.
* @param <T> the type of the object.
* @return the object.
*/
<T> T fromJson(String json, Class<T> type);
/**
* Convert the given object to an {@link InputStream}.
*
* @param o the object to convert.
* @param type the type of the object.
* @return the {@link InputStream}.
* @throws IOException if an I/O error occurs.
*/
InputStream toInputStream(Object o, Class<?> type) throws IOException;
}
private static final JsonCodec CODEC = loadCodec();
private static JsonCodec loadCodec() {<FILL_FUNCTION_BODY>}
/**
* Convert the given object to JSON.
*
* @param o the object to convert.
* @return the JSON string.
*/
public static String toJson(Object o) {
return CODEC.toJson(o);
}
/**
* Convert the given JSON string to an object of the given type.
*
* @param json the JSON string.
* @param type the type of the object.
* @param <T> the type of the object.
* @return the object.
*/
public static <T> T fromJson(String json, Class<T> type) {
return CODEC.fromJson(json, type);
}
/**
* Convert the given object to an {@link InputStream}.
*
* @param o the object to convert.
* @param type the type of the object.
* @return the {@link InputStream}.
* @throws IOException if an I/O error occurs.
*/
public static InputStream toInputStream(Object o, Class<?> type) throws IOException {
return CODEC.toInputStream(o, type);
}
} |
for (JsonCodecFactory factory : loadFactories(JsonCodecFactory.class)) {
return factory.create();
}
return new GsonJsonCodec();
| 599 | 45 | 644 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/internal/RetryUtils.java | Builder | withRetry | class Builder {
private int maxAttempts = 3;
private int delayMillis = 1000;
private double jitterScale = 0.2;
private double backoffExp = 1.5;
/**
* Construct a RetryPolicy.Builder.
*/
public Builder() {}
/**
* Sets the default maximum number of attempts.
* @param maxAttempts The maximum number of attempts.
* @return {@code this}
*/
public Builder maxAttempts(int maxAttempts) {
this.maxAttempts = maxAttempts;
return this;
}
/**
* Sets the base delay in milliseconds.
*
* <p>The delay is calculated as follows:
* <ol>
* <li>Calculate the raw delay in milliseconds as
* {@code delayMillis * Math.pow(backoffExp, attempt - 1)}.</li>
* <li>Calculate the jitter delay in milliseconds as
* {@code rawDelayMs + rand.nextInt((int) (rawDelayMs * jitterScale))}.</li>
* <li>Sleep for the jitter delay in milliseconds.</li>
* </ol>
*
* @param delayMillis The delay in milliseconds.
* @return {@code this}
*/
public Builder delayMillis(int delayMillis) {
this.delayMillis = delayMillis;
return this;
}
/**
* Sets the jitter scale.
*
* <p>The jitter delay in milliseconds is calculated as
* {@code rawDelayMs + rand.nextInt((int) (rawDelayMs * jitterScale))}.
*
* @param jitterScale The jitter scale.
* @return {@code this}
*/
public Builder jitterScale(double jitterScale) {
this.jitterScale = jitterScale;
return this;
}
/**
* Sets the backoff exponent.
* @param backoffExp The backoff exponent.
* @return {@code this}
*/
public Builder backoffExp(double backoffExp) {
this.backoffExp = backoffExp;
return this;
}
/**
* Builds a RetryPolicy.
* @return A RetryPolicy.
*/
public RetryPolicy build() {
return new RetryPolicy(maxAttempts, delayMillis, jitterScale, backoffExp);
}
}
private final int maxAttempts;
private final int delayMillis;
private final double jitterScale;
private final double backoffExp;
/**
* Construct a RetryPolicy.
* @param maxAttempts The maximum number of attempts.
* @param delayMillis The delay in milliseconds.
* @param jitterScale The jitter scale.
* @param backoffExp The backoff exponent.
*/
public RetryPolicy(
int maxAttempts,
int delayMillis,
double jitterScale,
double backoffExp) {
this.maxAttempts = maxAttempts;
this.delayMillis = delayMillis;
this.jitterScale = jitterScale;
this.backoffExp = backoffExp;
}
/**
* This method returns the raw delay in milliseconds for a given attempt.
* @param attempt The attempt number.
* @return The raw delay in milliseconds.
*/
public double rawDelayMs(int attempt) {
return ((double) delayMillis) * Math.pow(backoffExp, attempt - 1);
}
/**
* This method returns the jitter delay in milliseconds for a given attempt.
* @param attempt The attempt number.
* @return The jitter delay in milliseconds.
*/
public int jitterDelayMillis(int attempt) {
Random rand = new Random();
double delay = rawDelayMs(attempt);
double jitter = delay * jitterScale;
return (int) (delay + rand.nextInt((int) jitter));
}
/**
* This method sleeps for a given attempt.
* @param attempt The attempt number.
*/
@JacocoIgnoreCoverageGenerated
public void sleep(int attempt) {
try {
Thread.sleep(jitterDelayMillis(attempt));
} catch (InterruptedException ignored) {
// pass
}
}
/**
* This method attempts to execute a given action up to a specified number of times with a 1-second delay.
* If the action fails on all attempts, it throws a RuntimeException.
*
* @param action The action to be executed.
* @param <T> The type of the result of the action.
* @return The result of the action if it is successful.
* @throws RuntimeException if the action fails on all attempts.
*/
public <T> T withRetry(Callable<T> action) {
return withRetry(action, maxAttempts);
}
/**
* This method attempts to execute a given action up to a specified number of times with a 1-second delay.
* If the action fails on all attempts, it throws a RuntimeException.
*
* @param action The action to be executed.
* @param maxAttempts The maximum number of attempts to execute the action.
* @param <T> The type of the result of the action.
* @return The result of the action if it is successful.
* @throws RuntimeException if the action fails on all attempts.
*/
public <T> T withRetry(Callable<T> action, int maxAttempts) {<FILL_FUNCTION_BODY> |
int attempt = 1;
while (true) {
try {
return action.call();
} catch (Exception e) {
if (attempt == maxAttempts) {
throw new RuntimeException(e);
}
log.warn(format("Exception was thrown on attempt %s of %s", attempt, maxAttempts), e);
sleep(attempt);
}
attempt++;
}
| 1,453 | 110 | 1,563 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/internal/ValidationUtils.java | ValidationUtils | ensureNotEmpty | class ValidationUtils {
private ValidationUtils() {}
/**
* Ensure that the two values are equal.
* @param lhs the left hand side value.
* @param rhs the right hand side value.
* @param format the format string for the exception message.
* @param args the format arguments for the exception message.
*/
public static void ensureEq(Object lhs, Object rhs, String format, Object... args) {
if (!Objects.equals(lhs, rhs)) {
throw illegalArgument(format, args);
}
}
/**
* Ensures that the given object is not null.
* @param object The object to check.
* @param name The name of the object to be used in the exception message.
* @return The object if it is not null.
* @param <T> The type of the object.
* @throws IllegalArgumentException if the object is null.
*/
public static <T> T ensureNotNull(T object, String name) {
return ensureNotNull(object, "%s cannot be null", name);
}
/**
* Ensures that the given object is not null.
* @param object The object to check.
* @param format The format of the exception message.
* @param args The arguments for the exception message.
* @return The object if it is not null.
* @param <T> The type of the object.
*/
public static <T> T ensureNotNull(T object, String format, Object... args) {
if (object == null) {
throw illegalArgument(format, args);
}
return object;
}
/**
* Ensures that the given collection is not null and not empty.
* @param collection The collection to check.
* @param name The name of the collection to be used in the exception message.
* @return The collection if it is not null and not empty.
* @param <T> The type of the collection.
* @throws IllegalArgumentException if the collection is null or empty.
*/
public static <T extends Collection<?>> T ensureNotEmpty(T collection, String name) {<FILL_FUNCTION_BODY>}
/**
* Ensures that the given map is not null and not empty.
*
* @param map The map to check.
* @param name The name of the map to be used in the exception message.
* @param <K> The type of the key.
* @param <V> The type of the value.
* @return The map if it is not null and not empty.
* @throws IllegalArgumentException if the collection is null or empty.
*/
public static <K, V> Map<K, V> ensureNotEmpty(Map<K, V> map, String name) {
if (map == null || map.isEmpty()) {
throw illegalArgument("%s cannot be null or empty", name);
}
return map;
}
/**
* Ensures that the given string is not null and not blank.
* @param string The string to check.
* @param name The name of the string to be used in the exception message.
* @return The string if it is not null and not blank.
* @throws IllegalArgumentException if the string is null or blank.
*/
public static String ensureNotBlank(String string, String name) {
if (string == null || string.trim().isEmpty()) {
throw illegalArgument("%s cannot be null or blank", name);
}
return string;
}
/**
* Ensures that the given expression is true.
* @param expression The expression to check.
* @param msg The message to be used in the exception.
* @throws IllegalArgumentException if the expression is false.
*/
public static void ensureTrue(boolean expression, String msg) {
if (!expression) {
throw illegalArgument(msg);
}
}
/**
* Ensures that the given expression is true.
* @param i The expression to check.
* @param name The message to be used in the exception.
* @return The value if it is greater than zero.
* @throws IllegalArgumentException if the expression is false.
*/
public static int ensureGreaterThanZero(Integer i, String name) {
if (i == null || i <= 0) {
throw illegalArgument("%s must be greater than zero, but is: %s", name, i);
}
return i;
}
/**
* Ensures that the given Double value is in {@code [min, max]}.
* @param d The value to check.
* @param min The minimum value.
* @param max The maximum value.
* @param name The value name to be used in the exception.
* @return The value if it is in {@code [min, max]}.
* @throws IllegalArgumentException if the value is not in {@code [min, max]}.
*/
public static double ensureBetween(Double d, double min, double max, String name) {
if (d == null || d < min || d > max) {
throw illegalArgument("%s must be between %s and %s, but is: %s", name, min, max, d);
}
return d;
}
/**
* Ensures that the given Integer value is in {@code [min, max]}.
* @param i The value to check.
* @param min The minimum value.
* @param max The maximum value.
* @param name The value name to be used in the exception.
* @return The value if it is in {@code [min, max]}.
* @throws IllegalArgumentException if the value is not in {@code [min, max]}.
*/
public static int ensureBetween(Integer i, int min, int max, String name) {
if (i == null || i < min || i > max) {
throw illegalArgument("%s must be between %s and %s, but is: %s", name, min, max, i);
}
return i;
}
/**
* Ensures that the given Long value is in {@code [min, max]}.
* @param i The value to check.
* @param min The minimum value.
* @param max The maximum value.
* @param name The value name to be used in the exception.
* @return The value if it is in {@code [min, max]}.
* @throws IllegalArgumentException if the value is not in {@code [min, max]}.
*/
public static long ensureBetween(Long i, long min, long max, String name) {
if (i == null || i < min || i > max) {
throw illegalArgument("%s must be between %s and %s, but is: %s", name, min, max, i);
}
return i;
}
} |
if (collection == null || collection.isEmpty()) {
throw illegalArgument("%s cannot be null or empty", name);
}
return collection;
| 1,695 | 40 | 1,735 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/model/input/DefaultPromptTemplateFactory.java | DefaultTemplate | replaceAll | class DefaultTemplate implements Template {
private static final Pattern VARIABLE_PATTERN = Pattern.compile("\\{\\{(.+?)}}");
private final String template;
private final Set<String> allVariables;
public DefaultTemplate(String template) {
this.template = ensureNotBlank(template, "template");
this.allVariables = extractVariables(template);
}
private static Set<String> extractVariables(String template) {
Set<String> variables = new HashSet<>();
Matcher matcher = VARIABLE_PATTERN.matcher(template);
while (matcher.find()) {
variables.add(matcher.group(1));
}
return variables;
}
public String render(Map<String, Object> variables) {
ensureAllVariablesProvided(variables);
String result = template;
for (Map.Entry<String, Object> entry : variables.entrySet()) {
result = replaceAll(result, entry.getKey(), entry.getValue());
}
return result;
}
private void ensureAllVariablesProvided(Map<String, Object> providedVariables) {
for (String variable : allVariables) {
if (!providedVariables.containsKey(variable)) {
throw illegalArgument("Value for the variable '%s' is missing", variable);
}
}
}
private static String replaceAll(String template, String variable, Object value) {<FILL_FUNCTION_BODY>}
private static String inDoubleCurlyBrackets(String variable) {
return "{{" + variable + "}}";
}
} |
if (value == null || value.toString() == null) {
throw illegalArgument("Value for the variable '%s' is null", variable);
}
return template.replace(inDoubleCurlyBrackets(variable), value.toString());
| 414 | 60 | 474 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/model/input/Prompt.java | Prompt | equals | class Prompt {
private final String text;
/**
* Create a new Prompt.
* @param text the text of the prompt.
*/
public Prompt(String text) {
this.text = ensureNotBlank(text, "text");
}
/**
* The text of the prompt.
* @return the text of the prompt.
*/
public String text() {
return text;
}
/**
* Convert this prompt to a SystemMessage.
* @return the SystemMessage.
*/
public SystemMessage toSystemMessage() {
return systemMessage(text);
}
/**
* Convert this prompt to a UserMessage.
* @return the UserMessage.
*/
public UserMessage toUserMessage() {
return userMessage(text);
}
/**
* Convert this prompt to an AiMessage.
* @return the AiMessage.
*/
public AiMessage toAiMessage() {
return aiMessage(text);
}
@Override
public boolean equals(Object o) {<FILL_FUNCTION_BODY>}
@Override
public int hashCode() {
return Objects.hash(text);
}
@Override
public String toString() {
return "Prompt {" +
" text = " + quoted(text) +
" }";
}
/**
* Create a new Prompt.
* @param text the text of the prompt.
* @return the new Prompt.
*/
public static Prompt from(String text) {
return new Prompt(text);
}
} |
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Prompt that = (Prompt) o;
return Objects.equals(this.text, that.text);
| 419 | 63 | 482 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/model/input/PromptTemplate.java | PromptTemplate | factory | class PromptTemplate {
private static final PromptTemplateFactory FACTORY = factory();
private static PromptTemplateFactory factory() {<FILL_FUNCTION_BODY>}
static final String CURRENT_DATE = "current_date";
static final String CURRENT_TIME = "current_time";
static final String CURRENT_DATE_TIME = "current_date_time";
private final String templateString;
private final PromptTemplateFactory.Template template;
private final Clock clock;
/**
* Create a new PromptTemplate.
*
* <p>The {@code Clock} will be the system clock.</p>
*
* @param template the template string of the prompt.
*/
public PromptTemplate(String template) {
this(template, Clock.systemDefaultZone());
}
/**
* Create a new PromptTemplate.
*
* @param template the template string of the prompt.
* @param clock the clock to use for the special variables.
*/
PromptTemplate(String template, Clock clock) {
this.templateString = ensureNotBlank(template, "template");
this.template = FACTORY.create(new PromptTemplateFactory.Input() {
@Override
public String getTemplate() {
return template;
}
@Override
public String getName() {
return "template";
}
});
this.clock = ensureNotNull(clock, "clock");
}
/**
* @return A prompt template string.
*/
public String template() {
return templateString;
}
/**
* Applies a value to a template containing a single variable. The single variable should have the name {{it}}.
*
* @param value The value that will be injected in place of the {{it}} placeholder in the template.
* @return A Prompt object where the {{it}} placeholder in the template has been replaced by the provided value.
*/
public Prompt apply(Object value) {
return apply(singletonMap("it", value));
}
/**
* Applies multiple values to a template containing multiple variables.
*
* @param variables A map of variable names to values that will be injected in place of the corresponding placeholders in the template.
* @return A Prompt object where the placeholders in the template have been replaced by the provided values.
*/
public Prompt apply(Map<String, Object> variables) {
ensureNotNull(variables, "variables");
return Prompt.from(template.render(injectDateTimeVariables(variables)));
}
/**
* Injects the special variables {{current_date}}, {{current_time}}, and {{current_date_time}} into the given map.
*
* @param variables the map to inject the variables into.
* @return a copy of the map with the variables injected.
*/
private Map<String, Object> injectDateTimeVariables(Map<String, Object> variables) {
Map<String, Object> variablesCopy = new HashMap<>(variables);
variablesCopy.put(CURRENT_DATE, LocalDate.now(clock));
variablesCopy.put(CURRENT_TIME, LocalTime.now(clock));
variablesCopy.put(CURRENT_DATE_TIME, LocalDateTime.now(clock));
return variablesCopy;
}
/**
* Create a new PromptTemplate.
*
* @param template the template string of the prompt.
* @return the PromptTemplate.
*/
public static PromptTemplate from(String template) {
return new PromptTemplate(template);
}
} |
for (PromptTemplateFactory factory : loadFactories(PromptTemplateFactory.class)) {
return factory;
}
return new DefaultPromptTemplateFactory();
| 905 | 43 | 948 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/model/input/structured/DefaultStructuredPromptFactory.java | DefaultStructuredPromptFactory | toPrompt | class DefaultStructuredPromptFactory implements StructuredPromptFactory {
private static final Gson GSON = new GsonBuilder().setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE).create();
/**
* Create a default structured prompt factory.
*/
public DefaultStructuredPromptFactory() {}
@Override
public Prompt toPrompt(Object structuredPrompt) {<FILL_FUNCTION_BODY>}
/**
* Extracts the variables from the structured prompt.
* @param structuredPrompt The structured prompt.
* @return The variables map.
*/
private static Map<String, Object> extractVariables(Object structuredPrompt) {
String json = GSON.toJson(structuredPrompt);
TypeToken<Map<String, Object>> mapType = new TypeToken<Map<String, Object>>() {};
return GSON.fromJson(json, mapType);
}
} |
StructuredPrompt annotation = StructuredPrompt.Util.validateStructuredPrompt(structuredPrompt);
String promptTemplateString = StructuredPrompt.Util.join(annotation);
PromptTemplate promptTemplate = PromptTemplate.from(promptTemplateString);
Map<String, Object> variables = extractVariables(structuredPrompt);
return promptTemplate.apply(variables);
| 233 | 99 | 332 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/model/input/structured/StructuredPromptProcessor.java | StructuredPromptProcessor | factory | class StructuredPromptProcessor {
private StructuredPromptProcessor() {
}
private static final StructuredPromptFactory FACTORY = factory();
private static StructuredPromptFactory factory() {<FILL_FUNCTION_BODY>}
/**
* Converts the given structured prompt to a prompt.
*
* @param structuredPrompt the structured prompt.
* @return the prompt.
*/
public static Prompt toPrompt(Object structuredPrompt) {
return FACTORY.toPrompt(structuredPrompt);
}
} |
for (StructuredPromptFactory factory : loadFactories(StructuredPromptFactory.class)) {
return factory;
}
return new DefaultStructuredPromptFactory();
| 144 | 46 | 190 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/model/moderation/Moderation.java | Moderation | equals | class Moderation {
private final boolean flagged;
private final String flaggedText;
/**
* Construct a Moderation object that is not flagged.
*/
public Moderation() {
this.flagged = false;
this.flaggedText = null;
}
/**
* Construct a Moderation object that is flagged.
*
* @param flaggedText the text that was flagged.
*/
public Moderation(String flaggedText) {
this.flagged = true;
this.flaggedText = flaggedText;
}
/**
* Returns true if the text was flagged.
* @return true if the text was flagged, false otherwise.
*/
public boolean flagged() {
return flagged;
}
/**
* Returns the text that was flagged.
* @return the text that was flagged, or null if the text was not flagged.
*/
public String flaggedText() {
return flaggedText;
}
@Override
public boolean equals(Object o) {<FILL_FUNCTION_BODY>}
@Override
public int hashCode() {
return Objects.hash(flagged, flaggedText);
}
@Override
public String toString() {
return "Moderation {" +
" flagged = " + flagged +
", flaggedText = " + quoted(flaggedText) +
" }";
}
/**
* Constructs a Moderation object that is flagged.
* @param flaggedText the text that was flagged.
* @return a Moderation object.
*/
public static Moderation flagged(String flaggedText) {
return new Moderation(flaggedText);
}
/**
* Constructs a Moderation object that is not flagged.
* @return a Moderation object.
*/
public static Moderation notFlagged() {
return new Moderation();
}
} |
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Moderation that = (Moderation) o;
return this.flagged == that.flagged
&& Objects.equals(this.flaggedText, that.flaggedText);
| 516 | 81 | 597 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/model/output/Response.java | Response | equals | class Response<T> {
private final T content;
private final TokenUsage tokenUsage;
private final FinishReason finishReason;
/**
* Create a new Response.
*
* <p>Will contain {@code null} {@code TokenUsage} and {@code FinishReason}</p>
*
* @param content the content to wrap.
*/
public Response(T content) {
this(content, null, null);
}
/**
* Create a new Response.
*
* @param content the content to wrap.
* @param tokenUsage the token usage statistics, or {@code null}.
* @param finishReason the finish reason, or {@code null}.
*/
public Response(T content, TokenUsage tokenUsage, FinishReason finishReason) {
this.content = ensureNotNull(content, "content");
this.tokenUsage = tokenUsage;
this.finishReason = finishReason;
}
/**
* Get the content.
* @return the content.
*/
public T content() {
return content;
}
/**
* Get the token usage statistics.
* @return the token usage statistics, or {@code null}.
*/
public TokenUsage tokenUsage() {
return tokenUsage;
}
/**
* Get the finish reason.
* @return the finish reason, or {@code null}.
*/
public FinishReason finishReason() {
return finishReason;
}
@Override
public boolean equals(Object o) {<FILL_FUNCTION_BODY>}
@Override
public int hashCode() {
return Objects.hash(content, tokenUsage, finishReason);
}
@Override
public String toString() {
return "Response {" +
" content = " + content +
", tokenUsage = " + tokenUsage +
", finishReason = " + finishReason +
" }";
}
/**
* Create a new Response.
* @param content the content to wrap.
* @return the new Response.
* @param <T> the type of content.
*/
public static <T> Response<T> from(T content) {
return new Response<>(content);
}
/**
* Create a new Response.
* @param content the content to wrap.
* @param tokenUsage the token usage statistics.
* @return the new Response.
* @param <T> the type of content.
*/
public static <T> Response<T> from(T content, TokenUsage tokenUsage) {
return new Response<>(content, tokenUsage, null);
}
/**
* Create a new Response.
* @param content the content to wrap.
* @param tokenUsage the token usage statistics.
* @param finishReason the finish reason.
* @return the new Response.
* @param <T> the type of content.
*/
public static <T> Response<T> from(T content, TokenUsage tokenUsage, FinishReason finishReason) {
return new Response<>(content, tokenUsage, finishReason);
}
} |
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Response<?> that = (Response<?>) o;
return Objects.equals(this.content, that.content)
&& Objects.equals(this.tokenUsage, that.tokenUsage)
&& Objects.equals(this.finishReason, that.finishReason);
| 789 | 105 | 894 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/model/output/TokenUsage.java | TokenUsage | toString | class TokenUsage {
private final Integer inputTokenCount;
private final Integer outputTokenCount;
private final Integer totalTokenCount;
/**
* Creates a new {@link TokenUsage} instance with all fields set to null.
*/
public TokenUsage() {
this(null);
}
/**
* Creates a new {@link TokenUsage} instance with the given input token count.
*
* @param inputTokenCount The input token count.
*/
public TokenUsage(Integer inputTokenCount) {
this(inputTokenCount, null);
}
/**
* Creates a new {@link TokenUsage} instance with the given input and output token counts.
*
* @param inputTokenCount The input token count, or null if unknown.
* @param outputTokenCount The output token count, or null if unknown.
*/
public TokenUsage(Integer inputTokenCount, Integer outputTokenCount) {
this(inputTokenCount, outputTokenCount, sum(inputTokenCount, outputTokenCount));
}
/**
* Creates a new {@link TokenUsage} instance with the given input, output and total token counts.
*
* @param inputTokenCount The input token count, or null if unknown.
* @param outputTokenCount The output token count, or null if unknown.
* @param totalTokenCount The total token count, or null if unknown.
*/
public TokenUsage(Integer inputTokenCount, Integer outputTokenCount, Integer totalTokenCount) {
this.inputTokenCount = inputTokenCount;
this.outputTokenCount = outputTokenCount;
this.totalTokenCount = totalTokenCount;
}
/**
* Returns the input token count, or null if unknown.
*
* @return the input token count, or null if unknown.
*/
public Integer inputTokenCount() {
return inputTokenCount;
}
/**
* Returns the output token count, or null if unknown.
*
* @return the output token count, or null if unknown.
*/
public Integer outputTokenCount() {
return outputTokenCount;
}
/**
* Returns the total token count, or null if unknown.
*
* @return the total token count, or null if unknown.
*/
public Integer totalTokenCount() {
return totalTokenCount;
}
/**
* Adds the token usage of two responses together.
*
* <p>Fields which are null in both responses will be null in the result.
*
* @param that The token usage to add to this one.
* @return a new {@link TokenUsage} instance with the token usage of both responses added together.
*/
public TokenUsage add(TokenUsage that) {
if (that == null) {
return new TokenUsage(inputTokenCount, outputTokenCount, totalTokenCount);
}
return new TokenUsage(
sum(this.inputTokenCount, that.inputTokenCount),
sum(this.outputTokenCount, that.outputTokenCount),
sum(this.totalTokenCount, that.totalTokenCount)
);
}
/**
* Sum two integers, returning null if both are null.
*
* @param first The first integer, or null.
* @param second The second integer, or null.
* @return the sum of the two integers, or null if both are null.
*/
private static Integer sum(Integer first, Integer second) {
if (first == null && second == null) {
return null;
}
return getOrDefault(first, 0) + getOrDefault(second, 0);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TokenUsage that = (TokenUsage) o;
return Objects.equals(this.inputTokenCount, that.inputTokenCount)
&& Objects.equals(this.outputTokenCount, that.outputTokenCount)
&& Objects.equals(this.totalTokenCount, that.totalTokenCount);
}
@Override
public int hashCode() {
return Objects.hash(inputTokenCount, outputTokenCount, totalTokenCount);
}
@Override
public String toString() {<FILL_FUNCTION_BODY>}
} |
return "TokenUsage {" +
" inputTokenCount = " + inputTokenCount +
", outputTokenCount = " + outputTokenCount +
", totalTokenCount = " + totalTokenCount +
" }";
| 1,099 | 55 | 1,154 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/rag/DefaultRetrievalAugmentor.java | DefaultRetrievalAugmentor | log | class DefaultRetrievalAugmentor implements RetrievalAugmentor {
private static final Logger log = LoggerFactory.getLogger(DefaultRetrievalAugmentor.class);
private final QueryTransformer queryTransformer;
private final QueryRouter queryRouter;
private final ContentAggregator contentAggregator;
private final ContentInjector contentInjector;
private final Executor executor;
@Builder
public DefaultRetrievalAugmentor(QueryTransformer queryTransformer,
QueryRouter queryRouter,
ContentAggregator contentAggregator,
ContentInjector contentInjector,
Executor executor) {
this.queryTransformer = getOrDefault(queryTransformer, DefaultQueryTransformer::new);
this.queryRouter = ensureNotNull(queryRouter, "queryRouter");
this.contentAggregator = getOrDefault(contentAggregator, DefaultContentAggregator::new);
this.contentInjector = getOrDefault(contentInjector, DefaultContentInjector::new);
this.executor = getOrDefault(executor, Executors::newCachedThreadPool);
}
@Override
public UserMessage augment(UserMessage userMessage, Metadata metadata) {
Query originalQuery = Query.from(userMessage.text(), metadata);
Collection<Query> queries = queryTransformer.transform(originalQuery);
logQueries(originalQuery, queries);
Map<Query, CompletableFuture<Collection<List<Content>>>> queryToFutureContents = new ConcurrentHashMap<>();
queries.forEach(query -> {
CompletableFuture<Collection<List<Content>>> futureContents =
supplyAsync(() -> {
Collection<ContentRetriever> retrievers = queryRouter.route(query);
log(query, retrievers);
return retrievers;
},
executor
).thenCompose(retrievers -> retrieveFromAll(retrievers, query));
queryToFutureContents.put(query, futureContents);
});
Map<Query, Collection<List<Content>>> queryToContents = join(queryToFutureContents);
List<Content> contents = contentAggregator.aggregate(queryToContents);
log(queryToContents, contents);
UserMessage augmentedUserMessage = contentInjector.inject(contents, userMessage);
log(augmentedUserMessage);
return augmentedUserMessage;
}
private CompletableFuture<Collection<List<Content>>> retrieveFromAll(Collection<ContentRetriever> retrievers,
Query query) {
List<CompletableFuture<List<Content>>> futureContents = retrievers.stream()
.map(retriever -> supplyAsync(() -> retrieve(retriever, query), executor))
.collect(toList());
return allOf(futureContents.toArray(new CompletableFuture[0]))
.thenApply(ignored ->
futureContents.stream()
.map(CompletableFuture::join)
.collect(toList())
);
}
private static List<Content> retrieve(ContentRetriever retriever, Query query) {
List<Content> contents = retriever.retrieve(query);
log(query, retriever, contents);
return contents;
}
private static Map<Query, Collection<List<Content>>> join(
Map<Query, CompletableFuture<Collection<List<Content>>>> queryToFutureContents) {
return allOf(queryToFutureContents.values().toArray(new CompletableFuture[0]))
.thenApply(ignored ->
queryToFutureContents.entrySet().stream()
.collect(toMap(
Map.Entry::getKey,
entry -> entry.getValue().join()
))
).join();
}
private static void logQueries(Query originalQuery, Collection<Query> queries) {
if (queries.size() == 1) {
Query transformedQuery = queries.iterator().next();
if (!transformedQuery.equals(originalQuery)) {
log.debug("Transformed original query '{}' into '{}'",
originalQuery.text(), transformedQuery.text());
}
} else {
log.debug("Transformed original query '{}' into the following queries:\n{}",
originalQuery.text(), queries.stream()
.map(Query::text)
.map(query -> "- '" + query + "'")
.collect(joining("\n")));
}
}
private static void log(Query query, Collection<ContentRetriever> retrievers) {
// TODO use retriever id
if (retrievers.size() == 1) {
log.debug("Routing query '{}' to the following retriever: {}",
query.text(), retrievers.iterator().next());
} else {
log.debug("Routing query '{}' to the following retrievers:\n{}",
query.text(), retrievers.stream()
.map(retriever -> "- " + retriever.toString())
.collect(joining("\n")));
}
}
private static void log(Query query, ContentRetriever retriever, List<Content> contents) {<FILL_FUNCTION_BODY>}
private static void log(Map<Query, Collection<List<Content>>> queryToContents, List<Content> contents) {
int contentCount = 0;
for (Map.Entry<Query, Collection<List<Content>>> entry : queryToContents.entrySet()) {
for (List<Content> contentList : entry.getValue()) {
contentCount += contentList.size();
}
}
if (contentCount == contents.size()) {
return;
}
log.debug("Aggregated {} content(s) into {}", contentCount, contents.size());
log.trace("Aggregated {} content(s) into:\n{}",
contentCount, contents.stream()
.map(Content::textSegment)
.map(segment -> "- " + escapeNewlines(segment.text()))
.collect(joining("\n")));
}
private static void log(UserMessage augmentedUserMessage) {
log.trace("Augmented user message: " + escapeNewlines(augmentedUserMessage.singleText()));
}
private static String escapeNewlines(String text) {
return text.replace("\n", "\\n");
}
public static DefaultRetrievalAugmentorBuilder builder() {
return new DefaultRetrievalAugmentorBuilder();
}
public static class DefaultRetrievalAugmentorBuilder {
public DefaultRetrievalAugmentorBuilder contentRetriever(ContentRetriever contentRetriever) {
this.queryRouter = new DefaultQueryRouter(ensureNotNull(contentRetriever, "contentRetriever"));
return this;
}
}
} |
// TODO use retriever id
log.debug("Retrieved {} contents using query '{}' and retriever '{}'",
contents.size(), query.text(), retriever);
if (contents.size() > 0) {
log.trace("Retrieved {} contents using query '{}' and retriever '{}':\n{}",
contents.size(), query.text(), retriever, contents.stream()
.map(Content::textSegment)
.map(segment -> "- " + escapeNewlines(segment.text()))
.collect(joining("\n")));
}
| 1,700 | 151 | 1,851 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/rag/content/Content.java | Content | toString | class Content {
private final TextSegment textSegment;
public Content(String text) {
this(TextSegment.from(text));
}
public Content(TextSegment textSegment) {
this.textSegment = ensureNotNull(textSegment, "textSegment");
}
public TextSegment textSegment() {
return textSegment;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Content that = (Content) o;
return Objects.equals(this.textSegment, that.textSegment);
}
@Override
public int hashCode() {
return Objects.hash(textSegment);
}
@Override
public String toString() {<FILL_FUNCTION_BODY>}
public static Content from(String text) {
return new Content(text);
}
public static Content from(TextSegment textSegment) {
return new Content(textSegment);
}
} |
return "Content {" +
" textSegment = " + textSegment +
" }";
| 270 | 26 | 296 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/rag/content/aggregator/DefaultContentAggregator.java | DefaultContentAggregator | fuse | class DefaultContentAggregator implements ContentAggregator {
@Override
public List<Content> aggregate(Map<Query, Collection<List<Content>>> queryToContents) {
// First, for each query, fuse all contents retrieved from different sources using that query.
Map<Query, List<Content>> fused = fuse(queryToContents);
// Then, fuse all contents retrieved using all queries
return ReciprocalRankFuser.fuse(fused.values());
}
protected Map<Query, List<Content>> fuse(Map<Query, Collection<List<Content>>> queryToContents) {<FILL_FUNCTION_BODY>}
} |
Map<Query, List<Content>> fused = new LinkedHashMap<>();
for (Query query : queryToContents.keySet()) {
Collection<List<Content>> contents = queryToContents.get(query);
fused.put(query, ReciprocalRankFuser.fuse(contents));
}
return fused;
| 161 | 87 | 248 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/rag/content/aggregator/ReRankingContentAggregator.java | ReRankingContentAggregator | aggregate | class ReRankingContentAggregator implements ContentAggregator {
public static final Function<Map<Query, Collection<List<Content>>>, Query> DEFAULT_QUERY_SELECTOR =
(queryToContents) -> {
if (queryToContents.size() > 1) {
throw illegalArgument(
"The 'queryToContents' contains %s queries, making the re-ranking ambiguous. " +
"Because there are multiple queries, it is unclear which one should be " +
"used for re-ranking. Please provide a 'querySelector' in the constructor/builder.",
queryToContents.size()
);
}
return queryToContents.keySet().iterator().next();
};
private final ScoringModel scoringModel;
private final Function<Map<Query, Collection<List<Content>>>, Query> querySelector;
private final Double minScore;
public ReRankingContentAggregator(ScoringModel scoringModel) {
this(scoringModel, DEFAULT_QUERY_SELECTOR, null);
}
@Builder
public ReRankingContentAggregator(ScoringModel scoringModel,
Function<Map<Query, Collection<List<Content>>>, Query> querySelector,
Double minScore) {
this.scoringModel = ensureNotNull(scoringModel, "scoringModel");
this.querySelector = getOrDefault(querySelector, DEFAULT_QUERY_SELECTOR);
this.minScore = minScore;
}
@Override
public List<Content> aggregate(Map<Query, Collection<List<Content>>> queryToContents) {<FILL_FUNCTION_BODY>}
protected Map<Query, List<Content>> fuse(Map<Query, Collection<List<Content>>> queryToContents) {
Map<Query, List<Content>> fused = new LinkedHashMap<>();
for (Query query : queryToContents.keySet()) {
Collection<List<Content>> contents = queryToContents.get(query);
fused.put(query, ReciprocalRankFuser.fuse(contents));
}
return fused;
}
protected List<Content> reRankAndFilter(List<Content> contents, Query query) {
List<TextSegment> segments = contents.stream()
.map(Content::textSegment)
.collect(toList());
List<Double> scores = scoringModel.scoreAll(segments, query.text()).content();
Map<TextSegment, Double> segmentToScore = new HashMap<>();
for (int i = 0; i < segments.size(); i++) {
segmentToScore.put(segments.get(i), scores.get(i));
}
return segmentToScore.entrySet().stream()
.filter(entry -> minScore == null || entry.getValue() >= minScore)
.sorted(Map.Entry.<TextSegment, Double>comparingByValue().reversed())
.map(Map.Entry::getKey)
.map(Content::from)
.collect(toList());
}
} |
if (queryToContents.isEmpty()) {
return emptyList();
}
// Select a query against which all contents will be re-ranked
Query query = querySelector.apply(queryToContents);
// For each query, fuse all contents retrieved from different sources using that query
Map<Query, List<Content>> queryToFusedContents = fuse(queryToContents);
// Fuse all contents retrieved using all queries
List<Content> fusedContents = ReciprocalRankFuser.fuse(queryToFusedContents.values());
if (fusedContents.isEmpty()) {
return fusedContents;
}
// Re-rank all the fused contents against the query selected by the query selector
return reRankAndFilter(fusedContents, query);
| 744 | 196 | 940 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/rag/content/aggregator/ReciprocalRankFuser.java | ReciprocalRankFuser | fuse | class ReciprocalRankFuser {
/**
* Fuses multiple {@code List<Content>} into a single {@code List<Content>}
* using the Reciprocal Rank Fusion (RRF) algorithm with k=60.
*
* @param listsOfContents A {@link Collection} of {@code List<Content>} to be fused together.
* @return A single {@code List<Content>}, the result of the fusion.
*/
public static List<Content> fuse(Collection<List<Content>> listsOfContents) {
return fuse(listsOfContents, 60);
}
/**
* Fuses multiple {@code List<Content>} into a single {@code List<Content>}
* using the Reciprocal Rank Fusion (RRF) algorithm.
*
* @param listsOfContents A {@link Collection} of {@code List<Content>} to be fused together.
* @param k A ranking constant used to control the influence of individual ranks
* from different ranked lists being combined. A common value used is 60,
* based on empirical studies. However, the optimal value may vary depending
* on the specific application and the characteristics of the data.
* A larger value diminishes the differences between the ranks,
* leading to a more uniform distribution of fusion scores.
* A smaller value amplifies the importance of the top-ranked items in each list.
* K must be greater than or equal to 1.
* @return A single {@code List<Content>}, the result of the fusion.
*/
public static List<Content> fuse(Collection<List<Content>> listsOfContents, int k) {<FILL_FUNCTION_BODY>}
} |
ensureBetween(k, 1, Integer.MAX_VALUE, "k");
Map<Content, Double> scores = new LinkedHashMap<>();
for (List<Content> singleListOfContent : listsOfContents) {
for (int i = 0; i < singleListOfContent.size(); i++) {
Content content = singleListOfContent.get(i);
double currentScore = scores.getOrDefault(content, 0.0);
int rank = i + 1;
double newScore = currentScore + 1.0 / (k + rank);
scores.put(content, newScore);
}
}
List<Content> fused = new ArrayList<>(scores.keySet());
fused.sort(Comparator.comparingDouble(scores::get).reversed());
return fused;
| 423 | 207 | 630 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/rag/content/injector/DefaultContentInjector.java | DefaultContentInjector | format | class DefaultContentInjector implements ContentInjector {
public static final PromptTemplate DEFAULT_PROMPT_TEMPLATE = PromptTemplate.from(
"{{userMessage}}\n" +
"\n" +
"Answer using the following information:\n" +
"{{contents}}"
);
private final PromptTemplate promptTemplate;
private final List<String> metadataKeysToInclude;
public DefaultContentInjector() {
this(DEFAULT_PROMPT_TEMPLATE, null);
}
public DefaultContentInjector(List<String> metadataKeysToInclude) {
this(DEFAULT_PROMPT_TEMPLATE, ensureNotEmpty(metadataKeysToInclude, "metadataKeysToInclude"));
}
public DefaultContentInjector(PromptTemplate promptTemplate) {
this(ensureNotNull(promptTemplate, "promptTemplate"), null);
}
@Builder
public DefaultContentInjector(PromptTemplate promptTemplate, List<String> metadataKeysToInclude) {
this.promptTemplate = getOrDefault(promptTemplate, DEFAULT_PROMPT_TEMPLATE);
this.metadataKeysToInclude = copyIfNotNull(metadataKeysToInclude);
}
@Override
public UserMessage inject(List<Content> contents, UserMessage userMessage) {
if (contents.isEmpty()) {
return userMessage;
}
Prompt prompt = createPrompt(userMessage, contents);
return prompt.toUserMessage();
}
protected Prompt createPrompt(UserMessage userMessage, List<Content> contents) {
Map<String, Object> variables = new HashMap<>();
variables.put("userMessage", userMessage.text());
variables.put("contents", format(contents));
return promptTemplate.apply(variables);
}
protected String format(List<Content> contents) {
return contents.stream()
.map(this::format)
.collect(joining("\n\n"));
}
protected String format(Content content) {<FILL_FUNCTION_BODY>}
protected String format(Metadata metadata) {
StringBuilder formattedMetadata = new StringBuilder();
for (String metadataKey : metadataKeysToInclude) {
String metadataValue = metadata.get(metadataKey);
if (metadataValue != null) {
if (formattedMetadata.length() > 0) {
formattedMetadata.append("\n");
}
formattedMetadata.append(metadataKey).append(": ").append(metadataValue);
}
}
return formattedMetadata.toString();
}
protected String format(String segmentContent, String segmentMetadata) {
return segmentMetadata.isEmpty()
? segmentContent
: String.format("content: %s\n%s", segmentContent, segmentMetadata);
}
} |
TextSegment segment = content.textSegment();
if (isNullOrEmpty(metadataKeysToInclude)) {
return segment.text();
}
String segmentContent = segment.text();
String segmentMetadata = format(segment.metadata());
return format(segmentContent, segmentMetadata);
| 705 | 79 | 784 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/rag/content/retriever/EmbeddingStoreContentRetriever.java | EmbeddingStoreContentRetriever | loadEmbeddingModel | class EmbeddingStoreContentRetriever implements ContentRetriever {
public static final Function<Query, Integer> DEFAULT_MAX_RESULTS = (query) -> 3;
public static final Function<Query, Double> DEFAULT_MIN_SCORE = (query) -> 0.0;
public static final Function<Query, Filter> DEFAULT_FILTER = (query) -> null;
private final EmbeddingStore<TextSegment> embeddingStore;
private final EmbeddingModel embeddingModel;
private final Function<Query, Integer> maxResultsProvider;
private final Function<Query, Double> minScoreProvider;
private final Function<Query, Filter> filterProvider;
public EmbeddingStoreContentRetriever(EmbeddingStore<TextSegment> embeddingStore,
EmbeddingModel embeddingModel) {
this(
embeddingStore,
embeddingModel,
DEFAULT_MAX_RESULTS,
DEFAULT_MIN_SCORE,
DEFAULT_FILTER
);
}
public EmbeddingStoreContentRetriever(EmbeddingStore<TextSegment> embeddingStore,
EmbeddingModel embeddingModel,
int maxResults) {
this(
embeddingStore,
embeddingModel,
(query) -> maxResults,
DEFAULT_MIN_SCORE,
DEFAULT_FILTER
);
}
public EmbeddingStoreContentRetriever(EmbeddingStore<TextSegment> embeddingStore,
EmbeddingModel embeddingModel,
Integer maxResults,
Double minScore) {
this(
embeddingStore,
embeddingModel,
(query) -> maxResults,
(query) -> minScore,
DEFAULT_FILTER
);
}
@Builder
private EmbeddingStoreContentRetriever(EmbeddingStore<TextSegment> embeddingStore,
EmbeddingModel embeddingModel,
Function<Query, Integer> dynamicMaxResults,
Function<Query, Double> dynamicMinScore,
Function<Query, Filter> dynamicFilter) {
this.embeddingStore = ensureNotNull(embeddingStore, "embeddingStore");
this.embeddingModel = ensureNotNull(
getOrDefault(embeddingModel, EmbeddingStoreContentRetriever::loadEmbeddingModel),
"embeddingModel"
);
this.maxResultsProvider = getOrDefault(dynamicMaxResults, DEFAULT_MAX_RESULTS);
this.minScoreProvider = getOrDefault(dynamicMinScore, DEFAULT_MIN_SCORE);
this.filterProvider = getOrDefault(dynamicFilter, DEFAULT_FILTER);
}
private static EmbeddingModel loadEmbeddingModel() {<FILL_FUNCTION_BODY>}
public static class EmbeddingStoreContentRetrieverBuilder {
public EmbeddingStoreContentRetrieverBuilder maxResults(Integer maxResults) {
if (maxResults != null) {
dynamicMaxResults = (query) -> ensureGreaterThanZero(maxResults, "maxResults");
}
return this;
}
public EmbeddingStoreContentRetrieverBuilder minScore(Double minScore) {
if (minScore != null) {
dynamicMinScore = (query) -> ensureBetween(minScore, 0, 1, "minScore");
}
return this;
}
public EmbeddingStoreContentRetrieverBuilder filter(Filter filter) {
if (filter != null) {
dynamicFilter = (query) -> filter;
}
return this;
}
}
/**
* Creates an instance of an {@code EmbeddingStoreContentRetriever} from the specified {@link EmbeddingStore}
* and {@link EmbeddingModel} found through SPI (see {@link EmbeddingModelFactory}).
*/
public static EmbeddingStoreContentRetriever from(EmbeddingStore<TextSegment> embeddingStore) {
return builder().embeddingStore(embeddingStore).build();
}
@Override
public List<Content> retrieve(Query query) {
Embedding embeddedQuery = embeddingModel.embed(query.text()).content();
EmbeddingSearchRequest searchRequest = EmbeddingSearchRequest.builder()
.queryEmbedding(embeddedQuery)
.maxResults(maxResultsProvider.apply(query))
.minScore(minScoreProvider.apply(query))
.filter(filterProvider.apply(query))
.build();
EmbeddingSearchResult<TextSegment> searchResult = embeddingStore.search(searchRequest);
return searchResult.matches().stream()
.map(EmbeddingMatch::embedded)
.map(Content::from)
.collect(toList());
}
} |
Collection<EmbeddingModelFactory> factories = loadFactories(EmbeddingModelFactory.class);
if (factories.size() > 1) {
throw new RuntimeException("Conflict: multiple embedding models have been found in the classpath. " +
"Please explicitly specify the one you wish to use.");
}
for (EmbeddingModelFactory factory : factories) {
return factory.create();
}
return null;
| 1,140 | 112 | 1,252 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/rag/query/Metadata.java | Metadata | equals | class Metadata {
private final UserMessage userMessage;
private final Object chatMemoryId;
private final List<ChatMessage> chatMemory;
public Metadata(UserMessage userMessage, Object chatMemoryId, List<ChatMessage> chatMemory) {
this.userMessage = ensureNotNull(userMessage, "userMessage");
this.chatMemoryId = chatMemoryId;
this.chatMemory = copyIfNotNull(chatMemory);
}
/**
* @return an original {@link UserMessage} passed to the {@link RetrievalAugmentor#augment(UserMessage, Metadata)}.
*/
public UserMessage userMessage() {
return userMessage;
}
/**
* @return a chat memory ID. Present when {@link ChatMemory} is used. Can be used to distinguish between users.
* See {@code @dev.langchain4j.service.MemoryId} annotation from a {@code dev.langchain4j:langchain4j} module.
*/
public Object chatMemoryId() {
return chatMemoryId;
}
/**
* @return previous messages in the {@link ChatMemory}. Present when {@link ChatMemory} is used.
* Can be used to get more details about the context (conversation) in which the {@link Query} originated.
*/
public List<ChatMessage> chatMemory() {
return chatMemory;
}
@Override
public boolean equals(Object o) {<FILL_FUNCTION_BODY>}
@Override
public int hashCode() {
return Objects.hash(userMessage, chatMemoryId, chatMemory);
}
@Override
public String toString() {
return "Metadata {" +
" userMessage = " + userMessage +
", chatMemoryId = " + chatMemoryId +
", chatMemory = " + chatMemory +
" }";
}
public static Metadata from(UserMessage userMessage, Object chatMemoryId, List<ChatMessage> chatMemory) {
return new Metadata(userMessage, chatMemoryId, chatMemory);
}
} |
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Metadata that = (Metadata) o;
return Objects.equals(this.userMessage, that.userMessage)
&& Objects.equals(this.chatMemoryId, that.chatMemoryId)
&& Objects.equals(this.chatMemory, that.chatMemory);
| 511 | 102 | 613 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/rag/query/Query.java | Query | toString | class Query {
private final String text;
private final Metadata metadata;
public Query(String text) {
this.text = ensureNotBlank(text, "text");
this.metadata = null;
}
public Query(String text, Metadata metadata) {
this.text = ensureNotBlank(text, "text");
this.metadata = ensureNotNull(metadata, "metadata");
}
public String text() {
return text;
}
public Metadata metadata() {
return metadata;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Query that = (Query) o;
return Objects.equals(this.text, that.text)
&& Objects.equals(this.metadata, that.metadata);
}
@Override
public int hashCode() {
return Objects.hash(text, metadata);
}
@Override
public String toString() {<FILL_FUNCTION_BODY>}
public static Query from(String text) {
return new Query(text);
}
public static Query from(String text, Metadata metadata) {
return new Query(text, metadata);
}
} |
return "Query {" +
" text = " + quoted(text) +
", metadata = " + metadata +
" }";
| 336 | 36 | 372 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/rag/query/router/LanguageModelQueryRouter.java | LanguageModelQueryRouter | fallback | class LanguageModelQueryRouter implements QueryRouter {
private static final Logger log = LoggerFactory.getLogger(LanguageModelQueryRouter.class);
public static final PromptTemplate DEFAULT_PROMPT_TEMPLATE = PromptTemplate.from(
"Based on the user query, determine the most suitable data source(s) " +
"to retrieve relevant information from the following options:\n" +
"{{options}}\n" +
"It is very important that your answer consists of either a single number " +
"or multiple numbers separated by commas and nothing else!\n" +
"User query: {{query}}"
);
protected final ChatLanguageModel chatLanguageModel;
protected final PromptTemplate promptTemplate;
protected final String options;
protected final Map<Integer, ContentRetriever> idToRetriever;
protected final FallbackStrategy fallbackStrategy;
public LanguageModelQueryRouter(ChatLanguageModel chatLanguageModel,
Map<ContentRetriever, String> retrieverToDescription) {
this(chatLanguageModel, retrieverToDescription, DEFAULT_PROMPT_TEMPLATE, DO_NOT_ROUTE);
}
@Builder
public LanguageModelQueryRouter(ChatLanguageModel chatLanguageModel,
Map<ContentRetriever, String> retrieverToDescription,
PromptTemplate promptTemplate,
FallbackStrategy fallbackStrategy) {
this.chatLanguageModel = ensureNotNull(chatLanguageModel, "chatLanguageModel");
ensureNotEmpty(retrieverToDescription, "retrieverToDescription");
this.promptTemplate = getOrDefault(promptTemplate, DEFAULT_PROMPT_TEMPLATE);
Map<Integer, ContentRetriever> idToRetriever = new HashMap<>();
StringBuilder optionsBuilder = new StringBuilder();
int id = 1;
for (Map.Entry<ContentRetriever, String> entry : retrieverToDescription.entrySet()) {
idToRetriever.put(id, ensureNotNull(entry.getKey(), "ContentRetriever"));
if (id > 1) {
optionsBuilder.append("\n");
}
optionsBuilder.append(id);
optionsBuilder.append(": ");
optionsBuilder.append(ensureNotBlank(entry.getValue(), "ContentRetriever description"));
id++;
}
this.idToRetriever = idToRetriever;
this.options = optionsBuilder.toString();
this.fallbackStrategy = getOrDefault(fallbackStrategy, DO_NOT_ROUTE);
}
@Override
public Collection<ContentRetriever> route(Query query) {
Prompt prompt = createPrompt(query);
try {
String response = chatLanguageModel.generate(prompt.text());
return parse(response);
} catch (Exception e) {
log.warn("Failed to route query '{}'", query.text(), e);
return fallback(query, e);
}
}
protected Collection<ContentRetriever> fallback(Query query, Exception e) {<FILL_FUNCTION_BODY>}
protected Prompt createPrompt(Query query) {
Map<String, Object> variables = new HashMap<>();
variables.put("query", query.text());
variables.put("options", options);
return promptTemplate.apply(variables);
}
protected Collection<ContentRetriever> parse(String choices) {
return stream(choices.split(","))
.map(String::trim)
.map(Integer::parseInt)
.map(idToRetriever::get)
.collect(toList());
}
/**
* Strategy applied if the call to the LLM fails of if LLM does not return a valid response.
* It could be because it was formatted improperly, or it is unclear where to route.
*/
public enum FallbackStrategy {
/**
* In this case, the {@link Query} will not be routed to any {@link ContentRetriever},
* thus skipping the RAG flow. No content will be appended to the original {@link UserMessage}.
*/
DO_NOT_ROUTE,
/**
* In this case, the {@link Query} will be routed to all {@link ContentRetriever}s.
*/
ROUTE_TO_ALL,
/**
* In this case, an original exception will be re-thrown, and the RAG flow will fail.
*/
FAIL
}
} |
switch (fallbackStrategy) {
case DO_NOT_ROUTE:
log.debug("Fallback: query '{}' will not be routed", query.text());
return emptyList();
case ROUTE_TO_ALL:
log.debug("Fallback: query '{}' will be routed to all available content retrievers", query.text());
return new ArrayList<>(idToRetriever.values());
case FAIL:
default:
throw new RuntimeException(e);
}
| 1,114 | 130 | 1,244 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/rag/query/transformer/CompressingQueryTransformer.java | CompressingQueryTransformer | transform | class CompressingQueryTransformer implements QueryTransformer {
public static final PromptTemplate DEFAULT_PROMPT_TEMPLATE = PromptTemplate.from(
"Read and understand the conversation between the User and the AI. " +
"Then, analyze the new query from the User. " +
"Identify all relevant details, terms, and context from both the conversation and the new query. " +
"Reformulate this query into a clear, concise, and self-contained format suitable for information retrieval.\n" +
"\n" +
"Conversation:\n" +
"{{chatMemory}}\n" +
"\n" +
"User query: {{query}}\n" +
"\n" +
"It is very important that you provide only reformulated query and nothing else! " +
"Do not prepend a query with anything!"
);
protected final PromptTemplate promptTemplate;
protected final ChatLanguageModel chatLanguageModel;
public CompressingQueryTransformer(ChatLanguageModel chatLanguageModel) {
this(chatLanguageModel, DEFAULT_PROMPT_TEMPLATE);
}
@Builder
public CompressingQueryTransformer(ChatLanguageModel chatLanguageModel, PromptTemplate promptTemplate) {
this.chatLanguageModel = ensureNotNull(chatLanguageModel, "chatLanguageModel");
this.promptTemplate = getOrDefault(promptTemplate, DEFAULT_PROMPT_TEMPLATE);
}
@Override
public Collection<Query> transform(Query query) {<FILL_FUNCTION_BODY>}
protected String format(List<ChatMessage> chatMemory) {
return chatMemory.stream()
.map(this::format)
.filter(Objects::nonNull)
.collect(joining("\n"));
}
protected String format(ChatMessage message) {
if (message instanceof UserMessage) {
return "User: " + message.text();
} else if (message instanceof AiMessage) {
AiMessage aiMessage = (AiMessage) message;
if (aiMessage.hasToolExecutionRequests()) {
return null;
}
return "AI: " + aiMessage.text();
} else {
return null;
}
}
protected Prompt createPrompt(Query query, String chatMemory) {
Map<String, Object> variables = new HashMap<>();
variables.put("query", query.text());
variables.put("chatMemory", chatMemory);
return promptTemplate.apply(variables);
}
} |
List<ChatMessage> chatMemory = query.metadata().chatMemory();
if (chatMemory.isEmpty()) {
// no need to compress if there are no previous messages
return singletonList(query);
}
Prompt prompt = createPrompt(query, format(chatMemory));
String compressedQuery = chatLanguageModel.generate(prompt.text());
return singletonList(Query.from(compressedQuery));
| 625 | 106 | 731 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/rag/query/transformer/ExpandingQueryTransformer.java | ExpandingQueryTransformer | createPrompt | class ExpandingQueryTransformer implements QueryTransformer {
public static final PromptTemplate DEFAULT_PROMPT_TEMPLATE = PromptTemplate.from(
"Generate {{n}} different versions of a provided user query. " +
"Each version should be worded differently, using synonyms or alternative sentence structures, " +
"but they should all retain the original meaning. " +
"These versions will be used to retrieve relevant documents. " +
"It is very important to provide each query version on a separate line, " +
"without enumerations, hyphens, or any additional formatting!\n" +
"User query: {{query}}"
);
public static final int DEFAULT_N = 3;
protected final ChatLanguageModel chatLanguageModel;
protected final PromptTemplate promptTemplate;
protected final int n;
public ExpandingQueryTransformer(ChatLanguageModel chatLanguageModel) {
this(chatLanguageModel, DEFAULT_PROMPT_TEMPLATE, DEFAULT_N);
}
public ExpandingQueryTransformer(ChatLanguageModel chatLanguageModel, int n) {
this(chatLanguageModel, DEFAULT_PROMPT_TEMPLATE, n);
}
public ExpandingQueryTransformer(ChatLanguageModel chatLanguageModel, PromptTemplate promptTemplate) {
this(chatLanguageModel, ensureNotNull(promptTemplate, "promptTemplate"), DEFAULT_N);
}
@Builder
public ExpandingQueryTransformer(ChatLanguageModel chatLanguageModel, PromptTemplate promptTemplate, Integer n) {
this.chatLanguageModel = ensureNotNull(chatLanguageModel, "chatLanguageModel");
this.promptTemplate = getOrDefault(promptTemplate, DEFAULT_PROMPT_TEMPLATE);
this.n = ensureGreaterThanZero(getOrDefault(n, DEFAULT_N), "n");
}
@Override
public Collection<Query> transform(Query query) {
Prompt prompt = createPrompt(query);
String response = chatLanguageModel.generate(prompt.text());
return parse(response);
}
protected Prompt createPrompt(Query query) {<FILL_FUNCTION_BODY>}
protected List<Query> parse(String queries) {
return stream(queries.split("\n"))
.filter(Utils::isNotNullOrBlank)
.map(Query::from)
.collect(toList());
}
} |
Map<String, Object> variables = new HashMap<>();
variables.put("query", query.text());
variables.put("n", n);
return promptTemplate.apply(variables);
| 594 | 51 | 645 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/spi/ServiceHelper.java | ServiceHelper | loadFactories | class ServiceHelper {
/**
* Utility class, no public constructor.
*/
private ServiceHelper() {
}
/**
* Load all the services of a given type.
*
* @param clazz the type of service
* @param <T> the type of service
* @return the list of services, empty if none
*/
public static <T> Collection<T> loadFactories(Class<T> clazz) {
return loadFactories(clazz, null);
}
/**
* Load all the services of a given type.
*
* <p>Utility mechanism around {@code ServiceLoader.load()}</p>
*
* <ul>
* <li>If classloader is {@code null}, will try {@code ServiceLoader.load(clazz)}</li>
* <li>If classloader is not {@code null}, will try {@code ServiceLoader.load(clazz, classloader)}</li>
* </ul>
*
* <p>If the above return nothing, will fall back to {@code ServiceLoader.load(clazz, $this class loader$)}</p>
*
* @param clazz the type of service
* @param classLoader the classloader to use, may be null
* @param <T> the type of service
* @return the list of services, empty if none
*/
public static <T> Collection<T> loadFactories(Class<T> clazz, /* @Nullable */ ClassLoader classLoader) {<FILL_FUNCTION_BODY>}
/**
* Load all the services from a ServiceLoader.
*
* @param loader the loader
* @param <T> the type of service
* @return the list of services, empty if none
*/
private static <T> List<T> loadAll(ServiceLoader<T> loader) {
List<T> list = new ArrayList<>();
loader.iterator().forEachRemaining(list::add);
return list;
}
} |
List<T> result;
if (classLoader != null) {
result = loadAll(ServiceLoader.load(clazz, classLoader));
} else {
// this is equivalent to:
// ServiceLoader.load(clazz, TCCL);
result = loadAll(ServiceLoader.load(clazz));
}
if (result.isEmpty()) {
// By default, ServiceLoader.load uses the TCCL, this may not be enough in environment dealing with
// classloaders differently such as OSGi. So we should try to use the classloader having loaded this
// class. In OSGi it would be the bundle exposing vert.x and so have access to all its classes.
result = loadAll(ServiceLoader.load(clazz, ServiceHelper.class.getClassLoader()));
}
return result;
| 507 | 205 | 712 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/store/embedding/CosineSimilarity.java | CosineSimilarity | between | class CosineSimilarity {
private CosineSimilarity() {}
/**
* A small value to avoid division by zero.
*/
public static final float EPSILON = 1e-8f;
/**
* Calculates cosine similarity between two vectors.
* <p>
* Cosine similarity measures the cosine of the angle between two vectors, indicating their directional similarity.
* It produces a value in the range:
* <p>
* -1 indicates vectors are diametrically opposed (opposite directions).
* <p>
* 0 indicates vectors are orthogonal (no directional similarity).
* <p>
* 1 indicates vectors are pointing in the same direction (but not necessarily of the same magnitude).
* <p>
* Not to be confused with cosine distance ([0..2]), which quantifies how different two vectors are.
* <p>
* Embeddings of all-zeros vectors are considered orthogonal to all other vectors;
* including other all-zeros vectors.
*
* @param embeddingA first embedding vector
* @param embeddingB second embedding vector
* @return cosine similarity in the range [-1..1]
*/
public static double between(Embedding embeddingA, Embedding embeddingB) {<FILL_FUNCTION_BODY>}
/**
* Converts relevance score into cosine similarity.
*
* @param relevanceScore Relevance score in the range [0..1] where 0 is not relevant and 1 is relevant.
* @return Cosine similarity in the range [-1..1] where -1 is not relevant and 1 is relevant.
*/
public static double fromRelevanceScore(double relevanceScore) {
return relevanceScore * 2 - 1;
}
} |
ensureNotNull(embeddingA, "embeddingA");
ensureNotNull(embeddingB, "embeddingB");
float[] vectorA = embeddingA.vector();
float[] vectorB = embeddingB.vector();
if (vectorA.length != vectorB.length) {
throw illegalArgument("Length of vector a (%s) must be equal to the length of vector b (%s)",
vectorA.length, vectorB.length);
}
double dotProduct = 0.0;
double normA = 0.0;
double normB = 0.0;
for (int i = 0; i < vectorA.length; i++) {
dotProduct += vectorA[i] * vectorB[i];
normA += vectorA[i] * vectorA[i];
normB += vectorB[i] * vectorB[i];
}
// Avoid division by zero.
return dotProduct / Math.max(Math.sqrt(normA) * Math.sqrt(normB), EPSILON);
| 441 | 257 | 698 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/store/embedding/EmbeddingMatch.java | EmbeddingMatch | equals | class EmbeddingMatch<Embedded> {
private final Double score;
private final String embeddingId;
private final Embedding embedding;
private final Embedded embedded;
/**
* Creates a new instance.
* @param score The relevance score (derivative of cosine distance) of this embedding compared to
* a reference embedding during a search.
* @param embeddingId The ID of the embedding assigned when adding this embedding to the store.
* @param embedding The embedding that has been matched.
* @param embedded The original content that was embedded. Typically, this is a {@link dev.langchain4j.data.segment.TextSegment}.
*/
public EmbeddingMatch(Double score, String embeddingId, Embedding embedding, Embedded embedded) {
this.score = ensureNotNull(score, "score");
this.embeddingId = ensureNotBlank(embeddingId, "embeddingId");
this.embedding = embedding;
this.embedded = embedded;
}
/**
* Returns the relevance score (derivative of cosine distance) of this embedding compared to
* a reference embedding during a search.
* The current implementation assumes that the embedding store uses cosine distance when comparing embeddings.
*
* @return Relevance score, ranging from 0 (not relevant) to 1 (highly relevant).
*/
public Double score() {
return score;
}
/**
* The ID of the embedding assigned when adding this embedding to the store.
* @return The ID of the embedding assigned when adding this embedding to the store.
*/
public String embeddingId() {
return embeddingId;
}
/**
* Returns the embedding that has been matched.
* @return The embedding that has been matched.
*/
public Embedding embedding() {
return embedding;
}
/**
* Returns the original content that was embedded.
* @return The original content that was embedded. Typically, this is a {@link dev.langchain4j.data.segment.TextSegment}.
*/
public Embedded embedded() {
return embedded;
}
@Override
public boolean equals(Object o) {<FILL_FUNCTION_BODY>}
@Override
public int hashCode() {
return Objects.hash(score, embeddingId, embedding, embedded);
}
@Override
public String toString() {
return "EmbeddingMatch {" +
" score = " + score +
", embedded = " + embedded +
", embeddingId = " + embeddingId +
", embedding = " + embedding +
" }";
}
} |
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
EmbeddingMatch<?> that = (EmbeddingMatch<?>) o;
return Objects.equals(this.score, that.score)
&& Objects.equals(this.embeddingId, that.embeddingId)
&& Objects.equals(this.embedding, that.embedding)
&& Objects.equals(this.embedded, that.embedded);
| 656 | 128 | 784 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/store/embedding/filter/MetadataFilterBuilder.java | MetadataFilterBuilder | isIn | class MetadataFilterBuilder {
private final String key;
public MetadataFilterBuilder(String key) {
this.key = ensureNotBlank(key, "key");
}
@Experimental
public static MetadataFilterBuilder metadataKey(String key) {
return new MetadataFilterBuilder(key);
}
// isEqualTo
public Filter isEqualTo(String value) {
return new IsEqualTo(key, value);
}
public Filter isEqualTo(int value) {
return new IsEqualTo(key, value);
}
public Filter isEqualTo(long value) {
return new IsEqualTo(key, value);
}
public Filter isEqualTo(float value) {
return new IsEqualTo(key, value);
}
public Filter isEqualTo(double value) {
return new IsEqualTo(key, value);
}
// isNotEqualTo
public Filter isNotEqualTo(String value) {
return new IsNotEqualTo(key, value);
}
public Filter isNotEqualTo(int value) {
return new IsNotEqualTo(key, value);
}
public Filter isNotEqualTo(long value) {
return new IsNotEqualTo(key, value);
}
public Filter isNotEqualTo(float value) {
return new IsNotEqualTo(key, value);
}
public Filter isNotEqualTo(double value) {
return new IsNotEqualTo(key, value);
}
// isGreaterThan
public Filter isGreaterThan(String value) {
return new IsGreaterThan(key, value);
}
public Filter isGreaterThan(int value) {
return new IsGreaterThan(key, value);
}
public Filter isGreaterThan(long value) {
return new IsGreaterThan(key, value);
}
public Filter isGreaterThan(float value) {
return new IsGreaterThan(key, value);
}
public Filter isGreaterThan(double value) {
return new IsGreaterThan(key, value);
}
// isGreaterThanOrEqualTo
public Filter isGreaterThanOrEqualTo(String value) {
return new IsGreaterThanOrEqualTo(key, value);
}
public Filter isGreaterThanOrEqualTo(int value) {
return new IsGreaterThanOrEqualTo(key, value);
}
public Filter isGreaterThanOrEqualTo(long value) {
return new IsGreaterThanOrEqualTo(key, value);
}
public Filter isGreaterThanOrEqualTo(float value) {
return new IsGreaterThanOrEqualTo(key, value);
}
public Filter isGreaterThanOrEqualTo(double value) {
return new IsGreaterThanOrEqualTo(key, value);
}
// isLessThan
public Filter isLessThan(String value) {
return new IsLessThan(key, value);
}
public Filter isLessThan(int value) {
return new IsLessThan(key, value);
}
public Filter isLessThan(long value) {
return new IsLessThan(key, value);
}
public Filter isLessThan(float value) {
return new IsLessThan(key, value);
}
public Filter isLessThan(double value) {
return new IsLessThan(key, value);
}
// isLessThanOrEqualTo
public Filter isLessThanOrEqualTo(String value) {
return new IsLessThanOrEqualTo(key, value);
}
public Filter isLessThanOrEqualTo(int value) {
return new IsLessThanOrEqualTo(key, value);
}
public Filter isLessThanOrEqualTo(long value) {
return new IsLessThanOrEqualTo(key, value);
}
public Filter isLessThanOrEqualTo(float value) {
return new IsLessThanOrEqualTo(key, value);
}
public Filter isLessThanOrEqualTo(double value) {
return new IsLessThanOrEqualTo(key, value);
}
// isBetween
public Filter isBetween(String fromValue, String toValue) {
return isGreaterThanOrEqualTo(fromValue).and(isLessThanOrEqualTo(toValue));
}
public Filter isBetween(int fromValue, int toValue) {
return isGreaterThanOrEqualTo(fromValue).and(isLessThanOrEqualTo(toValue));
}
public Filter isBetween(long fromValue, long toValue) {
return isGreaterThanOrEqualTo(fromValue).and(isLessThanOrEqualTo(toValue));
}
public Filter isBetween(float fromValue, float toValue) {
return isGreaterThanOrEqualTo(fromValue).and(isLessThanOrEqualTo(toValue));
}
public Filter isBetween(double fromValue, double toValue) {
return isGreaterThanOrEqualTo(fromValue).and(isLessThanOrEqualTo(toValue));
}
// isIn
public Filter isIn(String... values) {
return new IsIn(key, asList(values));
}
public Filter isIn(int... values) {
return new IsIn(key, stream(values).boxed().collect(toList()));
}
public Filter isIn(long... values) {
return new IsIn(key, stream(values).boxed().collect(toList()));
}
public Filter isIn(float... values) {<FILL_FUNCTION_BODY>}
public Filter isIn(double... values) {
return new IsIn(key, stream(values).boxed().collect(toList()));
}
public Filter isIn(Collection<?> values) {
return new IsIn(key, values);
}
// isNotIn
public Filter isNotIn(String... values) {
return new IsNotIn(key, asList(values));
}
public Filter isNotIn(int... values) {
return new IsNotIn(key, stream(values).boxed().collect(toList()));
}
public Filter isNotIn(long... values) {
return new IsNotIn(key, stream(values).boxed().collect(toList()));
}
public Filter isNotIn(float... values) {
List<Float> valuesList = new ArrayList<>();
for (float value : values) {
valuesList.add(value);
}
return new IsNotIn(key, valuesList);
}
public Filter isNotIn(double... values) {
return new IsNotIn(key, stream(values).boxed().collect(toList()));
}
public Filter isNotIn(Collection<?> values) {
return new IsNotIn(key, values);
}
} |
List<Float> valuesList = new ArrayList<>();
for (float value : values) {
valuesList.add(value);
}
return new IsIn(key, valuesList);
| 1,850 | 50 | 1,900 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/store/embedding/filter/comparison/IsEqualTo.java | IsEqualTo | test | class IsEqualTo implements Filter {
private final String key;
private final Object comparisonValue;
public IsEqualTo(String key, Object comparisonValue) {
this.key = ensureNotBlank(key, "key");
this.comparisonValue = ensureNotNull(comparisonValue, "comparisonValue with key '" + key + "'");
}
public String key() {
return key;
}
public Object comparisonValue() {
return comparisonValue;
}
@Override
public boolean test(Object object) {<FILL_FUNCTION_BODY>}
} |
if (!(object instanceof Metadata)) {
return false;
}
Metadata metadata = (Metadata) object;
if (!metadata.containsKey(key)) {
return false;
}
Object actualValue = metadata.toMap().get(key);
ensureTypesAreCompatible(actualValue, comparisonValue, key);
if (actualValue instanceof Number) {
return compareAsBigDecimals(actualValue, comparisonValue) == 0;
}
return actualValue.equals(comparisonValue);
| 149 | 132 | 281 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/store/embedding/filter/comparison/IsGreaterThan.java | IsGreaterThan | test | class IsGreaterThan implements Filter {
private final String key;
private final Comparable<?> comparisonValue;
public IsGreaterThan(String key, Comparable<?> comparisonValue) {
this.key = ensureNotBlank(key, "key");
this.comparisonValue = ensureNotNull(comparisonValue, "comparisonValue with key '" + key + "'");
}
public String key() {
return key;
}
public Comparable<?> comparisonValue() {
return comparisonValue;
}
@Override
public boolean test(Object object) {<FILL_FUNCTION_BODY>}
} |
if (!(object instanceof Metadata)) {
return false;
}
Metadata metadata = (Metadata) object;
if (!metadata.containsKey(key)) {
return false;
}
Object actualValue = metadata.toMap().get(key);
ensureTypesAreCompatible(actualValue, comparisonValue, key);
if (actualValue instanceof Number) {
return compareAsBigDecimals(actualValue, comparisonValue) > 0;
}
return ((Comparable) actualValue).compareTo(comparisonValue) > 0;
| 168 | 141 | 309 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/store/embedding/filter/comparison/IsGreaterThanOrEqualTo.java | IsGreaterThanOrEqualTo | test | class IsGreaterThanOrEqualTo implements Filter {
private final String key;
private final Comparable<?> comparisonValue;
public IsGreaterThanOrEqualTo(String key, Comparable<?> comparisonValue) {
this.key = ensureNotBlank(key, "key");
this.comparisonValue = ensureNotNull(comparisonValue, "comparisonValue with key '" + key + "'");
}
public String key() {
return key;
}
public Comparable<?> comparisonValue() {
return comparisonValue;
}
@Override
public boolean test(Object object) {<FILL_FUNCTION_BODY>}
} |
if (!(object instanceof Metadata)) {
return false;
}
Metadata metadata = (Metadata) object;
if (!metadata.containsKey(key)) {
return false;
}
Object actualValue = metadata.toMap().get(key);
ensureTypesAreCompatible(actualValue, comparisonValue, key);
if (actualValue instanceof Number) {
return compareAsBigDecimals(actualValue, comparisonValue) >= 0;
}
return ((Comparable) actualValue).compareTo(comparisonValue) >= 0;
| 174 | 141 | 315 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/store/embedding/filter/comparison/IsIn.java | IsIn | test | class IsIn implements Filter {
private final String key;
private final Collection<?> comparisonValues;
public IsIn(String key, Collection<?> comparisonValues) {
this.key = ensureNotBlank(key, "key");
Set<?> copy = new HashSet<>(ensureNotEmpty(comparisonValues, "comparisonValues with key '" + key + "'"));
comparisonValues.forEach(value -> ensureNotNull(value, "comparisonValue with key '" + key + "'"));
this.comparisonValues = unmodifiableSet(copy);
}
public String key() {
return key;
}
public Collection<?> comparisonValues() {
return comparisonValues;
}
@Override
public boolean test(Object object) {<FILL_FUNCTION_BODY>}
} |
if (!(object instanceof Metadata)) {
return false;
}
Metadata metadata = (Metadata) object;
if (!metadata.containsKey(key)) {
return false;
}
Object actualValue = metadata.toMap().get(key);
ensureTypesAreCompatible(actualValue, comparisonValues.iterator().next(), key);
if (comparisonValues.iterator().next() instanceof Number) {
return containsAsBigDecimals(actualValue, comparisonValues);
}
return comparisonValues.contains(actualValue);
| 204 | 138 | 342 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/store/embedding/filter/comparison/IsLessThan.java | IsLessThan | test | class IsLessThan implements Filter {
private final String key;
private final Comparable<?> comparisonValue;
public IsLessThan(String key, Comparable<?> comparisonValue) {
this.key = ensureNotBlank(key, "key");
this.comparisonValue = ensureNotNull(comparisonValue, "comparisonValue with key '" + key + "'");
}
public String key() {
return key;
}
public Comparable<?> comparisonValue() {
return comparisonValue;
}
@Override
public boolean test(Object object) {<FILL_FUNCTION_BODY>}
} |
if (!(object instanceof Metadata)) {
return false;
}
Metadata metadata = (Metadata) object;
if (!metadata.containsKey(key)) {
return false;
}
Object actualValue = metadata.toMap().get(key);
ensureTypesAreCompatible(actualValue, comparisonValue, key);
if (actualValue instanceof Number) {
return compareAsBigDecimals(actualValue, comparisonValue) < 0;
}
return ((Comparable) actualValue).compareTo(comparisonValue) < 0;
| 168 | 141 | 309 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/store/embedding/filter/comparison/IsLessThanOrEqualTo.java | IsLessThanOrEqualTo | test | class IsLessThanOrEqualTo implements Filter {
private final String key;
private final Comparable<?> comparisonValue;
public IsLessThanOrEqualTo(String key, Comparable<?> comparisonValue) {
this.key = ensureNotBlank(key, "key");
this.comparisonValue = ensureNotNull(comparisonValue, "comparisonValue with key '" + key + "'");
}
public String key() {
return key;
}
public Comparable<?> comparisonValue() {
return comparisonValue;
}
@Override
public boolean test(Object object) {<FILL_FUNCTION_BODY>}
} |
if (!(object instanceof Metadata)) {
return false;
}
Metadata metadata = (Metadata) object;
if (!metadata.containsKey(key)) {
return false;
}
Object actualValue = metadata.toMap().get(key);
ensureTypesAreCompatible(actualValue, comparisonValue, key);
if (actualValue instanceof Number) {
return compareAsBigDecimals(actualValue, comparisonValue) <= 0;
}
return ((Comparable) actualValue).compareTo(comparisonValue) <= 0;
| 174 | 141 | 315 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/store/embedding/filter/comparison/IsNotEqualTo.java | IsNotEqualTo | test | class IsNotEqualTo implements Filter {
private final String key;
private final Object comparisonValue;
public IsNotEqualTo(String key, Object comparisonValue) {
this.key = ensureNotBlank(key, "key");
this.comparisonValue = ensureNotNull(comparisonValue, "comparisonValue with key '" + key + "'");
}
public String key() {
return key;
}
public Object comparisonValue() {
return comparisonValue;
}
@Override
public boolean test(Object object) {<FILL_FUNCTION_BODY>}
} |
if (!(object instanceof Metadata)) {
return false;
}
Metadata metadata = (Metadata) object;
if (!metadata.containsKey(key)) {
return true;
}
Object actualValue = metadata.toMap().get(key);
ensureTypesAreCompatible(actualValue, comparisonValue, key);
if (actualValue instanceof Number) {
return compareAsBigDecimals(actualValue, comparisonValue) != 0;
}
return !actualValue.equals(comparisonValue);
| 151 | 134 | 285 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/store/embedding/filter/comparison/IsNotIn.java | IsNotIn | test | class IsNotIn implements Filter {
private final String key;
private final Collection<?> comparisonValues;
public IsNotIn(String key, Collection<?> comparisonValues) {
this.key = ensureNotBlank(key, "key");
Set<?> copy = new HashSet<>(ensureNotEmpty(comparisonValues, "comparisonValues with key '" + key + "'"));
comparisonValues.forEach(value -> ensureNotNull(value, "comparisonValue with key '" + key + "'"));
this.comparisonValues = unmodifiableSet(copy);
}
public String key() {
return key;
}
public Collection<?> comparisonValues() {
return comparisonValues;
}
@Override
public boolean test(Object object) {<FILL_FUNCTION_BODY>}
} |
if (!(object instanceof Metadata)) {
return false;
}
Metadata metadata = (Metadata) object;
if (!metadata.containsKey(key)) {
return true;
}
Object actualValue = metadata.toMap().get(key);
ensureTypesAreCompatible(actualValue, comparisonValues.iterator().next(), key);
if (comparisonValues.iterator().next() instanceof Number) {
return !containsAsBigDecimals(actualValue, comparisonValues);
}
return !comparisonValues.contains(actualValue);
| 206 | 141 | 347 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/store/embedding/filter/comparison/NumberComparator.java | NumberComparator | toBigDecimal | class NumberComparator {
static int compareAsBigDecimals(Object actualNumber, Object comparisonNumber) {
return new BigDecimal(actualNumber.toString()).compareTo(new BigDecimal(comparisonNumber.toString()));
}
static boolean containsAsBigDecimals(Object actualNumber, Collection<?> comparisonNumbers) {
BigDecimal actualNumberAsBigDecimal = toBigDecimal(actualNumber);
return comparisonNumbers.stream()
.map(NumberComparator::toBigDecimal)
.anyMatch(comparisonNumberAsBigDecimal ->
comparisonNumberAsBigDecimal.compareTo(actualNumberAsBigDecimal) == 0);
}
private static BigDecimal toBigDecimal(Object actualNumber) {<FILL_FUNCTION_BODY>}
} |
if (actualNumber instanceof Integer) {
return BigDecimal.valueOf((int) actualNumber);
} else if (actualNumber instanceof Long) {
return BigDecimal.valueOf((long) actualNumber);
} else if (actualNumber instanceof Float) {
return BigDecimal.valueOf((float) actualNumber);
} else if (actualNumber instanceof Double) {
return BigDecimal.valueOf((double) actualNumber);
}
throw new IllegalArgumentException("Unsupported type: " + actualNumber.getClass().getName());
| 199 | 135 | 334 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-core/src/main/java/dev/langchain4j/store/embedding/filter/comparison/TypeChecker.java | TypeChecker | ensureTypesAreCompatible | class TypeChecker {
static void ensureTypesAreCompatible(Object actualValue, Object comparisonValue, String key) {<FILL_FUNCTION_BODY>}
} |
if (actualValue instanceof Number && comparisonValue instanceof Number) {
return;
}
if (actualValue.getClass() != comparisonValue.getClass()) {
throw illegalArgument(
"Type mismatch: actual value of metadata key \"%s\" (%s) has type %s, " +
"while comparison value (%s) has type %s",
key,
actualValue,
actualValue.getClass().getName(),
comparisonValue,
comparisonValue.getClass().getName()
);
}
| 43 | 133 | 176 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-dashscope/src/main/java/dev/langchain4j/model/dashscope/EnhancedFileTypeDetector.java | EnhancedFileTypeDetector | probeContentType | class EnhancedFileTypeDetector extends FileTypeDetector {
@Override
public String probeContentType(Path path) {<FILL_FUNCTION_BODY>}
} |
try (InputStream in = new BufferedInputStream(
Files.newInputStream(path, StandardOpenOption.READ))) {
return URLConnection.guessContentTypeFromStream(in);
} catch (IOException e) {
return URLConnection.guessContentTypeFromName(path.getFileName().toString());
}
| 45 | 78 | 123 | <methods>public abstract java.lang.String probeContentType(java.nio.file.Path) throws java.io.IOException<variables> |
langchain4j_langchain4j | langchain4j/langchain4j-dashscope/src/main/java/dev/langchain4j/model/dashscope/QwenChatModel.java | QwenChatModel | generateByNonMultimodalModel | class QwenChatModel implements ChatLanguageModel {
private final String apiKey;
private final String modelName;
private final Double topP;
private final Integer topK;
private final Boolean enableSearch;
private final Integer seed;
private final Float repetitionPenalty;
private final Float temperature;
private final List<String> stops;
private final Integer maxTokens;
private final Generation generation;
private final MultiModalConversation conv;
private final boolean isMultimodalModel;
@Builder
protected QwenChatModel(String baseUrl,
String apiKey,
String modelName,
Double topP,
Integer topK,
Boolean enableSearch,
Integer seed,
Float repetitionPenalty,
Float temperature,
List<String> stops,
Integer maxTokens) {
if (Utils.isNullOrBlank(apiKey)) {
throw new IllegalArgumentException("DashScope api key must be defined. It can be generated here: https://dashscope.console.aliyun.com/apiKey");
}
this.modelName = Utils.isNullOrBlank(modelName) ? QwenModelName.QWEN_PLUS : modelName;
this.enableSearch = enableSearch != null && enableSearch;
this.apiKey = apiKey;
this.topP = topP;
this.topK = topK;
this.seed = seed;
this.repetitionPenalty = repetitionPenalty;
this.temperature = temperature;
this.stops = stops;
this.maxTokens = maxTokens;
this.isMultimodalModel = QwenHelper.isMultimodalModel(modelName);
if (Utils.isNullOrBlank(baseUrl)) {
this.conv = isMultimodalModel ? new MultiModalConversation() : null;
this.generation = isMultimodalModel ? null : new Generation();
} else if (baseUrl.startsWith("wss://")) {
this.conv = isMultimodalModel ? new MultiModalConversation(Protocol.WEBSOCKET.getValue(), baseUrl) : null;
this.generation = isMultimodalModel ? null : new Generation(Protocol.WEBSOCKET.getValue(), baseUrl);
} else {
this.conv = isMultimodalModel ? new MultiModalConversation(Protocol.HTTP.getValue(), baseUrl) : null;
this.generation = isMultimodalModel ? null : new Generation(Protocol.HTTP.getValue(), baseUrl);
}
}
@Override
public Response<AiMessage> generate(List<ChatMessage> messages) {
return isMultimodalModel ? generateByMultimodalModel(messages) : generateByNonMultimodalModel(messages);
}
private Response<AiMessage> generateByNonMultimodalModel(List<ChatMessage> messages) {<FILL_FUNCTION_BODY>}
private Response<AiMessage> generateByMultimodalModel(List<ChatMessage> messages) {
try {
MultiModalConversationParam param = MultiModalConversationParam.builder()
.apiKey(apiKey)
.model(modelName)
.topP(topP)
.topK(topK)
.enableSearch(enableSearch)
.seed(seed)
.temperature(temperature)
.maxLength(maxTokens)
.messages(toQwenMultiModalMessages(messages))
.build();
MultiModalConversationResult result = conv.call(param);
String answer = answerFrom(result);
return Response.from(AiMessage.from(answer),
tokenUsageFrom(result), finishReasonFrom(result));
} catch (NoApiKeyException | UploadFileException e) {
throw new RuntimeException(e);
}
}
public static QwenChatModelBuilder builder() {
for (QwenChatModelBuilderFactory factory : loadFactories(QwenChatModelBuilderFactory.class)) {
return factory.get();
}
return new QwenChatModelBuilder();
}
public static class QwenChatModelBuilder {
public QwenChatModelBuilder() {
// This is public so it can be extended
// By default with Lombok it becomes package private
}
}
} |
try {
QwenParam.QwenParamBuilder<?, ?> builder = QwenParam.builder()
.apiKey(apiKey)
.model(modelName)
.topP(topP)
.topK(topK)
.enableSearch(enableSearch)
.seed(seed)
.repetitionPenalty(repetitionPenalty)
.temperature(temperature)
.maxTokens(maxTokens)
.messages(toQwenMessages(messages))
.resultFormat(MESSAGE);
if (stops != null) {
builder.stopStrings(stops);
}
GenerationResult generationResult = generation.call(builder.build());
String answer = answerFrom(generationResult);
return Response.from(AiMessage.from(answer),
tokenUsageFrom(generationResult), finishReasonFrom(generationResult));
} catch (NoApiKeyException | InputRequiredException e) {
throw new RuntimeException(e);
}
| 1,080 | 254 | 1,334 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-dashscope/src/main/java/dev/langchain4j/model/dashscope/QwenEmbeddingModel.java | QwenEmbeddingModel | embedAll | class QwenEmbeddingModel implements EmbeddingModel {
public static final String TYPE_KEY = "type";
public static final String TYPE_QUERY = "query";
public static final String TYPE_DOCUMENT = "document";
private final String apiKey;
private final String modelName;
private final TextEmbedding embedding;
@Builder
public QwenEmbeddingModel(String apiKey, String modelName) {
if (Utils.isNullOrBlank(apiKey)) {
throw new IllegalArgumentException("DashScope api key must be defined. It can be generated here: https://dashscope.console.aliyun.com/apiKey");
}
this.modelName = Utils.isNullOrBlank(modelName) ? QwenModelName.TEXT_EMBEDDING_V2 : modelName;
this.apiKey = apiKey;
this.embedding = new TextEmbedding();
}
private boolean containsDocuments(List<TextSegment> textSegments) {
return textSegments.stream()
.map(TextSegment::metadata)
.map(metadata -> metadata.get(TYPE_KEY))
.anyMatch(TYPE_DOCUMENT::equalsIgnoreCase);
}
private boolean containsQueries(List<TextSegment> textSegments) {
return textSegments.stream()
.map(TextSegment::metadata)
.map(metadata -> metadata.get(TYPE_KEY))
.anyMatch(TYPE_QUERY::equalsIgnoreCase);
}
private Response<List<Embedding>> embedTexts(List<TextSegment> textSegments, TextEmbeddingParam.TextType textType) {
TextEmbeddingParam param = TextEmbeddingParam.builder()
.apiKey(apiKey)
.model(modelName)
.textType(textType)
.texts(textSegments.stream()
.map(TextSegment::text)
.collect(Collectors.toList()))
.build();
try {
TextEmbeddingResult generationResult = embedding.call(param);
// total_tokens are the same as input_tokens in the embedding model
TokenUsage usage = new TokenUsage(generationResult.getUsage().getTotalTokens());
List<Embedding> embeddings = Optional.of(generationResult)
.map(TextEmbeddingResult::getOutput)
.map(TextEmbeddingOutput::getEmbeddings)
.orElse(Collections.emptyList())
.stream()
.sorted(Comparator.comparing(TextEmbeddingResultItem::getTextIndex))
.map(TextEmbeddingResultItem::getEmbedding)
.map(doubleList -> doubleList.stream().map(Double::floatValue).collect(Collectors.toList()))
.map(Embedding::from)
.collect(Collectors.toList());
return Response.from(embeddings, usage);
} catch (NoApiKeyException e) {
throw new RuntimeException(e);
}
}
@Override
public Response<List<Embedding>> embedAll(List<TextSegment> textSegments) {<FILL_FUNCTION_BODY>}
public static QwenEmbeddingModelBuilder builder() {
for (QwenEmbeddingModelBuilderFactory factory : loadFactories(QwenEmbeddingModelBuilderFactory.class)) {
return factory.get();
}
return new QwenEmbeddingModelBuilder();
}
public static class QwenEmbeddingModelBuilder {
public QwenEmbeddingModelBuilder() {
// This is public so it can be extended
// By default with Lombok it becomes package private
}
}
} |
boolean queries = containsQueries(textSegments);
if (!queries) {
// default all documents
return embedTexts(textSegments, DOCUMENT);
} else {
boolean documents = containsDocuments(textSegments);
if (!documents) {
return embedTexts(textSegments, QUERY);
} else {
// This is a mixed collection of queries and documents. Embed one by one.
List<Embedding> embeddings = new ArrayList<>(textSegments.size());
Integer tokens = null;
for (TextSegment textSegment : textSegments) {
Response<List<Embedding>> result;
if (TYPE_QUERY.equalsIgnoreCase(textSegment.metadata(TYPE_KEY))) {
result = embedTexts(singletonList(textSegment), QUERY);
} else {
result = embedTexts(singletonList(textSegment), DOCUMENT);
}
embeddings.addAll(result.content());
if (result.tokenUsage() == null) {
continue;
}
if (tokens == null) {
tokens = result.tokenUsage().inputTokenCount();
} else {
tokens += result.tokenUsage().inputTokenCount();
}
}
return Response.from(embeddings, new TokenUsage(tokens));
}
}
| 922 | 336 | 1,258 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-dashscope/src/main/java/dev/langchain4j/model/dashscope/QwenLanguageModel.java | QwenLanguageModel | generate | class QwenLanguageModel implements LanguageModel {
private final String apiKey;
private final String modelName;
private final Double topP;
private final Integer topK;
private final Boolean enableSearch;
private final Integer seed;
private final Float repetitionPenalty;
private final Float temperature;
private final List<String> stops;
private final Integer maxTokens;
private final Generation generation;
@Builder
public QwenLanguageModel(String baseUrl,
String apiKey,
String modelName,
Double topP,
Integer topK,
Boolean enableSearch,
Integer seed,
Float repetitionPenalty,
Float temperature,
List<String> stops,
Integer maxTokens) {
if (isNullOrBlank(apiKey)) {
throw new IllegalArgumentException("DashScope api key must be defined. It can be generated here: https://dashscope.console.aliyun.com/apiKey");
}
this.modelName = isNullOrBlank(modelName) ? QWEN_PLUS : modelName;
this.enableSearch = enableSearch != null && enableSearch;
this.apiKey = apiKey;
this.topP = topP;
this.topK = topK;
this.seed = seed;
this.repetitionPenalty = repetitionPenalty;
this.temperature = temperature;
this.stops = stops;
this.maxTokens = maxTokens;
if (Utils.isNullOrBlank(baseUrl)) {
this.generation = new Generation();
} else if (baseUrl.startsWith("wss://")) {
this.generation = new Generation(Protocol.WEBSOCKET.getValue(), baseUrl);
} else {
this.generation = new Generation(Protocol.HTTP.getValue(), baseUrl);
}
}
@Override
public Response<String> generate(String prompt) {<FILL_FUNCTION_BODY>}
public static QwenLanguageModelBuilder builder() {
for (QwenLanguageModelBuilderFactory factory : loadFactories(QwenLanguageModelBuilderFactory.class)) {
return factory.get();
}
return new QwenLanguageModelBuilder();
}
public static class QwenLanguageModelBuilder {
public QwenLanguageModelBuilder() {
// This is public so it can be extended
// By default with Lombok it becomes package private
}
}
} |
try {
QwenParam.QwenParamBuilder<?, ?> builder = QwenParam.builder()
.apiKey(apiKey)
.model(modelName)
.topP(topP)
.topK(topK)
.enableSearch(enableSearch)
.seed(seed)
.repetitionPenalty(repetitionPenalty)
.temperature(temperature)
.maxTokens(maxTokens)
.prompt(prompt)
.resultFormat(MESSAGE);
if (stops != null) {
builder.stopStrings(stops);
}
GenerationResult generationResult = generation.call(builder.build());
return Response.from(answerFrom(generationResult),
tokenUsageFrom(generationResult), finishReasonFrom(generationResult));
} catch (NoApiKeyException | InputRequiredException e) {
throw new RuntimeException(e);
}
| 617 | 238 | 855 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-dashscope/src/main/java/dev/langchain4j/model/dashscope/QwenStreamingChatModel.java | QwenStreamingChatModel | generateByMultimodalModel | class QwenStreamingChatModel implements StreamingChatLanguageModel {
private final String apiKey;
private final String modelName;
private final Double topP;
private final Integer topK;
private final Boolean enableSearch;
private final Integer seed;
private final Float repetitionPenalty;
private final Float temperature;
private final List<String> stops;
private final Integer maxTokens;
private final Generation generation;
private final MultiModalConversation conv;
private final boolean isMultimodalModel;
@Builder
public QwenStreamingChatModel(String baseUrl,
String apiKey,
String modelName,
Double topP,
Integer topK,
Boolean enableSearch,
Integer seed,
Float repetitionPenalty,
Float temperature,
List<String> stops,
Integer maxTokens) {
if (Utils.isNullOrBlank(apiKey)) {
throw new IllegalArgumentException("DashScope api key must be defined. It can be generated here: https://dashscope.console.aliyun.com/apiKey");
}
this.modelName = Utils.isNullOrBlank(modelName) ? QwenModelName.QWEN_PLUS : modelName;
this.enableSearch = enableSearch != null && enableSearch;
this.apiKey = apiKey;
this.topP = topP;
this.topK = topK;
this.seed = seed;
this.repetitionPenalty = repetitionPenalty;
this.temperature = temperature;
this.stops = stops;
this.maxTokens = maxTokens;
this.isMultimodalModel = QwenHelper.isMultimodalModel(modelName);
if (Utils.isNullOrBlank(baseUrl)) {
this.conv = isMultimodalModel ? new MultiModalConversation() : null;
this.generation = isMultimodalModel ? null : new Generation();
} else if (baseUrl.startsWith("wss://")) {
this.conv = isMultimodalModel ? new MultiModalConversation(Protocol.WEBSOCKET.getValue(), baseUrl) : null;
this.generation = isMultimodalModel ? null : new Generation(Protocol.WEBSOCKET.getValue(), baseUrl);
} else {
this.conv = isMultimodalModel ? new MultiModalConversation(Protocol.HTTP.getValue(), baseUrl) : null;
this.generation = isMultimodalModel ? null : new Generation(Protocol.HTTP.getValue(), baseUrl);
}
}
@Override
public void generate(List<ChatMessage> messages, StreamingResponseHandler<AiMessage> handler) {
if (isMultimodalModel) {
generateByMultimodalModel(messages, handler);
} else {
generateByNonMultimodalModel(messages, handler);
}
}
private void generateByNonMultimodalModel(List<ChatMessage> messages, StreamingResponseHandler<AiMessage> handler) {
try {
QwenParam.QwenParamBuilder<?, ?> builder = QwenParam.builder()
.apiKey(apiKey)
.model(modelName)
.topP(topP)
.topK(topK)
.enableSearch(enableSearch)
.seed(seed)
.repetitionPenalty(repetitionPenalty)
.temperature(temperature)
.maxTokens(maxTokens)
.incrementalOutput(true)
.messages(toQwenMessages(messages))
.resultFormat(MESSAGE);
if (stops != null) {
builder.stopStrings(stops);
}
QwenStreamingResponseBuilder responseBuilder = new QwenStreamingResponseBuilder();
generation.streamCall(builder.build(), new ResultCallback<GenerationResult>() {
@Override
public void onEvent(GenerationResult result) {
String delta = responseBuilder.append(result);
if (Utils.isNotNullOrBlank(delta)) {
handler.onNext(delta);
}
}
@Override
public void onComplete() {
handler.onComplete(responseBuilder.build());
}
@Override
public void onError(Exception e) {
handler.onError(e);
}
});
} catch (NoApiKeyException | InputRequiredException e) {
throw new RuntimeException(e);
}
}
private void generateByMultimodalModel(List<ChatMessage> messages, StreamingResponseHandler<AiMessage> handler) {<FILL_FUNCTION_BODY>}
public static QwenStreamingChatModelBuilder builder() {
for (QwenStreamingChatModelBuilderFactory factory : loadFactories(QwenStreamingChatModelBuilderFactory.class)) {
return factory.get();
}
return new QwenStreamingChatModelBuilder();
}
public static class QwenStreamingChatModelBuilder {
public QwenStreamingChatModelBuilder() {
// This is public so it can be extended
// By default with Lombok it becomes package private
}
}
} |
try {
MultiModalConversationParam param = MultiModalConversationParam.builder()
.apiKey(apiKey)
.model(modelName)
.topP(topP)
.topK(topK)
.enableSearch(enableSearch)
.seed(seed)
.temperature(temperature)
.maxLength(maxTokens)
.incrementalOutput(true)
.messages(toQwenMultiModalMessages(messages))
.build();
QwenStreamingResponseBuilder responseBuilder = new QwenStreamingResponseBuilder();
conv.streamCall(param, new ResultCallback<MultiModalConversationResult>() {
@Override
public void onEvent(MultiModalConversationResult result) {
String delta = responseBuilder.append(result);
if (Utils.isNotNullOrBlank(delta)) {
handler.onNext(delta);
}
}
@Override
public void onComplete() {
handler.onComplete(responseBuilder.build());
}
@Override
public void onError(Exception e) {
handler.onError(e);
}
});
} catch (NoApiKeyException | UploadFileException | InputRequiredException e) {
throw new RuntimeException(e);
}
| 1,298 | 316 | 1,614 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-dashscope/src/main/java/dev/langchain4j/model/dashscope/QwenStreamingLanguageModel.java | QwenStreamingLanguageModel | generate | class QwenStreamingLanguageModel implements StreamingLanguageModel {
private final String apiKey;
private final String modelName;
private final Double topP;
private final Integer topK;
private final Boolean enableSearch;
private final Integer seed;
private final Float repetitionPenalty;
private final Float temperature;
private final List<String> stops;
private final Integer maxTokens;
private final Generation generation;
@Builder
public QwenStreamingLanguageModel(String baseUrl,
String apiKey,
String modelName,
Double topP,
Integer topK,
Boolean enableSearch,
Integer seed,
Float repetitionPenalty,
Float temperature,
List<String> stops,
Integer maxTokens) {
if (isNullOrBlank(apiKey)) {
throw new IllegalArgumentException("DashScope api key must be defined. It can be generated here: https://dashscope.console.aliyun.com/apiKey");
}
this.modelName = isNullOrBlank(modelName) ? QWEN_PLUS : modelName;
this.enableSearch = enableSearch != null && enableSearch;
this.apiKey = apiKey;
this.topP = topP;
this.topK = topK;
this.seed = seed;
this.repetitionPenalty = repetitionPenalty;
this.temperature = temperature;
this.stops = stops;
this.maxTokens = maxTokens;
if (Utils.isNullOrBlank(baseUrl)) {
this.generation = new Generation();
} else if (baseUrl.startsWith("wss://")) {
this.generation = new Generation(Protocol.WEBSOCKET.getValue(), baseUrl);
} else {
this.generation = new Generation(Protocol.HTTP.getValue(), baseUrl);
}
}
@Override
public void generate(String prompt, StreamingResponseHandler<String> handler) {<FILL_FUNCTION_BODY>}
public static QwenStreamingLanguageModelBuilder builder() {
for (QwenStreamingLanguageModelBuilderFactory factory : loadFactories(QwenStreamingLanguageModelBuilderFactory.class)) {
return factory.get();
}
return new QwenStreamingLanguageModelBuilder();
}
public static class QwenStreamingLanguageModelBuilder {
public QwenStreamingLanguageModelBuilder() {
// This is public so it can be extended
// By default with Lombok it becomes package private
}
}
} |
try {
QwenParam.QwenParamBuilder<?, ?> builder = QwenParam.builder()
.apiKey(apiKey)
.model(modelName)
.topP(topP)
.topK(topK)
.enableSearch(enableSearch)
.seed(seed)
.repetitionPenalty(repetitionPenalty)
.temperature(temperature)
.maxTokens(maxTokens)
.incrementalOutput(true)
.prompt(prompt)
.resultFormat(MESSAGE);
if (stops != null) {
builder.stopStrings(stops);
}
QwenStreamingResponseBuilder responseBuilder = new QwenStreamingResponseBuilder();
generation.streamCall(builder.build(), new ResultCallback<GenerationResult>() {
@Override
public void onEvent(GenerationResult result) {
String delta = responseBuilder.append(result);
if (Utils.isNotNullOrBlank(delta)) {
handler.onNext(delta);
}
}
@Override
public void onComplete() {
Response<AiMessage> response = responseBuilder.build();
handler.onComplete(Response.from(
response.content().text(),
response.tokenUsage(),
response.finishReason()
));
}
@Override
public void onError(Exception e) {
handler.onError(e);
}
});
} catch (NoApiKeyException | InputRequiredException e) {
throw new RuntimeException(e);
}
| 641 | 399 | 1,040 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-dashscope/src/main/java/dev/langchain4j/model/dashscope/QwenStreamingResponseBuilder.java | QwenStreamingResponseBuilder | append | class QwenStreamingResponseBuilder {
private final StringBuilder generatedContent = new StringBuilder();
private Integer inputTokenCount;
private Integer outputTokenCount;
private FinishReason finishReason;
public QwenStreamingResponseBuilder() {}
public String append(GenerationResult partialResponse) {
if (partialResponse == null) {
return null;
}
GenerationUsage usage = partialResponse.getUsage();
if (usage != null) {
inputTokenCount = usage.getInputTokens();
outputTokenCount = usage.getOutputTokens();
}
FinishReason finishReason = finishReasonFrom(partialResponse);
if (finishReason != null) {
this.finishReason = finishReason;
if (!hasAnswer(partialResponse)) {
return null;
}
}
String partialContent = answerFrom(partialResponse);
generatedContent.append(partialContent);
return partialContent;
}
public String append(MultiModalConversationResult partialResponse) {<FILL_FUNCTION_BODY>}
public Response<AiMessage> build() {
return Response.from(
AiMessage.from(generatedContent.toString()),
new TokenUsage(inputTokenCount, outputTokenCount),
finishReason
);
}
} |
if (partialResponse == null) {
return null;
}
MultiModalConversationUsage usage = partialResponse.getUsage();
if (usage != null) {
inputTokenCount = usage.getInputTokens();
outputTokenCount = usage.getOutputTokens();
}
FinishReason finishReason = finishReasonFrom(partialResponse);
if (finishReason != null) {
this.finishReason = finishReason;
if (!hasAnswer(partialResponse)) {
return null;
}
}
String partialContent = answerFrom(partialResponse);
generatedContent.append(partialContent);
return partialContent;
| 330 | 168 | 498 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-dashscope/src/main/java/dev/langchain4j/model/dashscope/QwenTokenizer.java | QwenTokenizer | estimateTokenCountInText | class QwenTokenizer implements Tokenizer {
private final String apiKey;
private final String modelName;
private final Tokenization tokenizer;
public QwenTokenizer(String apiKey, String modelName) {
if (isNullOrBlank(apiKey)) {
throw new IllegalArgumentException("DashScope api key must be defined. It can be generated here: https://dashscope.console.aliyun.com/apiKey");
}
this.apiKey = apiKey;
this.modelName = getOrDefault(modelName, QWEN_PLUS);
this.tokenizer = new Tokenization();
}
@Override
public int estimateTokenCountInText(String text) {<FILL_FUNCTION_BODY>}
@Override
public int estimateTokenCountInMessage(ChatMessage message) {
return estimateTokenCountInMessages(Collections.singleton(message));
}
@Override
public int estimateTokenCountInMessages(Iterable<ChatMessage> messages) {
try {
QwenParam param = QwenParam.builder()
.apiKey(apiKey)
.model(modelName)
.messages(toQwenMessages(messages))
.build();
TokenizationResult result = tokenizer.call(param);
return result.getUsage().getInputTokens();
} catch (NoApiKeyException | InputRequiredException e) {
throw new RuntimeException(e);
}
}
@Override
public int estimateTokenCountInToolSpecifications(Iterable<ToolSpecification> toolSpecifications) {
throw new IllegalArgumentException("Tools are currently not supported by this tokenizer");
}
@Override
public int estimateTokenCountInToolExecutionRequests(Iterable<ToolExecutionRequest> toolExecutionRequests) {
throw new IllegalArgumentException("Tools are currently not supported by this tokenizer");
}
} |
try {
QwenParam param = QwenParam.builder()
.apiKey(apiKey)
.model(modelName)
.prompt(text)
.build();
TokenizationResult result = tokenizer.call(param);
return result.getUsage().getInputTokens();
} catch (NoApiKeyException | InputRequiredException e) {
throw new RuntimeException(e);
}
| 464 | 107 | 571 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-elasticsearch/src/main/java/dev/langchain4j/store/embedding/elasticsearch/ElasticsearchMetadataFilterMapper.java | ElasticsearchMetadataFilterMapper | map | class ElasticsearchMetadataFilterMapper {
static Query map(Filter filter) {<FILL_FUNCTION_BODY>}
private static Query mapEqual(IsEqualTo isEqualTo) {
return new Query.Builder().bool(b -> b.filter(f -> f.term(t ->
t.field(formatKey(isEqualTo.key(), isEqualTo.comparisonValue()))
.value(v -> v.anyValue(JsonData.of(isEqualTo.comparisonValue())))
))).build();
}
private static Query mapNotEqual(IsNotEqualTo isNotEqualTo) {
return new Query.Builder().bool(b -> b.mustNot(mn -> mn.term(t ->
t.field(formatKey(isNotEqualTo.key(), isNotEqualTo.comparisonValue()))
.value(v -> v.anyValue(JsonData.of(isNotEqualTo.comparisonValue())))
))).build();
}
private static Query mapGreaterThan(IsGreaterThan isGreaterThan) {
return new Query.Builder().bool(b -> b.filter(f -> f.range(r ->
r.field("metadata." + isGreaterThan.key())
.gt(JsonData.of(isGreaterThan.comparisonValue()))
))).build();
}
private static Query mapGreaterThanOrEqual(IsGreaterThanOrEqualTo isGreaterThanOrEqualTo) {
return new Query.Builder().bool(b -> b.filter(f -> f.range(r ->
r.field("metadata." + isGreaterThanOrEqualTo.key())
.gte(JsonData.of(isGreaterThanOrEqualTo.comparisonValue()))
))).build();
}
private static Query mapLessThan(IsLessThan isLessThan) {
return new Query.Builder().bool(b -> b.filter(f -> f.range(r ->
r.field("metadata." + isLessThan.key())
.lt(JsonData.of(isLessThan.comparisonValue()))
))).build();
}
private static Query mapLessThanOrEqual(IsLessThanOrEqualTo isLessThanOrEqualTo) {
return new Query.Builder().bool(b -> b.filter(f -> f.range(r ->
r.field("metadata." + isLessThanOrEqualTo.key())
.lte(JsonData.of(isLessThanOrEqualTo.comparisonValue()))
))).build();
}
public static Query mapIn(IsIn isIn) {
return new Query.Builder().bool(b -> b.filter(f -> f.terms(t ->
t.field(formatKey(isIn.key(), isIn.comparisonValues()))
.terms(terms -> {
List<FieldValue> values = isIn.comparisonValues().stream()
.map(it -> FieldValue.of(JsonData.of(it)))
.collect(toList());
return terms.value(values);
})
))).build();
}
public static Query mapNotIn(IsNotIn isNotIn) {
return new Query.Builder().bool(b -> b.mustNot(mn -> mn.terms(t ->
t.field(formatKey(isNotIn.key(), isNotIn.comparisonValues()))
.terms(terms -> {
List<FieldValue> values = isNotIn.comparisonValues().stream()
.map(it -> FieldValue.of(JsonData.of(it)))
.collect(toList());
return terms.value(values);
})
))).build();
}
private static Query mapAnd(And and) {
BoolQuery boolQuery = new BoolQuery.Builder()
.must(map(and.left()))
.must(map(and.right()))
.build();
return new Query.Builder().bool(boolQuery).build();
}
private static Query mapNot(Not not) {
BoolQuery boolQuery = new BoolQuery.Builder()
.mustNot(map(not.expression()))
.build();
return new Query.Builder().bool(boolQuery).build();
}
private static Query mapOr(Or or) {
BoolQuery boolQuery = new BoolQuery.Builder()
.should(map(or.left()))
.should(map(or.right()))
.build();
return new Query.Builder().bool(boolQuery).build();
}
private static String formatKey(String key, Object comparisonValue) {
if (comparisonValue instanceof String) {
return "metadata." + key + ".keyword";
} else {
return "metadata." + key;
}
}
private static String formatKey(String key, Collection<?> comparisonValues) {
if (comparisonValues.iterator().next() instanceof String) {
return "metadata." + key + ".keyword";
} else {
return "metadata." + key;
}
}
} |
if (filter instanceof IsEqualTo) {
return mapEqual((IsEqualTo) filter);
} else if (filter instanceof IsNotEqualTo) {
return mapNotEqual((IsNotEqualTo) filter);
} else if (filter instanceof IsGreaterThan) {
return mapGreaterThan((IsGreaterThan) filter);
} else if (filter instanceof IsGreaterThanOrEqualTo) {
return mapGreaterThanOrEqual((IsGreaterThanOrEqualTo) filter);
} else if (filter instanceof IsLessThan) {
return mapLessThan((IsLessThan) filter);
} else if (filter instanceof IsLessThanOrEqualTo) {
return mapLessThanOrEqual((IsLessThanOrEqualTo) filter);
} else if (filter instanceof IsIn) {
return mapIn((IsIn) filter);
} else if (filter instanceof IsNotIn) {
return mapNotIn((IsNotIn) filter);
} else if (filter instanceof And) {
return mapAnd((And) filter);
} else if (filter instanceof Not) {
return mapNot((Not) filter);
} else if (filter instanceof Or) {
return mapOr((Or) filter);
} else {
throw new UnsupportedOperationException("Unsupported filter type: " + filter.getClass().getName());
}
| 1,297 | 341 | 1,638 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-hugging-face/src/main/java/dev/langchain4j/model/huggingface/ApiKeyInsertingInterceptor.java | ApiKeyInsertingInterceptor | intercept | class ApiKeyInsertingInterceptor implements Interceptor {
private final String apiKey;
ApiKeyInsertingInterceptor(String apiKey) {
this.apiKey = ensureNotBlank(apiKey, "apiKey");
}
@Override
public Response intercept(Chain chain) throws IOException {<FILL_FUNCTION_BODY>}
} |
Request request = chain.request()
.newBuilder()
.addHeader("Authorization", "Bearer " + apiKey)
.build();
return chain.proceed(request);
| 95 | 52 | 147 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-hugging-face/src/main/java/dev/langchain4j/model/huggingface/DefaultHuggingFaceClient.java | DefaultHuggingFaceClient | toException | class DefaultHuggingFaceClient implements HuggingFaceClient {
private final HuggingFaceApi huggingFaceApi;
private final String modelId;
DefaultHuggingFaceClient(String apiKey, String modelId, Duration timeout) {
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(new ApiKeyInsertingInterceptor(apiKey))
.callTimeout(timeout)
.connectTimeout(timeout)
.readTimeout(timeout)
.writeTimeout(timeout)
.build();
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(LOWER_CASE_WITH_UNDERSCORES)
.create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://api-inference.huggingface.co")
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
this.huggingFaceApi = retrofit.create(HuggingFaceApi.class);
this.modelId = ensureNotBlank(modelId, "modelId");
}
@Override
public TextGenerationResponse chat(TextGenerationRequest request) {
return generate(request);
}
@Override
public TextGenerationResponse generate(TextGenerationRequest request) {
try {
retrofit2.Response<List<TextGenerationResponse>> retrofitResponse
= huggingFaceApi.generate(request, modelId).execute();
if (retrofitResponse.isSuccessful()) {
return toOneResponse(retrofitResponse);
} else {
throw toException(retrofitResponse);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static TextGenerationResponse toOneResponse(Response<List<TextGenerationResponse>> retrofitResponse) {
List<TextGenerationResponse> responses = retrofitResponse.body();
if (responses != null && responses.size() == 1) {
return responses.get(0);
} else {
throw new RuntimeException("Expected only one generated_text, but was: " + (responses == null ? 0 : responses.size()));
}
}
@Override
public List<float[]> embed(EmbeddingRequest request) {
try {
retrofit2.Response<List<float[]>> retrofitResponse = huggingFaceApi.embed(request, modelId).execute();
if (retrofitResponse.isSuccessful()) {
return retrofitResponse.body();
} else {
throw toException(retrofitResponse);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static RuntimeException toException(retrofit2.Response<?> response) throws IOException {<FILL_FUNCTION_BODY>}
} |
int code = response.code();
String body = response.errorBody().string();
String errorMessage = String.format("status code: %s; body: %s", code, body);
return new RuntimeException(errorMessage);
| 723 | 61 | 784 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-hugging-face/src/main/java/dev/langchain4j/model/huggingface/FactoryCreator.java | FactoryCreator | factory | class FactoryCreator {
static final HuggingFaceClientFactory FACTORY = factory();
private static HuggingFaceClientFactory factory() {<FILL_FUNCTION_BODY>}
static class DefaultHuggingFaceClientFactory implements HuggingFaceClientFactory {
@Override
public HuggingFaceClient create(Input input) {
return new DefaultHuggingFaceClient(input.apiKey(), input.modelId(), input.timeout());
}
}
} |
for (HuggingFaceClientFactory factory : loadFactories(HuggingFaceClientFactory.class)) {
return factory;
}
return new DefaultHuggingFaceClientFactory();
| 117 | 49 | 166 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-hugging-face/src/main/java/dev/langchain4j/model/huggingface/HuggingFaceChatModel.java | HuggingFaceChatModel | generate | class HuggingFaceChatModel implements ChatLanguageModel {
private final HuggingFaceClient client;
private final Double temperature;
private final Integer maxNewTokens;
private final Boolean returnFullText;
private final Boolean waitForModel;
public HuggingFaceChatModel(String accessToken,
String modelId,
Duration timeout,
Double temperature,
Integer maxNewTokens,
Boolean returnFullText,
Boolean waitForModel) {
this(HuggingFaceChatModel.builder()
.accessToken(accessToken)
.modelId(modelId)
.timeout(timeout)
.temperature(temperature)
.maxNewTokens(maxNewTokens)
.returnFullText(returnFullText)
.waitForModel(waitForModel));
}
public HuggingFaceChatModel(Builder builder) {
this.client = FactoryCreator.FACTORY.create(new HuggingFaceClientFactory.Input() {
@Override
public String apiKey() {
return builder.accessToken;
}
@Override
public String modelId() {
return builder.modelId;
}
@Override
public Duration timeout() {
return builder.timeout;
}
});
this.temperature = builder.temperature;
this.maxNewTokens = builder.maxNewTokens;
this.returnFullText = builder.returnFullText;
this.waitForModel = builder.waitForModel;
}
@Override
public Response<AiMessage> generate(List<ChatMessage> messages) {<FILL_FUNCTION_BODY>}
public static Builder builder() {
for (HuggingFaceChatModelBuilderFactory factory : loadFactories(HuggingFaceChatModelBuilderFactory.class)) {
return factory.get();
}
return new Builder();
}
public static final class Builder {
private String accessToken;
private String modelId = TII_UAE_FALCON_7B_INSTRUCT;
private Duration timeout = Duration.ofSeconds(15);
private Double temperature;
private Integer maxNewTokens;
private Boolean returnFullText = false;
private Boolean waitForModel = true;
public Builder accessToken(String accessToken) {
this.accessToken = accessToken;
return this;
}
public Builder modelId(String modelId) {
if (modelId != null) {
this.modelId = modelId;
}
return this;
}
public Builder timeout(Duration timeout) {
if (timeout != null) {
this.timeout = timeout;
}
return this;
}
public Builder temperature(Double temperature) {
this.temperature = temperature;
return this;
}
public Builder maxNewTokens(Integer maxNewTokens) {
this.maxNewTokens = maxNewTokens;
return this;
}
public Builder returnFullText(Boolean returnFullText) {
if (returnFullText != null) {
this.returnFullText = returnFullText;
}
return this;
}
public Builder waitForModel(Boolean waitForModel) {
if (waitForModel != null) {
this.waitForModel = waitForModel;
}
return this;
}
public HuggingFaceChatModel build() {
if (isNullOrBlank(accessToken)) {
throw new IllegalArgumentException("HuggingFace access token must be defined. It can be generated here: https://huggingface.co/settings/tokens");
}
return new HuggingFaceChatModel(this);
}
}
public static HuggingFaceChatModel withAccessToken(String accessToken) {
return builder().accessToken(accessToken).build();
}
} |
TextGenerationRequest request = TextGenerationRequest.builder()
.inputs(messages.stream()
.map(ChatMessage::text)
.collect(joining("\n")))
.parameters(Parameters.builder()
.temperature(temperature)
.maxNewTokens(maxNewTokens)
.returnFullText(returnFullText)
.build())
.options(Options.builder()
.waitForModel(waitForModel)
.build())
.build();
TextGenerationResponse textGenerationResponse = client.chat(request);
return Response.from(AiMessage.from(textGenerationResponse.generatedText()));
| 956 | 165 | 1,121 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-hugging-face/src/main/java/dev/langchain4j/model/huggingface/HuggingFaceEmbeddingModel.java | HuggingFaceEmbeddingModel | embedTexts | class HuggingFaceEmbeddingModel implements EmbeddingModel {
private static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(15);
private final HuggingFaceClient client;
private final boolean waitForModel;
@Builder
public HuggingFaceEmbeddingModel(String accessToken, String modelId, Boolean waitForModel, Duration timeout) {
if (accessToken == null || accessToken.trim().isEmpty()) {
throw new IllegalArgumentException("HuggingFace access token must be defined. It can be generated here: https://huggingface.co/settings/tokens");
}
this.client = FactoryCreator.FACTORY.create(new HuggingFaceClientFactory.Input() {
@Override
public String apiKey() {
return accessToken;
}
@Override
public String modelId() {
return modelId == null ? SENTENCE_TRANSFORMERS_ALL_MINI_LM_L6_V2 : modelId;
}
@Override
public Duration timeout() {
return timeout == null ? DEFAULT_TIMEOUT : timeout;
}
});
this.waitForModel = waitForModel == null || waitForModel;
}
@Override
public Response<List<Embedding>> embedAll(List<TextSegment> textSegments) {
List<String> texts = textSegments.stream()
.map(TextSegment::text)
.collect(toList());
return embedTexts(texts);
}
private Response<List<Embedding>> embedTexts(List<String> texts) {<FILL_FUNCTION_BODY>}
public static HuggingFaceEmbeddingModel withAccessToken(String accessToken) {
return builder().accessToken(accessToken).build();
}
public static HuggingFaceEmbeddingModelBuilder builder() {
for (HuggingFaceEmbeddingModelBuilderFactory factory : loadFactories(HuggingFaceEmbeddingModelBuilderFactory.class)) {
return factory.get();
}
return new HuggingFaceEmbeddingModelBuilder();
}
public static class HuggingFaceEmbeddingModelBuilder {
public HuggingFaceEmbeddingModelBuilder() {
// This is public so it can be extended
// By default with Lombok it becomes package private
}
}
} |
EmbeddingRequest request = new EmbeddingRequest(texts, waitForModel);
List<float[]> response = client.embed(request);
List<Embedding> embeddings = response.stream()
.map(Embedding::from)
.collect(toList());
return Response.from(embeddings);
| 582 | 85 | 667 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-hugging-face/src/main/java/dev/langchain4j/model/huggingface/HuggingFaceLanguageModel.java | Builder | build | class Builder {
private String accessToken;
private String modelId = TII_UAE_FALCON_7B_INSTRUCT;
private Duration timeout = Duration.ofSeconds(15);
private Double temperature;
private Integer maxNewTokens;
private Boolean returnFullText = false;
private Boolean waitForModel = true;
public Builder accessToken(String accessToken) {
this.accessToken = accessToken;
return this;
}
public Builder modelId(String modelId) {
if (modelId != null) {
this.modelId = modelId;
}
return this;
}
public Builder timeout(Duration timeout) {
if (timeout != null) {
this.timeout = timeout;
}
return this;
}
public Builder temperature(Double temperature) {
this.temperature = temperature;
return this;
}
public Builder maxNewTokens(Integer maxNewTokens) {
this.maxNewTokens = maxNewTokens;
return this;
}
public Builder returnFullText(Boolean returnFullText) {
if (returnFullText != null) {
this.returnFullText = returnFullText;
}
return this;
}
public Builder waitForModel(Boolean waitForModel) {
if (waitForModel != null) {
this.waitForModel = waitForModel;
}
return this;
}
public HuggingFaceLanguageModel build() {<FILL_FUNCTION_BODY>}
} |
if (accessToken == null || accessToken.trim().isEmpty()) {
throw new IllegalArgumentException("HuggingFace access token must be defined. It can be generated here: https://huggingface.co/settings/tokens");
}
return new HuggingFaceLanguageModel(this);
| 398 | 72 | 470 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-hugging-face/src/main/java/dev/langchain4j/model/huggingface/client/Options.java | Builder | waitForModel | class Builder {
private Boolean waitForModel = true;
private Boolean useCache;
public Builder waitForModel(Boolean waitForModel) {<FILL_FUNCTION_BODY>}
public Builder useCache(Boolean useCache) {
this.useCache = useCache;
return this;
}
public Options build() {
return new Options(this);
}
} |
if (waitForModel != null) {
this.waitForModel = waitForModel;
}
return this;
| 102 | 35 | 137 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-hugging-face/src/main/java/dev/langchain4j/model/huggingface/client/Parameters.java | Parameters | toString | class Parameters {
private final Integer topK;
private final Double topP;
private final Double temperature;
private final Double repetitionPenalty;
private final Integer maxNewTokens;
private final Double maxTime;
private final Boolean returnFullText;
private final Integer numReturnSequences;
private final Boolean doSample;
public Parameters(Builder builder) {
this.topK = builder.topK;
this.topP = builder.topP;
this.temperature = builder.temperature;
this.repetitionPenalty = builder.repetitionPenalty;
this.maxNewTokens = builder.maxNewTokens;
this.maxTime = builder.maxTime;
this.returnFullText = builder.returnFullText;
this.numReturnSequences = builder.numReturnSequences;
this.doSample = builder.doSample;
}
@Override
public boolean equals(Object another) {
if (this == another) return true;
return another instanceof Parameters
&& equalTo((Parameters) another);
}
private boolean equalTo(Parameters another) {
return Objects.equals(topK, another.topK)
&& Objects.equals(topP, another.topP)
&& Objects.equals(temperature, another.temperature)
&& Objects.equals(repetitionPenalty, another.repetitionPenalty)
&& Objects.equals(maxNewTokens, another.maxNewTokens)
&& Objects.equals(maxTime, another.maxTime)
&& Objects.equals(returnFullText, another.returnFullText)
&& Objects.equals(numReturnSequences, another.numReturnSequences)
&& Objects.equals(doSample, another.doSample);
}
@Override
public int hashCode() {
int h = 5381;
h += (h << 5) + Objects.hashCode(topK);
h += (h << 5) + Objects.hashCode(topP);
h += (h << 5) + Objects.hashCode(temperature);
h += (h << 5) + Objects.hashCode(repetitionPenalty);
h += (h << 5) + Objects.hashCode(maxNewTokens);
h += (h << 5) + Objects.hashCode(maxTime);
h += (h << 5) + Objects.hashCode(returnFullText);
h += (h << 5) + Objects.hashCode(numReturnSequences);
h += (h << 5) + Objects.hashCode(doSample);
return h;
}
@Override
public String toString() {<FILL_FUNCTION_BODY>}
public static Builder builder() {
return new Builder();
}
public static final class Builder {
private Integer topK;
private Double topP;
private Double temperature;
private Double repetitionPenalty;
private Integer maxNewTokens;
private Double maxTime;
private Boolean returnFullText;
private Integer numReturnSequences;
private Boolean doSample;
public Builder topK(Integer topK) {
this.topK = topK;
return this;
}
public Builder topP(Double topP) {
this.topP = topP;
return this;
}
public Builder temperature(Double temperature) {
this.temperature = temperature;
return this;
}
public Builder repetitionPenalty(Double repetitionPenalty) {
this.repetitionPenalty = repetitionPenalty;
return this;
}
public Builder maxNewTokens(Integer maxNewTokens) {
this.maxNewTokens = maxNewTokens;
return this;
}
public Builder maxTime(Double maxTime) {
this.maxTime = maxTime;
return this;
}
public Builder returnFullText(Boolean returnFullText) {
this.returnFullText = returnFullText;
return this;
}
public Builder numReturnSequences(Integer numReturnSequences) {
this.numReturnSequences = numReturnSequences;
return this;
}
public Builder doSample(Boolean doSample) {
this.doSample = doSample;
return this;
}
public Parameters build() {
return new Parameters(this);
}
}
} |
return "TextGenerationRequest {"
+ " topK = " + topK
+ ", topP = " + topP
+ ", temperature = " + temperature
+ ", repetitionPenalty = " + repetitionPenalty
+ ", maxNewTokens = " + maxNewTokens
+ ", maxTime = " + maxTime
+ ", returnFullText = " + returnFullText
+ ", numReturnSequences = " + numReturnSequences
+ ", doSample = " + doSample
+ " }";
| 1,122 | 133 | 1,255 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-hugging-face/src/main/java/dev/langchain4j/model/huggingface/client/TextGenerationRequest.java | TextGenerationRequest | hashCode | class TextGenerationRequest {
private final String inputs;
private final Parameters parameters;
private final Options options;
TextGenerationRequest(Builder builder) {
this.inputs = builder.inputs;
this.parameters = builder.parameters;
this.options = builder.options;
}
@Override
public boolean equals(Object another) {
if (this == another) return true;
return another instanceof TextGenerationRequest
&& equalTo((TextGenerationRequest) another);
}
private boolean equalTo(TextGenerationRequest another) {
return Objects.equals(inputs, another.inputs)
&& Objects.equals(parameters, another.parameters)
&& Objects.equals(options, another.options);
}
@Override
public int hashCode() {<FILL_FUNCTION_BODY>}
@Override
public String toString() {
return "TextGenerationRequest {"
+ " inputs = " + quoted(inputs)
+ ", parameters = " + parameters
+ ", options = " + options
+ " }";
}
public static Builder builder() {
return new Builder();
}
public static final class Builder {
private String inputs;
private Parameters parameters;
private Options options;
public Builder inputs(String inputs) {
this.inputs = inputs;
return this;
}
public Builder parameters(Parameters parameters) {
this.parameters = parameters;
return this;
}
public Builder options(Options options) {
this.options = options;
return this;
}
public TextGenerationRequest build() {
return new TextGenerationRequest(this);
}
}
} |
int h = 5381;
h += (h << 5) + Objects.hashCode(inputs);
h += (h << 5) + Objects.hashCode(parameters);
h += (h << 5) + Objects.hashCode(options);
return h;
| 436 | 73 | 509 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-infinispan/src/main/java/dev/langchain4j/store/embedding/infinispan/LangChainItemMarshaller.java | LangChainItemMarshaller | readFrom | class LangChainItemMarshaller implements MessageMarshaller<LangChainInfinispanItem> {
private final String typeName;
public LangChainItemMarshaller(String typeName) {
this.typeName = typeName;
}
@Override
public LangChainInfinispanItem readFrom(ProtoStreamReader reader) throws IOException {<FILL_FUNCTION_BODY>}
@Override
public void writeTo(ProtoStreamWriter writer, LangChainInfinispanItem item)
throws IOException {
writer.writeString("id", item.id());
writer.writeFloats("embedding", item.embedding());
writer.writeString("text", item.text());
writer.writeCollection("metadata", item.metadata(), LangChainMetadata.class);
}
@Override
public Class<? extends LangChainInfinispanItem> getJavaClass() {
return LangChainInfinispanItem.class;
}
@Override
public String getTypeName() {
return typeName;
}
} |
String id = reader.readString("id");
float[] embedding = reader.readFloats("embedding");
String text = reader.readString("text");
Set<LangChainMetadata> metadata = reader.readCollection("metadata", new HashSet<>(), LangChainMetadata.class);
return new LangChainInfinispanItem(id, embedding, text, metadata);
| 261 | 91 | 352 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-infinispan/src/main/java/dev/langchain4j/store/embedding/infinispan/LangChainMetadataMarshaller.java | LangChainMetadataMarshaller | readFrom | class LangChainMetadataMarshaller implements MessageMarshaller<LangChainMetadata> {
private final String typeName;
public LangChainMetadataMarshaller(String typeName) {
this.typeName = typeName;
}
@Override
public LangChainMetadata readFrom(ProtoStreamReader reader) throws IOException {<FILL_FUNCTION_BODY>}
@Override
public void writeTo(ProtoStreamWriter writer, LangChainMetadata item)
throws IOException {
writer.writeString("name", item.name());
writer.writeString("value", item.value());
}
@Override
public Class<? extends LangChainMetadata> getJavaClass() {
return LangChainMetadata.class;
}
@Override
public String getTypeName() {
return typeName;
}
} |
String name = reader.readString("name");
String value = reader.readString("value");
return new LangChainMetadata(name, value);
| 206 | 39 | 245 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-local-ai/src/main/java/dev/langchain4j/model/localai/LocalAiChatModel.java | LocalAiChatModel | builder | class LocalAiChatModel implements ChatLanguageModel {
private final OpenAiClient client;
private final String modelName;
private final Double temperature;
private final Double topP;
private final Integer maxTokens;
private final Integer maxRetries;
@Builder
public LocalAiChatModel(String baseUrl,
String modelName,
Double temperature,
Double topP,
Integer maxTokens,
Duration timeout,
Integer maxRetries,
Boolean logRequests,
Boolean logResponses) {
temperature = temperature == null ? 0.7 : temperature;
timeout = timeout == null ? ofSeconds(60) : timeout;
maxRetries = maxRetries == null ? 3 : maxRetries;
this.client = OpenAiClient.builder()
.openAiApiKey("ignored")
.baseUrl(ensureNotBlank(baseUrl, "baseUrl"))
.callTimeout(timeout)
.connectTimeout(timeout)
.readTimeout(timeout)
.writeTimeout(timeout)
.logRequests(logRequests)
.logResponses(logResponses)
.build();
this.modelName = ensureNotBlank(modelName, "modelName");
this.temperature = temperature;
this.topP = topP;
this.maxTokens = maxTokens;
this.maxRetries = maxRetries;
}
@Override
public Response<AiMessage> generate(List<ChatMessage> messages) {
return generate(messages, null, null);
}
@Override
public Response<AiMessage> generate(List<ChatMessage> messages, List<ToolSpecification> toolSpecifications) {
return generate(messages, toolSpecifications, null);
}
@Override
public Response<AiMessage> generate(List<ChatMessage> messages, ToolSpecification toolSpecification) {
return generate(messages, singletonList(toolSpecification), toolSpecification);
}
private Response<AiMessage> generate(List<ChatMessage> messages,
List<ToolSpecification> toolSpecifications,
ToolSpecification toolThatMustBeExecuted
) {
ChatCompletionRequest.Builder requestBuilder = ChatCompletionRequest.builder()
.model(modelName)
.messages(toOpenAiMessages(messages))
.temperature(temperature)
.topP(topP)
.maxTokens(maxTokens);
if (toolSpecifications != null && !toolSpecifications.isEmpty()) {
requestBuilder.functions(toFunctions(toolSpecifications));
}
if (toolThatMustBeExecuted != null) {
requestBuilder.functionCall(toolThatMustBeExecuted.name());
}
ChatCompletionRequest request = requestBuilder.build();
ChatCompletionResponse response = withRetry(() -> client.chatCompletion(request).execute(), maxRetries);
return Response.from(
aiMessageFrom(response),
null,
finishReasonFrom(response.choices().get(0).finishReason())
);
}
public static LocalAiChatModelBuilder builder() {<FILL_FUNCTION_BODY>}
public static class LocalAiChatModelBuilder {
public LocalAiChatModelBuilder() {
// This is public so it can be extended
// By default with Lombok it becomes package private
}
}
} |
for (LocalAiChatModelBuilderFactory factory : loadFactories(LocalAiChatModelBuilderFactory.class)) {
return factory.get();
}
return new LocalAiChatModelBuilder();
| 854 | 52 | 906 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-local-ai/src/main/java/dev/langchain4j/model/localai/LocalAiEmbeddingModel.java | LocalAiEmbeddingModel | embedAll | class LocalAiEmbeddingModel implements EmbeddingModel {
private final OpenAiClient client;
private final String modelName;
private final Integer maxRetries;
@Builder
public LocalAiEmbeddingModel(String baseUrl,
String modelName,
Duration timeout,
Integer maxRetries,
Boolean logRequests,
Boolean logResponses) {
timeout = timeout == null ? ofSeconds(60) : timeout;
maxRetries = maxRetries == null ? 3 : maxRetries;
this.client = OpenAiClient.builder()
.openAiApiKey("ignored")
.baseUrl(ensureNotBlank(baseUrl, "baseUrl"))
.callTimeout(timeout)
.connectTimeout(timeout)
.readTimeout(timeout)
.writeTimeout(timeout)
.logRequests(logRequests)
.logResponses(logResponses)
.build();
this.modelName = ensureNotBlank(modelName, "modelName");
this.maxRetries = maxRetries;
}
@Override
public Response<List<Embedding>> embedAll(List<TextSegment> textSegments) {<FILL_FUNCTION_BODY>}
public static LocalAiEmbeddingModelBuilder builder() {
for (LocalAiEmbeddingModelBuilderFactory factory : loadFactories(LocalAiEmbeddingModelBuilderFactory.class)) {
return factory.get();
}
return new LocalAiEmbeddingModelBuilder();
}
public static class LocalAiEmbeddingModelBuilder {
public LocalAiEmbeddingModelBuilder() {
// This is public so it can be extended
// By default with Lombok it becomes package private
}
}
} |
List<String> texts = textSegments.stream()
.map(TextSegment::text)
.collect(toList());
EmbeddingRequest request = EmbeddingRequest.builder()
.input(texts)
.model(modelName)
.build();
EmbeddingResponse response = withRetry(() -> client.embedding(request).execute(), maxRetries);
List<Embedding> embeddings = response.data().stream()
.map(openAiEmbedding -> Embedding.from(openAiEmbedding.embedding()))
.collect(toList());
return Response.from(embeddings);
| 449 | 163 | 612 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-local-ai/src/main/java/dev/langchain4j/model/localai/LocalAiLanguageModel.java | LocalAiLanguageModel | builder | class LocalAiLanguageModel implements LanguageModel {
private final OpenAiClient client;
private final String modelName;
private final Double temperature;
private final Double topP;
private final Integer maxTokens;
private final Integer maxRetries;
@Builder
public LocalAiLanguageModel(String baseUrl,
String modelName,
Double temperature,
Double topP,
Integer maxTokens,
Duration timeout,
Integer maxRetries,
Boolean logRequests,
Boolean logResponses) {
temperature = temperature == null ? 0.7 : temperature;
timeout = timeout == null ? ofSeconds(60) : timeout;
maxRetries = maxRetries == null ? 3 : maxRetries;
this.client = OpenAiClient.builder()
.openAiApiKey("ignored")
.baseUrl(ensureNotBlank(baseUrl, "baseUrl"))
.callTimeout(timeout)
.connectTimeout(timeout)
.readTimeout(timeout)
.writeTimeout(timeout)
.logRequests(logRequests)
.logResponses(logResponses)
.build();
this.modelName = ensureNotBlank(modelName, "modelName");
this.temperature = temperature;
this.topP = topP;
this.maxTokens = maxTokens;
this.maxRetries = maxRetries;
}
@Override
public Response<String> generate(String prompt) {
CompletionRequest request = CompletionRequest.builder()
.model(modelName)
.prompt(prompt)
.temperature(temperature)
.topP(topP)
.maxTokens(maxTokens)
.build();
CompletionResponse response = withRetry(() -> client.completion(request).execute(), maxRetries);
return Response.from(
response.text(),
null,
finishReasonFrom(response.choices().get(0).finishReason())
);
}
public static LocalAiLanguageModelBuilder builder() {<FILL_FUNCTION_BODY>}
public static class LocalAiLanguageModelBuilder {
public LocalAiLanguageModelBuilder() {
// This is public so it can be extended
// By default with Lombok it becomes package private
}
}
} |
for (LocalAiLanguageModelBuilderFactory factory : loadFactories(LocalAiLanguageModelBuilderFactory.class)) {
return factory.get();
}
return new LocalAiLanguageModelBuilder();
| 589 | 52 | 641 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-local-ai/src/main/java/dev/langchain4j/model/localai/LocalAiStreamingChatModel.java | LocalAiStreamingChatModel | generate | class LocalAiStreamingChatModel implements StreamingChatLanguageModel {
private final OpenAiClient client;
private final String modelName;
private final Double temperature;
private final Double topP;
private final Integer maxTokens;
@Builder
public LocalAiStreamingChatModel(String baseUrl,
String modelName,
Double temperature,
Double topP,
Integer maxTokens,
Duration timeout,
Boolean logRequests,
Boolean logResponses) {
temperature = temperature == null ? 0.7 : temperature;
timeout = timeout == null ? ofSeconds(60) : timeout;
this.client = OpenAiClient.builder()
.openAiApiKey("ignored")
.baseUrl(ensureNotBlank(baseUrl, "baseUrl"))
.callTimeout(timeout)
.connectTimeout(timeout)
.readTimeout(timeout)
.writeTimeout(timeout)
.logRequests(logRequests)
.logStreamingResponses(logResponses)
.build();
this.modelName = ensureNotBlank(modelName, "modelName");
this.temperature = temperature;
this.topP = topP;
this.maxTokens = maxTokens;
}
@Override
public void generate(List<ChatMessage> messages, StreamingResponseHandler<AiMessage> handler) {
generate(messages, null, null, handler);
}
@Override
public void generate(List<ChatMessage> messages, List<ToolSpecification> toolSpecifications, StreamingResponseHandler<AiMessage> handler) {
generate(messages, toolSpecifications, null, handler);
}
@Override
public void generate(List<ChatMessage> messages, ToolSpecification toolSpecification, StreamingResponseHandler<AiMessage> handler) {
generate(messages, singletonList(toolSpecification), toolSpecification, handler);
}
private void generate(List<ChatMessage> messages,
List<ToolSpecification> toolSpecifications,
ToolSpecification toolThatMustBeExecuted,
StreamingResponseHandler<AiMessage> handler
) {<FILL_FUNCTION_BODY>}
private static void handle(ChatCompletionResponse partialResponse,
StreamingResponseHandler<AiMessage> handler) {
List<ChatCompletionChoice> choices = partialResponse.choices();
if (choices == null || choices.isEmpty()) {
return;
}
Delta delta = choices.get(0).delta();
String content = delta.content();
if (content != null) {
handler.onNext(content);
}
}
public static LocalAiStreamingChatModelBuilder builder() {
for (LocalAiStreamingChatModelBuilderFactory factory : loadFactories(LocalAiStreamingChatModelBuilderFactory.class)) {
return factory.get();
}
return new LocalAiStreamingChatModelBuilder();
}
public static class LocalAiStreamingChatModelBuilder {
public LocalAiStreamingChatModelBuilder() {
// This is public so it can be extended
// By default with Lombok it becomes package private
}
}
} |
ChatCompletionRequest.Builder requestBuilder = ChatCompletionRequest.builder()
.stream(true)
.model(modelName)
.messages(toOpenAiMessages(messages))
.temperature(temperature)
.topP(topP)
.maxTokens(maxTokens);
if (toolSpecifications != null && !toolSpecifications.isEmpty()) {
requestBuilder.functions(toFunctions(toolSpecifications));
}
if (toolThatMustBeExecuted != null) {
requestBuilder.functionCall(toolThatMustBeExecuted.name());
}
ChatCompletionRequest request = requestBuilder.build();
OpenAiStreamingResponseBuilder responseBuilder = new OpenAiStreamingResponseBuilder(null);
client.chatCompletion(request)
.onPartialResponse(partialResponse -> {
responseBuilder.append(partialResponse);
handle(partialResponse, handler);
})
.onComplete(() -> {
Response<AiMessage> response = responseBuilder.build(null, false);
handler.onComplete(response);
})
.onError(handler::onError)
.execute();
| 796 | 289 | 1,085 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-local-ai/src/main/java/dev/langchain4j/model/localai/LocalAiStreamingLanguageModel.java | LocalAiStreamingLanguageModel | generate | class LocalAiStreamingLanguageModel implements StreamingLanguageModel {
private final OpenAiClient client;
private final String modelName;
private final Double temperature;
private final Double topP;
private final Integer maxTokens;
@Builder
public LocalAiStreamingLanguageModel(String baseUrl,
String modelName,
Double temperature,
Double topP,
Integer maxTokens,
Duration timeout,
Boolean logRequests,
Boolean logResponses) {
temperature = temperature == null ? 0.7 : temperature;
timeout = timeout == null ? ofSeconds(60) : timeout;
this.client = OpenAiClient.builder()
.openAiApiKey("ignored")
.baseUrl(ensureNotBlank(baseUrl, "baseUrl"))
.callTimeout(timeout)
.connectTimeout(timeout)
.readTimeout(timeout)
.writeTimeout(timeout)
.logRequests(logRequests)
.logStreamingResponses(logResponses)
.build();
this.modelName = ensureNotBlank(modelName, "modelName");
this.temperature = temperature;
this.topP = topP;
this.maxTokens = maxTokens;
}
@Override
public void generate(String prompt, StreamingResponseHandler<String> handler) {<FILL_FUNCTION_BODY>}
public static LocalAiStreamingLanguageModelBuilder builder() {
for (LocalAiStreamingLanguageModelBuilderFactory factory : loadFactories(LocalAiStreamingLanguageModelBuilderFactory.class)) {
return factory.get();
}
return new LocalAiStreamingLanguageModelBuilder();
}
public static class LocalAiStreamingLanguageModelBuilder {
public LocalAiStreamingLanguageModelBuilder() {
// This is public so it can be extended
// By default with Lombok it becomes package private
}
}
} |
CompletionRequest request = CompletionRequest.builder()
.model(modelName)
.prompt(prompt)
.temperature(temperature)
.topP(topP)
.maxTokens(maxTokens)
.build();
OpenAiStreamingResponseBuilder responseBuilder = new OpenAiStreamingResponseBuilder(null);
client.completion(request)
.onPartialResponse(partialResponse -> {
responseBuilder.append(partialResponse);
String token = partialResponse.text();
if (token != null) {
handler.onNext(token);
}
})
.onComplete(() -> {
Response<AiMessage> response = responseBuilder.build(null, false);
handler.onComplete(Response.from(
response.content().text(),
response.tokenUsage(),
response.finishReason()
));
})
.onError(handler::onError)
.execute();
| 487 | 243 | 730 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-milvus/src/main/java/dev/langchain4j/store/embedding/milvus/CollectionOperationsExecutor.java | CollectionOperationsExecutor | createCollection | class CollectionOperationsExecutor {
static void flush(MilvusServiceClient milvusClient, String collectionName) {
FlushParam request = buildFlushRequest(collectionName);
R<FlushResponse> response = milvusClient.flush(request);
checkResponseNotFailed(response);
}
static boolean hasCollection(MilvusServiceClient milvusClient, String collectionName) {
HasCollectionParam request = buildHasCollectionRequest(collectionName);
R<Boolean> response = milvusClient.hasCollection(request);
checkResponseNotFailed(response);
return response.getData();
}
static void createCollection(MilvusServiceClient milvusClient, String collectionName, int dimension) {<FILL_FUNCTION_BODY>}
static void dropCollection(MilvusServiceClient milvusClient, String collectionName) {
DropCollectionParam request = buildDropCollectionRequest(collectionName);
R<RpcStatus> response = milvusClient.dropCollection(request);
checkResponseNotFailed(response);
}
static void createIndex(MilvusServiceClient milvusClient,
String collectionName,
IndexType indexType,
MetricType metricType) {
CreateIndexParam request = CreateIndexParam.newBuilder()
.withCollectionName(collectionName)
.withFieldName(VECTOR_FIELD_NAME)
.withIndexType(indexType)
.withMetricType(metricType)
.build();
R<RpcStatus> response = milvusClient.createIndex(request);
checkResponseNotFailed(response);
}
static void insert(MilvusServiceClient milvusClient, String collectionName, List<InsertParam.Field> fields) {
InsertParam request = buildInsertRequest(collectionName, fields);
R<MutationResult> response = milvusClient.insert(request);
checkResponseNotFailed(response);
}
static void loadCollectionInMemory(MilvusServiceClient milvusClient, String collectionName) {
LoadCollectionParam request = buildLoadCollectionInMemoryRequest(collectionName);
R<RpcStatus> response = milvusClient.loadCollection(request);
checkResponseNotFailed(response);
}
static SearchResultsWrapper search(MilvusServiceClient milvusClient, SearchParam searchRequest) {
R<SearchResults> response = milvusClient.search(searchRequest);
checkResponseNotFailed(response);
return new SearchResultsWrapper(response.getData().getResults());
}
static QueryResultsWrapper queryForVectors(MilvusServiceClient milvusClient,
String collectionName,
List<String> rowIds,
ConsistencyLevelEnum consistencyLevel) {
QueryParam request = buildQueryRequest(collectionName, rowIds, consistencyLevel);
R<QueryResults> response = milvusClient.query(request);
checkResponseNotFailed(response);
return new QueryResultsWrapper(response.getData());
}
private static <T> void checkResponseNotFailed(R<T> response) {
if (response == null) {
throw new RequestToMilvusFailedException("Request to Milvus DB failed. Response is null");
} else if (response.getStatus() != R.Status.Success.getCode()) {
String message = format("Request to Milvus DB failed. Response status:'%d'.%n", response.getStatus());
throw new RequestToMilvusFailedException(message, response.getException());
}
}
} |
CreateCollectionParam request = CreateCollectionParam.newBuilder()
.withCollectionName(collectionName)
.addFieldType(FieldType.newBuilder()
.withName(ID_FIELD_NAME)
.withDataType(VarChar)
.withMaxLength(36)
.withPrimaryKey(true)
.withAutoID(false)
.build())
.addFieldType(FieldType.newBuilder()
.withName(TEXT_FIELD_NAME)
.withDataType(VarChar)
.withMaxLength(65535)
.build())
.addFieldType(FieldType.newBuilder()
.withName(METADATA_FIELD_NAME)
.withDataType(JSON)
.build())
.addFieldType(FieldType.newBuilder()
.withName(VECTOR_FIELD_NAME)
.withDataType(FloatVector)
.withDimension(dimension)
.build())
.build();
R<RpcStatus> response = milvusClient.createCollection(request);
checkResponseNotFailed(response);
| 871 | 278 | 1,149 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-milvus/src/main/java/dev/langchain4j/store/embedding/milvus/CollectionRequestBuilder.java | CollectionRequestBuilder | buildSearchRequest | class CollectionRequestBuilder {
static FlushParam buildFlushRequest(String collectionName) {
return FlushParam.newBuilder()
.withCollectionNames(singletonList(collectionName))
.build();
}
static HasCollectionParam buildHasCollectionRequest(String collectionName) {
return HasCollectionParam.newBuilder()
.withCollectionName(collectionName)
.build();
}
static DropCollectionParam buildDropCollectionRequest(String collectionName) {
return DropCollectionParam.newBuilder()
.withCollectionName(collectionName)
.build();
}
static InsertParam buildInsertRequest(String collectionName, List<InsertParam.Field> fields) {
return InsertParam.newBuilder()
.withCollectionName(collectionName)
.withFields(fields)
.build();
}
static LoadCollectionParam buildLoadCollectionInMemoryRequest(String collectionName) {
return LoadCollectionParam.newBuilder()
.withCollectionName(collectionName)
.build();
}
static SearchParam buildSearchRequest(String collectionName,
List<Float> vector,
Filter filter,
int maxResults,
MetricType metricType,
ConsistencyLevelEnum consistencyLevel) {<FILL_FUNCTION_BODY>}
static QueryParam buildQueryRequest(String collectionName,
List<String> rowIds,
ConsistencyLevelEnum consistencyLevel) {
return QueryParam.newBuilder()
.withCollectionName(collectionName)
.withExpr(buildQueryExpression(rowIds))
.withConsistencyLevel(consistencyLevel)
.withOutFields(singletonList(VECTOR_FIELD_NAME))
.build();
}
private static String buildQueryExpression(List<String> rowIds) {
return rowIds.stream()
.map(id -> format("%s == '%s'", ID_FIELD_NAME, id))
.collect(joining(" || "));
}
} |
SearchParam.Builder builder = SearchParam.newBuilder()
.withCollectionName(collectionName)
.withVectors(singletonList(vector))
.withVectorFieldName(VECTOR_FIELD_NAME)
.withTopK(maxResults)
.withMetricType(metricType)
.withConsistencyLevel(consistencyLevel)
.withOutFields(asList(ID_FIELD_NAME, TEXT_FIELD_NAME, METADATA_FIELD_NAME));
if (filter != null) {
builder.withExpr(MilvusMetadataFilterMapper.map(filter));
}
return builder.build();
| 489 | 163 | 652 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-milvus/src/main/java/dev/langchain4j/store/embedding/milvus/Generator.java | Generator | generateEmptyJsons | class Generator {
static List<String> generateRandomIds(int size) {
List<String> ids = new ArrayList<>();
for (int i = 0; i < size; i++) {
ids.add(Utils.randomUUID());
}
return ids;
}
static List<String> generateEmptyScalars(int size) {
String[] arr = new String[size];
Arrays.fill(arr, "");
return Arrays.asList(arr);
}
static List<JSONObject> generateEmptyJsons(int size) {<FILL_FUNCTION_BODY>}
} |
List<JSONObject> list = new ArrayList<>();
for (int i = 0; i < size; i++) {
list.add(new JSONObject(new HashMap<>()));
}
return list;
| 163 | 56 | 219 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-milvus/src/main/java/dev/langchain4j/store/embedding/milvus/Mapper.java | Mapper | toTextSegment | class Mapper {
static List<List<Float>> toVectors(List<Embedding> embeddings) {
return embeddings.stream()
.map(Embedding::vectorAsList)
.collect(toList());
}
static List<String> toScalars(List<TextSegment> textSegments, int size) {
return isNullOrEmpty(textSegments) ? generateEmptyScalars(size) : textSegmentsToScalars(textSegments);
}
static List<JSONObject> toMetadataJsons(List<TextSegment> textSegments, int size) {
return isNullOrEmpty(textSegments) ? generateEmptyJsons(size) : textSegments.stream()
.map(segment -> new JSONObject(segment.metadata().toMap()))
.collect(toList());
}
static List<String> textSegmentsToScalars(List<TextSegment> textSegments) {
return textSegments.stream()
.map(TextSegment::text)
.collect(toList());
}
static List<EmbeddingMatch<TextSegment>> toEmbeddingMatches(MilvusServiceClient milvusClient,
SearchResultsWrapper resultsWrapper,
String collectionName,
ConsistencyLevelEnum consistencyLevel,
boolean queryForVectorOnSearch) {
List<EmbeddingMatch<TextSegment>> matches = new ArrayList<>();
Map<String, Embedding> idToEmbedding = new HashMap<>();
if (queryForVectorOnSearch) {
try {
List<String> rowIds = (List<String>) resultsWrapper.getFieldWrapper(ID_FIELD_NAME).getFieldData();
idToEmbedding.putAll(queryEmbeddings(milvusClient, collectionName, rowIds, consistencyLevel));
} catch (ParamException e) {
// There is no way to check if the result is empty or not.
// If the result is empty, the exception will be thrown.
}
}
for (int i = 0; i < resultsWrapper.getRowRecords().size(); i++) {
double score = resultsWrapper.getIDScore(0).get(i).getScore();
String rowId = resultsWrapper.getIDScore(0).get(i).getStrID();
Embedding embedding = idToEmbedding.get(rowId);
TextSegment textSegment = toTextSegment(resultsWrapper.getRowRecords().get(i));
EmbeddingMatch<TextSegment> embeddingMatch = new EmbeddingMatch<>(
RelevanceScore.fromCosineSimilarity(score),
rowId,
embedding,
textSegment
);
matches.add(embeddingMatch);
}
return matches;
}
private static TextSegment toTextSegment(RowRecord rowRecord) {<FILL_FUNCTION_BODY>}
private static Metadata toMetadata(JSONObject metadata) {
Map<String, Object> metadataMap = metadata.getInnerMap();
metadataMap.forEach((key, value) -> {
if (value instanceof BigDecimal) {
// It is safe to convert. No information is lost, the "biggest" type allowed in Metadata is double.
metadataMap.put(key, ((BigDecimal) value).doubleValue());
}
});
return Metadata.from(metadataMap);
}
private static Map<String, Embedding> queryEmbeddings(MilvusServiceClient milvusClient,
String collectionName,
List<String> rowIds,
ConsistencyLevelEnum consistencyLevel) {
QueryResultsWrapper queryResultsWrapper = queryForVectors(
milvusClient,
collectionName,
rowIds,
consistencyLevel
);
Map<String, Embedding> idToEmbedding = new HashMap<>();
for (RowRecord row : queryResultsWrapper.getRowRecords()) {
String id = row.get(ID_FIELD_NAME).toString();
List<Float> vector = (List<Float>) row.get(VECTOR_FIELD_NAME);
idToEmbedding.put(id, Embedding.from(vector));
}
return idToEmbedding;
}
} |
String text = (String) rowRecord.get(TEXT_FIELD_NAME);
if (isNullOrBlank(text)) {
return null;
}
if (!rowRecord.getFieldValues().containsKey(METADATA_FIELD_NAME)) {
return TextSegment.from(text);
}
JSONObject metadata = (JSONObject) rowRecord.get(METADATA_FIELD_NAME);
return TextSegment.from(text, toMetadata(metadata));
| 1,047 | 122 | 1,169 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-milvus/src/main/java/dev/langchain4j/store/embedding/milvus/MilvusMetadataFilterMapper.java | MilvusMetadataFilterMapper | formatValues | class MilvusMetadataFilterMapper {
static String map(Filter filter) {
if (filter instanceof IsEqualTo) {
return mapEqual((IsEqualTo) filter);
} else if (filter instanceof IsNotEqualTo) {
return mapNotEqual((IsNotEqualTo) filter);
} else if (filter instanceof IsGreaterThan) {
return mapGreaterThan((IsGreaterThan) filter);
} else if (filter instanceof IsGreaterThanOrEqualTo) {
return mapGreaterThanOrEqual((IsGreaterThanOrEqualTo) filter);
} else if (filter instanceof IsLessThan) {
return mapLessThan((IsLessThan) filter);
} else if (filter instanceof IsLessThanOrEqualTo) {
return mapLessThanOrEqual((IsLessThanOrEqualTo) filter);
} else if (filter instanceof IsIn) {
return mapIn((IsIn) filter);
} else if (filter instanceof IsNotIn) {
return mapNotIn((IsNotIn) filter);
} else if (filter instanceof And) {
return mapAnd((And) filter);
} else if (filter instanceof Not) {
return mapNot((Not) filter);
} else if (filter instanceof Or) {
return mapOr((Or) filter);
} else {
throw new UnsupportedOperationException("Unsupported filter type: " + filter.getClass().getName());
}
}
private static String mapEqual(IsEqualTo isEqualTo) {
return format("%s == %s", formatKey(isEqualTo.key()), formatValue(isEqualTo.comparisonValue()));
}
private static String mapNotEqual(IsNotEqualTo isNotEqualTo) {
return format("%s != %s", formatKey(isNotEqualTo.key()), formatValue(isNotEqualTo.comparisonValue()));
}
private static String mapGreaterThan(IsGreaterThan isGreaterThan) {
return format("%s > %s", formatKey(isGreaterThan.key()), formatValue(isGreaterThan.comparisonValue()));
}
private static String mapGreaterThanOrEqual(IsGreaterThanOrEqualTo isGreaterThanOrEqualTo) {
return format("%s >= %s", formatKey(isGreaterThanOrEqualTo.key()), formatValue(isGreaterThanOrEqualTo.comparisonValue()));
}
private static String mapLessThan(IsLessThan isLessThan) {
return format("%s < %s", formatKey(isLessThan.key()), formatValue(isLessThan.comparisonValue()));
}
private static String mapLessThanOrEqual(IsLessThanOrEqualTo isLessThanOrEqualTo) {
return format("%s <= %s", formatKey(isLessThanOrEqualTo.key()), formatValue(isLessThanOrEqualTo.comparisonValue()));
}
public static String mapIn(IsIn isIn) {
return format("%s in %s", formatKey(isIn.key()), formatValues(isIn.comparisonValues()));
}
public static String mapNotIn(IsNotIn isNotIn) {
return format("%s not in %s", formatKey(isNotIn.key()), formatValues(isNotIn.comparisonValues()));
}
private static String mapAnd(And and) {
return format("%s and %s", map(and.left()), map(and.right()));
}
private static String mapNot(Not not) {
return format("not(%s)", map(not.expression()));
}
private static String mapOr(Or or) {
return format("(%s or %s)", map(or.left()), map(or.right()));
}
private static String formatKey(String key) {
return "metadata[\"" + key + "\"]";
}
private static String formatValue(Object value) {
if (value instanceof String) {
return "\"" + value + "\"";
} else {
return value.toString();
}
}
private static List<String> formatValues(Collection<?> values) {<FILL_FUNCTION_BODY>}
} |
return values.stream().map(value -> {
if (value instanceof String) {
return "\"" + value + "\"";
} else {
return value.toString();
}
}).collect(toList());
| 1,060 | 60 | 1,120 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-mistral-ai/src/main/java/dev/langchain4j/model/mistralai/DefaultMistralAiClient.java | Builder | onEvent | class Builder extends MistralAiClient.Builder<DefaultMistralAiClient, Builder> {
public DefaultMistralAiClient build() {
return new DefaultMistralAiClient(this);
}
}
DefaultMistralAiClient(Builder builder) {
OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder()
.callTimeout(builder.timeout)
.connectTimeout(builder.timeout)
.readTimeout(builder.timeout)
.writeTimeout(builder.timeout);
okHttpClientBuilder.addInterceptor(new MistralAiApiKeyInterceptor(builder.apiKey));
if (builder.logRequests) {
okHttpClientBuilder.addInterceptor(new MistralAiRequestLoggingInterceptor());
}
if (builder.logResponses) {
okHttpClientBuilder.addInterceptor(new MistralAiResponseLoggingInterceptor());
}
this.logStreamingResponses = builder.logResponses;
this.okHttpClient = okHttpClientBuilder.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(formattedUrlForRetrofit(builder.baseUrl))
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(GSON))
.build();
mistralAiApi = retrofit.create(MistralAiApi.class);
}
private static String formattedUrlForRetrofit(String baseUrl) {
return baseUrl.endsWith("/") ? baseUrl : baseUrl + "/";
}
@Override
public MistralAiChatCompletionResponse chatCompletion(MistralAiChatCompletionRequest request) {
try {
retrofit2.Response<MistralAiChatCompletionResponse> retrofitResponse
= mistralAiApi.chatCompletion(request).execute();
if (retrofitResponse.isSuccessful()) {
return retrofitResponse.body();
} else {
throw toException(retrofitResponse);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public void streamingChatCompletion(MistralAiChatCompletionRequest request, StreamingResponseHandler<AiMessage> handler) {
EventSourceListener eventSourceListener = new EventSourceListener() {
final StringBuffer contentBuilder = new StringBuffer();
List<ToolExecutionRequest> toolExecutionRequests;
TokenUsage tokenUsage;
FinishReason finishReason;
@Override
public void onOpen(EventSource eventSource, okhttp3.Response response) {
if (logStreamingResponses) {
LOGGER.debug("onOpen()");
}
}
@Override
public void onEvent(EventSource eventSource, String id, String type, String data) {<FILL_FUNCTION_BODY> |
if (logStreamingResponses) {
LOGGER.debug("onEvent() {}", data);
}
if ("[DONE]".equals(data)) {
AiMessage aiMessage;
if (!isNullOrEmpty(toolExecutionRequests)){
aiMessage = AiMessage.from(toolExecutionRequests);
} else {
aiMessage = AiMessage.from(contentBuilder.toString());
}
Response<AiMessage> response = Response.from(
aiMessage,
tokenUsage,
finishReason
);
handler.onComplete(response);
} else {
try {
MistralAiChatCompletionResponse chatCompletionResponse = GSON.fromJson(data, MistralAiChatCompletionResponse.class);
MistralAiChatCompletionChoice choice = chatCompletionResponse.getChoices().get(0);
String chunk = choice.getDelta().getContent();
if (isNotNullOrBlank(chunk)) {
contentBuilder.append(chunk);
handler.onNext(chunk);
}
List<MistralAiToolCall> toolCalls = choice.getDelta().getToolCalls();
if (!isNullOrEmpty(toolCalls)) {
toolExecutionRequests = toToolExecutionRequests(toolCalls);
}
MistralAiUsage usageInfo = chatCompletionResponse.getUsage();
if (usageInfo != null) {
this.tokenUsage = tokenUsageFrom(usageInfo);
}
String finishReasonString = choice.getFinishReason();
if (finishReasonString != null) {
this.finishReason = finishReasonFrom(finishReasonString);
}
} catch (Exception e) {
handler.onError(e);
throw new RuntimeException(e);
}
}
| 735 | 466 | 1,201 | <methods>public non-sealed void <init>() ,public static Builder#RAW builder() ,public abstract dev.langchain4j.model.mistralai.MistralAiChatCompletionResponse chatCompletion(dev.langchain4j.model.mistralai.MistralAiChatCompletionRequest) ,public abstract dev.langchain4j.model.mistralai.MistralAiEmbeddingResponse embedding(dev.langchain4j.model.mistralai.MistralAiEmbeddingRequest) ,public abstract dev.langchain4j.model.mistralai.MistralAiModelResponse listModels() ,public abstract void streamingChatCompletion(dev.langchain4j.model.mistralai.MistralAiChatCompletionRequest, StreamingResponseHandler<dev.langchain4j.data.message.AiMessage>) <variables> |
langchain4j_langchain4j | langchain4j/langchain4j-mistral-ai/src/main/java/dev/langchain4j/model/mistralai/MistralAiApiKeyInterceptor.java | MistralAiApiKeyInterceptor | intercept | class MistralAiApiKeyInterceptor implements Interceptor {
private final String apiKey;
MistralAiApiKeyInterceptor(String apiKey) {
this.apiKey = apiKey;
}
@Override
public Response intercept(Chain chain) throws IOException {<FILL_FUNCTION_BODY>}
} |
Request request = chain.request()
.newBuilder()
.addHeader("Authorization", "Bearer " + apiKey)
.build();
return chain.proceed(request);
| 90 | 50 | 140 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-mistral-ai/src/main/java/dev/langchain4j/model/mistralai/MistralAiChatModel.java | MistralAiChatModel | generate | class MistralAiChatModel implements ChatLanguageModel {
private final MistralAiClient client;
private final String modelName;
private final Double temperature;
private final Double topP;
private final Integer maxTokens;
private final Boolean safePrompt;
private final Integer randomSeed;
private final String responseFormat;
private final Integer maxRetries;
/**
* Constructs a MistralAiChatModel with the specified parameters.
*
* @param baseUrl the base URL of the Mistral AI API. It uses the default value if not specified
* @param apiKey the API key for authentication
* @param modelName the name of the Mistral AI model to use
* @param temperature the temperature parameter for generating chat responses
* @param topP the top-p parameter for generating chat responses
* @param maxTokens the maximum number of new tokens to generate in a chat response
* @param safePrompt a flag indicating whether to use a safe prompt for generating chat responses
* @param randomSeed the random seed for generating chat responses
* @param responseFormat the response format for generating chat responses.
* <p>
* Current values supported are "text" and "json_object".
* @param timeout the timeout duration for API requests
* <p>
* The default value is 60 seconds
* @param logRequests a flag indicating whether to log API requests
* @param logResponses a flag indicating whether to log API responses
* @param maxRetries the maximum number of retries for API requests. It uses the default value 3 if not specified
*/
@Builder
public MistralAiChatModel(String baseUrl,
String apiKey,
String modelName,
Double temperature,
Double topP,
Integer maxTokens,
Boolean safePrompt,
Integer randomSeed,
String responseFormat,
Duration timeout,
Boolean logRequests,
Boolean logResponses,
Integer maxRetries) {
this.client = MistralAiClient.builder()
.baseUrl(getOrDefault(baseUrl, MISTRALAI_API_URL))
.apiKey(apiKey)
.timeout(getOrDefault(timeout, Duration.ofSeconds(60)))
.logRequests(getOrDefault(logRequests, false))
.logResponses(getOrDefault(logResponses, false))
.build();
this.modelName = getOrDefault(modelName, MistralAiChatModelName.OPEN_MISTRAL_7B.toString());
this.temperature = temperature;
this.topP = topP;
this.maxTokens = maxTokens;
this.safePrompt = safePrompt;
this.randomSeed = randomSeed;
this.responseFormat = responseFormat;
this.maxRetries = getOrDefault(maxRetries, 3);
}
/**
* Creates a MistralAiChatModel with the specified API key.
*
* @param apiKey the API key for authentication
* @return a MistralAiChatModel instance
*/
public static MistralAiChatModel withApiKey(String apiKey) {
return builder().apiKey(apiKey).build();
}
/**
* Generates chat response based on the given list of messages.
*
* @param messages the list of chat messages
*/
@Override
public Response<AiMessage> generate(List<ChatMessage> messages) {
return generate(messages, null, null);
}
/**
* Generates an AI message response based on the given list of chat messages and tool specifications.
*
* @param messages the list of chat messages
* @param toolSpecifications the list of tool specifications. tool_choice is set to AUTO.
* @return a Response containing the generated AI message
*/
@Override
public Response<AiMessage> generate(List<ChatMessage> messages, List<ToolSpecification> toolSpecifications) {
return generate(messages, toolSpecifications, null);
}
/**
* Generates an AI message response based on the given list of chat messages and a tool specification.
*
* @param messages the list of chat messages
* @param toolSpecification the tool specification that must be executed. tool_choice is set to ANY.
* @return a Response containing the generated AI message
*/
@Override
public Response<AiMessage> generate(List<ChatMessage> messages, ToolSpecification toolSpecification) {
return generate(messages, singletonList(toolSpecification), toolSpecification);
}
private Response<AiMessage> generate(List<ChatMessage> messages,
List<ToolSpecification> toolSpecifications,
ToolSpecification toolThatMustBeExecuted) {<FILL_FUNCTION_BODY>}
public static MistralAiChatModelBuilder builder() {
for (MistralAiChatModelBuilderFactory factory : loadFactories(MistralAiChatModelBuilderFactory.class)) {
return factory.get();
}
return new MistralAiChatModelBuilder();
}
public static class MistralAiChatModelBuilder {
public MistralAiChatModelBuilder() {
}
public MistralAiChatModelBuilder modelName(String modelName) {
this.modelName = modelName;
return this;
}
public MistralAiChatModelBuilder modelName(MistralAiChatModelName modelName) {
this.modelName = modelName.toString();
return this;
}
public MistralAiChatModelBuilder responseFormat(String responseFormat) {
this.responseFormat = responseFormat;
return this;
}
public MistralAiChatModelBuilder responseFormat(MistralAiResponseFormatType responseFormat) {
this.responseFormat = responseFormat.toString();
return this;
}
}
} |
ensureNotEmpty(messages, "messages");
MistralAiChatCompletionRequest.MistralAiChatCompletionRequestBuilder requestBuilder = MistralAiChatCompletionRequest.builder()
.model(this.modelName)
.messages(toMistralAiMessages(messages))
.temperature(this.temperature)
.maxTokens(this.maxTokens)
.topP(this.topP)
.randomSeed(this.randomSeed)
.safePrompt(this.safePrompt)
.responseFormat(toMistralAiResponseFormat(this.responseFormat))
.stream(false);
if (!isNullOrEmpty(toolSpecifications)) {
requestBuilder.tools(toMistralAiTools(toolSpecifications));
requestBuilder.toolChoice(MistralAiToolChoiceName.AUTO);
} else if (toolThatMustBeExecuted != null) {
requestBuilder.tools(toMistralAiTools(singletonList(toolThatMustBeExecuted)));
requestBuilder.toolChoice(MistralAiToolChoiceName.ANY); // MistralAi does not support toolChoice as Function object. ANY force to the model to call a function
}
MistralAiChatCompletionRequest request = requestBuilder.build();
MistralAiChatCompletionResponse response = withRetry(() -> client.chatCompletion(request), maxRetries);
return Response.from(
aiMessageFrom(response),
tokenUsageFrom(response.getUsage()),
finishReasonFrom(response.getChoices().get(0).getFinishReason())
);
| 1,493 | 415 | 1,908 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-mistral-ai/src/main/java/dev/langchain4j/model/mistralai/MistralAiClient.java | MistralAiClient | builder | class MistralAiClient {
public abstract MistralAiChatCompletionResponse chatCompletion(MistralAiChatCompletionRequest request);
public abstract void streamingChatCompletion(MistralAiChatCompletionRequest request, StreamingResponseHandler<AiMessage> handler);
public abstract MistralAiEmbeddingResponse embedding(MistralAiEmbeddingRequest request);
public abstract MistralAiModelResponse listModels();
@SuppressWarnings("rawtypes")
public static MistralAiClient.Builder builder() {<FILL_FUNCTION_BODY>}
@SuppressWarnings("unchecked")
public abstract static class Builder<T extends MistralAiClient, B extends Builder<T, B>> {
public String baseUrl;
public String apiKey;
public Duration timeout;
public Boolean logRequests;
public Boolean logResponses;
public abstract T build();
public B baseUrl(String baseUrl) {
if (baseUrl == null || baseUrl.trim().isEmpty()) {
throw new IllegalArgumentException("baseUrl cannot be null or empty");
}
this.baseUrl = baseUrl.endsWith("/") ? baseUrl : baseUrl + "/";
return (B) this;
}
public B apiKey(String apiKey) {
if (apiKey == null || apiKey.trim().isEmpty()) {
throw new IllegalArgumentException("MistralAI API Key must be defined. It can be generated here: https://console.mistral.ai/user/api-keys");
}
this.apiKey = apiKey;
return (B) this;
}
public B timeout(Duration timeout) {
if (timeout == null) {
throw new IllegalArgumentException("callTimeout cannot be null");
}
this.timeout = timeout;
return (B) this;
}
public B logRequests() {
return logRequests(true);
}
public B logRequests(Boolean logRequests) {
if (logRequests == null) {
logRequests = false;
}
this.logRequests = logRequests;
return (B) this;
}
public B logResponses() {
return logResponses(true);
}
public B logResponses(Boolean logResponses) {
if (logResponses == null) {
logResponses = false;
}
this.logResponses = logResponses;
return (B) this;
}
}
} |
for (MistralAiClientBuilderFactory factory : ServiceHelper.loadFactories(MistralAiClientBuilderFactory.class)) {
return factory.get();
}
// fallback to the default
return DefaultMistralAiClient.builder();
| 644 | 67 | 711 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-mistral-ai/src/main/java/dev/langchain4j/model/mistralai/MistralAiEmbeddingModel.java | MistralAiEmbeddingModel | builder | class MistralAiEmbeddingModel implements EmbeddingModel {
private final MistralAiClient client;
private final String modelName;
private final Integer maxRetries;
/**
* Constructs a new MistralAiEmbeddingModel instance.
*
* @param baseUrl the base URL of the Mistral AI API. It use a default value if not specified
* @param apiKey the API key for authentication
* @param modelName the name of the embedding model. It uses a default value if not specified
* @param timeout the timeout duration for API requests. It uses a default value of 60 seconds if not specified
* <p>
* The default value is 60 seconds
* @param logRequests a flag indicating whether to log API requests
* @param logResponses a flag indicating whether to log API responses
* @param maxRetries the maximum number of retries for API requests. It uses a default value of 3 if not specified
*/
@Builder
public MistralAiEmbeddingModel(String baseUrl,
String apiKey,
String modelName,
Duration timeout,
Boolean logRequests,
Boolean logResponses,
Integer maxRetries) {
this.client = MistralAiClient.builder()
.baseUrl(getOrDefault(baseUrl, MISTRALAI_API_URL))
.apiKey(apiKey)
.timeout(getOrDefault(timeout, Duration.ofSeconds(60)))
.logRequests(getOrDefault(logRequests, false))
.logResponses(getOrDefault(logResponses, false))
.build();
this.modelName = getOrDefault(modelName, MistralAiEmbeddingModelName.MISTRAL_EMBED.toString());
this.maxRetries = getOrDefault(maxRetries, 3);
}
/**
* Creates a new MistralAiEmbeddingModel instance with the specified API key.
*
* @param apiKey the Mistral AI API key for authentication
* @return a new MistralAiEmbeddingModel instance
*/
public static MistralAiEmbeddingModel withApiKey(String apiKey) {
return builder().apiKey(apiKey).build();
}
/**
* Embeds a list of text segments using the Mistral AI embedding model.
*
* @param textSegments the list of text segments to embed
* @return a Response object containing the embeddings and token usage information
*/
@Override
public Response<List<Embedding>> embedAll(List<TextSegment> textSegments) {
MistralAiEmbeddingRequest request = MistralAiEmbeddingRequest.builder()
.model(modelName)
.input(textSegments.stream().map(TextSegment::text).collect(toList()))
.encodingFormat(MISTRALAI_API_CREATE_EMBEDDINGS_ENCODING_FORMAT)
.build();
MistralAiEmbeddingResponse response = withRetry(() -> client.embedding(request), maxRetries);
List<Embedding> embeddings = response.getData().stream()
.map(mistralAiEmbedding -> Embedding.from(mistralAiEmbedding.getEmbedding()))
.collect(toList());
return Response.from(
embeddings,
tokenUsageFrom(response.getUsage())
);
}
public static MistralAiEmbeddingModelBuilder builder() {<FILL_FUNCTION_BODY>}
public static class MistralAiEmbeddingModelBuilder {
public MistralAiEmbeddingModelBuilder() {
}
public MistralAiEmbeddingModelBuilder modelName(String modelName) {
this.modelName = modelName;
return this;
}
public MistralAiEmbeddingModelBuilder modelName(MistralAiEmbeddingModelName modelName) {
this.modelName = modelName.toString();
return this;
}
}
} |
for (MistralAiEmbeddingModelBuilderFactory factory : loadFactories(MistralAiEmbeddingModelBuilderFactory.class)){
return factory.get();
}
return new MistralAiEmbeddingModelBuilder();
| 1,040 | 63 | 1,103 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-mistral-ai/src/main/java/dev/langchain4j/model/mistralai/MistralAiModels.java | MistralAiModels | builder | class MistralAiModels {
private final MistralAiClient client;
private final Integer maxRetries;
/**
* Constructs a new instance of MistralAiModels.
*
* @param baseUrl the base URL of the Mistral AI API. It uses the default value if not specified
* @param apiKey the API key for authentication
* @param timeout the timeout duration for API requests. It uses the default value of 60 seconds if not specified
* @param logRequests a flag whether to log raw HTTP requests
* @param logResponses a flag whether to log raw HTTP responses
* @param maxRetries the maximum number of retries for API requests. It uses the default value of 3 if not specified
*/
@Builder
public MistralAiModels(String baseUrl,
String apiKey,
Duration timeout,
Boolean logRequests,
Boolean logResponses,
Integer maxRetries) {
this.client = MistralAiClient.builder()
.baseUrl(getOrDefault(baseUrl, MISTRALAI_API_URL))
.apiKey(apiKey)
.timeout(getOrDefault(timeout, Duration.ofSeconds(60)))
.logRequests(getOrDefault(logRequests, false))
.logResponses(getOrDefault(logResponses, false))
.build();
this.maxRetries = getOrDefault(maxRetries, 3);
}
/**
* Creates a new instance of MistralAiModels with the specified API key.
*
* @param apiKey the API key for authentication
* @return a new instance of MistralAiModels
*/
public static MistralAiModels withApiKey(String apiKey) {
return builder().apiKey(apiKey).build();
}
/**
* Retrieves the list of all available models.
*
* @return the response containing the list of models
*/
public Response<List<MistralAiModelCard>> availableModels() {
MistralAiModelResponse response = withRetry(client::listModels, maxRetries);
return Response.from(response.getData());
}
public static MistralAiModelsBuilder builder() {<FILL_FUNCTION_BODY>}
public static class MistralAiModelsBuilder {
public MistralAiModelsBuilder(){
}
}
} |
for (MistralAiModelsBuilderFactory factory : loadFactories(MistralAiModelsBuilderFactory.class)){
return factory.get();
}
return new MistralAiModelsBuilder();
| 619 | 54 | 673 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-mistral-ai/src/main/java/dev/langchain4j/model/mistralai/MistralAiRequestLoggingInterceptor.java | MistralAiRequestLoggingInterceptor | getBody | class MistralAiRequestLoggingInterceptor implements Interceptor {
private static final Logger LOGGER = LoggerFactory.getLogger(MistralAiRequestLoggingInterceptor.class);
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
this.log(request);
return chain.proceed(request);
}
private void log(Request request) {
try {
LOGGER.debug("Request:\n- method: {}\n- url: {}\n- headers: {}\n- body: {}",
request.method(), request.url(), getHeaders(request.headers()), getBody(request));
} catch (Exception e) {
LOGGER.warn("Error while logging request: {}", e.getMessage());
}
}
private static String getBody(Request request) {<FILL_FUNCTION_BODY>}
} |
try {
Buffer buffer = new Buffer();
if (request.body() == null) {
return "";
}
request.body().writeTo(buffer);
return buffer.readUtf8();
} catch (Exception e) {
LOGGER.warn("Exception while getting body", e);
return "Exception while getting body: " + e.getMessage();
}
| 228 | 98 | 326 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-mistral-ai/src/main/java/dev/langchain4j/model/mistralai/MistralAiResponseLoggingInterceptor.java | MistralAiResponseLoggingInterceptor | log | class MistralAiResponseLoggingInterceptor implements Interceptor {
private static final Logger LOGGER = LoggerFactory.getLogger(MistralAiResponseLoggingInterceptor.class);
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(request);
this.log(response);
return response;
}
private void log(Response response) {<FILL_FUNCTION_BODY>}
private String getBody(Response response) throws IOException {
return isEventStream(response)
? "[skipping response body due to streaming]"
: response.peekBody(Long.MAX_VALUE).string();
}
private static boolean isEventStream(Response response) {
String contentType = response.header("Content-Type");
return contentType != null && contentType.contains("event-stream");
}
} |
try {
LOGGER.debug("Response:\n- status code: {}\n- headers: {}\n- body: {}",
response.code(), getHeaders(response.headers()), this.getBody(response));
} catch (Exception e) {
LOGGER.warn("Error while logging response: {}", e.getMessage());
}
| 234 | 85 | 319 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-mistral-ai/src/main/java/dev/langchain4j/model/mistralai/MistralAiStreamingChatModel.java | MistralAiStreamingChatModel | generate | class MistralAiStreamingChatModel implements StreamingChatLanguageModel {
private final MistralAiClient client;
private final String modelName;
private final Double temperature;
private final Double topP;
private final Integer maxTokens;
private final Boolean safePrompt;
private final Integer randomSeed;
private final String responseFormat;
/**
* Constructs a MistralAiStreamingChatModel with the specified parameters.
*
* @param baseUrl the base URL of the Mistral AI API. It uses the default value if not specified
* @param apiKey the API key for authentication
* @param modelName the name of the Mistral AI model to use
* @param temperature the temperature parameter for generating chat responses
* @param topP the top-p parameter for generating chat responses
* @param maxTokens the maximum number of new tokens to generate in a chat response
* @param safePrompt a flag indicating whether to use a safe prompt for generating chat responses
* @param randomSeed the random seed for generating chat responses
* (if not specified, a random number is used)
* @param responseFormat the response format for generating chat responses. Current values supported are "text" and "json_object".
* @param logRequests a flag indicating whether to log raw HTTP requests
* @param logResponses a flag indicating whether to log raw HTTP responses
* @param timeout the timeout duration for API requests
*/
@Builder
public MistralAiStreamingChatModel(String baseUrl,
String apiKey,
String modelName,
Double temperature,
Double topP,
Integer maxTokens,
Boolean safePrompt,
Integer randomSeed,
String responseFormat,
Boolean logRequests,
Boolean logResponses,
Duration timeout) {
this.client = MistralAiClient.builder()
.baseUrl(getOrDefault(baseUrl, MISTRALAI_API_URL))
.apiKey(apiKey)
.timeout(getOrDefault(timeout, Duration.ofSeconds(60)))
.logRequests(getOrDefault(logRequests, false))
.logResponses(getOrDefault(logResponses, false))
.build();
this.modelName = getOrDefault(modelName, MistralAiChatModelName.OPEN_MISTRAL_7B.toString());
this.temperature = temperature;
this.topP = topP;
this.maxTokens = maxTokens;
this.safePrompt = safePrompt;
this.randomSeed = randomSeed;
this.responseFormat = responseFormat;
}
/**
* Creates a MistralAiStreamingChatModel with the specified API key.
*
* @param apiKey the API key for authentication
* @return a MistralAiStreamingChatModel instance
*/
public static MistralAiStreamingChatModel withApiKey(String apiKey) {
return builder().apiKey(apiKey).build();
}
/**
* Generates streamed token response based on the given list of messages and tool specifications.
*
* @param messages the list of chat messages
* @param toolSpecifications the list of tool specifications. tool_choice is set to AUTO.
* @param handler the response handler for processing the generated chat chunk responses
*/
@Override
public void generate(List<ChatMessage> messages, List<ToolSpecification> toolSpecifications, StreamingResponseHandler<AiMessage> handler) {
generate(messages, toolSpecifications, null, handler);
}
/**
* Generates streamed token response based on the given list of messages and tool specification.
*
* @param messages the list of chat messages
* @param toolSpecification the tool specification. tool_choice is set to ANY.
* @param handler the response handler for processing the generated chat chunk responses
*/
@Override
public void generate(List<ChatMessage> messages, ToolSpecification toolSpecification, StreamingResponseHandler<AiMessage> handler) {
generate(messages, null,toolSpecification, handler);
}
/**
* Generates streamed token response based on the given list of messages.
*
* @param messages the list of chat messages
* @param handler the response handler for processing the generated chat chunk responses
*/
@Override
public void generate(List<ChatMessage> messages, StreamingResponseHandler<AiMessage> handler) {
generate(messages, null, null, handler);
}
private void generate(List<ChatMessage> messages,
List<ToolSpecification> toolSpecifications,
ToolSpecification toolThatMustBeExecuted,
StreamingResponseHandler<AiMessage> handler){<FILL_FUNCTION_BODY>}
public static MistralAiStreamingChatModelBuilder builder() {
for (MistralAiStreamingChatModelBuilderFactory factory : loadFactories(MistralAiStreamingChatModelBuilderFactory.class)) {
return factory.get();
}
return new MistralAiStreamingChatModelBuilder();
}
public static class MistralAiStreamingChatModelBuilder {
public MistralAiStreamingChatModelBuilder() {
}
public MistralAiStreamingChatModelBuilder modelName(String modelName) {
this.modelName = modelName;
return this;
}
public MistralAiStreamingChatModelBuilder modelName(MistralAiChatModelName modelName) {
this.modelName = modelName.toString();
return this;
}
public MistralAiStreamingChatModelBuilder responseFormat(String responseFormat) {
this.responseFormat = responseFormat;
return this;
}
public MistralAiStreamingChatModelBuilder responseFormat(MistralAiResponseFormatType responseFormat) {
this.responseFormat = responseFormat.toString();
return this;
}
}
} |
ensureNotEmpty(messages, "messages");
MistralAiChatCompletionRequest.MistralAiChatCompletionRequestBuilder requestBuilder = MistralAiChatCompletionRequest.builder()
.model(this.modelName)
.messages(toMistralAiMessages(messages))
.temperature(this.temperature)
.maxTokens(this.maxTokens)
.topP(this.topP)
.randomSeed(this.randomSeed)
.safePrompt(this.safePrompt)
.stream(true)
.responseFormat(toMistralAiResponseFormat(this.responseFormat));
if (!isNullOrEmpty(toolSpecifications)) {
requestBuilder.tools(toMistralAiTools(toolSpecifications));
requestBuilder.toolChoice(MistralAiToolChoiceName.AUTO);
} else if (toolThatMustBeExecuted != null) {
requestBuilder.tools(toMistralAiTools(singletonList(toolThatMustBeExecuted)));
requestBuilder.toolChoice(MistralAiToolChoiceName.ANY); // MistralAi does not support toolChoice as Function object. ANY force to the model to call a function
}
MistralAiChatCompletionRequest request = requestBuilder.build();
client.streamingChatCompletion(request, handler);
| 1,493 | 346 | 1,839 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-mongodb-atlas/src/main/java/dev/langchain4j/store/embedding/mongodb/MappingUtils.java | MappingUtils | writeMetadata | class MappingUtils {
private MappingUtils() throws InstantiationException {
throw new InstantiationException("can't instantiate this class");
}
static MongoDbDocument toMongoDbDocument(String id, Embedding embedding, TextSegment textSegment) {
if (textSegment == null) {
return new MongoDbDocument(id, embedding.vectorAsList(), null, null);
}
return new MongoDbDocument(id, embedding.vectorAsList(), textSegment.text(), textSegment.metadata().asMap());
}
static EmbeddingMatch<TextSegment> toEmbeddingMatch(MongoDbMatchedDocument matchedDocument) {
TextSegment textSegment = null;
if (matchedDocument.getText() != null) {
textSegment = matchedDocument.getMetadata() == null ? TextSegment.from(matchedDocument.getText()) :
TextSegment.from(matchedDocument.getText(), Metadata.from(matchedDocument.getMetadata()));
}
return new EmbeddingMatch<>(matchedDocument.getScore(), matchedDocument.getId(), Embedding.from(matchedDocument.getEmbedding()), textSegment);
}
static Document fromIndexMapping(IndexMapping indexMapping) {
Document mapping = new Document();
mapping.append("dynamic", false);
Document fields = new Document();
writeEmbedding(indexMapping.getDimension(), fields);
Set<String> metadataFields = indexMapping.getMetadataFieldNames();
if (metadataFields != null && !metadataFields.isEmpty()) {
writeMetadata(metadataFields, fields);
}
mapping.append("fields", fields);
return new Document("mappings", mapping);
}
private static void writeMetadata(Set<String> metadataFields, Document fields) {<FILL_FUNCTION_BODY>}
private static void writeMetadataField(Document metadataFieldDoc, String fieldName) {
Document field = new Document();
field.append("type", "token");
metadataFieldDoc.append(fieldName, field);
}
private static void writeEmbedding(int dimensions, Document fields) {
Document embedding = new Document();
embedding.append("dimensions", dimensions);
embedding.append("similarity", "cosine");
embedding.append("type", "knnVector");
fields.append("embedding", embedding);
}
} |
Document metadata = new Document();
metadata.append("dynamic", false);
metadata.append("type", "document");
Document metadataFieldDoc = new Document();
metadataFields.forEach(field -> writeMetadataField(metadataFieldDoc, field));
metadata.append("fields", metadataFieldDoc);
fields.append("metadata", metadata);
| 579 | 87 | 666 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-mongodb-atlas/src/main/java/dev/langchain4j/store/embedding/mongodb/MongoDbEmbeddingStore.java | Builder | findRelevant | class Builder {
private MongoClient mongoClient;
private String databaseName;
private String collectionName;
private String indexName;
private Long maxResultRatio;
private CreateCollectionOptions createCollectionOptions;
private Bson filter;
private IndexMapping indexMapping;
/**
* Whether MongoDB Atlas is deployed in cloud
*
* <p>if true, you need to create index in <a href="https://cloud.mongodb.com/">MongoDB Atlas</a></p>
* <p>if false, {@link MongoDbEmbeddingStore} will create collection and index automatically</p>
*/
private Boolean createIndex;
/**
* Build Mongo Client, Please close the client to release resources after usage
*/
public Builder fromClient(MongoClient mongoClient) {
this.mongoClient = mongoClient;
return this;
}
public Builder databaseName(String databaseName) {
this.databaseName = databaseName;
return this;
}
public Builder collectionName(String collectionName) {
this.collectionName = collectionName;
return this;
}
public Builder indexName(String indexName) {
this.indexName = indexName;
return this;
}
public Builder maxResultRatio(Long maxResultRatio) {
this.maxResultRatio = maxResultRatio;
return this;
}
public Builder createCollectionOptions(CreateCollectionOptions createCollectionOptions) {
this.createCollectionOptions = createCollectionOptions;
return this;
}
/**
* Document query filter, all fields included in filter must be contained in {@link IndexMapping#metadataFieldNames}
*
* <p>For example:</p>
*
* <ul>
* <li>AND filter: Filters.and(Filters.in("type", asList("TXT", "md")), Filters.eqFull("test-key", "test-value"))</li>
* <li>OR filter: Filters.or(Filters.in("type", asList("TXT", "md")), Filters.eqFull("test-key", "test-value"))</li>
* </ul>
*
* @param filter document query filter
* @return builder
*/
public Builder filter(Bson filter) {
this.filter = filter;
return this;
}
/**
* set MongoDB search index fields mapping
*
* <p>if {@link Builder#createIndex} is true, then indexMapping not work</p>
*
* @param indexMapping MongoDB search index fields mapping
* @return builder
*/
public Builder indexMapping(IndexMapping indexMapping) {
this.indexMapping = indexMapping;
return this;
}
/**
* Set whether in production mode, production mode will not create index automatically
*
* <p>default value is false</p>
*
* @param createIndex whether in production mode
* @return builder
*/
public Builder createIndex(Boolean createIndex) {
this.createIndex = createIndex;
return this;
}
public MongoDbEmbeddingStore build() {
return new MongoDbEmbeddingStore(mongoClient, databaseName, collectionName, indexName, maxResultRatio, createCollectionOptions, filter, indexMapping, createIndex);
}
}
@Override
public String add(Embedding embedding) {
String id = randomUUID();
add(id, embedding);
return id;
}
@Override
public void add(String id, Embedding embedding) {
addInternal(id, embedding, null);
}
@Override
public String add(Embedding embedding, TextSegment textSegment) {
String id = randomUUID();
addInternal(id, embedding, textSegment);
return id;
}
@Override
public List<String> addAll(List<Embedding> embeddings) {
List<String> ids = embeddings.stream()
.map(ignored -> randomUUID())
.collect(toList());
addAllInternal(ids, embeddings, null);
return ids;
}
@Override
public List<String> addAll(List<Embedding> embeddings, List<TextSegment> embedded) {
List<String> ids = embeddings.stream()
.map(ignored -> randomUUID())
.collect(toList());
addAllInternal(ids, embeddings, embedded);
return ids;
}
@Override
public List<EmbeddingMatch<TextSegment>> findRelevant(Embedding referenceEmbedding, int maxResults, double minScore) {<FILL_FUNCTION_BODY> |
List<Double> queryVector = referenceEmbedding.vectorAsList().stream()
.map(Float::doubleValue)
.collect(toList());
long numCandidates = maxResults * maxResultRatio;
List<Bson> pipeline = Arrays.asList(
vectorSearch(
fieldPath("embedding"),
queryVector,
indexName,
numCandidates,
maxResults,
vectorSearchOptions),
project(
fields(
metaVectorSearchScore("score"),
include("embedding", "metadata", "text")
)
),
match(
Filters.gte("score", minScore)
));
try {
AggregateIterable<MongoDbMatchedDocument> results = collection.aggregate(pipeline, MongoDbMatchedDocument.class);
return StreamSupport.stream(results.spliterator(), false)
.map(MappingUtils::toEmbeddingMatch)
.collect(Collectors.toList());
} catch (MongoCommandException e) {
if (log.isErrorEnabled()) {
log.error("Error in MongoDBEmbeddingStore.findRelevant", e);
}
throw new RuntimeException(e);
}
| 1,205 | 315 | 1,520 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-neo4j/src/main/java/dev/langchain4j/rag/content/retriever/neo4j/Neo4jContentRetriever.java | Neo4jContentRetriever | executeQuery | class Neo4jContentRetriever implements ContentRetriever {
private static final PromptTemplate DEFAULT_PROMPT_TEMPLATE = PromptTemplate.from("""
Based on the Neo4j graph schema below, write a Cypher query that would answer the user's question:
{{schema}}
Question: {{question}}
Cypher query:
""");
private static final Pattern BACKTICKS_PATTERN = Pattern.compile("```(.*?)```", Pattern.MULTILINE | Pattern.DOTALL);
private final Neo4jGraph graph;
private final ChatLanguageModel chatLanguageModel;
private final PromptTemplate promptTemplate;
@Builder
public Neo4jContentRetriever(Neo4jGraph graph, ChatLanguageModel chatLanguageModel, PromptTemplate promptTemplate) {
this.graph = ensureNotNull(graph, "graph");
this.chatLanguageModel = ensureNotNull(chatLanguageModel, "chatLanguageModel");
this.promptTemplate = getOrDefault(promptTemplate, DEFAULT_PROMPT_TEMPLATE);
}
@Override
public List<Content> retrieve(Query query) {
String question = query.text();
String schema = graph.getSchema();
String cypherQuery = generateCypherQuery(schema, question);
List<String> response = executeQuery(cypherQuery);
return response.stream().map(Content::from).toList();
}
private String generateCypherQuery(String schema, String question) {
Prompt cypherPrompt = promptTemplate.apply(Map.of("schema", schema, "question", question));
String cypherQuery = chatLanguageModel.generate(cypherPrompt.text());
Matcher matcher = BACKTICKS_PATTERN.matcher(cypherQuery);
if (matcher.find()) {
return matcher.group(1);
}
return cypherQuery;
}
private List<String> executeQuery(String cypherQuery) {<FILL_FUNCTION_BODY>}
} |
List<Record> records = graph.executeRead(cypherQuery);
return records.stream()
.flatMap(r -> r.values().stream())
.map(value -> value instanceof NodeValue ? value.asMap().toString() : value.toString())
.toList();
| 528 | 75 | 603 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-neo4j/src/main/java/dev/langchain4j/store/embedding/neo4j/Neo4jEmbeddingUtils.java | Neo4jEmbeddingUtils | toRecord | class Neo4jEmbeddingUtils {
/* not-configurable strings, just used under-the-hood in `UNWIND $rows ...` statement */
public static final String EMBEDDINGS_ROW_KEY = "embeddingRow";
/* default configs */
public static final String DEFAULT_ID_PROP = "id";
public static final String DEFAULT_DATABASE_NAME = "neo4j";
public static final String DEFAULT_EMBEDDING_PROP = "embedding";
public static final String PROPS = "props";
public static final String DEFAULT_IDX_NAME = "vector";
public static final String DEFAULT_LABEL = "Document";
public static final String DEFAULT_TEXT_PROP = "text";
public static final long DEFAULT_AWAIT_INDEX_TIMEOUT = 60L;
public static EmbeddingMatch<TextSegment> toEmbeddingMatch(Neo4jEmbeddingStore store, Record neo4jRecord) {
Map<String, String> metaData = new HashMap<>();
neo4jRecord.get("metadata").asMap().forEach((key, value) -> {
if (!store.getNotMetaKeys().contains(key)) {
String stringValue = value == null ? null : value.toString();
metaData.put(key.replace(store.getMetadataPrefix(), ""), stringValue);
}
});
Metadata metadata = new Metadata(metaData);
Value text = neo4jRecord.get(store.getTextProperty());
TextSegment textSegment = text.isNull()
? null
: TextSegment.from(text.asString(), metadata);
List<Float> embeddingList = neo4jRecord.get(store.getEmbeddingProperty())
.asList(Value::asFloat);
Embedding embedding = Embedding.from(embeddingList);
return new EmbeddingMatch<>(neo4jRecord.get("score").asDouble(),
neo4jRecord.get(store.getIdProperty()).asString(),
embedding,
textSegment);
}
public static Map<String, Object> toRecord(Neo4jEmbeddingStore store, int idx, List<String> ids, List<Embedding> embeddings, List<TextSegment> embedded) {<FILL_FUNCTION_BODY>}
public static Stream<List<Map<String, Object>>> getRowsBatched(Neo4jEmbeddingStore store, List<String> ids, List<Embedding> embeddings, List<TextSegment> embedded) {
int batchSize = 10_000;
AtomicInteger batchCounter = new AtomicInteger();
int total = ids.size();
int batchNumber = (int) Math.ceil( (double) total / batchSize );
return IntStream.range(0, batchNumber)
.mapToObj(part -> {
List<Map<String, Object>> maps = ids.subList(Math.min(part * batchSize, total), Math.min((part + 1) * batchSize, total))
.stream()
.map(i -> toRecord(store, batchCounter.getAndIncrement(), ids, embeddings, embedded))
.toList();
return maps;
}
);
}
public static String sanitizeOrThrows(String value, String config) {
return sanitize(value)
.orElseThrow(() -> {
String invalidSanitizeValue = String.format("The value %s, to assign to configuration %s, cannot be safely quoted",
value,
config);
return new RuntimeException(invalidSanitizeValue);
});
}
} |
String id = ids.get(idx);
Embedding embedding = embeddings.get(idx);
Map<String, Object> row = new HashMap<>();
row.put(store.getIdProperty(), id);
Map<String, Object> properties = new HashMap<>();
if (embedded != null) {
TextSegment segment = embedded.get(idx);
properties.put(store.getTextProperty(), segment.text());
Map<String, String> metadata = segment.metadata().asMap();
metadata.forEach((k, v) -> properties.put(store.getMetadataPrefix() + k, Values.value(v)));
}
row.put(EMBEDDINGS_ROW_KEY, Values.value(embedding.vector()));
row.put(PROPS, properties);
return row;
| 906 | 206 | 1,112 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-neo4j/src/main/java/dev/langchain4j/store/graph/neo4j/Neo4jGraph.java | Neo4jGraph | refreshSchema | class Neo4jGraph implements AutoCloseable {
private static final String NODE_PROPERTIES_QUERY = """
CALL apoc.meta.data()
YIELD label, other, elementType, type, property
WHERE NOT type = "RELATIONSHIP" AND elementType = "node"
WITH label AS nodeLabels, collect({property:property, type:type}) AS properties
RETURN {labels: nodeLabels, properties: properties} AS output
""";
private static final String REL_PROPERTIES_QUERY = """
CALL apoc.meta.data()
YIELD label, other, elementType, type, property
WHERE NOT type = "RELATIONSHIP" AND elementType = "relationship"
WITH label AS nodeLabels, collect({property:property, type:type}) AS properties
RETURN {type: nodeLabels, properties: properties} AS output
""";
private static final String RELATIONSHIPS_QUERY = """
CALL apoc.meta.data()
YIELD label, other, elementType, type, property
WHERE type = "RELATIONSHIP" AND elementType = "node"
UNWIND other AS other_node
RETURN {start: label, type: property, end: toString(other_node)} AS output
""";
private final Driver driver;
@Getter
private String schema;
@Builder
public Neo4jGraph(final Driver driver) {
this.driver = ensureNotNull(driver, "driver");
this.driver.verifyConnectivity();
try {
refreshSchema();
} catch (ClientException e) {
if ("Neo.ClientError.Procedure.ProcedureNotFound".equals(e.code())) {
throw new Neo4jException("Please ensure the APOC plugin is installed in Neo4j", e);
}
throw e;
}
}
public ResultSummary executeWrite(String queryString) {
try (Session session = this.driver.session()) {
return session.executeWrite(tx -> tx.run(queryString).consume());
} catch (ClientException e) {
throw new Neo4jException("Error executing query: " + queryString, e);
}
}
public List<Record> executeRead(String queryString) {
try (Session session = this.driver.session()) {
return session.executeRead(tx -> {
Query query = new Query(queryString);
Result result = tx.run(query);
return result.list();
});
} catch (ClientException e) {
throw new Neo4jException("Error executing query: " + queryString, e);
}
}
public void refreshSchema() {<FILL_FUNCTION_BODY>}
private List<String> formatNodeProperties(List<Record> records) {
return records.stream()
.map(this::getOutput)
.map(r -> String.format("%s %s", r.asMap().get("labels"), formatMap(r.get("properties").asList(Value::asMap))))
.toList();
}
private List<String> formatRelationshipProperties(List<Record> records) {
return records.stream()
.map(this::getOutput)
.map(r -> String.format("%s %s", r.get("type"), formatMap(r.get("properties").asList(Value::asMap))))
.toList();
}
private List<String> formatRelationships(List<Record> records) {
return records.stream()
.map(r -> getOutput(r).asMap())
.map(r -> String.format("(:%s)-[:%s]->(:%s)", r.get("start"), r.get("type"), r.get("end")))
.toList();
}
private Value getOutput(Record record) {
return record.get("output");
}
private String formatMap(List<Map<String, Object>> properties) {
return properties.stream()
.map(prop -> prop.get("property") + ":" + prop.get("type"))
.collect(Collectors.joining(", ", "{", "}"));
}
@Override
public void close() {
this.driver.close();
}
} |
List<String> nodeProperties = formatNodeProperties(executeRead(NODE_PROPERTIES_QUERY));
List<String> relationshipProperties = formatRelationshipProperties(executeRead(REL_PROPERTIES_QUERY));
List<String> relationships = formatRelationships(executeRead(RELATIONSHIPS_QUERY));
this.schema = "Node properties are the following:\n" +
String.join("\n", nodeProperties) + "\n\n" +
"Relationship properties are the following:\n" +
String.join("\n", relationshipProperties) + "\n\n" +
"The relationships are the following:\n" +
String.join("\n", relationships);
| 1,095 | 171 | 1,266 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-nomic/src/main/java/dev/langchain4j/model/nomic/NomicClient.java | NomicClient | toException | class NomicClient {
private static final Gson GSON = new GsonBuilder()
.setFieldNamingPolicy(LOWER_CASE_WITH_UNDERSCORES)
.setPrettyPrinting()
.create();
private final NomicApi nomicApi;
private final String authorizationHeader;
@Builder
NomicClient(String baseUrl, String apiKey, Duration timeout, Boolean logRequests, Boolean logResponses) {
OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder()
.callTimeout(timeout)
.connectTimeout(timeout)
.readTimeout(timeout)
.writeTimeout(timeout);
if (logRequests) {
okHttpClientBuilder.addInterceptor(new RequestLoggingInterceptor());
}
if (logResponses) {
okHttpClientBuilder.addInterceptor(new ResponseLoggingInterceptor());
}
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(okHttpClientBuilder.build())
.addConverterFactory(GsonConverterFactory.create(GSON))
.build();
this.nomicApi = retrofit.create(NomicApi.class);
this.authorizationHeader = "Bearer " + ensureNotBlank(apiKey, "apiKey");
}
public EmbeddingResponse embed(EmbeddingRequest request) {
try {
retrofit2.Response<EmbeddingResponse> retrofitResponse
= nomicApi.embed(request, authorizationHeader).execute();
if (retrofitResponse.isSuccessful()) {
return retrofitResponse.body();
} else {
throw toException(retrofitResponse);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static RuntimeException toException(retrofit2.Response<?> response) throws IOException {<FILL_FUNCTION_BODY>}
} |
int code = response.code();
String body = response.errorBody().string();
String errorMessage = String.format("status code: %s; body: %s", code, body);
return new RuntimeException(errorMessage);
| 497 | 59 | 556 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-nomic/src/main/java/dev/langchain4j/model/nomic/NomicEmbeddingModel.java | NomicEmbeddingModel | embedAll | class NomicEmbeddingModel implements EmbeddingModel {
private static final String DEFAULT_BASE_URL = "https://api-atlas.nomic.ai/v1/";
private final NomicClient client;
private final String modelName;
private final String taskType;
private final Integer maxRetries;
@Builder
public NomicEmbeddingModel(
String baseUrl,
String apiKey,
String modelName,
String taskType,
Duration timeout,
Integer maxRetries,
Boolean logRequests,
Boolean logResponses
) {
this.client = NomicClient.builder()
.baseUrl(getOrDefault(baseUrl, DEFAULT_BASE_URL))
.apiKey(ensureNotBlank(apiKey, "apiKey"))
.timeout(getOrDefault(timeout, ofSeconds(60)))
.logRequests(getOrDefault(logRequests, false))
.logResponses(getOrDefault(logResponses, false))
.build();
this.modelName = getOrDefault(modelName, "nomic-embed-text-v1");
this.taskType = taskType;
this.maxRetries = getOrDefault(maxRetries, 3);
}
public static NomicEmbeddingModel withApiKey(String apiKey) {
return NomicEmbeddingModel.builder().apiKey(apiKey).build();
}
@Override
public Response<List<Embedding>> embedAll(List<TextSegment> textSegments) {<FILL_FUNCTION_BODY>}
} |
EmbeddingRequest request = EmbeddingRequest.builder()
.model(modelName)
.texts(textSegments.stream().map(TextSegment::text).collect(toList()))
.taskType(taskType)
.build();
EmbeddingResponse response = withRetry(() -> client.embed(request), maxRetries);
List<Embedding> embeddings = response.getEmbeddings().stream()
.map(Embedding::from).collect(toList());
TokenUsage tokenUsage = new TokenUsage(response.getUsage().getTotalTokens(), 0);
return Response.from(embeddings, tokenUsage);
| 399 | 166 | 565 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-nomic/src/main/java/dev/langchain4j/model/nomic/RequestLoggingInterceptor.java | RequestLoggingInterceptor | getBody | class RequestLoggingInterceptor implements Interceptor {
private static final Logger log = LoggerFactory.getLogger(RequestLoggingInterceptor.class);
private static final Pattern BEARER_PATTERN = Pattern.compile("(Bearer\\s*)([\\w-]{5})([\\w-]+)([\\w-]{2})");
public Response intercept(Interceptor.Chain chain) throws IOException {
Request request = chain.request();
log(request);
return chain.proceed(request);
}
private void log(Request request) {
log.debug(
"Request:\n" +
"- method: {}\n" +
"- url: {}\n" +
"- headers: {}\n" +
"- body: {}",
request.method(),
request.url(),
inOneLine(request.headers()),
getBody(request)
);
}
static String inOneLine(Headers headers) {
return stream(headers.spliterator(), false)
.map((header) -> {
String headerKey = header.component1();
String headerValue = header.component2();
if (headerKey.equals("Authorization")) {
headerValue = maskAuthorizationHeaderValue(headerValue);
}
return String.format("[%s: %s]", headerKey, headerValue);
}).collect(Collectors.joining(", "));
}
private static String maskAuthorizationHeaderValue(String authorizationHeaderValue) {
try {
Matcher matcher = BEARER_PATTERN.matcher(authorizationHeaderValue);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, matcher.group(1) + matcher.group(2) + "..." + matcher.group(4));
}
matcher.appendTail(sb);
return sb.toString();
} catch (Exception e) {
return "[failed to mask the API key]";
}
}
private static String getBody(Request request) {<FILL_FUNCTION_BODY>}
} |
try {
Buffer buffer = new Buffer();
request.body().writeTo(buffer);
return buffer.readUtf8();
} catch (Exception e) {
log.warn("Exception happened while reading request body", e);
return "[Exception happened while reading request body. Check logs for more details.]";
}
| 538 | 83 | 621 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-nomic/src/main/java/dev/langchain4j/model/nomic/ResponseLoggingInterceptor.java | ResponseLoggingInterceptor | log | class ResponseLoggingInterceptor implements Interceptor {
private static final Logger log = LoggerFactory.getLogger(ResponseLoggingInterceptor.class);
public Response intercept(Interceptor.Chain chain) throws IOException {
Request request = chain.request();
Response response = chain.proceed(request);
log(response);
return response;
}
void log(Response response) {<FILL_FUNCTION_BODY>}
private String getBody(Response response) {
try {
return response.peekBody(Long.MAX_VALUE).string();
} catch (IOException e) {
log.warn("Failed to log response", e);
return "[failed to log response]";
}
}
} |
log.debug(
"Response:\n" +
"- status code: {}\n" +
"- headers: {}\n" +
"- body: {}",
response.code(),
inOneLine(response.headers()),
getBody(response)
);
| 187 | 71 | 258 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-ollama/src/main/java/dev/langchain4j/model/ollama/ImageUtils.java | ImageUtils | base64Image | class ImageUtils {
private static final List<String> SUPPORTED_URL_SCHEMES = Arrays.asList("http", "https", "file");
static List<String> base64EncodeImageList(List<ImageContent> contentList) {
return contentList.stream()
.map(ImageContent::image)
.map(ImageUtils::base64Image)
.collect(Collectors.toList());
}
static String base64Image(Image image) {<FILL_FUNCTION_BODY>}
private static String httpScheme(Image image) {
byte[] imageBytes = Utils.readBytes(image.url().toString());
return Base64.getEncoder().encodeToString(imageBytes);
}
private static String fileScheme(Image image) {
byte[] fileBytes = readAllBytes(Paths.get(image.url()));
return Base64.getEncoder().encodeToString(fileBytes);
}
private static byte[] readAllBytes(Path path) {
try {
return Files.readAllBytes(path);
} catch (IOException e) {
throw new RuntimeException("cant read file", e);
}
}
} |
if (image.base64Data() != null && !image.base64Data().isEmpty()) {
return image.base64Data();
} else {
if (SUPPORTED_URL_SCHEMES.contains(image.url().getScheme())) {
return image.url().getScheme().startsWith("http") ? httpScheme(image) : fileScheme(image);
} else {
throw new RuntimeException("ollama integration only supports http/https and file urls. unsupported url scheme: " + image.url().getScheme());
}
}
| 304 | 148 | 452 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-ollama/src/main/java/dev/langchain4j/model/ollama/OllamaChatModel.java | OllamaChatModel | generate | class OllamaChatModel implements ChatLanguageModel {
private final OllamaClient client;
private final String modelName;
private final Options options;
private final String format;
private final Integer maxRetries;
@Builder
public OllamaChatModel(String baseUrl,
String modelName,
Double temperature,
Integer topK,
Double topP,
Double repeatPenalty,
Integer seed,
Integer numPredict,
Integer numCtx,
List<String> stop,
String format,
Duration timeout,
Integer maxRetries) {
this.client = OllamaClient.builder()
.baseUrl(baseUrl)
.timeout(getOrDefault(timeout, ofSeconds(60)))
.build();
this.modelName = ensureNotBlank(modelName, "modelName");
this.options = Options.builder()
.temperature(temperature)
.topK(topK)
.topP(topP)
.repeatPenalty(repeatPenalty)
.seed(seed)
.numPredict(numPredict)
.numCtx(numCtx)
.stop(stop)
.build();
this.format = format;
this.maxRetries = getOrDefault(maxRetries, 3);
}
@Override
public Response<AiMessage> generate(List<ChatMessage> messages) {<FILL_FUNCTION_BODY>}
public static OllamaChatModelBuilder builder() {
for (OllamaChatModelBuilderFactory factory : loadFactories(OllamaChatModelBuilderFactory.class)) {
return factory.get();
}
return new OllamaChatModelBuilder();
}
public static class OllamaChatModelBuilder {
public OllamaChatModelBuilder() {
// This is public so it can be extended
// By default with Lombok it becomes package private
}
}
} |
ensureNotEmpty(messages, "messages");
ChatRequest request = ChatRequest.builder()
.model(modelName)
.messages(toOllamaMessages(messages))
.options(options)
.format(format)
.stream(false)
.build();
ChatResponse response = withRetry(() -> client.chat(request), maxRetries);
return Response.from(
AiMessage.from(response.getMessage().getContent()),
new TokenUsage(response.getPromptEvalCount(), response.getEvalCount())
);
| 491 | 145 | 636 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-ollama/src/main/java/dev/langchain4j/model/ollama/OllamaClient.java | OllamaClient | onResponse | class OllamaClient {
private static final Gson GSON = new GsonBuilder()
.setFieldNamingPolicy(LOWER_CASE_WITH_UNDERSCORES)
.create();
private final OllamaApi ollamaApi;
@Builder
public OllamaClient(String baseUrl, Duration timeout) {
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.callTimeout(timeout)
.connectTimeout(timeout)
.readTimeout(timeout)
.writeTimeout(timeout)
.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(GSON))
.build();
ollamaApi = retrofit.create(OllamaApi.class);
}
public CompletionResponse completion(CompletionRequest request) {
try {
retrofit2.Response<CompletionResponse> retrofitResponse
= ollamaApi.completion(request).execute();
if (retrofitResponse.isSuccessful()) {
return retrofitResponse.body();
} else {
throw toException(retrofitResponse);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public ChatResponse chat(ChatRequest request) {
try {
retrofit2.Response<ChatResponse> retrofitResponse
= ollamaApi.chat(request).execute();
if (retrofitResponse.isSuccessful()) {
return retrofitResponse.body();
} else {
throw toException(retrofitResponse);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void streamingCompletion(CompletionRequest request, StreamingResponseHandler<String> handler) {
ollamaApi.streamingCompletion(request).enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> retrofitResponse) {<FILL_FUNCTION_BODY>}
@Override
public void onFailure(Call<ResponseBody> call, Throwable throwable) {
handler.onError(throwable);
}
});
}
public void streamingChat(ChatRequest request, StreamingResponseHandler<AiMessage> handler) {
ollamaApi.streamingChat(request).enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> retrofitResponse) {
try (InputStream inputStream = retrofitResponse.body().byteStream()) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
StringBuilder contentBuilder = new StringBuilder();
while (true) {
String partialResponse = reader.readLine();
ChatResponse chatResponse = GSON.fromJson(partialResponse, ChatResponse.class);
String content = chatResponse.getMessage().getContent();
contentBuilder.append(content);
handler.onNext(content);
if (TRUE.equals(chatResponse.getDone())) {
Response<AiMessage> response = Response.from(
AiMessage.from(contentBuilder.toString()),
new TokenUsage(
chatResponse.getPromptEvalCount(),
chatResponse.getEvalCount()
)
);
handler.onComplete(response);
return;
}
}
}
} catch (Exception e) {
handler.onError(e);
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable throwable) {
handler.onError(throwable);
}
});
}
public EmbeddingResponse embed(EmbeddingRequest request) {
try {
retrofit2.Response<EmbeddingResponse> retrofitResponse = ollamaApi.embedd(request).execute();
if (retrofitResponse.isSuccessful()) {
return retrofitResponse.body();
} else {
throw toException(retrofitResponse);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public ModelsListResponse listModels() {
try {
retrofit2.Response<ModelsListResponse> retrofitResponse = ollamaApi.listModels().execute();
if (retrofitResponse.isSuccessful()) {
return retrofitResponse.body();
} else {
throw toException(retrofitResponse);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public OllamaModelCard showInformation(ShowModelInformationRequest showInformationRequest) {
try {
retrofit2.Response<OllamaModelCard> retrofitResponse = ollamaApi.showInformation(showInformationRequest).execute();
if (retrofitResponse.isSuccessful()) {
return retrofitResponse.body();
} else {
throw toException(retrofitResponse);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private RuntimeException toException(retrofit2.Response<?> response) throws IOException {
int code = response.code();
String body = response.errorBody().string();
String errorMessage = String.format("status code: %s; body: %s", code, body);
return new RuntimeException(errorMessage);
}
} |
try (InputStream inputStream = retrofitResponse.body().byteStream()) {
StringBuilder contentBuilder = new StringBuilder();
while (true) {
byte[] bytes = new byte[1024];
int len = inputStream.read(bytes);
String partialResponse = new String(bytes, 0, len);
CompletionResponse completionResponse = GSON.fromJson(partialResponse, CompletionResponse.class);
contentBuilder.append(completionResponse.getResponse());
handler.onNext(completionResponse.getResponse());
if (TRUE.equals(completionResponse.getDone())) {
Response<String> response = Response.from(
contentBuilder.toString(),
new TokenUsage(
completionResponse.getPromptEvalCount(),
completionResponse.getEvalCount()
)
);
handler.onComplete(response);
return;
}
}
} catch (Exception e) {
handler.onError(e);
}
| 1,392 | 244 | 1,636 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-ollama/src/main/java/dev/langchain4j/model/ollama/OllamaEmbeddingModel.java | OllamaEmbeddingModel | embedAll | class OllamaEmbeddingModel implements EmbeddingModel {
private final OllamaClient client;
private final String modelName;
private final Integer maxRetries;
@Builder
public OllamaEmbeddingModel(String baseUrl,
String modelName,
Duration timeout,
Integer maxRetries) {
this.client = OllamaClient.builder()
.baseUrl(baseUrl)
.timeout(getOrDefault(timeout, ofSeconds(60)))
.build();
this.modelName = ensureNotBlank(modelName, "modelName");
this.maxRetries = getOrDefault(maxRetries, 3);
}
@Override
public Response<List<Embedding>> embedAll(List<TextSegment> textSegments) {<FILL_FUNCTION_BODY>}
public static OllamaEmbeddingModelBuilder builder() {
for (OllamaEmbeddingModelBuilderFactory factory : loadFactories(OllamaEmbeddingModelBuilderFactory.class)) {
return factory.get();
}
return new OllamaEmbeddingModelBuilder();
}
public static class OllamaEmbeddingModelBuilder {
public OllamaEmbeddingModelBuilder() {
// This is public so it can be extended
// By default with Lombok it becomes package private
}
}
} |
List<Embedding> embeddings = new ArrayList<>();
textSegments.forEach(textSegment -> {
EmbeddingRequest request = EmbeddingRequest.builder()
.model(modelName)
.prompt(textSegment.text())
.build();
EmbeddingResponse response = withRetry(() -> client.embed(request), maxRetries);
embeddings.add(Embedding.from(response.getEmbedding()));
});
return Response.from(embeddings);
| 343 | 128 | 471 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-ollama/src/main/java/dev/langchain4j/model/ollama/OllamaLanguageModel.java | OllamaLanguageModel | generate | class OllamaLanguageModel implements LanguageModel {
private final OllamaClient client;
private final String modelName;
private final Options options;
private final String format;
private final Integer maxRetries;
@Builder
public OllamaLanguageModel(String baseUrl,
String modelName,
Double temperature,
Integer topK,
Double topP,
Double repeatPenalty,
Integer seed,
Integer numPredict,
Integer numCtx,
List<String> stop,
String format,
Duration timeout,
Integer maxRetries) {
this.client = OllamaClient.builder()
.baseUrl(baseUrl)
.timeout(getOrDefault(timeout, ofSeconds(60)))
.build();
this.modelName = ensureNotBlank(modelName, "modelName");
this.options = Options.builder()
.temperature(temperature)
.topK(topK)
.topP(topP)
.repeatPenalty(repeatPenalty)
.seed(seed)
.numPredict(numPredict)
.numCtx(numCtx)
.stop(stop)
.build();
this.format = format;
this.maxRetries = getOrDefault(maxRetries, 3);
}
@Override
public Response<String> generate(String prompt) {<FILL_FUNCTION_BODY>}
public static OllamaLanguageModelBuilder builder() {
for (OllamaLanguageModelBuilderFactory factory : loadFactories(OllamaLanguageModelBuilderFactory.class)) {
return factory.get();
}
return new OllamaLanguageModelBuilder();
}
public static class OllamaLanguageModelBuilder {
public OllamaLanguageModelBuilder() {
// This is public so it can be extended
// By default with Lombok it becomes package private
}
}
} |
CompletionRequest request = CompletionRequest.builder()
.model(modelName)
.prompt(prompt)
.options(options)
.format(format)
.stream(false)
.build();
CompletionResponse response = withRetry(() -> client.completion(request), maxRetries);
return Response.from(
response.getResponse(),
new TokenUsage(response.getPromptEvalCount(), response.getEvalCount())
);
| 483 | 126 | 609 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-ollama/src/main/java/dev/langchain4j/model/ollama/OllamaMessagesUtils.java | OllamaMessagesUtils | messagesWithImageSupport | class OllamaMessagesUtils {
private final static Predicate<ChatMessage> isUserMessage =
chatMessage -> chatMessage instanceof UserMessage;
private final static Predicate<UserMessage> hasImages =
userMessage -> userMessage.contents().stream()
.anyMatch(content -> IMAGE.equals(content.type()));
static List<Message> toOllamaMessages(List<ChatMessage> messages) {
return messages.stream()
.map(message -> isUserMessage.test(message) && hasImages.test((UserMessage) message) ?
messagesWithImageSupport((UserMessage) message)
: otherMessages(message)
).collect(Collectors.toList());
}
private static Message messagesWithImageSupport(UserMessage userMessage) {<FILL_FUNCTION_BODY>}
private static Message otherMessages(ChatMessage chatMessage) {
return Message.builder()
.role(toOllamaRole(chatMessage.type()))
.content(chatMessage.text())
.build();
}
private static Role toOllamaRole(ChatMessageType chatMessageType) {
switch (chatMessageType) {
case SYSTEM:
return Role.SYSTEM;
case USER:
return Role.USER;
case AI:
return Role.ASSISTANT;
default:
throw new IllegalArgumentException("Unknown ChatMessageType: " + chatMessageType);
}
}
} |
Map<ContentType, List<Content>> groupedContents = userMessage.contents().stream()
.collect(Collectors.groupingBy(Content::type));
if (groupedContents.get(TEXT).size() != 1) {
throw new RuntimeException("Expecting single text content, but got: " + userMessage.contents());
}
String text = ((TextContent) groupedContents.get(TEXT).get(0)).text();
List<ImageContent> imageContents = groupedContents.get(IMAGE).stream()
.map(content -> (ImageContent) content)
.collect(Collectors.toList());
return Message.builder()
.role(toOllamaRole(userMessage.type()))
.content(text)
.images(ImageUtils.base64EncodeImageList(imageContents))
.build();
| 361 | 214 | 575 | <no_super_class> |
langchain4j_langchain4j | langchain4j/langchain4j-ollama/src/main/java/dev/langchain4j/model/ollama/OllamaModels.java | OllamaModels | modelCard | class OllamaModels {
private final OllamaClient client;
private final Integer maxRetries;
@Builder
public OllamaModels(String baseUrl,
Duration timeout,
Integer maxRetries) {
this.client = OllamaClient.builder()
.baseUrl(baseUrl)
.timeout((getOrDefault(timeout, Duration.ofSeconds(60))))
.build();
this.maxRetries = getOrDefault(maxRetries, 3);
}
public Response<List<OllamaModel>> availableModels() {
ModelsListResponse response = withRetry(client::listModels, maxRetries);
return Response.from(response.getModels());
}
public Response<OllamaModelCard> modelCard(OllamaModel ollamaModel) {
return modelCard(ollamaModel.getName());
}
public Response<OllamaModelCard> modelCard(String modelName) {<FILL_FUNCTION_BODY>}
} |
OllamaModelCard response = withRetry(() -> client.showInformation(
ShowModelInformationRequest.builder()
.name(modelName)
.build()
), maxRetries);
return Response.from(response);
| 256 | 61 | 317 | <no_super_class> |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.