@@ -47,77 +47,70 @@ def prepare_body_headers_with_data(request):
47
47
48
48
body = {
49
49
"messages" : request_messages ,
50
- "temperature" : env_helper .AZURE_OPENAI_TEMPERATURE ,
51
- "max_tokens" : env_helper .AZURE_OPENAI_MAX_TOKENS ,
52
- "top_p" : env_helper .AZURE_OPENAI_TOP_P ,
50
+ "temperature" : float ( env_helper .AZURE_OPENAI_TEMPERATURE ) ,
51
+ "max_tokens" : int ( env_helper .AZURE_OPENAI_MAX_TOKENS ) ,
52
+ "top_p" : float ( env_helper .AZURE_OPENAI_TOP_P ) ,
53
53
"stop" : (
54
54
env_helper .AZURE_OPENAI_STOP_SEQUENCE .split ("|" )
55
55
if env_helper .AZURE_OPENAI_STOP_SEQUENCE
56
56
else None
57
57
),
58
58
"stream" : env_helper .SHOULD_STREAM ,
59
- "dataSources " : [
59
+ "data_sources " : [
60
60
{
61
- "type" : "AzureCognitiveSearch " ,
61
+ "type" : "azure_search " ,
62
62
"parameters" : {
63
+ # authentication is set below
63
64
"endpoint" : env_helper .AZURE_SEARCH_SERVICE ,
64
- "key" : env_helper .AZURE_SEARCH_KEY ,
65
- "indexName" : env_helper .AZURE_SEARCH_INDEX ,
66
- "fieldsMapping" : {
67
- "contentField" : (
65
+ "index_name" : env_helper .AZURE_SEARCH_INDEX ,
66
+ "fields_mapping" : {
67
+ "content_fields" : (
68
68
env_helper .AZURE_SEARCH_CONTENT_COLUMNS .split ("|" )
69
69
if env_helper .AZURE_SEARCH_CONTENT_COLUMNS
70
70
else []
71
71
),
72
- "titleField" : (
73
- env_helper .AZURE_SEARCH_TITLE_COLUMN
74
- if env_helper .AZURE_SEARCH_TITLE_COLUMN
75
- else None
76
- ),
77
- "urlField" : (
78
- env_helper .AZURE_SEARCH_URL_COLUMN
79
- if env_helper .AZURE_SEARCH_URL_COLUMN
80
- else None
81
- ),
82
- "filepathField" : (
83
- env_helper .AZURE_SEARCH_FILENAME_COLUMN
84
- if env_helper .AZURE_SEARCH_FILENAME_COLUMN
85
- else None
72
+ "title_field" : env_helper .AZURE_SEARCH_TITLE_COLUMN or None ,
73
+ "url_field" : env_helper .AZURE_SEARCH_URL_COLUMN or None ,
74
+ "filepath_field" : (
75
+ env_helper .AZURE_SEARCH_FILENAME_COLUMN or None
86
76
),
87
77
},
88
- "inScope " : env_helper .AZURE_SEARCH_ENABLE_IN_DOMAIN ,
89
- "topNDocuments " : env_helper .AZURE_SEARCH_TOP_K ,
90
- "queryType " : (
78
+ "in_scope " : env_helper .AZURE_SEARCH_ENABLE_IN_DOMAIN ,
79
+ "top_n_documents " : env_helper .AZURE_SEARCH_TOP_K ,
80
+ "query_type " : (
91
81
"semantic"
92
82
if env_helper .AZURE_SEARCH_USE_SEMANTIC_SEARCH
93
83
else "simple"
94
84
),
95
- "semanticConfiguration " : (
85
+ "semantic_configuration " : (
96
86
env_helper .AZURE_SEARCH_SEMANTIC_SEARCH_CONFIG
97
87
if env_helper .AZURE_SEARCH_USE_SEMANTIC_SEARCH
98
88
and env_helper .AZURE_SEARCH_SEMANTIC_SEARCH_CONFIG
99
89
else ""
100
90
),
101
- "roleInformation " : env_helper .AZURE_OPENAI_SYSTEM_MESSAGE ,
91
+ "role_information " : env_helper .AZURE_OPENAI_SYSTEM_MESSAGE ,
102
92
},
103
93
}
104
94
],
105
95
}
106
96
107
- chatgpt_url = f"{ env_helper .AZURE_OPENAI_ENDPOINT } openai/deployments/{ env_helper .AZURE_OPENAI_MODEL } "
108
- if env_helper .is_chat_model ():
109
- chatgpt_url += "/chat/completions?api-version=2023-12-01-preview"
110
- else :
111
- chatgpt_url += "/completions?api-version=2023-12-01-preview"
112
-
113
97
headers = {
114
98
"Content-Type" : "application/json" ,
115
- "api-key" : env_helper .AZURE_OPENAI_API_KEY ,
116
- "chatgpt_url" : chatgpt_url ,
117
- "chatgpt_key" : env_helper .AZURE_OPENAI_API_KEY ,
118
99
"x-ms-useragent" : "GitHubSampleWebApp/PublicAPI/1.0.0" ,
119
100
}
120
101
102
+ if env_helper .AZURE_AUTH_TYPE == "rbac" :
103
+ body ["data_sources" ][0 ]["parameters" ]["authentication" ] = {
104
+ "type" : "system_assigned_managed_identity"
105
+ }
106
+ headers ["Authorization" ] = f"Bearer { env_helper .AZURE_TOKEN_PROVIDER ()} "
107
+ else :
108
+ body ["data_sources" ][0 ]["parameters" ]["authentication" ] = {
109
+ "type" : "api_key" ,
110
+ "key" : env_helper .AZURE_SEARCH_KEY ,
111
+ }
112
+ headers ["api-key" ] = env_helper .AZURE_OPENAI_API_KEY
113
+
121
114
return body , headers
122
115
123
116
@@ -128,37 +121,54 @@ def stream_with_data(body, headers, endpoint):
128
121
"model" : "" ,
129
122
"created" : 0 ,
130
123
"object" : "" ,
131
- "choices" : [{"messages" : []}],
124
+ "choices" : [
125
+ {
126
+ "messages" : [
127
+ {
128
+ "content" : "" ,
129
+ "end_turn" : False ,
130
+ "role" : "tool" ,
131
+ },
132
+ {
133
+ "content" : "" ,
134
+ "end_turn" : False ,
135
+ "role" : "assistant" ,
136
+ },
137
+ ]
138
+ }
139
+ ],
132
140
}
133
141
try :
134
142
with s .post (endpoint , json = body , headers = headers , stream = True ) as r :
135
143
for line in r .iter_lines (chunk_size = 10 ):
136
144
if line :
137
- lineJson = json .loads (line .lstrip (b"data:" ).decode ("utf-8" ))
145
+ lineJson = json .loads (line .lstrip (b"data: " ).decode ("utf-8" ))
138
146
if "error" in lineJson :
139
147
yield json .dumps (lineJson , ensure_ascii = False ) + "\n "
148
+ return
149
+
150
+ if lineJson ["choices" ][0 ]["end_turn" ]:
151
+ response ["choices" ][0 ]["messages" ][1 ]["end_turn" ] = True
152
+ yield json .dumps (response , ensure_ascii = False ) + "\n "
153
+ return
154
+
140
155
response ["id" ] = lineJson ["id" ]
141
156
response ["model" ] = lineJson ["model" ]
142
157
response ["created" ] = lineJson ["created" ]
143
158
response ["object" ] = lineJson ["object" ]
144
159
145
- role = lineJson ["choices" ][0 ]["messages" ][0 ]["delta" ].get ("role" )
146
- if role == "tool" :
147
- response ["choices" ][0 ]["messages" ].append (
148
- lineJson ["choices" ][0 ]["messages" ][0 ]["delta" ]
149
- )
150
- elif role == "assistant" :
151
- response ["choices" ][0 ]["messages" ].append (
152
- {"role" : "assistant" , "content" : "" }
160
+ delta = lineJson ["choices" ][0 ]["delta" ]
161
+ role = delta .get ("role" )
162
+
163
+ if role == "assistant" :
164
+ response ["choices" ][0 ]["messages" ][0 ]["content" ] = json .dumps (
165
+ delta ["context" ],
166
+ ensure_ascii = False ,
153
167
)
154
168
else :
155
- deltaText = lineJson ["choices" ][0 ]["messages" ][0 ]["delta" ] [
169
+ response ["choices" ][0 ]["messages" ][1 ]["content" ] += delta [
156
170
"content"
157
171
]
158
- if deltaText != "[DONE]" :
159
- response ["choices" ][0 ]["messages" ][1 ][
160
- "content"
161
- ] += deltaText
162
172
163
173
yield json .dumps (response , ensure_ascii = False ) + "\n "
164
174
except Exception as e :
@@ -167,39 +177,68 @@ def stream_with_data(body, headers, endpoint):
167
177
168
178
def conversation_with_data (request ):
169
179
body , headers = prepare_body_headers_with_data (request )
170
- endpoint = f"{ env_helper .AZURE_OPENAI_ENDPOINT } openai/deployments/{ env_helper .AZURE_OPENAI_MODEL } /extensions/ chat/completions?api-version={ env_helper .AZURE_OPENAI_API_VERSION } "
180
+ endpoint = f"{ env_helper .AZURE_OPENAI_ENDPOINT } openai/deployments/{ env_helper .AZURE_OPENAI_MODEL } /chat/completions?api-version={ env_helper .AZURE_OPENAI_API_VERSION } "
171
181
172
182
if not env_helper .SHOULD_STREAM :
173
183
r = requests .post (endpoint , headers = headers , json = body )
174
184
status_code = r .status_code
175
185
r = r .json ()
176
186
177
- return Response (json .dumps (r , ensure_ascii = False ), status = status_code )
187
+ response = {
188
+ "id" : r ["id" ],
189
+ "model" : r ["model" ],
190
+ "created" : r ["created" ],
191
+ "object" : r ["object" ],
192
+ "choices" : [
193
+ {
194
+ "messages" : [
195
+ {
196
+ "content" : json .dumps (
197
+ r ["choices" ][0 ]["message" ]["context" ],
198
+ ensure_ascii = False ,
199
+ ),
200
+ "end_turn" : False ,
201
+ "role" : "tool" ,
202
+ },
203
+ {
204
+ "content" : r ["choices" ][0 ]["message" ]["content" ],
205
+ "end_turn" : True ,
206
+ "role" : "assistant" ,
207
+ },
208
+ ]
209
+ }
210
+ ],
211
+ }
212
+
213
+ return Response (json .dumps (response , ensure_ascii = False ), status = status_code )
178
214
else :
179
- if request .method == "POST" :
180
- return Response (
181
- stream_with_data (body , headers , endpoint ),
182
- mimetype = "application/json-lines" ,
183
- )
184
- else :
185
- return Response (None , mimetype = "application/json-lines" )
215
+ return Response (
216
+ stream_with_data (body , headers , endpoint ),
217
+ mimetype = "application/json-lines" ,
218
+ )
186
219
187
220
188
221
def stream_without_data (response ):
189
222
responseText = ""
190
223
for line in response :
191
- deltaText = line ["choices" ][0 ]["delta" ].get ("content" )
192
- if deltaText and deltaText != "[DONE]" :
193
- responseText += deltaText
224
+ if not line .choices :
225
+ continue
226
+
227
+ deltaText = line .choices [0 ].delta .content
228
+
229
+ if deltaText is None :
230
+ return
231
+
232
+ responseText += deltaText
194
233
195
234
response_obj = {
196
- "id" : line [ "id" ] ,
197
- "model" : line [ " model" ] ,
198
- "created" : line [ " created" ] ,
199
- "object" : line [ " object" ] ,
235
+ "id" : line . id ,
236
+ "model" : line . model ,
237
+ "created" : line . created ,
238
+ "object" : line . object ,
200
239
"choices" : [{"messages" : [{"role" : "assistant" , "content" : responseText }]}],
201
240
}
202
- yield json .dumps (response_obj ). replace ( " \n " , " \\ n" ) + "\n "
241
+ yield json .dumps (response_obj , ensure_ascii = False ) + "\n "
203
242
204
243
205
244
def conversation_without_data (request ):
@@ -239,7 +278,7 @@ def conversation_without_data(request):
239
278
240
279
if not env_helper .SHOULD_STREAM :
241
280
response_obj = {
242
- "id" : response ,
281
+ "id" : response . id ,
243
282
"model" : response .model ,
244
283
"created" : response .created ,
245
284
"object" : response .object ,
@@ -257,15 +296,12 @@ def conversation_without_data(request):
257
296
258
297
return jsonify (response_obj ), 200
259
298
else :
260
- if request .method == "POST" :
261
- return Response (
262
- stream_without_data (response ), mimetype = "application/json-lines"
263
- )
264
- else :
265
- return Response (None , mimetype = "application/json-lines" )
299
+ return Response (
300
+ stream_without_data (response ), mimetype = "application/json-lines"
301
+ )
266
302
267
303
268
- @app .route ("/api/conversation/azure_byod" , methods = ["GET" , " POST" ])
304
+ @app .route ("/api/conversation/azure_byod" , methods = ["POST" ])
269
305
def conversation_azure_byod ():
270
306
try :
271
307
if env_helper .should_use_data ():
@@ -297,7 +333,7 @@ def get_orchestrator_config():
297
333
return ConfigHelper .get_active_config_or_default ().orchestrator
298
334
299
335
300
- @app .route ("/api/conversation/custom" , methods = ["GET" , " POST" ])
336
+ @app .route ("/api/conversation/custom" , methods = ["POST" ])
301
337
def conversation_custom ():
302
338
message_orchestrator = get_message_orchestrator ()
303
339
0 commit comments