[PoC] Implement a state machine in the AiServices #861
+394
−32
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
For now this is only a PoC to start a discussion around the possibility of implementing a state machine inside the AiServces. This pull request is a complete rework of this other one and tries to put my proposal at work using the same use case that I implemented here.
In my opinion the original pull request was a bit inflexible, trying to model all the states and possible transactions among them inside the AiServices itself, and this also led to a more cumbersome API. The idea here is that how the transactions among the different states are modeled should be totally external to the AiServices. This increased flexibility also allows to model the state machine with other external tools, like a rule engine in my case.
Note that this pull request (like the original one) still lacks the possibility to define a state machine for each user/conversation, but I believe this could be implemented with an id-based mechanism, very similar to the one already used for the chat memory. Moreover, since in my test example I'm using ollama that doesn't support tools, I didn't try to put also them under the control of the state machine, but once we agreed on the implementation this could also be added in a relatively easy way.
While working on this feature however I realized that probably also the concept of a proper state machine is quite over-engineered for the langchain4j needs, and what would be necessary is more simply a way to make system and user messages (and eventually tools) configurable in a programmatic way. I developed this idea in another pull request.