-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Copy pathassistant_content_generation.py
198 lines (162 loc) · 7.28 KB
/
assistant_content_generation.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# Copyright (c) Microsoft. All rights reserved.
from typing import TYPE_CHECKING, Any
from openai import AsyncOpenAI
from openai.types.beta.threads.image_file_content_block import ImageFileContentBlock
from openai.types.beta.threads.text_content_block import TextContentBlock
from semantic_kernel.contents.annotation_content import AnnotationContent
from semantic_kernel.contents.chat_message_content import ChatMessageContent
from semantic_kernel.contents.file_reference_content import FileReferenceContent
from semantic_kernel.contents.function_call_content import FunctionCallContent
from semantic_kernel.contents.function_result_content import FunctionResultContent
from semantic_kernel.contents.image_content import ImageContent
from semantic_kernel.contents.text_content import TextContent
from semantic_kernel.contents.utils.author_role import AuthorRole
from semantic_kernel.exceptions.agent_exceptions import AgentExecutionException
if TYPE_CHECKING:
from openai.resources.beta.threads.messages import Message
from openai.resources.beta.threads.runs.runs import Run
from openai.types.beta.threads.annotation import Annotation
from openai.types.beta.threads.runs.tool_call import ToolCall
###################################################################
# The methods in this file are used with OpenAIAssistantAgent #
# related code. They are used to create chat messages, or #
# generate message content. #
###################################################################
async def create_chat_message(
client: AsyncOpenAI,
thread_id: str,
message: "ChatMessageContent",
allowed_message_roles: list[str] = [AuthorRole.USER, AuthorRole.ASSISTANT],
) -> "Message":
"""Class method to add a chat message, callable from class or instance.
Args:
client: The client to use for creating the message.
thread_id: The thread id.
message: The chat message.
allowed_message_roles: The allowed message roles.
Returns:
Message: The message.
"""
if message.role.value not in allowed_message_roles and message.role != AuthorRole.TOOL:
raise AgentExecutionException(
f"Invalid message role `{message.role.value}`. Allowed roles are {allowed_message_roles}."
)
message_contents: list[dict[str, Any]] = get_message_contents(message=message)
return await client.beta.threads.messages.create(
thread_id=thread_id,
role="assistant" if message.role == AuthorRole.TOOL else message.role.value, # type: ignore
content=message_contents, # type: ignore
)
def get_message_contents(message: "ChatMessageContent") -> list[dict[str, Any]]:
"""Get the message contents.
Args:
message: The message.
"""
contents: list[dict[str, Any]] = []
for content in message.items:
if isinstance(content, TextContent):
contents.append({"type": "text", "text": content.text})
elif isinstance(content, ImageContent) and content.uri:
contents.append(content.to_dict())
elif isinstance(content, FileReferenceContent):
contents.append({
"type": "image_file",
"image_file": {"file_id": content.file_id},
})
elif isinstance(content, FunctionResultContent):
contents.append({"type": "text", "text": content.result})
return contents
def generate_message_content(assistant_name: str, message: "Message") -> ChatMessageContent:
"""Generate message content."""
role = AuthorRole(message.role)
content: ChatMessageContent = ChatMessageContent(role=role, name=assistant_name) # type: ignore
for item_content in message.content:
if item_content.type == "text":
assert isinstance(item_content, TextContentBlock) # nosec
content.items.append(
TextContent(
text=item_content.text.value,
)
)
for annotation in item_content.text.annotations:
content.items.append(generate_annotation_content(annotation))
elif item_content.type == "image_file":
assert isinstance(item_content, ImageFileContentBlock) # nosec
content.items.append(
FileReferenceContent(
file_id=item_content.image_file.file_id,
)
)
return content
def generate_function_call_content(agent_name: str, fccs: list[FunctionCallContent]) -> ChatMessageContent:
"""Generate function call content.
Args:
agent_name: The agent name.
fccs: The function call contents.
Returns:
ChatMessageContent: The chat message content containing the function call content as the items.
"""
return ChatMessageContent(role=AuthorRole.TOOL, name=agent_name, items=fccs) # type: ignore
def generate_function_result_content(
agent_name: str, function_step: FunctionCallContent, tool_call: "ToolCall"
) -> ChatMessageContent:
"""Generate function result content."""
function_call_content: ChatMessageContent = ChatMessageContent(role=AuthorRole.TOOL, name=agent_name) # type: ignore
function_call_content.items.append(
FunctionResultContent(
function_name=function_step.function_name,
plugin_name=function_step.plugin_name,
id=function_step.id,
result=tool_call.function.output, # type: ignore
)
)
return function_call_content
def get_function_call_contents(run: "Run", function_steps: dict[str, FunctionCallContent]) -> list[FunctionCallContent]:
"""Extract function call contents from the run.
Args:
run: The run.
function_steps: The function steps
Returns:
The list of function call contents.
"""
function_call_contents: list[FunctionCallContent] = []
required_action = getattr(run, "required_action", None)
if not required_action or not getattr(required_action, "submit_tool_outputs", False):
return function_call_contents
for tool in required_action.submit_tool_outputs.tool_calls:
fcc = FunctionCallContent(
id=tool.id,
index=getattr(tool, "index", None),
name=tool.function.name,
arguments=tool.function.arguments,
)
function_call_contents.append(fcc)
function_steps[tool.id] = fcc
return function_call_contents
def generate_code_interpreter_content(agent_name: str, code: str) -> "ChatMessageContent":
"""Generate code interpreter content.
Args:
agent_name: The agent name.
code: The code.
Returns:
ChatMessageContent: The chat message content.
"""
return ChatMessageContent(
role=AuthorRole.ASSISTANT,
content=code,
name=agent_name,
metadata={"code": True},
)
def generate_annotation_content(annotation: "Annotation") -> AnnotationContent:
"""Generate annotation content."""
file_id = None
if hasattr(annotation, "file_path"):
file_id = annotation.file_path.file_id
elif hasattr(annotation, "file_citation"):
file_id = annotation.file_citation.file_id
return AnnotationContent(
file_id=file_id,
quote=annotation.text,
start_index=annotation.start_index,
end_index=annotation.end_index,
)