Skip to content

Commit

Permalink
edits to langchain4j blog draft and addition of image to this post
Browse files Browse the repository at this point in the history
  • Loading branch information
Grace Jansen committed Mar 27, 2024
1 parent 3e7e144 commit f3fda0e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
Binary file added img/blog/langchain4j.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
56 changes: 50 additions & 6 deletions posts/2024-03-28-open-liberty-with-langchain4j-example.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ Gilbert Kwan <https://github.com/gkwan-ibm>
:example-location: https://github.com/langchain4j/langchain4j-examples/tree/main/jakartaee-microprofile-example


"Large language models (LLMs) are a category of foundation models trained on immense amounts of data making them capable of understanding and generating natural language and other types of content to perform a wide range of tasks." ... _from https://www.ibm.com/topics/large-language-models_

https://github.com/langchain4j/langchain4j[LangChain4j APIs] provide the abilities to intergate LLMs into your Java application for different AI platforms, such as OpenAI and Hugging Face, etc.

If you want to experience AI technologies, you can try out the `jakartaee-microprofile-example` project in the https://github.com/langchain4j/langchain4j-examples[langchain4j-examples] GitHub repository. It is an example that demostrates how to use LangChain4j APIs in Jakarta EE and MicroProfile application on Open Liberty.
Artificial Intelligence (AI) is an exciting and disruptive field that is already transforming businesses, and even entire industries, by enabling automation, improving decision-making and unlocking new insights from data. With the rise in large language models, or LLMs, such as ChatGPT, over the last few years there has been a significant shift in the performance of AI and its potential to drive enterprise value. So, how will this impact on software development and the creation of cloud-native Java applications for enterprises?

* <<what_Are_LLMs, Find out what LLMs are>>
* <<Java_LLMs, Explore frameworks that simplify how LLMs and generative AI can be applied to applications>>
* <<using_Langchain4j, Discover how LangChain can be used in Java applications>>
* <<tryout, Try out the jakartaee-microprofile-example application>>
- <<env_setup, Environment Set Up>>
- <<start_app, Start the application>>
Expand All @@ -38,9 +37,54 @@ If you want to experience AI technologies, you can try out the `jakartaee-microp
* <<where_to_next, Where to next?>>
* <<help_links, Helpful links>>

[#what_Are_LLMs]
== What are LLMs?

A language model is a model of natural language based on probabilities. They are able to generate probabilities of a series of words together. https://www.ibm.com/topics/large-language-models[Large language models (LLMs)] are simply langauge models that are categorized by their large size.They are trained on immense amounts of data, possibly billions of paramters, using self-supervised and semi-supervised learning techniques. This makes them capable of understanding and generating natural language and other types of content to perform a wide range of tasks, including:

* *Text generation*: language generation abilities, such as writing emails, blog posts or other mid-to-long form content in response to prompts that can be refined and polished. An excellent example is retrieval-augmented generation (RAG).

* *Content summarization*: summarize long articles, news stories, research reports, corporate documentation and even customer history into thorough texts tailored in length to the output format.

* *AI assistants*: chatbots that answer customer queries, perform backend tasks and provide detailed information in natural language as a part of an integrated, self-serve customer care solution.

* *Code generation*: assists developers in building applications, finding errors in code and uncovering security issues in multiple programming languages, even “translating” between them.

* *Sentiment analysis*: analyze text to determine the customer’s tone in order understand customer feedback at scale and aid in brand reputation management.

* *Language translation*: provides wider coverage to organizations across languages and geographies with fluent translations and multilingual capabilities.


See the introductory video below for more information on LLMs, including what they are, how they work and business applications.

<iframe width="560" height="315" src="https://www.youtube.com/embed/5sLYAQS9sWQ?si=X9v5pPdzlK24bCsK" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>

LLMs can be found in the service offerings of almost all of the major cloud service providers, e.g. IBM offers models through it's https://www.ibm.com/watsonx[watsonx] services, https://azure.microsoft.com/en-us/solutions/ai[Microsoft Azure] offers LLMs like Llama 2 and OpenAI GPT-4, and https://aws.amazon.com/bedrock/[Amazon Bedrock] offers models from a range of AI companies.

[#Java_LLMs]
== How can we take advantage of LLMs in Java applications?

Developing applications powered by large language models isn't always an easy task. Integrating AI/LLM capabilities into an application can be challenging. To make this easier, LangChain, an open source framework was developed in 2022 to help streamline the process of creating generative AI apps.

LangChain provides tools and abstractions to improve the customization, accuracy, and relevancy of the information the models generate. For example, developers can use LangChain components to build new prompt chains or customize existing templates. LangChain also includes components that allow LLMs to access new data sets without retraining and organizes the large quantities of data these models require so that they can be accessed with ease.

Although LangChain is primarily available in Python and JavaScript/TypeScript versions, there are options available to use LangChain in Java applications through community projects like https://github.com/langchain4j/langchain4j[LangChain4j]. LangChain4j APIs provide the ability to intergate LLMs into your Java application for different AI platforms, such as OpenAI and Hugging Face, etc.

image::/img/blog/langchain4j.png[langchain4j GitHub README image,width=50%,align="center"]

[#using_Langchain4j]
== How to use LangChain4j in a Jakarta EE and MicroProfile application

Langchain4j has a very useful open source https://github.com/langchain4j/langchain4j-examples[langchain4j-examples] GitHub repositry where it stores example applications. However, although this repo provided useful examples of using LangChain4j in general Java apps, we could not find any examples showcasing how you could experience these AI technologies in a Jakarta EE/MicroProfile based application. So, we decided to build one ourselves called `jakartaee-microprofile-example` which can now be found in this https://github.com/langchain4j/langchain4j-examples[langchain4j-examples] GitHub repository. This demo application highlights how to use LangChain4j APIs in an application using Jakarta EE and MicroProfile on Open Liberty.


[#tryout]
== Try out the jakartaee-microprofile-example application

To see how you could apply LangChain4j to your own Jakarta EE and/or MicroProfile application, check out this example project for yourself.

[#pre-reqs]
=== Pre-requisites
Before you clone the application to your machine, install JDK 17 and ensure that your `JAVA_HOME` environment variable is set. You can make use of the https://developer.ibm.com/languages/java/semeru-runtimes/downloads[IBM Semeru Runtime] as your chosen Java runtime. This runtime offers performance benefits from deep technology investment in projects such as Eclipse OpenJ9 and is available across a wide variety of hardware and software platforms. To find out more about IBM Semeru Runtime, see https://openliberty.io/blog/2022/08/19/open-liberty-semeru-performance.html[Open Liberty and Semeru Runtimes, cloud-native performance that matters].

The application uses Hugging Face. You need to get a Hugging Face API Key:
Expand Down Expand Up @@ -295,7 +339,7 @@ chatProcessingTime_seconds_sum{mp_scope="application",} 31.674357666
chatProcessingTime_seconds_max{mp_scope="application",} 13.191547042
----

If you interest on other simply ways to use LangChain4j APIs, you can study the REST APIs that are provided by the {example-location}/src/main/java/dev/langchain4j/example/rest/ModelResource.java[`src/main/java/dev/langchain4j/example/rest/ModelResource.java`] file.
If you are interested in other ways to use the LangChain4j APIs, you can study the REST APIs that are provided by the {example-location}/src/main/java/dev/langchain4j/example/rest/ModelResource.java[`src/main/java/dev/langchain4j/example/rest/ModelResource.java`] file.


[#where_to_next]
Expand Down

0 comments on commit f3fda0e

Please sign in to comment.