Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Spring AI presentation for Edtalks

Michael Isvy
September 05, 2024

Spring AI presentation for Edtalks

Michael Isvy

September 05, 2024
Tweet

More Decks by Michael Isvy

Other Decks in Technology

Transcript

  1. Who I am • Michael Isvy • Formerly part of

    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
  2. What we will talk about • The AI Space •

    Spring AI • Retrieval Augmented Generation • Vector Databases
  3. Quiz • There will be 4 quiz questions during this

    presentation • Win some vouchers!
  4. Artificial Intelligence: which Way? Conventional AI Generative AI LLM, ChatGPT,

    DALL-E, Gemini, Mistral, Ollama, Generative AI… Programming: mostly API calls Based on custom-trained models Programming: requires AI engineers
  5. Conventional AI example: Licence Plate Recognition • Find a base

    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
  6. Conventional AI model = MobileNetV2(weights='imagenet') # Prepare a sample input

    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
  7. Generative AI: usually an API call! • Example: API call

    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
  8. Quiz 1 • In the below example, there is something

    you should never do. What is it? #!/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
  9. Generative AI • General-Purpose models • Use-cases ◦ Chatbot ◦

    Image recognition (read invoices…) ◦ Search a large number of documents ▪ And ask questions from chatbot ◦ …
  10. Quiz 2 • I work for a Payment company and

    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?
  11. The Generative AI landscape OpenAI ChatGPT Mistral AI Mistral Anthropic

    Claude Google Gemini Spin off from OpenAI Ollama In the Cloud On premise / on your laptop Llama3.1 Mixtral LLava tinyllama … Choose your local model
  12. What is Spring AI? • AI for Java developers! •

    Simplifies interactions with LLMs ◦ change model by changing one line of configuration! • Simplifies interactions with Vector databases
  13. The Spring AI ecosystem • Created in 2023 by Mark

    Pollack and Christian Tzolov • Current version: Spring AI 1.0.0-M2 ◦ Not in final release version yet! • Based on Spring and Spring Boot
  14. Using Spring AI • Create a Spring Boot Project •

    Add the Spring AI dependencies • Add your API key • Use Spring AI to prompt queries to your model
  15. Spring AI dependencies <dependencies> <dependency> <groupId>org.springframework.ai</groupId> <artifactId> spring-ai-openai-spring-boot-starter </artifactId> </dependency>

    </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)
  16. • Best practice: store the API key is an env

    variable application.properties spring.application.name=spring-ai-samples spring.ai.openai.api-key=${OPENAI_API_KEY} spring.ai.openai.chat.options.model=gpt-4o application.properties IntelliJ Community Edition
  17. Making a call @Service public class MusicService { private final

    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)
  18. • Give generic guidance to the prompt Adding a system

    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(); } }
  19. Calling from a JUnit test • Run configuration should include

    the environment variable @SpringBootTest class MusicServiceTest { @Autowired private MusicService musicService; private static final Logger logger = LoggerFactory.getLogger(MusicService.class); @Test void shouldFindBestSongs() { String response = this.musicService.findBestSongs(); logger.info(response); } } Demo
  20. OpenAI vs Ollama • OpenAI config • Ollama config <dependency>

    <groupId>org.springframework.ai</groupId> <artifactId> spring-ai-openai-spring-boot-starter </artifactId> </dependency> pom.xml <dependency> <groupId>org.springframework.ai</groupId> <artifactId> spring-ai-ollama-spring-boot-starter </artifactId> </dependency> pom.xml spring.ai.openai.api-key=${OPENAI_API_KEY} spring.ai.openai.chat.options.model=gpt-4o application.properties spring.ai.ollama.chat.model=tinyllama application.properties Demo
  21. public String recommendMovie(String topic) { return this.chatClient.prompt() .user( userSpec ->

    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.
  22. Mapping a prompt to an entity @Service public class ChatService

    { 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
  23. JSON schema under the hood 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) {} 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\" } } }
  24. Quiz 4 • The below API shows a chain of

    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(); }
  25. Working with images @Service class ImageService { @Value("classpath:images/singapore-weather.png") private Resource

    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
  26. Retrieval Augmented Generation • Bring your own data to the

    prompt • Give a lot of context to the prompt ◦ Text content, excel, pdf, etc. ◦ ChatGPT does it as well! (custom prompts)
  27. Why bringing your own data • Models have only be

    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
  28. Step 1 - Loading an st file into a Service

    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
  29. Step 2 - Using the prompt @Value("classpath:/olympics/context.st") private Resource queryTemplate;

    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
  30. Quiz 5 • Using GPT-4’s prompt context window, up to

    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
  31. Going beyond the prompt • How to do when: ◦

    Your data is too big to fit into the prompt context window? ◦ You’re spending too much because of the prompt context Context window
  32. Solution: Vector databases • Split your data into chunks, and

    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
  33. Definition of a Vector • A Vector is just a

    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
  34. How are Vectors created? • Vectors typically represent the output

    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
  35. How to search vectors: Similarity Search • Selects the closest

    Vector(s) {0.345, -0.465, 0.856, 0.1543} {-0.436, 0.578, 0.935, 0.2193} {-0.445, 0.565, 0.956, 0.2543} {0.545, 0.665, 0.056, 0.3543} {0.645, 0.765, 0.156, -0.4543} {0.745, 0.865, 0.256, 0.5543} {0.845, 0.965, -0.356, 0.6543} SELECT id, name, vector <=> '[0.436, 0.578, 0.935, 0.2193]' AS distance FROM items ORDER BY distance LIMIT 10; sample SQL query with pgvector
  36. Vector database providers • Most SQL and NoSQL databases are

    working on their Vector support ◦ PostgreSQL (pgvector), MongoDB, ElasticSearch, Cassandra, … • Some databases are specialised Vector databases ◦ Chroma, Milvus, …
  37. SimpleVectorStore • Spring AI comes with a file-system based VectorStore

    implementation ◦ To be used for Educational purpose only VectorStore PGVectorStore ChromaVectorStore SimpleVectorStore …
  38. SimpleVectorStore example with OpenAI @Configuration class VectorStoreConfiguration { @Bean SimpleVectorStore

    simpleVectorStore(EmbeddingModel embeddingModel) throws IOException { var simpleVectorStore = new SimpleVectorStore(embeddingModel); //... return simpleVectorStore; } } { "aec18bbc-21dc-4763-b93e-2f2ee49f9024" : { "embedding" : [ -0.0671, -0.0342, -0.0103, …], "content" : "He raised the guitar, and Henri …", "id" : "aec18bbc-21dc-4763-b93e-2f2ee49f9024", "metadata" : { "source" : "crime-in-paris.txt" } } sample vector.json file OpenAIEmbeddingModel is injected
  39. Example: encoding a text into a Vector database • Step

    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
  40. Cost of 1 month of experimenting with Spring AI •

    OpenAI ◦ $0.65 • Ollama ◦ $0.00
  41. Is Conventional AI dead? • Absolutely not! • Conventional AI

    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
  42. My favorite Spring AI Resources online • https://www.youtube.com/@DanVega (Dan Vega)

    • https://www.youtube.com/@springinaction (Craig Walls) • Videos from Spring I/O Barcelona conferences • My demos: https://github.com/michaelisvy/demo-spring-ai 52