-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
130 lines (118 loc) · 4.4 KB
/
main.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
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
import os
from openai import AzureOpenAI
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
from dotenv import load_dotenv
from payment.process import send_information_order
import json
# Load environment variables
load_dotenv()
# Azure OpenAI configuration
endpoint = os.environ["AZURE_OPENAI_ENDPOINT"]
api_key = os.environ["OPENAI_API_KEY"]
deployment = os.environ["CHAT_COMPLETIONS_DEPLOYMENT_NAME"]
token_provider = get_bearer_token_provider(DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default")
# Initialize client
client = AzureOpenAI(
azure_endpoint=endpoint,
api_key=api_key,
api_version="2024-02-01",
)
PROMPT_FILE_NAME = os.environ["PROMPT_FILE_NAME"]
# Read system prompt from file
with open('prompt/' + PROMPT_FILE_NAME, "r") as file:
system_prompt = file.read()
# Initialize message history with system prompt
message_history = [
{"role": "system", "content": system_prompt},
]
tools = [
{
"type": "function",
"function": {
"name": "send_information_order",
"description": "Report Purchase send to email",
"parameters": {
"type": "object",
"properties": {
"total_price": {
"type": "integer",
"description": "total price of pizza order",
},
"product_list": {
"type": "array",
"items": {
"type": "string",
},
"quantity": {
"type": "integer",
},
"description": "items that customer already choose to order",
},
"email": {
"type": "string",
"description": "email customer so report will be send here",
}
},
"required": ["total_price", "product_list", "email"],
}
}
}
]
available_functions = {
"send_information_order": send_information_order
}
def get_user_input():
return input("User: ")
def get_response_from_openai(messages, available_functions):
print(messages)
response = client.chat.completions.create(
model=deployment,
messages=messages,
tools=tools,
tool_choice="auto"
)
response_message = response.choices[0].message
response_tool_calls = response_message.tool_calls
if response_tool_calls == None:
return response_message.content
print("appending messages")
messages.append(response_message)
for tool_call in response_tool_calls:
# Step 3: call the function
# Note: the JSON response may not always be valid; be sure to handle errors
function_name = tool_call.function.name
# verify function exists
if function_name not in available_functions:
return "Function " + function_name + " does not exist"
function_to_call = available_functions[function_name]
function_args = json.loads(tool_call.function.arguments)
function_response = function_to_call(**function_args)
# verify function has correct number of arguments
print("Output of function call:")
print(function_response)
print()
messages.append(
{
"tool_call_id": tool_call.id,
"role": "tool",
"name": function_name,
"content": function_response,
}
) # extend conversation with function response
second_response = client.chat.completions.create(
model=deployment,
messages=messages,
) # get a new response from the model where it can see the function response
return second_response.choices[0].message.content
return
# Main loop
while True:
user_message = get_user_input()
message_history.append({"role": "user", "content": user_message})
response = get_response_from_openai(message_history, available_functions)
print(f"Assistant: {response}")
message_history.append({"role": "assistant", "content": response})
# Optional: Save history to a file for persistence
with open("chat_history.txt", "a") as file:
file.write(f"User: {user_message}\n")
file.write(f"Assistant: {response}\n")