LangChain4j's AI Services provide annotations like @SystemMessage and @UserMessage to control how prompts are constructed and sent to the LLM. These annotations allow you to define system instructions and format user inputs without manually creating message objects.
Please check out this tutorial to construct messages without annotations.
@SystemMessage Annotation
The @SystemMessage annotation adds a system prompt that defines the assistant's role, behavior, or constraints. This is particularly useful for:
- Setting the assistant's personality or expertise
- Defining response format requirements
- Adding safety instructions or constraints
- Providing context about the task domain
Definition of SystemMessageVersion: 1.10.0 package dev.langchain4j.service;
@Target({ TYPE, METHOD })
@Retention(RUNTIME)
public @interface SystemMessage {
String[] value() default "";
String delimiter() default "\n";
String fromResource() default "";
}
Quick Example
@SystemMessage("You are a helpful assistant")
@UserMessage Annotation
The @UserMessage annotation formats the user input. You can include template variables that reference method parameters using {{it}} for single parameters or named variables for multiple parameters.
Definition of UserMessageVersion: 1.10.0 package dev.langchain4j.service;
@Retention(RUNTIME)
@Target({ METHOD, PARAMETER })
public @interface UserMessage {
String[] value() default "";
String delimiter() default "\n";
String fromResource() default "";
}
Quick Example
@UserMessage("What is artificial intelligence?")
Example
The following example uses Ollama and phi3:mini-128k which is good for a demo and learning but not good for production-grade applications because it has limited reasoning capabilities and accuracy for complex tasks.
package com.logicbig.example;
import dev.langchain4j.model.ollama.OllamaChatModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.SystemMessage;
import dev.langchain4j.service.UserMessage;
public class SystemUserMessagesExample {
interface FormalAssistant {
@SystemMessage("You are a formal business assistant. Use professional language.")
String chat(String userMessage);
}
interface CasualAssistant {
@SystemMessage("You are a friendly assistant. Use casual, conversational language.")
String chat(String userMessage);
}
interface Translator {
@SystemMessage("You are a translator")
@UserMessage("Translate to French: Hi there")
String translate();
}
public static void main(String[] args) {
OllamaChatModel model = OllamaChatModel.builder()
.baseUrl("http://localhost:11434")
.modelName("phi3:mini-128k")
.temperature(0.7)
.build();
// Test different system messages
FormalAssistant formal = AiServices.create(FormalAssistant.class, model);
CasualAssistant casual = AiServices.create(CasualAssistant.class, model);
Translator translator = AiServices.create(Translator.class, model);
String question = "What is artificial intelligence?";
System.out.println("Formal response:");
System.out.println(formal.chat(question));
System.out.println("\nCasual response:");
System.out.println(casual.chat(question));
System.out.println("\nTranslation:");
System.out.println(translator.translate());
}
}
OutputFormal response: Artificial Intelligence, often abbreviated as AI, refers to the simulation of human intelligence in machines that are designed to think and act like humans. It involves creating algorithms capable of learning from experience, adapting to new inputs, improving over time without explicit programming for each specific task or problem domain. This encompasses a wide array of technologies such as machine learning (ML), natural language processing (NLP), computer vision, robotics and more which enable machines not only to perform complex tasks but also interpret data with minimal human intervention. AI is integrated into numerous applications across various industries including healthcare, automotive, finance, retail among others improving efficiency and decision making processes.
Casual response: Hey there! Artificial intelligence, or AI for short, is like teaching computers to think and act almost like humans do�making decisions, recognizing speech, playing games, driving cars, you name it. It's all about creating smart machines that can learn from experience using algorithms instead of being explicitly programmed for each task they undertake. Pretty cool stuff!
Translation: Hey, salut !
Conclusion
The output demonstrates how different system messages influence the LLM's response style. The @SystemMessage annotation allows you to control the assistant's behavior declaratively, while @UserMessage provides flexibility in formatting user inputs. The AI Service proxy automatically combines these messages with the appropriate message types before sending them to the LLM.
Example ProjectDependencies and Technologies Used: - langchain4j 1.10.0 (Build LLM-powered applications in Java: chatbots, agents, RAG, and much more)
- langchain4j-ollama 1.10.0 (LangChain4j :: Integration :: Ollama)
- slf4j-simple 2.0.9 (SLF4J Simple Provider)
- JDK 17
- Maven 3.9.11
|