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

Decouple model_context from AssistantAgent #4681

Merged
merged 13 commits into from
Dec 20, 2024

Conversation

akurniawan
Copy link
Contributor

@akurniawan akurniawan commented Dec 12, 2024

Why are these changes needed?

This is to decouple model_context from AssistantAgent to provide flexibility in augmenting incoming message to the agent. This could be useful when we have a specific template for an agent that is a part of a GroupChat, discussion here: #4668

Usage example

class MyModelContext()
  .....
  ....
my_model_context = MyModelContext(...)
agent = AssistantAgent(model_client =(..), model_context=(my_model_context...))
team = RoundRobinGroupChat(participantants=[agent] ...)

Related issue number

Checks

@akurniawan
Copy link
Contributor Author

@akurniawan please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@microsoft-github-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@microsoft-github-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@microsoft-github-policy-service agree company="Microsoft"

Contributor License Agreement

@microsoft-github-policy-service agree

@rysweet rysweet requested review from ekzhu and Copilot December 12, 2024 16:22

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 1 out of 1 changed files in this pull request and generated no suggestions.

Comments skipped due to low confidence (1)

python/packages/autogen-agentchat/src/autogen_agentchat/agents/_assistant_agent.py:317

  • The error message 'No tools are available.' could be more descriptive. Consider changing it to 'No tools or handoff tools are available for execution.'
raise ValueError("No tools are available.")
@rysweet rysweet requested a review from jackgerrits December 12, 2024 16:25
@ekzhu
Copy link
Collaborator

ekzhu commented Dec 13, 2024

@victordibia please take a look at this and see the implication to #4438 .

@akurniawan thanks for the PR, since this is very closely related to the aforementioned PR, please change this to DRAFT state and we will need to coordinate the work.

@victordibia
Copy link
Collaborator

victordibia commented Dec 14, 2024

thanks @akurniawan , @ekzhu, @jackgerrits
PR is looking good.

Current PR

From my understanding this PR does the following:

  • adds model_context as a argument to AssistantAgent .. based on the ChatCompletionContext class
  • uses this model_context when passed or adds and instantiates one based on UnboundedBufferedChatCompletionContext
  • The expected usage/benefit is that at design time, the developer can pass in their own model_context object and control behaviors on how/what context is stored by overloading the add_message method e.g., transform the structure of the message before it is added, drop some messages, replace etc.

@akurniawan .. can you confirm the above is correct? Also, if possible can you update the PR description with an example usage ...e.g., below?

class MyModelContext()
  .....
  ....
my_model_context = MyModelContext(...)
agent = AssistantAgent(model_client =(..), model_context=(my_model_context...))
team = RoundRobinGroupChat(participantants=[agent] ...)

PR #4438 - Memory in AgentChat

#4438 focuses on

  • adding adding a memoryargument toAssistantAgent`.
  • At runtime, if a memory object is passed, effort is made to query it to update the exact context message just in time (RAG) by retrieving from the memory store. E.g., I pass in a memory argument with my preferences (e.g., I am vegan) and that is retrieved and added to a query like provide a meal plan. (stuff can be added/removed from memory external to the assistant agent resulting in dynamic behaviours during runtime)

My feeling is that there are differences - while model_context is focused fixed design time updates to all messages, memory is more towards just-in-time updates. I feel both can coexist

  • model_context is used to transform all messages
  • memory is used to append stuff relevant, each time the agent receives a message. Implementation would be to add retrieved memory messages to llm_messages

Thoughts welcome.

@ekzhu
Copy link
Collaborator

ekzhu commented Dec 14, 2024

My feeling is that there can be two things:

  1. memory, which has a method that generates or transforms a model context
  2. model context, an object that contains the LLMMessage that will be sent to the model

I think that the current model context interface is not quite complete, because a model context should also include tools or tool schema -- something a memory module should also provide augmentation for.

In this regard, I think the model context object could be potentially transient -- the agent only create one on demand when model inference is performed.

@akurniawan akurniawan marked this pull request as draft December 14, 2024 05:54
@akurniawan akurniawan force-pushed the agent_context branch 2 times, most recently from 1942071 to 1966d7e Compare December 14, 2024 05:56
@akurniawan
Copy link
Contributor Author

@victordibia yes that's correct and I have updated the PR description as suggested

Copy link
Collaborator

@ekzhu ekzhu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@akurniawan thanks.

@victordibia and I have discussed and we think this PR can be marked as Ready for Review.

@ekzhu ekzhu marked this pull request as ready for review December 16, 2024 20:22
@ekzhu
Copy link
Collaborator

ekzhu commented Dec 18, 2024

@akurniawan I think this PR is almost at the finish line. I believe we just need to resolve the few remaining issues.

Would you like us to take over and get it done?

@ekzhu
Copy link
Collaborator

ekzhu commented Dec 18, 2024

@akurniawan I am going to push some changes.

@akurniawan
Copy link
Contributor Author

@akurniawan I think this PR is almost at the finish line. I believe we just need to resolve the few remaining issues.

Would you like us to take over and get it done?

Hey sorry was not available yesterday. I can complete the changes you suggested today.

@akurniawan
Copy link
Contributor Author

it seems you have already changed it, thanks for the help!

@ekzhu
Copy link
Collaborator

ekzhu commented Dec 19, 2024

You are welcome 😃

Join our office hours and discord to discuss more.

https://aka.ms/autogen-officehour

https://aka.ms/autogen-discord

Copy link
Collaborator

@victordibia victordibia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks.

to add memory, we can do something like

...
# Inner messages.
        inner_messages: List[AgentEvent | ChatMessage] = [] 

        # Transform the model context for each "memory source"
        if memory and isinstance(memory, list):
            for m in memory: 
                self._model_context = await m.transform(self._model_context, transform=True) 
                

@ekzhu , what do you think?

Copy link
Contributor

@husseinmozannar husseinmozannar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can either use a list or the ChatCompletionContext without anything breaking right?

@ekzhu
Copy link
Collaborator

ekzhu commented Dec 20, 2024

Looks good, thanks.

to add memory, we can do something like

...

# Inner messages.

        inner_messages: List[AgentEvent | ChatMessage] = [] 



        # Transform the model context for each "memory source"

        if memory and isinstance(memory, list):

            for m in memory: 

                self._model_context = await m.transform(self._model_context, transform=True) 

                

@ekzhu , what do you think?

Yes. That's what I was thinking. The model context is essentially the input or the "working set" that model consumes.

@ekzhu
Copy link
Collaborator

ekzhu commented Dec 20, 2024

We can either use a list or the ChatCompletionContext without anything breaking right?

Right now no one is using this new constructor argument so it will not be breaking change.

@ekzhu
Copy link
Collaborator

ekzhu commented Dec 20, 2024

Looks good, thanks.

to add memory, we can do something like

...

# Inner messages.

        inner_messages: List[AgentEvent | ChatMessage] = [] 



        # Transform the model context for each "memory source"

        if memory and isinstance(memory, list):

            for m in memory: 

                self._model_context = await m.transform(self._model_context, transform=True) 

                

@ekzhu , what do you think?

Perhaps the memory can directly mutate the context. Right now the code snippet just shows direct update. We want be prepared for a scenario in which a remote model context hosted on a service. Eg OpenAI Assistant API's thread.

@victordibia
Copy link
Collaborator

Perhaps the memory can directly mutate the context. Right now the code snippet just shows direct update. We want be prepared for a scenario in which a remote model context hosted on a service. Eg OpenAI Assistant API's thread.

Makes sense ... we can discuss this on the memory PR once this is merged.
Looks ready to merge.

@ekzhu ekzhu merged commit c989181 into microsoft:main Dec 20, 2024
48 checks passed
@akurniawan akurniawan deleted the agent_context branch December 20, 2024 06:52
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

Successfully merging this pull request may close these issues.

5 participants