Repository navigation
Expand file tree
/
Copy pathazure_python_code_interpreter_function_calling.py
More file actions
83 lines (63 loc) · 2.51 KB
/
Copy pathazure_python_code_interpreter_function_calling.py
File metadata and controls
83 lines (63 loc) · 2.51 KB
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
# Copyright (c) Microsoft. All rights reserved.
import asyncio
from azure.identity import AzureCliCredential
from semantic_kernel.connectors.ai.function_choice_behavior import FunctionChoiceBehavior
from semantic_kernel.connectors.ai.open_ai.prompt_execution_settings.azure_chat_prompt_execution_settings import (
AzureChatPromptExecutionSettings,
)
from semantic_kernel.connectors.ai.open_ai.services.azure_chat_completion import AzureChatCompletion
from semantic_kernel.contents.chat_history import ChatHistory
from semantic_kernel.core_plugins.sessions_python_tool.sessions_python_plugin import SessionsPythonTool
from semantic_kernel.core_plugins.time_plugin import TimePlugin
from semantic_kernel.functions.kernel_arguments import KernelArguments
from semantic_kernel.kernel import Kernel
kernel = Kernel()
service_id = "sessions-tool"
credential = AzureCliCredential()
chat_service = AzureChatCompletion(service_id=service_id, credential=credential)
kernel.add_service(chat_service)
sessions_tool = SessionsPythonTool(credential=credential)
kernel.add_plugin(sessions_tool, "SessionsTool")
kernel.add_plugin(TimePlugin(), "Time")
chat_function = kernel.add_function(
prompt="{{$chat_history}}{{$user_input}}",
plugin_name="ChatBot",
function_name="Chat",
)
req_settings = AzureChatPromptExecutionSettings(service_id=service_id, tool_choice="auto")
req_settings.function_choice_behavior = FunctionChoiceBehavior.Auto(filters={"excluded_plugins": ["ChatBot"]})
arguments = KernelArguments(settings=req_settings)
history = ChatHistory()
async def chat() -> bool:
try:
user_input = input("User:> ")
except KeyboardInterrupt:
print("\n\nExiting chat...")
return False
except EOFError:
print("\n\nExiting chat...")
return False
if user_input == "exit":
print("\n\nExiting chat...")
return False
arguments["chat_history"] = history
arguments["user_input"] = user_input
answer = await kernel.invoke(
function=chat_function,
arguments=arguments,
)
print(f"Mosscap:> {answer}")
history.add_user_message(user_input)
history.add_assistant_message(str(answer))
return True
async def main() -> None:
print(
"Welcome to the chat bot!\
\n Type 'exit' to exit.\
\n Try a Python code execution question to see the function calling in action (i.e. what is 1+1?)."
)
chatting = True
while chatting:
chatting = await chat()
if __name__ == "__main__":
asyncio.run(main())