feat: 98 generative AI agent for classification #105
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.
✨ Summary
These changes introduce the framework for using Generative AI Large Language models to perform classifications on VectorStoreSearchResult objects, and integrates with the existing VectorStore search pipeline.
It is adding a new module called agents, which function similarly to the Vectorisers class/module. Currently implemented is the GcpVectoriser. This class has a
transform()method which intakes aVectorStoreSearchOutputobject and transforms it in some way using generative AI but still outputs a validVectorStoreSearchOutputobject.For example, we can instantiate an agent that classifies the top K semantic search candidates with the following Python code:
The instantiated agent has a
transformmethod that accepts aVectorStoreSearchOutputobject:This standalone agent can then be injected into the VectorStore, so that it automatically runs when the
VectorStore.search()method is called, by setting the 'agent' attribute. Either on instantiation:or by setting the attribute to a VectorStore already in runtime:
my_vector_store.agent = my_agentInner workings and framework design
The GcpVectoriser inherits from a base class that specifies the Agents must take in and output a
VectorStoreSearchOutputobject.This introduces a core concepts of how we use Generative AI in the ClassifAI workflow - i.e. it should only manipulate existing results objects.
There are 4 key components to the GcpAgent model that has been created:
using this 4 step approach makes it possible to create different ways of using the Agent, by changing the system prompt instruction and the corresponding post-processing function - all while grounding the behaviour to make sure we're only manipulating valid VectorStoreSearchOutput objects.
Other agent models such as a HuggingfaceAgent (still to be developed), could follow the same paradigm, and it would give guidance to package users who may wish to implement their own custom generative model by inheriting from the base class.
Classification example
Both the System Prompt and the post-processing function can vary in behaviour based on which task type the agent model is instantiated with. Currently only 'classification' task type is available.
With the classification task type the system prompt is set as (paraphrasing for briefness):
(the actual system prompt is more detailed, contains an example of the data structure, and guidelines - see this in the code files)
The system prompt and formatted search results are combined and passed to the generative model.
The classification post-processing function then assesses if the generative AI output is of the correct format using Pydantic. If it is, it takes the chosen ID and reduces the original VectorStoreSearchOutput object down to the chosen row using the ID generated by the generative AI. If the correct format response was not generated, the post-processing function simply returns the original VectorStoreSearchOutput with no changes.
Considerations:
Agents as hooks instead of as a specific attribute of the VectorStore
One specific consideration, since all the agent is doing is transforming the VectorStoreSearchOutput object into another VectorStoreSearchOutput: it could be treated simply as a post-processing hook on the VectorStore search method. This would be in line with the hooks update we did recently and would simplify the integration of the agent with the VectorStore, although the integration currently is not complex. We could just provide the agents as a kind of fancy pre-build hook ready for use by users.
📜 Changes Introduced
✅ Checklist
terraform fmt&terraform validate)🔍 How to Test
Installing this branch as normal, and then running through the new notebook would be a good way to explore and test the features of the changes. One note, is to make sure to use a google cloud project with the generative language api activated. Otherwise the requests to that api won't be successful