-
Notifications
You must be signed in to change notification settings - Fork 273
Expand file tree
/
Copy pathutils.py
More file actions
204 lines (174 loc) · 6.64 KB
/
Copy pathutils.py
File metadata and controls
204 lines (174 loc) · 6.64 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
import dataclasses
import json
import logging
import os
import requests
AZURE_SEARCH_PERMITTED_GROUPS_COLUMN = os.environ.get(
"AZURE_SEARCH_PERMITTED_GROUPS_COLUMN"
)
class JSONEncoder(json.JSONEncoder):
def default(self, o):
if dataclasses.is_dataclass(o):
return dataclasses.asdict(o)
return super().default(o)
async def format_as_ndjson(r):
try:
async for event in r:
yield json.dumps(event, cls=JSONEncoder) + "\n"
except Exception as error:
logging.exception("Exception while generating response stream: %s", error)
yield json.dumps({"error": str(error)})
def parse_multi_columns(columns: str) -> list:
if "|" in columns:
return columns.split("|")
else:
return columns.split(",")
def fetchUserGroups(userToken, nextLink=None):
# Recursively fetch group membership
if nextLink:
endpoint = nextLink
else:
endpoint = "https://graph.microsoft.com/v1.0/me/transitiveMemberOf?$select=id"
headers = {"Authorization": "bearer " + userToken}
try:
r = requests.get(endpoint, headers=headers)
if r.status_code != 200:
logging.error(f"Error fetching user groups: {r.status_code} {r.text}")
return []
r = r.json()
if "@odata.nextLink" in r:
nextLinkData = fetchUserGroups(userToken, r["@odata.nextLink"])
r["value"].extend(nextLinkData)
return r["value"]
except Exception as e:
logging.error(f"Exception in fetchUserGroups: {e}")
return []
def generateFilterString(userToken):
# Get list of groups user is a member of
userGroups = fetchUserGroups(userToken)
# Construct filter string
if not userGroups:
logging.debug("No user groups found")
group_ids = ", ".join([obj["id"] for obj in userGroups])
return f"{AZURE_SEARCH_PERMITTED_GROUPS_COLUMN}/any(g:search.in(g, '{group_ids}'))"
def format_non_streaming_response(chatCompletion, history_metadata, apim_request_id):
response_obj = {
"id": chatCompletion.id,
"model": chatCompletion.model,
"created": chatCompletion.created,
"object": chatCompletion.object,
"choices": [{"messages": []}],
"history_metadata": history_metadata,
"apim-request-id": apim_request_id,
}
if len(chatCompletion.choices) > 0:
message = chatCompletion.choices[0].message
if message:
if hasattr(message, "context"):
response_obj["choices"][0]["messages"].append(
{
"role": "tool",
"content": json.dumps(message.context),
}
)
response_obj["choices"][0]["messages"].append(
{
"role": "assistant",
"content": message.content,
}
)
return response_obj
return {}
def format_stream_response(chatCompletionChunk, history_metadata, apim_request_id):
response_obj = {
"id": chatCompletionChunk.id,
"model": chatCompletionChunk.model,
"created": chatCompletionChunk.created,
"object": chatCompletionChunk.object,
"choices": [{"messages": []}],
"history_metadata": history_metadata,
"apim-request-id": apim_request_id,
}
if len(chatCompletionChunk.choices) > 0:
delta = chatCompletionChunk.choices[0].delta
if delta:
if hasattr(delta, "context"):
messageObj = {"role": "tool", "content": json.dumps(delta.context)}
response_obj["choices"][0]["messages"].append(messageObj)
return response_obj
if delta.role == "assistant" and hasattr(delta, "context"):
messageObj = {
"role": "assistant",
"context": delta.context,
}
response_obj["choices"][0]["messages"].append(messageObj)
return response_obj
else:
if delta.content:
messageObj = {
"role": "assistant",
"content": delta.content,
}
response_obj["choices"][0]["messages"].append(messageObj)
return response_obj
return {}
def format_pf_non_streaming_response(
chatCompletion,
history_metadata,
response_field_name,
citations_field_name,
message_uuid=None,
):
if chatCompletion is None:
logging.error(
"chatCompletion object is None - Increase PROMPTFLOW_RESPONSE_TIMEOUT parameter"
)
return {
"error": "No response received from promptflow endpoint increase PROMPTFLOW_RESPONSE_TIMEOUT parameter or check the promptflow endpoint."
}
if "error" in chatCompletion:
logging.error(f"Error in promptflow response api: {chatCompletion['error']}")
return {"error": chatCompletion["error"]}
logging.debug(f"chatCompletion: {chatCompletion}")
try:
messages = []
if response_field_name in chatCompletion:
messages.append(
{"role": "assistant", "content": chatCompletion[response_field_name]}
)
if citations_field_name in chatCompletion:
messages.append(
{"role": "tool", "content": chatCompletion[citations_field_name]}
)
response_obj = {
"id": chatCompletion["id"],
"model": "",
"created": "",
"object": "",
"choices": [
{
"messages": messages,
"history_metadata": history_metadata,
}
],
}
return response_obj
except Exception as e:
logging.error(f"Exception in format_pf_non_streaming_response: {e}")
return {}
def convert_to_pf_format(input_json, request_field_name, response_field_name):
output_json = []
logging.debug(f"Input json: {input_json}")
# align the input json to the format expected by promptflow chat flow
for message in input_json["messages"]:
if message:
if message["role"] == "user":
new_obj = {
"inputs": {request_field_name: message["content"]},
"outputs": {response_field_name: ""},
}
output_json.append(new_obj)
elif message["role"] == "assistant" and len(output_json) > 0:
output_json[-1]["outputs"][response_field_name] = message["content"]
logging.debug(f"PF formatted response: {output_json}")
return output_json