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

Add new ChatMemory implementation to be used for stateful data extraction #1067

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mariofusco
Copy link
Contributor

This pull request implements the ChatMemory that I have discussed and proposed here. This should be a good fit to be used for stateful data extraction, so for instance the included test case produces the following output:

User: hi
User: my name is Mario Fusco
User: I'm 50
Extracted Customer { firstName = "Mario", lastName = "Fusco", age = 50 }

In essence what this ChatMemory does is simply concatenating the values of variables sent by a user at each iteration, recreating at each step the user message from the original prompt template and those concatenated variables. In this way the user message sent to the LLM at the 3rd prompt of my example above will be something like:

"Extract information about a customer from this text 'hi. my name is Mario Fusco. I'm 50'. The response must contain only the JSON with customer's data and without any other sentence. You must answer strictly in the following JSON format: {\n"firstName": (type: string),\n"lastName": (type: string),\n"age": (type: integer)"

In order to implement this feature I had to add to the UserMessage both the prompt template and the set of variables from which it has been created. I believe that carrying those information can be useful also beyond the specific needs of this pull request. In reality probably it would be an ever better design if the UserMessage would know how to render itself and use the PromptTemplate internally instead of having a text populated from the outside as it does now. I'm open to also implement this further improvement, but for now I just wanted to demonstrate the general idea with the smallest possible set of changes.

/cc @sebastienblanc

@langchain4j langchain4j added the P2 High priority label May 10, 2024
@langchain4j
Copy link
Owner

Hi @mariofusco, thanks a lot! Will try to review it asap

@mariofusco
Copy link
Contributor Author

Do you have any news about this? I'm seeing that this pull request could be relevant also in the light of broader feature requests related to chat memories. More in general we may want to introduce some sort of minimal SPI to facilitate the pluggability of custom chat memory implementations and maybe rewrite the existing memories in terms of this SPI. I could sketch this idea in a different pull request if you're interested, or maybe did you already have something similar in mind?

@langchain4j
Copy link
Owner

@mariofusco sorry, I did not have time to look at it yet, I will try to do it today

@langchain4j
Copy link
Owner

langchain4j commented Jun 4, 2024

Hi @mariofusco! If I understand correctly, this use case assumes interactive conversation with the user to collect all the needed details, right? But in the test there is no response from the model (with guidance what information should be provided) because it outputs a Customer object, not a text. How exactly should this be used? Thanks!

@mariofusco
Copy link
Contributor Author

Hi @mariofusco! If I understand correctly, this use case assumes interactive conversation with the user to collect all the needed details, right? But in the test there is no response from the model (with guidance what information should be provided) because it outputs a Customer object, not a text. How exactly should this be used? Thanks!

That's correct, this chat memory is designed to be used with extractors so it cannot provide any message for the user. This means that a second AI service needs to be used together with this in order to give some feedback to user. Here you can see an example of how I used a very similar strategy. In fact here for each state of the conversation I had to use both a [Customer/Flight]Extractor and [Customer/Flight]ChatService. I have no idea if there's a better way to achieve a similar result and in particular if it is possible to avoid the double AI service for this situation. Any advice is welcome.

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

Successfully merging this pull request may close these issues.

None yet

2 participants