-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathstep3_chat.py
87 lines (69 loc) · 3.12 KB
/
step3_chat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Copyright (c) Microsoft. All rights reserved.
import asyncio
from semantic_kernel import Kernel
from semantic_kernel.agents import AgentGroupChat, ChatCompletionAgent
from semantic_kernel.agents.strategies import TerminationStrategy
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion
from semantic_kernel.contents import AuthorRole, ChatMessageContent
###################################################################
# The following sample demonstrates how to create a simple, #
# agent group chat that utilizes An Art Director Chat Completion #
# Agent along with a Copy Writer Chat Completion Agent to #
# complete a task. #
###################################################################
def _create_kernel_with_chat_completion(service_id: str) -> Kernel:
kernel = Kernel()
kernel.add_service(AzureChatCompletion(service_id=service_id))
return kernel
class ApprovalTerminationStrategy(TerminationStrategy):
"""A strategy for determining when an agent should terminate."""
async def should_agent_terminate(self, agent, history):
"""Check if the agent should terminate."""
return "approved" in history[-1].content.lower()
async def main():
REVIEWER_NAME = "ArtDirector"
REVIEWER_INSTRUCTIONS = """
You are an art director who has opinions about copywriting born of a love for David Ogilvy.
The goal is to determine if the given copy is acceptable to print.
If so, state that it is approved.
If not, provide insight on how to refine suggested copy without example.
"""
agent_reviewer = ChatCompletionAgent(
service_id="artdirector",
kernel=_create_kernel_with_chat_completion("artdirector"),
name=REVIEWER_NAME,
instructions=REVIEWER_INSTRUCTIONS,
)
COPYWRITER_NAME = "CopyWriter"
COPYWRITER_INSTRUCTIONS = """
You are a copywriter with ten years of experience and are known for brevity and a dry humor.
The goal is to refine and decide on the single best copy as an expert in the field.
Only provide a single proposal per response.
You're laser focused on the goal at hand.
Don't waste time with chit chat.
Consider suggestions when refining an idea.
"""
agent_writer = ChatCompletionAgent(
service_id="copywriter",
kernel=_create_kernel_with_chat_completion("copywriter"),
name=COPYWRITER_NAME,
instructions=COPYWRITER_INSTRUCTIONS,
)
group_chat = AgentGroupChat(
agents=[
agent_writer,
agent_reviewer,
],
termination_strategy=ApprovalTerminationStrategy(
agents=[agent_reviewer],
maximum_iterations=10,
),
)
input = "a slogan for a new line of electric cars."
await group_chat.add_chat_message(ChatMessageContent(role=AuthorRole.USER, content=input))
print(f"# User: '{input}'")
async for content in group_chat.invoke():
print(f"# Agent - {content.name or '*'}: '{content.content}'")
print(f"# IS COMPLETE: {group_chat.is_complete}")
if __name__ == "__main__":
asyncio.run(main())