大模型实战-【Langchain4J中Using AI Services in Spring Boot Application②】

发布于:2024-06-18 ⋅ 阅读:(150) ⋅ 点赞:(0)

Using AI Services in Spring Boot Application

LangChain4j Spring Boot starter
greatly simplifies using AI Services in Spring Boot applications.

@SystemMessage

Now, let’s look at a more complicated example.
We’ll force the LLM reply using slang 😉

This is usually achieved by providing instructions in the SystemMessage.

interface Friend {
   

    @SystemMessage("You are a good friend of mine. Answer using slang.")
    String chat(String userMessage);
}

Friend friend = AiServices.create(Friend.class, model);

String answer = friend.chat("Hello"); // Hey! What's up?

In this example, we have added the @SystemMessage annotation with a system prompt we want to use.
This will be converted into a SystemMessage behind the scenes and sent to the LLM along with the UserMessage.

System Message Provider

System messages can also be defined dynamically with the system message provider:

Friend friend = AiServices.builder(Friend.class)
    .chatLanguageModel(model)
    .systemMessageProvider(chatMemoryId -> "You are a good friend of mine. Answer using slang.")
    .build();

As you can see, you can provide different system messages based on a chat memory ID (user or conversation).

@UserMessage

Now, let’s assume the model we use does not support system messages,
or maybe we just want to use UserMessage for that purpose.

interface Friend {
   

    @UserMessage("You are a good friend of mine. Answer using slang. {
   {it}}")
    String chat(String userMessage);
}

Friend friend = AiServices.create(Friend.class, model);

String answer = friend.chat("Hello"); // Hey! What's shakin'?

We have replaced the @SystemMessage annotation with @UserMessage
and specified a prompt template with the variable it to refer to the only method argument.

Additionally, it’s possible to annotate the String userMessage with @V
and assign a custom name to the prompt template variable:

interface Friend {
   

    @UserMessage("You are a good friend of mine

网站公告

今日签到

点亮在社区的每一天
去签到