Posts

Structured Output Converter in Spring AI

 Structured Output Converter in Spring AI Suppose you ask OpenAI: "Provide the major cities of India." Normally, AI returns plain text . India has many major cities such as Mumbai, Delhi, Bengaluru, Hyderabad and Chennai. In Java, you'll get: String response = chatClient.prompt() .user( "Provide major cities of India" ) .call() .content() ; Type: String Problem: It's just text. You have to parse it yourself. AI may change the format every time. Step 2: Structured Output Instead of asking AI for plain text, we ask: "Return the response in a structured format." { "country" : "India" , "cities" : [ "Mumbai" , "Delhi" , "Pune" ] } This is called Structured Output . It means: AI returns JSON instead of plain text.   Step 3: POJO (Plain Old Java Object) Now create a Java class (or record) to hold this JSON...

Spring AI ChatClient– Response Types Explained

 Spring AI ChatClient– Response Types Explained  Whenyou invoke .call() on a ChatClient, there are different ways to get the result, depending on what you want to do with it Streaming Responses •  To stream reponses, we can use. stream() instead of. call()  • Good for real-time or chunked responses (like streaming output to UI. Structured Output Converter in Spring AI Why DoWe Need Structured Output? LLMs typically return plain text responses. But in real-world apps, we often need structured data like, • JSON • XML • JavaClasses (POJOs) Structured data is easier to parse, use, and integrate in applications. What is a Structured Output Converter ? Before sending prompt to LLM: ➤Addsformatting instructions to guide the model. ➤Ensures it replies in a parseable format. After getting response from LLM: ➤Converts raw text into a Java object, like Map, List, or custom class Structured Output Converter in Spring AI Example: Convert reponse to a Java object CountryCitie...

Chat Client APIs and Streaming

 Chat Client APIs and Streaming call(). content() (Most Common) Returns only the generated text. @GetMapping ( "/content" ) public String content ( @RequestParam String question) { return chatClient.prompt() .user(question) .call() .content() ; } Flow: User │ ▼ ChatClient │ ▼ LLM │ ▼ String Use when you only need the answer. call().chatResponse() Returns the complete response object, not just the text. @GetMapping ( "/response" ) public ChatResponse response ( @RequestParam String question) { return chatClient.prompt() .user(question) .call() .chatResponse() ; } Chat Response contains much more than the content. http://localhost:8080/api/response?question=Explain Kafka output are in json format .  Streaming Instead of waiting for the complete answer, stream it token by token. @GetMapping (value= "/stream" , produces = MediaType. TEXT_EVENT_STREAM_VALUE ...