Skip to content

Instantly share code, notes, and snippets.

View glaforge's full-sized avatar

Guillaume Laforge glaforge

View GitHub Profile
@glaforge
glaforge / CustomMcpClient.java
Created April 2, 2025 20:39
Raw MCP client / server example with the MCP JAVA SDK and LangChain4j's MCP client module
import dev.langchain4j.agent.tool.ToolExecutionRequest;
import dev.langchain4j.mcp.McpToolProvider;
import dev.langchain4j.mcp.client.DefaultMcpClient;
import dev.langchain4j.mcp.client.McpClient;
import dev.langchain4j.mcp.client.transport.McpTransport;
import dev.langchain4j.mcp.client.transport.http.HttpMcpTransport;
import dev.langchain4j.model.vertexai.VertexAiGeminiChatModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.SystemMessage;
import dev.langchain4j.service.tool.ToolProvider;
public class SentenceWindowRetrieval {
public static void main(String[] args) throws IOException {
Document capitalDocument = Document.from("...");
VertexAiEmbeddingModel embeddingModel = VertexAiEmbeddingModel.builder()
.project(System.getenv("GCP_PROJECT_ID"))
.endpoint(System.getenv("GCP_VERTEXAI_ENDPOINT"))
.location(System.getenv("GCP_LOCATION"))
.publisher("google")
@glaforge
glaforge / new-store.md
Created February 16, 2025 13:30
Create a new embedding store

Okay, here's a breakdown of how to create a new LangChain4j embedding store module for Google Cloud Firestore, along with the key steps and considerations, mirroring the structure of existing modules like langchain4j-milvus.

Project Structure

Your project structure should follow the established pattern. I'll create a simplified version based on the most relevant parts from the provided file listing. The full structure would be much larger (like the main langchain4j project), but this captures the essentials:

langchain4j/
└── langchain4j-embedding-store-google-firestore/  (or similar name)
    ├── pom.xml                                     (Your module's Maven build file)
@glaforge
glaforge / new-module.md
Created February 16, 2025 13:28
How to contribute a new LLM module

Okay, let's outline the steps to create a new language model module for LangChain4j and propose its inclusion. Based on the provided file structure, you'll be focusing on creating a new module similar to the existing ones (e.g., langchain4j-open-ai, langchain4j-ollama, etc.). Here's a breakdown of the process, referencing the structure you've provided:

Key Steps and Considerations

  1. Understand the Abstractions and SPI: LangChain4j, like its Python counterpart, is built around core abstractions. You need to understand these to implement your integration correctly. The core abstractions you must implement are:

    • ChatLanguageModel / StreamingChatLanguageModel: For conversational models (like ChatGPT, Gemini). Implement ChatLanguageModel for synchronous responses, and StreamingChatLanguageModel if the model supports streaming responses token by token.
  • LanguageModel / StreamingLanguageModel: For models with a simpler text-in, text-out interface (less com
@glaforge
glaforge / SkyjoCardCounter.java
Created December 13, 2024 17:18
SkyjoCardCounter.java with Gemini 2.0 Flash and LangChain4j
import com.google.gson.Gson;
import dev.langchain4j.data.message.*;
import dev.langchain4j.model.output.Response;
import dev.langchain4j.model.vertexai.SchemaHelper;
import dev.langchain4j.model.vertexai.VertexAiGeminiChatModel;
import java.io.IOException;
import java.nio.file.FileVisitOption;
import java.nio.file.Files;
import java.nio.file.Path;
@glaforge
glaforge / ProgrammingIdioms.java
Last active November 29, 2024 15:56
Version #1 of ProgrammingIdioms with embeddings of the title, the description, and the code
package experiments;
import com.google.cloud.vertexai.api.Schema;
import com.google.cloud.vertexai.api.Type;
import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;
import dev.langchain4j.data.document.Metadata;
import dev.langchain4j.data.embedding.Embedding;
import dev.langchain4j.data.message.SystemMessage;
import dev.langchain4j.data.message.UserMessage;
@glaforge
glaforge / RedactPii.java
Created November 25, 2024 08:52
Redact sensitive information with Google Cloud Data Loss Prevention API before sending it to a Large Language Model
package experiments;
import com.google.cloud.dlp.v2.DlpServiceClient;
import com.google.privacy.dlp.v2.*;
import dev.langchain4j.model.vertexai.VertexAiGeminiChatModel;
import java.io.IOException;
import java.util.stream.Stream;
public class RedactPii {
@glaforge
glaforge / CallGemma.java
Created April 2, 2024 11:34
Ollama conditional image creation
package gemini.workshop;
import com.github.dockerjava.api.model.Image;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.ollama.OllamaChatModel;
import org.testcontainers.DockerClientFactory;
import org.testcontainers.ollama.OllamaContainer;
import org.testcontainers.utility.DockerImageName;
import java.io.IOException;
@glaforge
glaforge / CallGemma.java
Created April 2, 2024 08:45
Conditionally create the derived container if it hasn't been already created
package gemini.workshop;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.ollama.OllamaChatModel;
import org.testcontainers.ollama.OllamaContainer;
import org.testcontainers.utility.DockerImageName;
import java.io.IOException;
public class CallGemma {
@glaforge
glaforge / slugifyTest.groovy
Created January 4, 2024 01:25
Creating slugs from any text (including in non-latin languages) using the Slugify library and ICU for transliteration
@Grab('com.ibm.icu:icu4j')
@Grab('com.github.slugify:slugify')
import com.github.slugify.*
var slugify = Slugify.builder()
.transliterator(true)
.locale(Locale.ENGLISH)
.build()
println slugify.slugify("Un bel été où l'hôtel François à rebours des îles de Noël tenait tête")