1 / 9

Unlocking the Potential of AI in Spring

Explore the limitless possibilities as we delve into the transformative journey of unlocking AI's potential within the Spring framework.

inextures
Download Presentation

Unlocking the Potential of AI in Spring

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Unlocking the Potential of AI in Spring •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 •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

  2. •While ChatGPT and its generative AI capabilities have captivated users through text input and output, many models and companies offer diverse inputs and outputs. •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. •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. •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. •Tokens •Tokens serve as the building blocks of how an AI model works.

  3. •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. •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). •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: oFine 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. oPrompt 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

  4. •Generate Spring AI Project Using CLI •Download the Zip of Spring cli according to your system •Extract it •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 •To generate a Spring boot project use the command: spring boot new ai •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

  5. Create Models of City City CityList

  6. 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

  7. 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. Now, we have two endpoints. 1. http://localhost:8080/spring/ai/cities/{state} oIn the above endpoint, we must provide the state in the input, and it will give the list of cities according to the state. oAs displayed below, we got the response in proper format as we have provided parser.getFormat()

  8. 2. http://localhost:8080/spring/ai/question/Top singer details of India 1.In the above endpoint, we can provide any text input and it will generate the response accordingly. 2.As displayed below, we got the response in simple text format. Originally published by: Unlocking the Potential of AI in Spring

More Related