-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai(1).py
More file actions
209 lines (160 loc) · 7.44 KB
/
ai(1).py
File metadata and controls
209 lines (160 loc) · 7.44 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
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
199
200
201
202
203
204
205
206
207
208
209
import os
from dotenv import load_dotenv
import utils
# Add OpenAI import. (Add code here)
from openai import AzureOpenAI
def main():
try:
load_dotenv()
utils.initLogFile()
azure_oai_endpoint = os.getenv("AZURE_OAI_ENDPOINT")
azure_oai_key = os.getenv("AZURE_OAI_KEY")
azure_oai_model = os.getenv("AZURE_OAI_MODEL")
azure_oai_version = "2023-12-01-preview"
# Define Azure OpenAI client (function1-3)
client = AzureOpenAI( azure_endpoint=azure_oai_endpoint,
api_key=azure_oai_key,
api_version=azure_oai_version,
)
# Define Azure OpenAI client (function4)
client = AzureOpenAI( base_url=f"{azure_oai_endpoint}/openai/deployments/{azure_oai_model}/extensions",
api_key=azure_oai_key,
api_version=azure_oai_version,
)
function_map = {
"1": function1,
"2": function2,
"3": function3,
"4": function4
}
while True:
print('1: Validate PoC\n' +
'2: Company chatbot\n' +
'3: Developer tasks\n' +
'4: Use company data\n' +
'\'quit\' to exit the program\n')
command = input('Enter a number:')
if command.strip() in function_map:
function_map[command](client, azure_oai_model)
elif command.strip().lower() == 'quit':
print('Exiting program...')
break
else :
print("Invalid input. Please enter number 1, 2, 3, 4, or 5.")
except Exception as ex:
print(ex)
# Task 1: Validate PoC
def function1(aiClient, aiModel):
inputText = utils.getPromptInput("Task 1: Validate PoC", "sample-text.txt")
# Build messages to send to Azure OpenAI model. (Add code here)
messages=[
{"role": "system", "content": "You are a helpful assistant." + inputText}
]
# Define argument list (Add code here)
apiParams = {
"model" : aiModel,
"messages": messages,
}
utils.writeLog("API Parameters:\n", apiParams)
# Call chat completion connection. (Add code here)
# Use the call name and **apiParams to reference our argument list
response = aiClient.chat.completions.create( **apiParams)
utils.writeLog("Response:\n", str(response))
print("Response: " + response.choices[0].message.content + "\n")
return response
# Task 2: Company chatbot
def function2(aiClient, aiModel):
inputText = utils.getPromptInput("Task 2: Company chatbot", "sample-text.txt")
# Build messages to send to Azure OpenAI model. (Add code here)
messages=[
{"role":"system", "content": "You are a helpful assistant.You must reply in English and Spanish, in a casual tone."},
{"role":"user","content":" Where can I find the company phone number?"},
{"role":"assistant","content":" You can find it on the footer of every page on our website. Hope that helps! Thanks for using Contoso, Ltd." },
{"role":"user","content":"Please reply using the following format\n###\nEnglish Reply: ......\n\nSpanish Reply......\n###" + inputText},
]
# Define argument list (Add code here)
apiParams = {
"messages": messages,
"model" : aiModel,
"temperature": 0.5,
"max_tokens": 1000,
}
utils.writeLog("API Parameters:\n", apiParams)
# Call chat completion connection. (Add code here)
# Use the call name and **apiParams to reference our argument list
response = aiClient.chat.completions.create( **apiParams)
utils.writeLog("Response:\n", str(response))
print("Response: " + response.choices[0].message.content + "\nHope that helps! Thanks for using Contoso, Ltd.")
return response
def function3(aiClient, aiModel):
while True:
print('\n1: Add comments to my function\n' +
'2: Write unit tests for my function\n' +
'\"quit\" to exit the program\n')
command = input('Enter a number to select a task:')
if command == '1':
file = open(file="../../legacyCode.py", encoding="utf8").read()
newfile = "../../legacyCode.py"
prompt = "Add comments to the following function. Return all code and comments.\n---\n" + file
break
elif command =='2':
file = open(file="../../fibonacci.py", encoding="utf8").read()
newfile = "../../fibonacci.py"
prompt = "Write five unit tests for the following function.Return all code and comments.\n---\n" + file
break
else :
print("Invalid input. Please try again.")
inputText = utils.getPromptInput("Task 3: Developer tasks", "sample-text.txt")
# Build messages to send to Azure OpenAI model. (Add code here)
messages=[
{"role":"system", "content": "You are a helpful AI assistant that helps programmers write code." + prompt}
]
# Define argument list (Add code here)
apiParams = {
"messages": messages,
"model" : aiModel
}
utils.writeLog("API Parameters:\n", apiParams)
# Call chat completion connection. (Add code here)
# Use the call name and **apiParams to reference our argument list
response = aiClient.chat.completions.create( **apiParams)
results_file = open(file=newfile, mode="w", encoding="utf8")
results_file.write(response.choices[0].message.content)
utils.writeLog("Response:\n", str(response))
print("Response: " + response.choices[0].message.content + "\n")
return response
def function4(aiClient, aiModel):
inputText = utils.getPromptInput("Task 4: Use company data", "sample-text.txt")
azure_search_endpoint = os.getenv("SEARCH_ENDPOINT")
azure_search_key = os.getenv("SEARCH_KEY")
azure_search_index = os.getenv("SEARCH_INDEX")
extension_config = dict(dataSources = [
{
"type": "AzureCognitiveSearch",
"parameters": {
"endpoint":azure_search_endpoint,
"key": azure_search_key,
"indexName": azure_search_index,
}
}]
)
# Build messages to send to Azure OpenAI model. (Add code here)
messages = [
{"role": "system", "content": "You are a helpful travel agent."},
{"role": "user", "content": inputText}
]
# Define connection and argument list (Add code here)
apiParams = {
"messages": messages,
"model" : aiModel,
"extra_body" : extension_config
}
utils.writeLog("API Parameters:\n", apiParams)
# Call chat completion connection. Will be the same as function1 (Add code here)
# Use the call name and **apiParams to reference our argument list
response = aiClient.chat.completions.create( **apiParams)
utils.writeLog("Response:\n", str(response))
print("Response: " + response.choices[0].message.content + "\n")
return
if __name__ == '__main__':
main()