From the course: Foundations of AI and Machine Learning for Java Developers

Unlock this course with a free trial

Join today to access over 24,500 courses taught by industry experts.

Demo: Connecting to an LLM

Demo: Connecting to an LLM

- [Instructor] So let's look at some specific Java code to connect to a language model. I have a few examples here, both using pure Java and then using LangChain4j. Now let's look at the first one. So right here, I have simple.java. And simple.java is a very, very short page of code. Here I get the API key and this is the key that you get when you register at the OpenAI site. It's a long encoded string and it's the environment. So I use getenv to grab that string. Then I have the payload that goes to the language model. And for OpenAI, it's this mini language here. I say I want to use this particular model and here are my messages. I'm saying that's a regular user prompt. And the prompt is tell me a good dad joke about cats. So I use Java's text block, the triple double quotes, and then I create an instance of HTTP request, and here's this variable request. I specify the rest endpoint, which is chat slash completions. Tell the language model I want JSON formatted data as a response…

Contents