-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.py
98 lines (84 loc) · 3.23 KB
/
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
88
89
90
91
92
93
94
95
96
97
98
from dotenv import load_dotenv
import os
from os.path import dirname
import sqlite3
import semantic_kernel as sk
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion
# Load environment variables from .env file
load_dotenv()
deployment_name = os.environ["AZURE_OPENAI_DEPLOYMENT_NAME"]
endpoint = os.environ["AZURE_OPENAI_ENDPOINT"]
api_key = os.environ["AZURE_OPENAI_API_KEY"]
# Database setup
db_path = os.path.join('conversation_history.db')
conn = sqlite3.connect(db_path)
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS conversation_history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
role TEXT NOT NULL,
content TEXT NOT NULL
)
''')
conn.commit()
# Function to save conversation to the database
def save_to_db(role, content):
c.execute('INSERT INTO conversation_history (role, content) VALUES (?, ?)', (role, content))
conn.commit()
async def main():
kernel = sk.Kernel()
kernel.add_chat_service("dv", AzureChatCompletion(
deployment_name=deployment_name,
endpoint=endpoint,
api_key=api_key)
)
# Define multiple conversation flows
# Flow 1: Espresso Variables
espresso_prompt = kernel.create_semantic_function("""
I need to understand what are the variables involved in making outstanding espresso besides
a good machine. For example what is the combination of roast, grind, tamp, and water temperature.
Include 3 practical steps to practice and improve each variable.
""")
# Flow 2: Coffee Beans
coffee_beans_prompt = kernel.create_semantic_function("""
Explain the differences between Arabica and Robusta coffee beans, including their flavor profiles,
growing conditions, and common uses.
""")
# Flow 3: Brewing Methods
brewing_methods_prompt = kernel.create_semantic_function("""
Describe different methods of brewing coffee, such as French press, pour-over, and espresso.
Explain the unique characteristics of each method and provide tips for achieving the best results.
""")
# Function to run a specific conversation flow
def run_conversation_flow(prompt_function):
response = prompt_function()
return response.result
# User selection of conversation flow
print("Choose a conversation flow:")
print("1. Espresso Variables")
print("2. Coffee Beans")
print("3. Brewing Methods")
choice = input("Enter the number of your choice: ")
if choice == '1':
save_to_db('user', 'Selected: Espresso Variables')
response = run_conversation_flow(espresso_prompt)
print(response)
save_to_db('assistant', response)
elif choice == '2':
save_to_db('user', 'Selected: Coffee Beans')
response = run_conversation_flow(coffee_beans_prompt)
print(response)
save_to_db('assistant', response)
elif choice == '3':
save_to_db('user', 'Selected: Brewing Methods')
response = run_conversation_flow(brewing_methods_prompt)
print(response)
save_to_db('assistant', response)
else:
print("Invalid choice. Exiting.")
save_to_db('user', 'Selected: Invalid choice')
# Run the main function
if __name__ == "__main__":
import asyncio
asyncio.run(main())
conn.close()