सरल भाषा में
ज़्यादातर large language models के साथ बनाने वाले tutorials मानकर चलते हैं कि आप Python लिखते हैं। लेकिन दुनिया के बहुत बड़े हिस्से के backend systems Java और Spring Boot पर चलते हैं — बैंक, बीमा कंपनियाँ, retailers, telecom। अगर आपकी दुनिया यही है, तो model को कॉल करने के लिए मौजूदा Spring stack में Python microservice जोड़ना आपको शायद ही चाहिए। आप model को उसी तरह कॉल करना चाहेंगे जैसे आज आप database या HTTP service को करते हैं।

Spring AI Spring का आधिकारिक project है जो ठीक यही करता है। यह LLM कॉल, tool use, retrieval-augmented generation और structured output को Spring framework में उन idioms के साथ लाता है जिन्हें Java developers पहले से जानते हैं: dependency injection, auto-configuration, starters और fluent builder APIs। अगर आपने Spring का RestClient या WebClient इस्तेमाल किया है, तो मुख्य entry point — ChatClient — आपको तुरंत जाना-पहचाना लगेगा।
Spring AI को JVM के लिए language models का JDBC समझें। जैसे JDBC ने Java को Postgres, MySQL या Oracle से बात करने का एक सुसंगत तरीका दिया, वैसे ही Spring AI आपको OpenAI, Anthropic, Google, Ollama या एक दर्जन से ज़्यादा दूसरे providers से बात करने का एक सुसंगत तरीका देता है। आप एक dependency और कुछ properties बदलते हैं; आपके application code में मुश्किल से कोई बदलाव होता है।
यह क्यों मायने रखता है
Enterprise में AI feature ship करने का कठिन हिस्सा शायद ही model call होता है — असली काम उसके आसपास की हर चीज़ का होता है। Spring AI इसलिए मायने रखता है क्योंकि यह उन integration समस्याओं को हल करता है जिन्हें एक raw HTTP client आपके हाथ से संभालने के लिए छोड़ देता है।
- यह Spring की भाषा बोलता है। Models, vector stores और embedding clients सामान्य Spring beans हैं। आप इन्हें
@Autowiredकरते हैं,application.propertiesमें configure करते हैं, और उन्हीं tools से test करते हैं जो आप पहले से इस्तेमाल करते हैं। कोई नया mental model नहीं, कोई समानांतर runtime नहीं। - Providers के बीच portability। आपका code OpenAI के SDK या Anthropic के SDK पर नहीं, बल्कि
ChatModel/ChatClientinterface पर निर्भर करता है। Providers बदलना — या development के लिए Ollama के साथ local model चलाना और production में hosted model — एक dependency और config का बदलाव है, rewrite नहीं। - Structured output जो आपके types से मेल खाए। एक backend को शायद ही prose की दीवार चाहिए; उसे typed object चाहिए जिसे वह persist कर सके या JSON के रूप में लौटा सके। Spring AI model output को सीधे आपके Java records और POJOs में map करता है, ताकि model भी बस एक और typed data source बन जाए।
- बिना glue code के RAG और tools। Retrieval-augmented generation, document ingestion और tool/function calling समझदार defaults के साथ first-class features हैं, न कि ऐसी चीज़ जिसे आपको शुरू से जोड़ना पड़े।
किसे इसकी फ़िक्र होनी चाहिए? कोई भी team जो पहले से Spring Boot में काम करती है और जिससे chatbot, document-search feature, summarizer या internal agent जोड़ने को कहा जाता है — और जो अलग Python stack को maintain, deploy और secure करने के बजाय मौजूदा service को ही बढ़ाना पसंद करेगी। ऐसी teams के लिए Spring AI LLM-संचालित एप्लिकेशन की दुनिया में पहुँचने का सबसे आसान रास्ता है।
यह कैसे काम करता है
Spring AI Spring के क्लासिक ढाँचे का पालन करता है: एक starter dependency auto-configuration लाती है, properties credentials और model name को wire करती हैं, और काम करने के लिए आप कुछ fluent client beans inject करते हैं। केंद्रीय abstraction ChatClient है।
ChatClient request का lifecycle
ChatClient एक निचले स्तर के ChatModel को builder-style, fluent API के साथ wrap करता है। आप एक request बनाते हैं — system text, user text, options, tools, output type — उसे call करते हैं, और response को convert करते हैं। Build और response के बीच की हर चीज़ वह जगह है जहाँ Spring AI की features जुड़ती हैं।
एक minimal call idiomatic Spring जैसा दिखता है। आप ChatClient.Builder inject करते हैं (classpath पर जो भी provider starter है उसके हिसाब से auto-configured) और कुछ methods chain करते हैं:
@RestController
class ChatController {
private final ChatClient chatClient;
ChatController(ChatClient.Builder builder) {
this.chatClient = builder.build();
}
@GetMapping("/joke")
String joke(@RequestParam String topic) {
return chatClient.prompt()
.system("You are a concise, friendly assistant.")
.user("Tell me a short joke about " + topic)
.call()
.content(); // -> the model's reply as a String
}
}Structured output: response → POJO
.content() (एक raw String) की जगह आप .entity(Type.class) माँग सकते हैं। Spring AI model को आपके type के shape से मेल खाता data लौटाने का निर्देश देता है, फिर response को एक असली Java object में parse करता है। Model एक typed source बन जाता है जिसे आप सीधे लौटा सकते हैं या persist कर सकते हैं।
record Recipe(String title, List<String> ingredients, List<String> steps) {}
Recipe recipe = chatClient.prompt()
.user("A simple recipe for vegetable soup")
.call()
.entity(Recipe.class); // parsed JSON -> Recipe instanceAdvisors: LLM कॉल के लिए interceptor pattern
Advisors Spring AI का तरीका है हर request को intercept और enrich करने का — वही विचार जो servlet filters या Spring के HandlerInterceptor में है, जिसे model calls पर लगाया गया है। कोई advisor retrieved documents inject कर सकता है (RAG इसी तरह जुड़ता है), conversation history attach कर सकता है (chat memory), या हर prompt और response को log कर सकता है। आप इन्हें builder पर एक बार जोड़ते हैं और हर कॉल इस chain से होकर गुज़रती है।
Spring AI में tools और RAG
दो features chatbot को सचमुच उपयोगी बनाते हैं: model को आपका code call करने देना, और उसे आपके documents पढ़ने देना। Spring AI दोनों को Java-native ergonomics के साथ देता है।
Tool (function) calling
tool calling के साथ model framework से आपके किसी method को चलाने का अनुरोध कर सकता है — live data देखने, किसी internal API को hit करने, या कोई action करने के लिए — फिर परिणाम के साथ reasoning जारी रखता है। Spring AI में आप एक साधारण method पर @Tool annotation लगाते हैं; framework schema बनाता है, उसे model के सामने रखता है, माँगे जाने पर method चलाता है, और return value को conversation में वापस फीड करता है।
class WeatherTools {
@Tool(description = "Get the current temperature for a city")
String currentTemperature(@ToolParam(description = "City name") String city) {
// call your own service / API here
return weatherService.tempFor(city); // e.g. "18C"
}
}
String answer = chatClient.prompt()
.user("Should I bring a jacket in Oslo right now?")
.tools(new WeatherTools()) // model may call currentTemperature
.call()
.content();Retrieval-augmented generation
Spring AI में RAG का पूरा सपोर्ट है: documents पढ़ने और split करने के लिए एक ETL pipeline, chunks को vectors में बदलने के लिए EmbeddingModel, 18+ databases (PGVector, Pinecone, Qdrant, Weaviate, Redis, और भी) के adapters वाला VectorStore interface, और QuestionAnswerAdvisor जो प्रासंगिक chunks retrieve करता है और उन्हें prompt में अपने आप inject कर देता है।
क्योंकि advisor retrieval संभालता है, मौजूदा ChatClient पर RAG जोड़ना अक्सर एक-लाइन का बदलाव होता है: .defaultAdvisors(new QuestionAnswerAdvisor(vectorStore)) के साथ client बनाएँ, और हर prompt आपके documents पर आधारित हो जाता है।
Spring AI बनाम LangChain4j
अगर आप JVM पर हैं, तो दो मुख्य विकल्प Spring AI और LangChain4j हैं। दोनों परिपक्व हैं, दोनों chat, tools, RAG और structured output कवर करते हैं। ईमानदार अंतर features से कम और आपके stack और पसंद के fit से ज़्यादा है।
| पहलू | Spring AI | LangChain4j |
|---|---|---|
| शुरुआत | आधिकारिक Spring portfolio project | स्वतंत्र community project |
| सबसे उपयुक्त | जो teams पहले से Spring Boot पर हैं | कोई भी JVM ऐप (Quarkus, सामान्य Java, Spring) |
| मूल अनुभव | Spring के idioms: beans, starters, auto-config | Python LangChain के abstractions से प्रेरित |
| Tools / function calling | @Tool से annotated methods | @Tool से annotated methods / AI Services |
| RAG सपोर्ट | ETL + VectorStore + advisors | Embedding store + content retrievers |
| Dependency injection | नेटिव Spring DI | Framework-agnostic; Spring/Quarkus starters उपलब्ध हैं |
एक सरल नियम: अगर आपका application पहले से Spring Boot service है और आप बाकी Spring ecosystem के साथ कसकर integration को महत्व देते हैं, तो Spring AI स्वाभाविक चुनाव है। अगर आप किसी दूसरे JVM framework (जैसे Quarkus) पर हैं या ऐसी library चाहते हैं जो Spring मानकर न चले, तो LangChain4j अधिक तटस्थ विकल्प है। दोनों में से कोई भी गलत जवाब नहीं है — दोनों एक ही क्षमताओं की ओर बढ़ रहे हैं, बस अलग सांस्कृतिक शुरुआत से।
आम गलतियाँ
Spring AI बहुत सारा boilerplate हटा देता है, पर कुछ गलतियाँ नए लोगों को फँसाती हैं — इनमें से ज़्यादातर model को एक साधारण, deterministic service मान लेने से जुड़ी हैं, जबकि वह वैसा नहीं है।
- यह भूल जाना कि model non-deterministic है। वही prompt हर बार अलग text लौटा सकता है। ऐसे assertions न लिखें जो एक exact string की उम्मीद करें; shape की जाँच करें (क्या वह POJO में parse होता है? क्या सही tool कॉल होता है?), न कि ठीक शब्दों की।
- Structured output को guaranteed मान लेना।
.entity(Type.class)भरोसेमंद है, पर जादुई नहीं — model ऐसा output दे सकता है जो map न हो। Parse failures को हैंडल करें और खासकर user-facing चीज़ों के लिए result को validate करने पर विचार करें। - गलत dependency लीक करना। हर provider का अपना starter है (OpenAI, Anthropic, Ollama, वगैरह)। सिर्फ वही लें जिसे आपने सच में configure किया है; starters मिलाना या classpath की dependency के साथ configured model name का मेल न बैठना पहली बार चलाने की आम गलती है।
- लागत और tokens को नज़रअंदाज़ करना। हर call की token लागत और latency होती है। लंबे RAG contexts और बातूनी tool loops तेज़ी से जुड़ते जाते हैं। अपने context window के उपयोग पर नज़र रखें और सिर्फ़ ज़रूरी chunks retrieve करें।
- Retrieved या tool data पर आँख मूँदकर भरोसा करना। Prompt में खींचे गए documents और external API results untrusted input हैं, जिनमें prompt injection हो सकता है। Retrieved content को साफ़ तौर पर अलग करें और उसे कभी निर्देश के रूप में काम न करने दें।
आगे और गहराई में
एक बार बुनियादी बातें समझ आ जाएँ — ChatClient, structured output, tools, advisors, RAG — तो Spring AI में बढ़ने के लिए काफ़ी गहराई है। कुछ दिशाएँ जानने लायक हैं।
Chat memory और conversations। डिफ़ॉल्ट रूप से हर call stateless होता है। Spring AI एक ChatMemory abstraction और advisors देता है जो पिछले turns को conversation id से जोड़ते हैं, ताकि आप history हाथ से संभाले बिना multi-turn assistants बना सकें। Restarts के बाद भी बचे रहने के लिए इसे persistent store के साथ जोड़ें।
Streaming responses। Chat UIs के लिए आप आम तौर पर पूरे जवाब का इंतज़ार नहीं करना चाहते। ChatClient एक reactive Flux लौटाने वाला .stream() देता है, ताकि tokens धीरे-धीरे पहुँचें — Spring WebFlux और server-sent events के लिए यह स्वाभाविक फिट है।
Observability। चूँकि यह एक Spring project है, Spring AI model calls के लिए metrics और traces भेजने हेतु Micrometer के साथ integrate होता है — token counts, latency, errors — जिन्हें आप अपने मौजूदा observability stack में भेज सकते हैं। एक बार LLM feature production में आ जाए और आपको यह देखना हो कि वह कितना खर्च कर रहा है और कहाँ धीमा है, तब यह बहुत मायने रखता है।
Agents, MCP और evaluation। सिंगल calls से आगे, आप tools, memory और retrieval को agentic loops में compose कर सकते हैं, जहाँ model तय करता है कि आगे क्या करना है। Spring AI Model Context Protocol को भी सपोर्ट करता है ताकि standardized external tools और data sources जोड़े जा सकें। इसमें एक evaluation API भी है, जो जाँचता है कि जवाब retrieved context से faithful रहते हैं या नहीं — वही अनुशासन जो demo को भरोसेमंद system से अलग करता है।
स्थायी सबक वही है जो हर LLM framework पर लागू होता है: framework plumbing को आसान बनाता है, पर आपके application की गुणवत्ता अब भी आपके data और prompts में रहती है। Spring AI का योगदान यह है कि एक Java team उस असली काम तक पहुँच सके, बिना उस ecosystem को छोड़े जिसमें वह पहले से बनाना, test करना, deploy करना और operate करना जानती है।
अक्सर पूछे जाने वाले प्रश्न
Spring AI किसलिए इस्तेमाल होता है?
Spring AI Java और Spring Boot applications के भीतर LLM-संचालित features बनाने के लिए आधिकारिक Spring project है। यह chat completions, tool/function calling, retrieval-augmented generation (RAG), POJOs में structured output mapping और embeddings संभालता है — ये सब dependency injection और auto-configuration जैसे जाने-पहचाने Spring idioms का इस्तेमाल करके।
Spring AI में ChatClient क्या है?
ChatClient Spring AI का मुख्य entry point है: chat models से बात करने के लिए एक fluent, builder-style API, जो Spring के WebClient और RestClient के आधार पर बना है। आप system और user text के साथ request बनाते हैं, वैकल्पिक रूप से tools या output type जोड़ते हैं, उसे call करते हैं, और response को String या typed Java object में बदलते हैं।
क्या Spring AI कई model providers सपोर्ट करता है?
हाँ। Spring AI डिज़ाइन से ही provider-agnostic है। आपका code ChatModel/ChatClient interface पर निर्भर करता है, और ठोस provider — OpenAI, Anthropic, Google, Ollama और कई दूसरे — इस पर चुना जाता है कि आप कौन-सी starter dependency शामिल करते हैं और properties कैसे configure करते हैं। Providers बदलना ज़्यादातर एक config का बदलाव है, rewrite नहीं।
Spring AI बनाम LangChain4j — मुझे कौन-सा इस्तेमाल करना चाहिए?
दोनों परिपक्व JVM विकल्प हैं जो chat, tools, RAG और structured output कवर करते हैं। अगर आप पहले से Spring Boot पर हैं तो Spring AI, जो आधिकारिक Spring portfolio project है, स्वाभाविक रूप से फिट बैठता है। LangChain4j एक स्वतंत्र, framework-neutral library है जो Quarkus या सामान्य Java के लिए बेहतर फिट है। वह चुनें जिसके idioms आपके stack से मेल खाते हों।
Spring AI सादे text की जगह structured output कैसे लौटाता है?
Raw String के लिए .content() कॉल करने की जगह आप .entity(YourType.class) कॉल करते हैं। Spring AI model को आपके type के shape से मेल खाता data लौटाने का निर्देश देता है, और फिर response को एक असली Java record या POJO में parse करता है, ताकि model का output एक typed object बन जाए जिसे आप सीधे लौटा या persist कर सकें।
क्या Spring AI RAG और vector databases सपोर्ट करता है?
हाँ। Spring AI में documents लोड और chunk करने के लिए एक ETL pipeline, उन्हें vectors में बदलने के लिए EmbeddingModel, 18+ databases (PGVector, Pinecone, Qdrant, Weaviate, Redis, और भी) के adapters वाला VectorStore interface, और QuestionAnswerAdvisor शामिल है जो प्रासंगिक chunks retrieve करता है और उन्हें अपने आप prompt में inject कर देता है।
