Provides programmatic access to OpenAI language models through authenticated requests, with usage charged by input and output tokens. Set a usage limit to control costs. section 6.3.1
A practical integration involves:
- Create an OpenAI account, generate a secret API key, and store it because it cannot be viewed again. section 6.3.1
- Add the LangChain4j OpenAI library, choose a model such as gpt-3.5-turbo or gpt-4o, and send a prompt. section 6.3.2
- Request structured JSON test data, parse it into a Java object, and use it in an automated contact-form check. section 6.3.2
Generated contact-form data
A prompt asks for a name, email, UK-format phone number, a subject over 20 characters, and a description over 50 characters. The returned JSON populates a contact form, whose success message is then asserted. section 6.3.2
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-open-ai</artifactId>
<version>0.31.0</version>
</dependency>
Shows the Maven dependency used to connect Java code with OpenAI through LangChain4j. section 6.3.2



