Repository navigation
Expand file tree
/
Copy pathfunction_app.py
More file actions
139 lines (132 loc) · 5.46 KB
/
Copy pathfunction_app.py
File metadata and controls
139 lines (132 loc) · 5.46 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
import azure.functions as func
import logging
from openai import AzureOpenAI
import json
app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
# Hidden variables that contain API Keys
@app.route(route="http_trigger")
def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
messages = req.params.get('messages')
if not messages:
try:
req_body = req.get_json()
except ValueError:
pass
else:
messages = req_body.get('messages')
client = AzureOpenAI(
azure_endpoint = endpoint,
api_key = subscription_key,
api_version = "2024-05-01-preview",
)
completion = client.chat.completions.create(
model=deployment,
messages= messages,
# past_messages=10,
max_tokens=800,
temperature=0,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
stop=None,
stream=False
,
extra_body={
"data_sources": [{
"type": "azure_search",
"parameters": {
"endpoint": f"{search_endpoint}",
"index_name": "newrhsaiindex",
"semantic_configuration": "default",
"query_type": "vector_semantic_hybrid",
"fields_mapping": {},
"in_scope": True,
"role_information": "You are an AI chatbot that lives on the Rocklin High School website, more commonly referred to as \"RHS\". Answer questions people have! When people refer to \"you\", they're usually referring to Rocklin High School",
"filter": None,
"strictness": 3,
"top_n_documents": 5,
"authentication": {
"type": "api_key",
"key": f"{search_key}"
},
"embedding_dependency": {
"type": "deployment_name",
"deployment_name": "text-embedding-ada-002"
}
}
}]
},
response_format={ "type": "json_object" }
)
response = json.loads(completion.to_json())
return func.HttpResponse(
json.dumps(response),
status_code=200,
mimetype="application/json"
)
if messages:
messages = req.params.get('messages')
client = AzureOpenAI(
azure_endpoint = endpoint,
api_key = subscription_key,
api_version = "2024-05-01-preview",
)
completion = client.chat.completions.create(
model=deployment,
messages= [
{
"role": "system",
"content": "You are an AI chatbot that lives on the Rocklin High School website, more commonly referred to as \"RHS\". Answer questions people have! When people refer to \"you\", they're usually referring to Rocklin High School"
},
{
"role": "user",
"content": f"{messages}"
}
],
# past_messages=10,
max_tokens=800,
temperature=0,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
stop=None,
stream=False
,
extra_body={
"data_sources": [{
"type": "azure_search",
"parameters": {
"endpoint": f"{search_endpoint}",
"index_name": "newrhsaiindex",
"semantic_configuration": "default",
"query_type": "vector_semantic_hybrid",
"fields_mapping": {},
"in_scope": True,
"role_information": "You are an AI chatbot that lives on the Rocklin High School website, more commonly referred to as \"RHS\". Answer questions people have! When people refer to \"you\", they're usually referring to Rocklin High School",
"filter": None,
"strictness": 3,
"top_n_documents": 5,
"authentication": {
"type": "api_key",
"key": f"{search_key}"
},
"embedding_dependency": {
"type": "deployment_name",
"deployment_name": "text-embedding-ada-002"
}
}
}]
},
response_format={ "type": "json_object" }
)
response = json.loads(completion.to_json())["choices"][0]["message"]["content"]
return func.HttpResponse(
f"{response}",
status_code=200
)
else:
return func.HttpResponse(
"This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
status_code=200
)