Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the AiService to inject AiMessage into the prompt #905

Open
andreadimaio opened this issue Sep 18, 2024 · 16 comments
Open

Allow the AiService to inject AiMessage into the prompt #905

andreadimaio opened this issue Sep 18, 2024 · 16 comments

Comments

@andreadimaio
Copy link
Collaborator

Might be useful to have an annotation that allows the developer to use a template for the AiMessage.

Example:

@RegisterAiService
public interface AiService {

    @SystemMessage("You are a helpful assistant")
    @UserMessage("Input: {question}")
    @AiMessage("Output: ")
    public String answer(String question);
}

This is particularly useful if you are using models that use tags.
I don't know if this is something that might come from langchain4j.

@andreadimaio
Copy link
Collaborator Author

For example if I'm using llama-3-1-70b-instruct the output should be:

<|begin_of_text|><|start_header_id|>system<|end_header_id|>
You are a helpful assistant<|eot_id|><|start_header_id|>user<|end_header_id|>
Input: Hi<|eot_id|><|start_header_id|>assistant<|end_header_id|>
Output:

@geoand
Copy link
Collaborator

geoand commented Sep 18, 2024

Hm, again I wonder if this is something that would be handled by @SeedMemory

@andreadimaio
Copy link
Collaborator Author

No, at least I don't think so from what I know about @SeedMemory.

The @SeedMemory helps me to inject some data into the ChatMemory in the format I prefer, so this help me to have an AiMessage with the correct prefix, but what happens when a new message is sent to the LLM? The "Output: " will disappear.

@geoand
Copy link
Collaborator

geoand commented Sep 18, 2024

Fair enough.

Question: wouldn't having AiMessage after @UserMessage and without another user input break the inference?
I think I recall OpenAI requiring a specific order of messages.

@andreadimaio
Copy link
Collaborator Author

I see your point... in this case the AiMessage specified in the AiService is something that is only used to get the input message that is sent to the LLM, it does not have to be part of the memory.

@andreadimaio
Copy link
Collaborator Author

What I'm tryng to say is, in the example above the result of the memory after a call should be something like:

SystemMessage: You are a helpful assistant
UserMessage: Input: {question}
AiMessage: Output: <value_returned_by_llm> 

and not:

SystemMessage: You are a helpful assistant
UserMessage: Input: {question}
AiMessage: Output:
AiMessage: <value_returned_by_llm> 

@geoand
Copy link
Collaborator

geoand commented Sep 18, 2024

I see. @langchain4j is this something that would make sense for the project?

@andreadimaio
Copy link
Collaborator Author

I'm thinking more about the scenarios where I could use the @AiMessage annotation in the AiService and they are all "one shot calls" (LLM without memory). In this case I can get a "good" prompt without the use of the @AiMessage.

I understand that for providers using a "chat" API this can be a bit weird. In any case, I know how to go on without adding this feature, from my side it is also possible to close this issue. :)

@geoand
Copy link
Collaborator

geoand commented Sep 18, 2024

Thanks for the update.

Let's see what @langchain4j thinks

@langchain4j
Copy link
Collaborator

Hi @andreadimaio, could you provide a (real world) example when this can be useful?

There is a related request for Anthropic, but there we agreed to handle it on ChatLanguageModel level. It seems that it might indeed be useful to have something like @AiMessage, or more precisely, @AiMessagePrefix annotation.

@andreadimaio
Copy link
Collaborator Author

andreadimaio commented Sep 18, 2024

We can use the first message as example.

Let's say I'm using llama-3-1-70b-instruct and I need to construct a prompt with a @SystemMessage, @UserMessage and to get the best response from the model it's useful to also send a prefix for the @AiMessage in the request body.

So suppose the request to get a best answer has to be formatted like this:

<|begin_of_text|><|start_header_id|>system<|end_header_id|>
You are a helpful assistant<|eot_id|><|start_header_id|>user<|end_header_id|>
Input: Hi<|eot_id|><|start_header_id|>assistant<|end_header_id|>
Output:

Where:

  • You are a helpful assistant is part of the @SystemMessage,
  • Input: Hi is part of the @UserMessage
  • Output: is part of the @AiMessage

Today, it is not possible to use the annotations in the AiService to achieve this behaviour, because if I write something like this

@RegisterAiService
public interface AiService {

    @SystemMessage("You are a helpful assistant")
    @UserMessage("Input: {question}\nOutput:")
    public String answer(String question);
}

the result will be:

<|begin_of_text|><|start_header_id|>system<|end_header_id|>
You are a helpful assistant<|eot_id|><|start_header_id|>user<|end_header_id|>
Input: Hi\nOutput:<|eot_id|><|start_header_id|>assistant<|end_header_id|>

As you can see in this case, the "Output:" prefix is in the @UserMessage and it is not what I'm looking for.

@andreadimaio
Copy link
Collaborator Author

From my point of view, the @AiMessagePrefix is a good solution to solve this scenario.

@langchain4j
Copy link
Collaborator

@andreadimaio sorry for confusion, but I am still not sure what is the purpose of "Output:" here?
Is it to nudge the model to start the answer with this specific text, or is it to follow the formatting of how the model was fine-tuned (can't find "Output:" in the documentation)?

@andreadimaio
Copy link
Collaborator Author

Is it to nudge the model to start the answer with this specific text, or is it to follow the formatting of how the model was fine-tuned (can't find "Output:" in the documentation)?

This is just an example, but yes the ability to add a prefix gives the developer more flexibility when writing a prompt, and as you said, some models could be fine-tuned to respond better with a prefix.

@langchain4j
Copy link
Collaborator

@andreadimaio sure, let's go with @AiMessagePrefix. But I am still curious about real-world example of a prefix, cause "Output:" does not seem particulary useful.

@andreadimaio
Copy link
Collaborator Author

andreadimaio commented Sep 19, 2024

@andreadimaio sure, let's go with @AiMessagePrefix. But I am still curious about real-world example of a prefix, cause "Output:" does not seem particulary useful.

No, personally I don't have any real-world scenario, as I wrote in some message above, my use cases can do without the @AiMessagePrefix. However, I think this is a good method to customise the prompt to send to the LLM :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants