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

Transform to add an agent's name into the message content #4

Merged
merged 2 commits into from
Sep 4, 2024

Conversation

marklysze
Copy link
Collaborator

The recent addition of non-OpenAI client classes has expanded the utility of AutoGen to a lot of different models through other cloud providers as well as the ability to run it with local LLMs.

However, the name field on each message is removed as only OpenAI can accept and utilise that field.

This means that functionality, like the GroupChat's select speaker, isn't as effective as the LLM may not be able to determine which 'agent' belongs to each message.

This PR introduces a transform that can add an agent's name to the start or end of message's text content. As this is a transform, it isn't permanent and doesn't change the original message.

How it works:

from autogen.agentchat.contrib.capabilities import transform_messages, transforms

# Create a transform
name_transform = TextMessageContentName(position="start", format_string="'{name}' said:\n")

# Create the TransformMessages
context_handling = transform_messages.TransformMessages(
            transforms=[
                name_transform
            ]
        )

# Add it to an agent so when they run inference it will apply to the messages
context_handling.add_to_agent(my_agent)

# Example
# Messages before
[
        {"role": "system", "content": "I am the system."},
        {"role": "user", "name": "charlie", "content": "I think the sky is blue."},
        {"role": "user", "name": "mary", "content": "The sky is red."},
        {"role": "user", "name": "bob", "content": "The sky is crimson."},
    ]

# Messages going into LLM (where name is removed, non-OpenAI clients)
[
        {"role": "system", "content": "I am the system."},
        {"role": "user", "content": "'charlie' said:\nI think the sky is blue."},
        {"role": "user", "content": "'mary' said:\nThe sky is red."},
        {"role": "user", "content": "'bob' said:\nThe sky is crimson."},
    ]

Documentation to be done.

Dependency

For the GroupChat select speaker to be able to utilise this, PR microsoft#2719 needs to be merged first.

Why are these changes needed?

To provide the necessary name context for non-OpenAI inference.

Related issue number

Checks

(3334)

@sonichi
Copy link
Collaborator

sonichi commented Aug 31, 2024

Thanks. Could you include the code snippet in "how it works" in docstr and a test?

@sonichi sonichi requested review from BeibinLi and Hk669 August 31, 2024 14:21
Comment on lines 472 to 473
assert isinstance(position, str) and position is not None
assert position in ["start", "end"]
Copy link
Collaborator

Choose a reason for hiding this comment

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

can we just combine these checks.

Suggested change
assert isinstance(position, str) and position is not None
assert position in ["start", "end"]
assert isinstance(position, str) and position in ["start", "end"]

Copy link
Collaborator Author

@marklysze marklysze Sep 1, 2024

Choose a reason for hiding this comment

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

I'll just check that None is handled correctly.

Copy link
Collaborator Author

@marklysze marklysze Sep 1, 2024

Choose a reason for hiding this comment

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

Okay, None handled okay in both cases, thanks - will change

Comment on lines 474 to 475
assert isinstance(format_string, str) and format_string is not None
assert "{name}" in format_string
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
assert isinstance(format_string, str) and format_string is not None
assert "{name}" in format_string
assert isinstance(format_string, str) and "{name}" in format_string

@Hk669
Copy link
Collaborator

Hk669 commented Aug 31, 2024

@marklysze , ive also noticed, gpt-4o-mini doesnt support name and breaks the flow.

@marklysze
Copy link
Collaborator Author

@marklysze , ive also noticed, gpt-4o-mini doesnt support name and breaks the flow.

This was noted in another issue, do you have a code sample? I couldn't replicate an issue with name included.

@marklysze
Copy link
Collaborator Author

Thanks. Could you include the code snippet in "how it works" in docstr and a test?

Thanks @sonichi, I've added the code snipped to the class docstring.

For the tests, the test_transforms.py has a number of tests for including the name related to position and filtering. Was there some you thought were missing? Reference

@marklysze
Copy link
Collaborator Author

A note: use of this transform is included in website doc changes in PR #2

@sonichi sonichi merged commit dd32fb8 into main Sep 4, 2024
140 of 152 checks passed
@sonichi sonichi deleted the transformwithname branch September 4, 2024 15:57
odoochain pushed a commit to odoochain/autogen that referenced this pull request Nov 10, 2024
Co-authored-by: Kosta Petan <Kosta.Petan@microsoft.com>
odoochain pushed a commit to odoochain/autogen that referenced this pull request Nov 10, 2024
odoochain pushed a commit to odoochain/autogen that referenced this pull request Nov 10, 2024
* fix dotnet distributed tracing

* regenerate protos for python
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
alt-models Non-OpenAI Models / Client Classes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants