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

Approaches and Java REST libraries - Java Tutorial

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

Approaches and Java REST libraries

- [Speaker] Now that we know there are rest APIs for writing gen AI applications with Java, let's see how we can proceed. There are a couple of possible approaches. One way is to write our own Java code. Modern Java has excellent libraries for handling Http and Https to connect to a server using REST and get back a result. This is pretty straightforward. You can ask a language model for some code to get started connected to your language model provider of choice. Building simple code to access a REST endpoint is very straightforward, by the way, since you're typically getting results from the model in JSON format, you'll also need JSON parsing code to retrieve the relevant data. Also, your language model provider may release a version incompatible with the code you wrote, so you may have to change that. I personally encountered this unfortunate issue when writing code for an early version of ChatGPT three. You may also have to add production quality connection code for timeouts, retries, error handling, et cetera. So while writing your code as possible and certainly educational, you must take on a lot of additional responsibility as a professional Java developer. Another approach is to use a library from an AI provider. So this is great. So whether it's OpenAI, anthropic, mystral, Google, or any other provider, a library from that provider is very helpful. Typically, the provider only offers a native Python or maybe even a JavaScript API. So you may not always have a native Java API to use, but thankfully, practically, all large language model providers offer a rest interface. Another obvious possibility is that you search GitHub for a library that provides a wrapper around the REST APIs. Of course, if you decide to use a GitHub library, you should check with your company's security and legal department to confirm that that specific library has their seal of approval. Also, having access to the source code is a wonderful thing, but you need to consider who is supporting that library, who is fixing bugs, who is adding new features. These are issues that senior Java developers who work for big companies have to consider. There are two very popular REST-based Java libraries for accessing OpenAI's gen AI services. One of these libraries is LangChain4j. Some developers at Lang Chain initiated this library. Now, LangChain is an open-source developer framework. It's called LangChain because it allows you to chain large language model components to build your applications. It's a very popular framework. Now, the goal of LangChain4j is to simplify the integration of language models into Java applications. It's specifically for Java developers. Very cool. It's an easy to use library that many Java developers use. The LangChain4j library provides a high-level abstraction to multiple language models. It is one API that covers most of the functionality of all of the language models. Also include our abstract APIs for embedding data stores and similarity calculations for building rag systems. It's a very useful unified approach to language models. So you can develop in LangChange4j and use OpenAI's language models and services. And just by changing some configurations, you can then use Anthropics language models or Google's Gemini models without changing your Java API code. So this is very, very helpful. There are other possibilities if you need to write OpenAI specific code. There's one library called Simple OpenAI that you can find on GitHub, which is very popular. It is a very simplified way of programming using the OpenAI APIs.

Contents