SpringSource / Pivotal / VMware ◦ Started teaching Spring in 2007 ◦ Started partnering with Ravi (Edforce CEO) in 2009 • VP of Engineering of cynapse.ai since 2022 ◦ Conventional AI, Generative AI, Computer Vision
model online Typically on Github or HuggingFace • Evaluate the model Identify gaps (example: doesn’t work with Singapore truck license plates) • Prepare a fine-tuning dataset • Spend 3-4 days training the model
image img = image.load_img('path_to_image.jpg', target_size=(224, 224)) # … # Run inference predictions = model.predict(img_array) # … print(f"Predicted class: {predicted_class[1]}") Load the model Prediction Conventional AI is typically done in Python or C++ Python code
to ChatGPT #!/bin/bash API_KEY="sk-proj-7devtvnBIsYXVHJuHBQAT3BlbkFJNBB4uz8Iog5F2y" curl https://api.openai.com/v1/chat/completions \ -H "Content-Type: application/json" -H "Authorization: Bearer $API_KEY" \ -d '{ "model": "gpt-4o", "messages": [ {"role": "user", "content": "Tell me a joke." } ] }' Linux/OSX In GenAI, models are much more complex. But most of the time you don’t need to build them
we need to setup a system for Fraud detection. We will have a lot of custom rules in order to identify fraud patterns • This is critical to our business • Should I use Conventional AI or Generative AI for that?
</dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.ai</groupId> <artifactId> spring-ai-bom </artifactId> <version>1.0.0-M2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> pom.xml Spring Boot starter (brings in all needed dependencies) defines versions for all Spring AI dependencies (Bill Of Materials)
ChatClient chatClient; public MusicService(ChatClient.Builder builder) { this.chatClient = builder.build(); } public String findBestSongs() { return this.chatClient.prompt() .user("which were the best songs in 1993?") .call().content(); } } • Use ChatClient’s fluent API Generic (does not depend on the LLM implementation)
prompt @Service public class MusicService { private final ChatClient chatClient; public MusicService(ChatClient.Builder builder) { this.chatClient = builder.build(); } public String findBestSongs() { return this.chatClient.prompt() .system(" You are a helpful assistant writing in English from the 1990s ") .user("which were the best songs in 1993?") .call().content(); } }
userSpec.text("Can you recommend a movie about about {topic}") .param("topic", topic)) .call() .content(); } Using a prompt with Parameters var response = this.movieService.recommendMovie("computers"); this.logger.info(response); Certainly! One highly regarded film that delves into the world of computers is "The Imitation Game" (2014). This biographical drama stars Benedict Cumberbatch as Alan Turing, a pioneering computer scientist and mathematician.
{ private final ChatClient chatClient; public ChatService(ChatClient.Builder builder) { this.chatClient = builder.build(); } public ActorFilms generateResponse() { return this.chatClient.prompt() .user("Generate the 10 most popular movies starring Bruce Willis") .call() .entity(ActorFilms.class); } } public record ActorFilms(String actor, List<String> movies) {} works with Java records Demo
this.chatClient.prompt() .user("Generate the 10 most popular movies starring Bruce Willis") .call() .entity(ActorFilms.class); } public record ActorFilms(String actor, List<String> movies) {} Demo Do not include any explanations, only provide an RFC8259 compliant JSON response … { \"$schema\" : \"https://json-schema.org/draft/2020-12/schema\", \"type\" : \"object\", \"properties\" : { \"actor\" : { \"type\" : \"string\" }, \"movies\" : { \"type\" : \"array\", \"items\" : { \"type\" : \"string\" } } }
method calls. There is a special name for that kind of API. How is it called? • Bonus: can you name 3 Java frameworks or components which use the same technique? public String findBestSongs() { return this.chatClient.prompt() .system(" You are a helpful assistant writing in English from the 1990s ") .user("which were the best songs in 1993?") .call().content(); }
imageResourceWeather; // constructor public String analyseWeather() { return this.chatClient.prompt() .user( userSpec -> userSpec.text("what will be the weather like on Tuesday") .media(MimeTypeUtils.IMAGE_PNG, this.imageResourceWeather) ) .call() .content(); } } Import image file as a Resource Optical Character Recognition Demo
trained on what is available on Internet • Models are not real time ◦ They all have a cutoff date ◦ Example: ChatGPT-4o has been trained on data up to September 2023
class @Service public class OlympicsService { @Value("classpath:/olympics/context.st") private Resource queryTemplate; } Structured text file Use the following pieces of context to answer the question at the end. {context} Question: {question} context.st org.springframework.core.io.Resource
public String findOlympicSports() throws IOException { return this.chatClient.prompt() .user( userSpec -> userSpec.text(this.queryTemplate) .param( "context" , "Archery, athletics, badminton, basketball , boxing") .param( "question" ,"How many sports are being included in the 2024 Summer Olympics?") ) .call().content(); } Structured text file Use the following pieces of context to answer the question at the end. Archery, athletics, badminton, basketball , boxing Question: How many sports are being included in the 2024 Summer Olympics? Demo
how many Harry Potter books can I fit at most? (talking about the last one that had over 700 pages) ◦ 10% of a book ◦ 50% of a book ◦ 3 books Context window
encode each chunk into numbers that the ML model can understand {0.345, 0.465, 0.856, …, 0.1543} {0.545, 0.665, 0.056, …, 0.3543} {0.645, 0.765, 0.156, …, 0.4543} “My house is black” “My garden is big” “My dog is playful” AI Model Each Vector is an array of 1,536 numbers
type of data ◦ Typically an array of 1,536 decimal numbers ◦ Value between -1 and 1 CREATE TABLE paragraph ( id SERIAL PRIMARY KEY, paragraph_text TEXT, vector VECTOR(1536) ); example with pgvector {-0.345, 0.465, 0.856, …, 0.1543} Each Vector is an array of 1,536 numbers “Vector” and “Embedding” are similar concepts. For simplicity, we use the word “Vector” whenever possible in this course
generated by Machine Learning models {0.345, 0.465, 0.856, 0.1543} {0.545, 0.665, 0.056, 0.3543} {0.645, 0.765, 0.156, 0.4543} “My house is black” “My garden is big” “My dog is playful” AI Model The above example is simplified and uses random numbers It is based on text AI models. Vectors may also be used with Computer Vision AI models or audio-based AI models
working on their Vector support ◦ PostgreSQL (pgvector), MongoDB, ElasticSearch, Cassandra, … • Some databases are specialised Vector databases ◦ Chroma, Milvus, …
3: Similarity Search public String answerQuestion(String question) { return chatClient.prompt() .user(question) .call() .content(); } 1. Calls OpenAI Model in order to encode question 2. Compares question against all vectors inside database 3. Returns list of closest vectors 4. Sends Vector to ChatGPT together with question Demo
is still the best when: ◦ Working with a private or confidential dataset ◦ There is high accuracy requirement requiring model fine-tuning • Generative AI shines for: ◦ Text generation Quick embedding of AI features in your application As of now, Spring AI focuses on Generative AI
• https://www.youtube.com/@springinaction (Craig Walls) • Videos from Spring I/O Barcelona conferences • My demos: https://github.com/michaelisvy/demo-spring-ai 52