This example shows how to use OpenAI model in LangChain4j.
Example
The following example uses the demo proxy endpoint http://langchain4j.dev/demo/openai/v1 and the demo key provided by LangChain4j. When using the demo key, all requests to the OpenAI API need to go through the proxy, which injects the real key before forwarding your request to the OpenAI API. The demo key has a quota. This access is restricted to the gpt-4o-mini model and is ideal for educational purposes.
package com.logicbig.example;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
public class OpenAiDemoExample {
public static void main (String[] args) {
ChatModel model = OpenAiChatModel.builder()
.baseUrl("http://langchain4j.dev/demo/openai/v1")
.apiKey("demo")
.modelName("gpt-4o-mini")
.build();
String response = model.chat("Hi there!");
System.out.println(response);
}
}