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

会会会会 2024-06-24 10:01:01 阅读 61

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



声明

本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。