logo

Get in touch

Awesome Image Awesome Image

AI/Machine Learning February 12, 2024

Unlocking the Potential of AI in Spring

Written by Mahipalsinh Rana

24,485

  • Spring AI is a newly released Spring project, and it takes inspiration from notable Python projects, such as LangChain and LlamaIndex.
  • The Spring AI project aims to simplify the creation of applications integrating artificial intelligence capabilities by reducing unnecessary complexity.
  • With the help of Spring AI, we can integrate the AI APIs (OpenAI) in the spring applications.

Artificial Intelligence (AI) Concept

  1. Model
  • AI models are algorithms designed to process and generate information.
  • These models can make predictions, images, text, or other outputs by learning patterns and from large datasets
  • There are many different types of AI models, and Each model is suited for the specific use case
  • While ChatGPT and its generative AI capabilities have captivated users through text input and output, many models and companies offer diverse inputs and outputs.
  1. Prompts
  • Prompts refer to the query or input that we provide to an AI model to get the desired response
  • Prompts are what guide the AI model’s output and influence its tone, style, and quality.
  • Prompts can include instructions, questions, or any other type of input, depending on the intended use of the model.
  1. Prompt Templates
  • Prompt Template is one type of predefined or reusable structure for the prompt
  • Prompt templates provide a standardized format or set of instructions that we can follow to interact with language models effectively.
  • These templates include placeholders where we can insert specific information relevant to their task.
  • Spring AI employs the OSS library, String Template, for this purpose.
  • Ex: “Translate the following text into {language}: {text}”.
  • The above example is of a Prompt Template where language and text are placeholder which will be taken from the request.
  1. Embeddings
  • Embeddings transform text into numerical arrays or vectors, enabling AI models to process and interpret language data.
  • This transformation from text to numbers and back is a key element in how AI interacts with and understands human language.
  1. Tokens
  • Tokens serve as the building blocks of how an AI model works.
  • On input, models convert words to tokens. On output, they convert tokens back to words
  • Both input and output contribute to the overall token count. Also, models are subject to token limits, which restrict the amount of text processed in a single API call.
  • This threshold is often referred to as the ‘context window’. The model does not process any text that exceeds this limit.
  1. Output Parsing
  • AI models typically generate responses in string format, but output parsers come into use when you need the output in different formats.
  • These parsers reformat these raw strings into more programmer-friendly structures like CSV (Comma-Separated Values) or JSON (JavaScript Object Notation).
  1. Bringing Your Data to the AI model
  • When you ask the question beyond the model dates then the model says that it does not know the answer to questions that require knowledge beyond that date
  • In GPT 3.5/4.0, the dataset extends only until September 2021. So, It gives the same answer when you ask questions beyond September 2021
  • Two techniques exist to customize the AI model to incorporate your data:
    • Fine Tuning: This traditional machine-learning technique involves tailoring the model and changing its internal weighting. However, it is Expensive and Hard to do. Additionally, some models might not offer this option.
    • Prompt Stuffing: A more practical alternative involves embedding your data within the prompt provided to the model. Given a model’s token limits, techniques are required to present relevant data within the model’s context window. This approach is commonly referred to as “stuffing the prompt.”

Also Learn: Guide to Spring Reactive Programming using Spring WebFlux

Generate Spring AI Project Using CLI

  1. Download the Zip of Spring cli according to your system
  2. Extract it
  3. Open the Terminal and add the below command to use the spring command

            alias spring=’java -jar $HOME/path to your jar file /spring-cli-0.8.1.jar’

NOTE: path to your jar file replace this with your jar file path

  1. To generate a Spring boot project use the command: spring boot new ai
  2. Go Inside the project and hit the below command to generate spring ai Apis spring boot add ai

Generate Spring AI Project Using Spring Initializer

Definition: In the input user will provide the state and Spring AI will give the List of the cities from the provided state

Generate Spring boot project with below dependency and Repository inside pom.xml

For the Spring AI, org.springframework.ai dependency and repository are required

Spring AI Project Using Spring Initializer

Create Models of City

City

Create Models of City

CityList

Create CityService with the below code

Here,

ChatClient is an interface used to interact with AI models

The design of the ChatClient interface centers around two primary goals:

Portability: The system facilitates seamless integration with various AI Models, enabling developers to transition b BeanOutputParser between different models with minimal adjustments to the codebase. This approach is in harmony with Spring’s ethos of modularity and flexibility.

Simplicity: Leveraging companion classes such as Prompt for input encapsulation and ChatResponse for output handling, the ChatClient interface streamlines interactions with AI Models. It abstracts away the intricacies of request formulation and response interpretation, providing a straightforward and simplified API interaction experience.

Here,

BeanOutputParser is used to get the output in a specific format by default it will return in normal text format

PrompTemplate provides a standardized format that we can follow to interact with language models effectively

The prompt is used to generate the specific output based on the provided string

ChatClient.call(prompt) will generate the response based on a given prompt

Create CityController with the below code.

Call methods of service from this controller. getAnswer API is the basic example for passing a question in a URL and getting the answers.

Create CityController

Now, we have two endpoints.

1. http://localhost:8080/spring/ai/cities/{state}

    • In the above endpoint, we must provide the state in the input, and it will give the list of cities according to the state.
    • As displayed below, we got the response in proper format as we have provided parser.getFormat()

2. http://localhost:8080/spring/ai/question/Top singer details of India

    • In the above endpoint, we can provide any text input and it will generate the response accordingly.
    • As displayed below, we got the response in simple text format.

Bringing Software Development Expertise to Every
Corner of the World

United States

India

Germany

United Kingdom

Canada

Singapore

Australia

New Zealand

Dubai

Qatar

Kuwait

Finland

Brazil

Netherlands

Ireland

Japan

Kenya

South Africa