-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
shared_utils.py
68 lines (51 loc) · 2.85 KB
/
shared_utils.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
# Copyright (c) Microsoft. All rights reserved.
import logging
from semantic_kernel.connectors.ai.function_choice_behavior import FunctionChoiceType
from semantic_kernel.contents.chat_history import ChatHistory
from semantic_kernel.contents.function_result_content import FunctionResultContent
from semantic_kernel.contents.utils.author_role import AuthorRole
from semantic_kernel.exceptions.service_exceptions import ServiceInvalidRequestError
from semantic_kernel.functions.kernel_function_metadata import KernelFunctionMetadata
logger: logging.Logger = logging.getLogger(__name__)
def filter_system_message(chat_history: ChatHistory) -> str | None:
"""Filter the first system message from the chat history.
If there are multiple system messages, raise an error.
If there are no system messages, return None.
"""
if len([message for message in chat_history if message.role == AuthorRole.SYSTEM]) > 1:
raise ServiceInvalidRequestError(
"Multiple system messages in chat history. Only one system message is expected."
)
for message in chat_history:
if message.role == AuthorRole.SYSTEM:
return message.content
return None
FUNCTION_CHOICE_TYPE_TO_GOOGLE_FUNCTION_CALLING_MODE = {
FunctionChoiceType.AUTO: "AUTO",
FunctionChoiceType.NONE: "NONE",
FunctionChoiceType.REQUIRED: "ANY",
}
# The separator used in the fully qualified name of the function instead of the default "-" separator.
# This is required since Gemini doesn't work well with "-" in the function name.
# https://ai.google.dev/gemini-api/docs/function-calling#function_declarations
GEMINI_FUNCTION_NAME_SEPARATOR = "_"
def format_function_result_content_name_to_gemini_function_name(function_result_content: FunctionResultContent) -> str:
"""Format the function result content name to the Gemini function name."""
return (
f"{function_result_content.plugin_name}{GEMINI_FUNCTION_NAME_SEPARATOR}{function_result_content.function_name}"
if function_result_content.plugin_name
else function_result_content.function_name
)
def format_kernel_function_fully_qualified_name_to_gemini_function_name(metadata: KernelFunctionMetadata) -> str:
"""Format the kernel function fully qualified name to the Gemini function name."""
return (
f"{metadata.plugin_name}{GEMINI_FUNCTION_NAME_SEPARATOR}{metadata.name}"
if metadata.plugin_name
else metadata.name
)
def format_gemini_function_name_to_kernel_function_fully_qualified_name(gemini_function_name: str) -> str:
"""Format the Gemini function name to the kernel function fully qualified name."""
if GEMINI_FUNCTION_NAME_SEPARATOR in gemini_function_name:
plugin_name, function_name = gemini_function_name.split(GEMINI_FUNCTION_NAME_SEPARATOR, 1)
return f"{plugin_name}-{function_name}"
return gemini_function_name