Code Monkey home page Code Monkey logo

native-langchain4j's Introduction

Native LangChain4J example

Overview

This example is based on a LangChain4j tutorial. It produces a GraalVM native version of a chatbot leveraging LangChain4j and the OPENAI API.

Prerequisites

  • GraalVM (sdk install java 21.0.2-graal)

  • Maven

  • (optional) OPENAI API key

Note
To interact with the OpenAI API, you need to either get and store an OPENAI API key in an environment variable OPENAI_API_KEY, or replace the value with demo.

Package and run on the JVM

mvn package

Interact with the bot

You can now chat with your bot. You can execute the jar directly:

$ java -jar ./target/native-langchain4j-1.0-jar-with-dependencies.jar "your prompt"

Or use the script from the repo for conciseness:

$ ./run-jvm.sh "what is Java?"

"Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is a general-purpose programming language intended to let application developers write once, run anywhere (WORA), meaning that compiled Java code can run on all platforms that support Java without the need for recompilation. Java is widely used for developing web applications, software, and mobile applications."

$ ./run-jvm.sh "what is GraalVM?"

"GraalVM is a high-performance runtime that provides significant improvements in application performance and efficiency. It is designed for applications written in JavaScript, Python, Ruby, R, JVM-based languages like Java, Scala, Groovy, Kotlin, and LLVM-based languages such as C and C++. GraalVM offers capabilities like ahead-of-time compilation and the ability to compile to a native executable to improve startup time, reduce memory footprint, and enable distribution of pre-compiled executables. It also supports interoperability between different programming languages, allowing you to write polyglot applications."

$ ./run-jvm.sh "what is love?"

"Love is a complex set of emotions, behaviors, and beliefs associated with strong feelings of affection, protectiveness, warmth, and respect for another person. It can also be described as a profound, passionate affection for another person and can emerge in various forms, such as familial love, romantic love, platonic love, and self-love. Love can also be understood as a function to keep human beings together against threats and to facilitate the continuation of the species."

Package and run as Native Image

This demo works out of the box, but to get rid of a warning from a 4th party dependency, build it with config files (come with the repo):

native-image -jar ./target/native-langchain4j-1.0-jar-with-dependencies.jar -o native-langchain4j -H:ConfigurationFileDirectories=./resources/META-INF/native-image -H:+AllowDeprecatedBuilderClassesOnImageClasspath

Alternatively, you can run the build script: ./build-native.sh.

This will produce a nice self-contained executable version of the app:

$ eza -l
.rwxr-xr-x 44M ayurenko 16 Mar 16:45 native-langchain4j

You can now chat with the native app:

$ ./native-langchain4j "what's the answer to life, the universe, and everything?"

42, according to "The Hitchhiker's Guide to the Galaxy" by Douglas Adams

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.