-
Notifications
You must be signed in to change notification settings - Fork 22
/
run.py
81 lines (68 loc) · 2.44 KB
/
run.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
import asyncio
import os
import sys
from semantic_kernel.connectors.search_engine import BingConnector
from semantic_kernel.utils.settings import azure_openai_settings_from_dot_env_as_dict
# to allow the strange structure and the import of the new pieces
sys.path.append(os.getcwd())
from python.src.connectors import (
AzureChatCompletion,
StreamingResultToStdOutHook,
TokenUsageHook,
)
from python.src.kernel import newKernel as Kernel
from python.src.plugins import SemanticFunction, SKPlugin
sys.path.append(os.getcwd() + "/python/samples/03-SimpleRag/Plugins")
from SearchPlugin.search import Search
async def runner():
# create services and chat
search_plugin = SKPlugin.from_class(
"Search",
Search(bing_connector=BingConnector(api_key=os.getenv("BING_API_KEY"))),
)
search_plugin.add_function(
SemanticFunction.from_path(
path=os.getcwd()
+ "/python/samples/03-SimpleRag/plugins/SearchPlugin/GetSearchQuery.prompt.yaml"
)
)
gpt35turbo = AzureChatCompletion(
**azure_openai_settings_from_dot_env_as_dict(include_api_version=True),
)
chat_function = SemanticFunction.from_path(
path=os.getcwd()
+ "/python/samples/03-SimpleRag/plugins/ChatPlugin/GroundedChat.prompt.yaml"
)
# create kernel
kernel = Kernel(
ai_services=[gpt35turbo],
plugins=[search_plugin],
hooks=[StreamingResultToStdOutHook(), TokenUsageHook()],
)
# create chat_history
chat_history = gpt35turbo.create_new_chat()
# loop with input
while True:
user_input = input("User:> ")
if user_input == "exit":
break
chat_history.add_user_message(user_input)
stream = True
# get response
response = await kernel.run_async(
chat_function,
variables={
"persona": "You are a snarky (yet helpful) teenage assistant. Make sure to use hip slang in every response.",
"messages": chat_history,
},
request_settings={"stream": stream},
)
if not stream:
# print response
print(f"Assistant:> {response[chat_function.output_variable_name]}")
print(f"Token usage: {response['token_usage']}")
chat_history.add_assistant_message(response[chat_function.output_variable_name])
def __main__():
asyncio.run(runner())
if __name__ == "__main__":
__main__()