Skip to content

Commit 626b69d

Browse files
committed
Obtains list of URLs the model believes it should read as background material.
1 parent 35b1754 commit 626b69d

File tree

1 file changed

+48
-28
lines changed

1 file changed

+48
-28
lines changed

app.py

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ def prepare_model_args(request_body, request_headers, system_preamble = None, sy
193193
messages = []
194194
system_message = system_prompt if system_prompt is not None else (system_preamble + "\n\n" if system_preamble is not None else "") + app_settings.azure_openai.system_message
195195

196+
#print(f"System message in: {system_message}")
197+
196198
if not app_settings.datasource:
197199
messages = [
198200
{
@@ -428,47 +430,65 @@ async def search_bing(search):
428430
print(f"Error: {e}")
429431
return None
430432

431-
async def send_private_chat(bg_request_body, request_headers, system_preamble = None, system_message = None):
432-
result = await stream_chat_request(bg_request_body, request_headers, system_preamble, None)
433+
async def send_private_chat(request_body, request_headers, system_preamble = None, system_message = None):
434+
bg_request_body = copy.deepcopy(request_body)
435+
bg_request_body["history_metadata"] = None
436+
result = await stream_chat_request(bg_request_body, request_headers, system_preamble, system_message)
433437
response = await make_response(format_as_ndjson(result))
434438
response.timeout = None
435439
response.mimetype = "application/json-lines"
436440
response_raw = await response.get_data()
437-
combined_json = process_raw_response(response_raw)
438-
return json.loads(combined_json["messages"][0]["content"])
441+
combined_json = process_raw_response(response_raw)
442+
return combined_json["messages"][0]["content"]
439443

440444
async def get_search_results(searches):
441445
allresults = None
446+
# Get the top 3 results from each search
442447
for search in searches:
443448
results = await search_bing(search);
444-
if allresults == None:
445-
allresults = results
449+
if results == None:
450+
return "Search error."
446451
else:
447-
allresults += results
452+
if allresults == None:
453+
allresults = results[:3]
454+
else:
455+
allresults += results[:3]
448456
# Remove extraneous fields
449-
proparray = ["isNavigational", "isFamilyFriendly", "displayUrl", "searchTags", "noCache", "cachedPageUrl", "datePublishedDisplayText", "datePublished", "id", "primaryImageOfPage", "thumbnailUrl"]
450-
for obj in allresults:
451-
for prop in proparray:
452-
if prop in obj:
453-
del obj[prop]
454-
return allresults
457+
proparray = ["richFacts", "isNavigational", "isFamilyFriendly", "displayUrl", "searchTags", "noCache", "cachedPageUrl", "datePublishedDisplayText", "datePublished", "id", "primaryImageOfPage", "thumbnailUrl"]
458+
for obj in allresults:
459+
for prop in proparray:
460+
if prop in obj:
461+
del obj[prop]
462+
return allresults
463+
464+
async def identify_searches(request_body, request_headers):
465+
system_preamble = "The Original System Prompt that follows is your primary objective, but right now for this chat, you just need to provide a simple list of a few searches you need me to perform in order to fully research and document your suggestion for the feedback and URL provided, as specified in the Original System Prompt. If you can answer with full confidence without any searches, then reply with simply 'No searches required.'. Otherwise, send a comma delimited array of searches with one or several searches you would like me to perform for you to give you all the background data and links you need. Do nothing else but provide the array of search strings or the 'No searches required.' message.\nOriginal System Prompt:\n"
466+
searches = await send_private_chat(request_body, request_headers, system_preamble)
467+
if isinstance(searches, str):
468+
if searches == "No searches required.":
469+
return ""
470+
else:
471+
if searches[0] != "[":
472+
searches = "[" + searches
473+
if searches[-1] != "]":
474+
searches = searches + "]"
475+
searches = json.loads(searches)
476+
return searches
477+
478+
async def get_urls_to_browse(request_body, request_headers, searches):
479+
searchresults = await get_search_results(searches)
480+
if searchresults == "Search error.":
481+
return "Search error."
482+
else:
483+
strsearchresults = json.dumps(searchresults, indent=4)
484+
system_prompt = "You are tasked with helping content developers resolve customer feedback on their content on learn.microsoft.com. Right now, you've searched and identified the following list of potential URLs for further research. Return nothing except an array of strings, with each string being a URL we should browse to research further, so we can fully address the feedback and document our sources. Here is the list of possible sites we can browse:\n\n" + strsearchresults
485+
URLsToBrowse = await send_private_chat(request_body, request_headers, None, system_prompt)
486+
return URLsToBrowse
455487

456488
async def search_and_add_background_references(request_body, request_headers):
457-
458-
# Prepare background conversation
459-
bg_request_body = copy.deepcopy(request_body)
460-
bg_request_body["history_metadata"] = None
461-
462-
system_preamble = "The Original System Prompt that follows is your primary objective, but right now for this subchat we've created on the back-end, you just need to provide a simple list of searches you will need me to perform in order to fully research and document your suggestion for the feedback and URL provided, as specified in the Original System Prompt. If you can answer with full confidence without any searches, then reply with simply 'No searches required.'. Otherwise, send a comma delimited array of searches with one or several searches you would like me to perform for you to give you all the background data and links you need. Do nothing else but provide the array of search strings or the 'No searches required.' message.\nOriginal System Prompt:\n"
463-
searches = await send_private_chat(bg_request_body, request_headers, system_preamble)
464-
searchresults = await get_search_results(searches)
465-
466-
print(f"Search results: {searchresults}")
467-
468-
#system_preamble = "The Original System Prompt that follows is your primary objective, but right now for this subchat we've created on the back-end, we are searching to find relevant background references for you to answer well. I obtained the following search results. Please identify a list of URLs from the results that you need to browse to research in order to fully answer the question. Then I will provide their details. Return a comma delimited array of strings with the URLs you need to see. Here are the search results:\n\n" + searchresults + "\n\nOriginal System Prompt:\n"
469-
#print(f"system_preamble: {system_preamble}")
470-
#webPagesToBrowse = await send_private_chat(bg_request_body, request_headers, system_preamble)
471-
#print(f"Web pages to browse: {webPagesToBrowse}")
489+
searches = await identify_searches(request_body, request_headers)
490+
URLsToBrowse = await get_urls_to_browse(request_body, request_headers, searches)
491+
print(f"Web pages to browse: {URLsToBrowse}")
472492

473493

474494

0 commit comments

Comments
 (0)