استدعاء النماذج اللغوية الكبيرة (LLMs) من Spring
في هذه الصفحة 8 موضوعات
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
نظرة عامة
الموضوعات
- Spring AI ChatClient
- موجّهات النظام (System prompts)
- المخرجات المهيكلة إلى records
- البث (Streaming)
- المهلات وإعادة المحاولة
- تكلفة الرموز (Tokens)
- التعامل مع مفتاح الـ API
- الاختبار بنموذج وهمي
تعلّم أولًا
3 ساعة تدريب (تقدير)
مخرجات التعلم
- Explain how an LLM API call works: messages, system prompt, model parameters, tokens, and cost.
- Call a model from Spring with Spring AI's
ChatClient, keeping the provider behind configuration. - Convert model output into Java records with structured output, and validate it before use.
- Stream responses to clients and set timeouts, retries, and a token budget per request.
- Hide the model behind an application interface and test the feature with a fake implementation.
الدروس
- 01ماذا يحدث في استدعاء API لنموذج لغوي
- 02مفاهيم Spring AI: ChatClient والموجّهات (Prompts) والـ advisors
- 03المخرجات المهيكلة إلى records والتحقق منها
- 04بث الاستجابات (Streaming)
- 05المهلات وإعادة المحاولة وميزانيات الرموز (Tokens)
- 06المفاتيح والإعدادات والاختبار بنموذج وهمي
- 07عرض عملي: مساعد ذكي لكتابة أوصاف المنتجات في Shop API
المختبر والتمارين
Lab
JNR-056-LAB1 · AI helper for Shop
Outcomes: 1–5 · Time: ~3 h Add a ProductDescriptionAssistant (or a natural-language product search) to Shop API behind an interface. The Spring AI implementation returns a validated record; a fake implementation is used in tests.
Acceptance:
- provider and model are set only in configuration
- the API key comes from the environment
- timeout and max-token limit are configured
- invalid model output is handled with a clear error or a fallback
- controller and service tests pass without network access
Exercises (assessment: extend the helper with a new field and keep all tests deterministic)
| ID | Outcome | Task |
|---|---|---|
| JNR-056-EX01 | 1 | Given a request with 400 input tokens and 150 output tokens, explain what is billed and how a lower max_tokens changes cost. |
| JNR-056-EX02 | 2 | Call a model with ChatClient using a system message and a templated user message; show where the provider is configured. |
| JNR-056-EX03 | 3 | Return a Description record with .entity(...) and reject results that fail Bean Validation. |
| JNR-056-EX04 | 4 | Add a streaming endpoint that returns pieces as server-sent events. |
| JNR-056-EX05 | 4 | Configure a timeout, a retry policy for transient errors, and a max-token limit; list which errors you retry. |
| JNR-056-EX06 | 5 | Extract an interface, write a fake, and test both the valid and the invalid-output path with no network. |
أظهر الحلول
- EX01 You pay for 400 input plus up to 150 output tokens; lowering the limit caps the output share of the cost (and may cut the reply short).
- EX02
chatClient.prompt().system("...").user(u -> u.text("... {name}").param("name", n)).call().content(); provider and model inapplication.yml(property names per the Spring AI reference) and the starter dependency. - EX03
.entity(Description.class)thenvalidator.validate(d); if the set is not empty, throw an exception mapped to a clear error response. - EX04
produces = TEXT_EVENT_STREAM_VALUEreturningchatClient.prompt()...stream().content(). - EX05 Retry timeouts,
429,5xx; do not retry400or authentication errors. - EX06
ProductDescriptionAssistantinterface; a fake returns fixed values or an invalid record;@WebMvcTestregisters the fake as a test bean (verify the current mock/bean annotation for your Spring Boot version).
Outcome 1 is also checked by quiz Q01 to Q03. Add a new field (for example keywords) to the record, validation, fake, and tests for the assessment.
الاختبار
Outcomes: O1 how a call works · O2 ChatClient · O3 structured output · O4 streaming, timeouts, budgets · O5 interface and fake.
اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.
الجزء العملي
Six exercises JNR-056-EX01 to EX06 plus the AI-helper lab. Pass: at least five exercises and a lab meeting every acceptance item; the assessment extension (a new field) must keep all tests offline and deterministic.
أخطاء شائعة
Parsing free text instead of requesting structured output; no timeout on model calls; API keys in application.yml; tests that call the real model.
التقييم
Extend the helper with a new field and keep all tests deterministic.
الصلة بالمسار الوظيفي
LLM features are increasingly part of backend work; JNR-057 secures them, and MID-067 to MID-070 add retrieval, tools, and evaluation.