liveBook · Manning

LangChain4j

As explained in Software Testing with Generative AI

LangChain4j is a Java library for building applications that integrate with large language models.

LangChain4j is a Java library for connecting applications and tests to large language models. It provides OpenAI-specific access for sending prompts and broader AI-services support for managing integrations. section 6.3.2 section 9.4

Common uses include:

  • Generating structured test data from prompts, including JSON fields with format and length constraints. section 6.3.2
  • Connecting LLM-based agents to application tools through function calling, @Tool, and @P annotations. section 9.4
  • Selecting different GPT models while balancing capability and price. section 6.3.2

Generating contact-form data

An OpenAiChatModel receives a delimited prompt requesting a name, email, UK-format phone number, and minimum-length subject and description. The returned JSON is deserialized into ContactFormDetails, entered into a contact form, and checked for a success message. Generated data still requires verification. section 6.3.2

<dependency>
    <groupId>dev.langchain4j</groupId>
    <artifactId>langchain4j-open-ai</artifactId>
    <version>0.31.0</version>
</dependency>

The Maven dependency shows the OpenAI integration used by the Java example. section 6.3.2

How Software Testing with Generative AI covers it

How deep it goes
Demonstrates dependency setup, prompting, response handling, deserialization, and tool annotations, but not library internals.
Examples
Uses an OpenAI contact-form test that generates constrained JSON and maps it into a Java data class.
What it assumes
Assumes Java test code and basic prompt construction while demonstrating OpenAI model access through a library.
What you can do after
Build it from scratch by adding the OpenAI dependency, sending a prompt, parsing JSON, and using the data in a test.
How it connects
Connects large language models, prompt engineering, AI agents, function calling, and automated testing.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Software Testing with Generative AI. The section links point to the parts of the book it draws on.