Skip to content

Commit 2a401c3

Browse files
authored
Python: intro allowed content types in chat history channel receive. Add mixed chat image sample. (#10347)
### Motivation and Context During a group chat, any file reference content created by an assistant agent, doesn't need to be communicated to a chat completion agent. Filter these types out and only include other types, like text, if available. <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> ### Description This PR: - Adds a mixed chat image sample to have an assistant agent generate an image, along with text, and call the chat completion agent successfully with only the allowed types (like text). - Adds a unit test to exercise the same behavior. - Closes #10317 <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [X] The code builds clean without any errors or warnings - [X] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [X] All unit tests pass, and I have added new tests where possible - [X] I didn't break anyone 😄
1 parent f4f8637 commit 2a401c3

File tree

4 files changed

+171
-2
lines changed

4 files changed

+171
-2
lines changed

python/samples/concepts/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
- [Mixed Chat Agents](./agents/mixed_chat_agents.py)
1515
- [Mixed Chat Agents Plugins](./agents/mixed_chat_agents_plugins.py)
1616
- [Mixed Chat Files](./agents/mixed_chat_files.py)
17+
- [Mixed Chat Images](./agents/mixed_chat_images.py)
1718
- [Mixed Chat Reset](./agents/mixed_chat_reset.py)
1819
- [Mixed Chat Streaming](./agents/mixed_chat_streaming.py)
1920

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Copyright (c) Microsoft. All rights reserved.
2+
3+
import asyncio
4+
5+
from semantic_kernel.agents import AgentGroupChat, ChatCompletionAgent
6+
from semantic_kernel.agents.open_ai import OpenAIAssistantAgent
7+
from semantic_kernel.agents.open_ai.azure_assistant_agent import AzureAssistantAgent
8+
from semantic_kernel.connectors.ai.open_ai.services.azure_chat_completion import AzureChatCompletion
9+
from semantic_kernel.contents.annotation_content import AnnotationContent
10+
from semantic_kernel.contents.chat_message_content import ChatMessageContent
11+
from semantic_kernel.contents.utils.author_role import AuthorRole
12+
from semantic_kernel.kernel import Kernel
13+
14+
#####################################################################
15+
# The following sample demonstrates how to create an OpenAI #
16+
# assistant using either Azure OpenAI or OpenAI, a chat completion #
17+
# agent and have them participate in a group chat working with #
18+
# image content. #
19+
#####################################################################
20+
21+
22+
def _create_kernel_with_chat_completion(service_id: str) -> Kernel:
23+
kernel = Kernel()
24+
kernel.add_service(AzureChatCompletion(service_id=service_id))
25+
return kernel
26+
27+
28+
async def invoke_agent(
29+
chat: AgentGroupChat, agent: ChatCompletionAgent | OpenAIAssistantAgent, input: str | None = None
30+
) -> None:
31+
"""Invoke the agent with the user input."""
32+
if input:
33+
await chat.add_chat_message(message=ChatMessageContent(role=AuthorRole.USER, content=input))
34+
print(f"# {AuthorRole.USER}: '{input}'")
35+
36+
async for content in chat.invoke(agent=agent):
37+
print(f"# {content.role} - {content.name or '*'}: '{content.content}'")
38+
if len(content.items) > 0:
39+
for item in content.items:
40+
if isinstance(item, AnnotationContent):
41+
print(f"\n`{item.quote}` => {item.file_id}")
42+
response_content = await agent.client.files.content(item.file_id)
43+
print(response_content.text)
44+
45+
46+
async def main():
47+
try:
48+
ANALYST_NAME = "Analyst"
49+
ANALYST_INSTRUCTIONS = "Create charts as requested without explanation."
50+
analyst_agent = await AzureAssistantAgent.create(
51+
kernel=Kernel(),
52+
enable_code_interpreter=True,
53+
name=ANALYST_NAME,
54+
instructions=ANALYST_INSTRUCTIONS,
55+
)
56+
57+
SUMMARIZER_NAME = "Summarizer"
58+
SUMMARIZER_INSTRUCTIONS = "Summarize the entire conversation for the user in natural language."
59+
service_id = "summary"
60+
summary_agent = ChatCompletionAgent(
61+
service_id=service_id,
62+
kernel=_create_kernel_with_chat_completion(service_id=service_id),
63+
instructions=SUMMARIZER_INSTRUCTIONS,
64+
name=SUMMARIZER_NAME,
65+
)
66+
67+
chat = AgentGroupChat()
68+
69+
await invoke_agent(
70+
chat=chat,
71+
agent=analyst_agent,
72+
input="""
73+
Graph the percentage of storm events by state using a pie chart:
74+
75+
State, StormCount
76+
TEXAS, 4701
77+
KANSAS, 3166
78+
IOWA, 2337
79+
ILLINOIS, 2022
80+
MISSOURI, 2016
81+
GEORGIA, 1983
82+
MINNESOTA, 1881
83+
WISCONSIN, 1850
84+
NEBRASKA, 1766
85+
NEW YORK, 1750
86+
""",
87+
)
88+
await invoke_agent(chat=chat, agent=summary_agent)
89+
finally:
90+
if analyst_agent is not None:
91+
[await analyst_agent.delete_file(file_id=file_id) for file_id in analyst_agent.code_interpreter_file_ids]
92+
await analyst_agent.delete()
93+
94+
95+
if __name__ == "__main__":
96+
asyncio.run(main())

python/semantic_kernel/agents/channels/chat_history_channel.py

+28-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@
33
import sys
44
from collections import deque
55
from collections.abc import AsyncIterable
6+
from copy import deepcopy
7+
8+
from semantic_kernel.contents.image_content import ImageContent
9+
from semantic_kernel.contents.streaming_text_content import StreamingTextContent
10+
from semantic_kernel.contents.text_content import TextContent
611

712
if sys.version_info >= (3, 12):
813
from typing import override # pragma: no cover
914
else:
1015
from typing_extensions import override # pragma: no cover
1116

1217
from abc import abstractmethod
13-
from typing import TYPE_CHECKING, Deque, Protocol, runtime_checkable
18+
from typing import TYPE_CHECKING, ClassVar, Deque, Protocol, runtime_checkable
1419

1520
from semantic_kernel.agents.channels.agent_channel import AgentChannel
1621
from semantic_kernel.contents import ChatMessageContent
@@ -45,6 +50,14 @@ def invoke_stream(self, history: "ChatHistory") -> AsyncIterable["ChatMessageCon
4550
class ChatHistoryChannel(AgentChannel, ChatHistory):
4651
"""An AgentChannel specialization for that acts upon a ChatHistoryHandler."""
4752

53+
ALLOWED_CONTENT_TYPES: ClassVar[tuple[type, ...]] = (
54+
ImageContent,
55+
FunctionCallContent,
56+
FunctionResultContent,
57+
StreamingTextContent,
58+
TextContent,
59+
)
60+
4861
@override
4962
async def invoke(
5063
self,
@@ -142,10 +155,23 @@ async def receive(
142155
) -> None:
143156
"""Receive the conversation messages.
144157
158+
Do not include messages that only contain file references.
159+
145160
Args:
146161
history: The history of messages in the conversation.
147162
"""
148-
self.messages.extend(history)
163+
filtered_history: list[ChatMessageContent] = []
164+
for message in history:
165+
new_message = deepcopy(message)
166+
if new_message.items is None:
167+
new_message.items = []
168+
allowed_items = [item for item in new_message.items if isinstance(item, self.ALLOWED_CONTENT_TYPES)]
169+
if not allowed_items:
170+
continue
171+
new_message.items.clear()
172+
new_message.items.extend(allowed_items)
173+
filtered_history.append(new_message)
174+
self.messages.extend(filtered_history)
149175

150176
@override
151177
async def get_history( # type: ignore

python/tests/unit/agents/test_chat_history_channel.py

+46
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
from semantic_kernel.agents.channels.chat_history_channel import ChatHistoryAgentProtocol, ChatHistoryChannel
99
from semantic_kernel.contents.chat_message_content import ChatMessageContent
10+
from semantic_kernel.contents.file_reference_content import FileReferenceContent
1011
from semantic_kernel.contents.function_result_content import FunctionResultContent
12+
from semantic_kernel.contents.streaming_file_reference_content import StreamingFileReferenceContent
1113
from semantic_kernel.contents.utils.author_role import AuthorRole
1214
from semantic_kernel.exceptions import ServiceInvalidTypeError
1315

@@ -200,3 +202,47 @@ async def test_reset_history():
200202
await channel.reset()
201203

202204
assert len(channel.messages) == 0
205+
206+
207+
async def test_receive_skips_file_references():
208+
channel = ChatHistoryChannel()
209+
210+
file_ref_item = FileReferenceContent()
211+
streaming_file_ref_item = StreamingFileReferenceContent()
212+
normal_item_1 = FunctionResultContent(id="test_id", result="normal content 1")
213+
normal_item_2 = FunctionResultContent(id="test_id_2", result="normal content 2")
214+
215+
msg_with_file_only = ChatMessageContent(
216+
role=AuthorRole.USER,
217+
content="Normal message set as TextContent",
218+
items=[file_ref_item],
219+
)
220+
221+
msg_with_mixed = ChatMessageContent(
222+
role=AuthorRole.USER,
223+
content="Mixed content message",
224+
items=[streaming_file_ref_item, normal_item_1],
225+
)
226+
227+
msg_with_normal = ChatMessageContent(
228+
role=AuthorRole.USER,
229+
content="Normal message",
230+
items=[normal_item_2],
231+
)
232+
233+
history = [msg_with_file_only, msg_with_mixed, msg_with_normal]
234+
await channel.receive(history)
235+
236+
assert len(channel.messages) == 3
237+
238+
assert channel.messages[0].content == "Normal message set as TextContent"
239+
assert len(channel.messages[0].items) == 1
240+
241+
assert channel.messages[1].content == "Mixed content message"
242+
assert len(channel.messages[0].items) == 1
243+
assert channel.messages[1].items[0].result == "normal content 1"
244+
245+
assert channel.messages[2].content == "Normal message"
246+
assert len(channel.messages[2].items) == 2
247+
assert channel.messages[2].items[0].result == "normal content 2"
248+
assert channel.messages[2].items[1].text == "Normal message"

0 commit comments

Comments
 (0)