Skip to content

Commit eab811b

Browse files
committed
Merge branch 'main' into flask-cmd
2 parents 84e2382 + 46e75e1 commit eab811b

File tree

13 files changed

+72
-53
lines changed

13 files changed

+72
-53
lines changed

README.md

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,15 @@ The repo includes sample data so it's ready to try end to end. In this sample ap
2727
### Prerequisites
2828

2929
#### To Run Locally
30-
- [Azure Developer CLI](https://aka.ms/azure-dev/install)
31-
- [Python 3+](https://www.python.org/downloads/)
32-
- **Important**: Python and the pip package manager must be in the path in Windows for the setup scripts to work.
33-
- **Important**: Ensure you can run `python --version` from console. On Ubuntu, you might need to run `sudo apt install python-is-python3` to link `python` to `python3`.
34-
- [Node.js](https://nodejs.org/en/download/)
35-
- [Git](https://git-scm.com/downloads)
36-
- [Powershell 7+ (pwsh)](https://github.com/powershell/powershell) - For Windows users only.
37-
- **Important**: Ensure you can run `pwsh.exe` from a PowerShell command. If this fails, you likely need to upgrade PowerShell.
30+
31+
* [Azure Developer CLI](https://aka.ms/azure-dev/install)
32+
* [Python 3+](https://www.python.org/downloads/)
33+
* **Important**: Python and the pip package manager must be in the path in Windows for the setup scripts to work.
34+
* **Important**: Ensure you can run `python --version` from console. On Ubuntu, you might need to run `sudo apt install python-is-python3` to link `python` to `python3`.
35+
* [Node.js](https://nodejs.org/en/download/)
36+
* [Git](https://git-scm.com/downloads)
37+
* [Powershell 7+ (pwsh)](https://github.com/powershell/powershell) - For Windows users only.
38+
* **Important**: Ensure you can run `pwsh.exe` from a PowerShell command. If this fails, you likely need to upgrade PowerShell.
3839

3940
>NOTE: Your Azure Account must have `Microsoft.Authorization/roleAssignments/write` permissions, such as [User Access Administrator](https://learn.microsoft.com/azure/role-based-access-control/built-in-roles#user-access-administrator) or [Owner](https://learn.microsoft.com/azure/role-based-access-control/built-in-roles#owner).
4041
@@ -54,21 +55,21 @@ You can run this repo virtually by using GitHub Codespaces or VS Code Remote Con
5455
1. Run `azd init -t azure-search-openai-demo`
5556
* note that this command will initialize a git repository and you do not need to clone this repository
5657

57-
#### Starting from scratch:
58+
#### Starting from scratch
5859

5960
Execute the following command, if you don't have any pre-existing Azure services and want to start from a fresh deployment.
6061

6162
1. Run `azd up` - This will provision Azure resources and deploy this sample to those resources, including building the search index based on the files found in the `./data` folder.
62-
* For the target location, the regions that currently support the models used in this sample are **East US** or **South Central US**. For an up-to-date list of regions and models, check [here](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/concepts/models)
63+
* For the target location, the regions that currently support the models used in this sample are **East US**, **France Central**, **South Central US**, **UK South**, and **West Europe**. For an up-to-date list of regions and models, check [here](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/concepts/models)
6364
1. After the application has been successfully deployed you will see a URL printed to the console. Click that URL to interact with the application in your browser.
6465

6566
It will look like the following:
6667

6768
!['Output from running azd up'](assets/endpoint.png)
68-
69+
6970
> NOTE: It may take a minute for the application to be fully deployed. If you see a "Python Developer" welcome screen, then wait a minute and refresh the page.
7071
71-
#### Use existing resources:
72+
#### Use existing resources
7273

7374
1. Run `azd env set AZURE_OPENAI_SERVICE {Name of existing OpenAI service}`
7475
1. Run `azd env set AZURE_OPENAI_RESOURCE_GROUP {Name of existing resource group that OpenAI service is provisioned to}`
@@ -78,10 +79,12 @@ It will look like the following:
7879

7980
> NOTE: You can also use existing Search and Storage Accounts. See `./infra/main.parameters.json` for list of environment variables to pass to `azd env set` to configure those existing resources.
8081
81-
#### Deploying or re-deploying a local clone of the repo:
82+
#### Deploying or re-deploying a local clone of the repo
83+
8284
* Simply run `azd up`
8385

84-
#### Running locally:
86+
#### Running locally
87+
8588
1. Run `azd login`
8689
2. Change dir to `app`
8790
3. Run `./start.ps1` or `./start.sh` or run the "VS Code Task: Start App" to start the project locally.
@@ -101,6 +104,7 @@ Run the following if you want to give someone else access to completely deployed
101104
* Running locally: navigate to 127.0.0.1:5000
102105

103106
Once in the web app:
107+
104108
* Try different topics in chat or Q&A context. For chat, try follow up questions, clarifications, ask to simplify or elaborate on answer, etc.
105109
* Explore citations and sources
106110
* Click on "settings" to try different options, tweak prompts, etc.
@@ -112,6 +116,7 @@ Once in the web app:
112116
* [Azure OpenAI Service](https://learn.microsoft.com/azure/cognitive-services/openai/overview)
113117

114118
### Note
119+
115120
>Note: The PDF documents used in this demo contain information generated using a language model (Azure OpenAI Service). The information contained in these documents is only for demonstration purposes and does not reflect the opinions or beliefs of Microsoft. Microsoft makes no representations or warranties of any kind, express or implied, about the completeness, accuracy, reliability, suitability or availability with respect to the information contained in this document. All rights reserved to Microsoft.
116121
117122
### FAQ
@@ -122,6 +127,6 @@ Once in the web app:
122127

123128
### Troubleshooting
124129

125-
If you see this error while running `azd deploy`: `read /tmp/azd1992237260/backend_env/lib64: is a directory`, then delete the `./app/backend/backend_env folder` and re-run the `azd deploy` command. This issue is being tracked here: https://github.com/Azure/azure-dev/issues/1237
130+
If you see this error while running `azd deploy`: `read /tmp/azd1992237260/backend_env/lib64: is a directory`, then delete the `./app/backend/backend_env folder` and re-run the `azd deploy` command. This issue is being tracked here: <https://github.com/Azure/azure-dev/issues/1237>
126131

127-
If the web app fails to deploy and you receive a '404 Not Found' message in your browser, run 'azd deploy'.
132+
If the web app fails to deploy and you receive a '404 Not Found' message in your browser, run `azd deploy`.

app/backend/app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def static_file(path):
7777
@app.route("/content/<path>")
7878
def content_file(path):
7979
blob = blob_container.get_blob_client(path).download_blob()
80-
if not blob.properties or "content_settings" not in blob.properties:
80+
if not blob.properties or not blob.properties.has_key("content_settings"):
8181
abort(404)
8282
mime_type = blob.properties["content_settings"]["content_type"]
8383
if mime_type == "application/octet-stream":

app/backend/approaches/chatreadretrieveread.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@
66
from approaches.approach import Approach
77
from text import nonewlines
88

9-
# Simple retrieve-then-read implementation, using the Cognitive Search and OpenAI APIs directly. It first retrieves
10-
# top documents from search, then constructs a prompt with them, and then uses OpenAI to generate an completion
11-
# (answer) with that prompt.
129
class ChatReadRetrieveReadApproach(Approach):
10+
"""
11+
Simple retrieve-then-read implementation, using the Cognitive Search and OpenAI APIs directly. It first retrieves
12+
top documents from search, then constructs a prompt with them, and then uses OpenAI to generate an completion
13+
(answer) with that prompt.
14+
"""
15+
1316
prompt_prefix = """<|im_start|>system
1417
Assistant helps the company employees with their healthcare plan questions, and questions about the employee handbook. Be brief in your answers.
1518
Answer ONLY with the facts listed in the list of sources below. If there isn't enough information below, say you don't know. Do not generate answers that don't use the sources below. If asking a clarifying question to the user would help, ask the question.
1619
For tabular information return it as an html table. Do not return markdown format.
17-
Each source has a name followed by colon and the actual information, always include the source name for each fact you use in the response. Use square brakets to reference the source, e.g. [info1.txt]. Don't combine sources, list each source separately, e.g. [info1.txt][info2.pdf].
20+
Each source has a name followed by colon and the actual information, always include the source name for each fact you use in the response. Use square brackets to reference the source, e.g. [info1.txt]. Don't combine sources, list each source separately, e.g. [info1.txt][info2.pdf].
1821
{follow_up_questions_prompt}
1922
{injected_prompt}
2023
Sources:
@@ -112,5 +115,5 @@ def get_chat_history_as_text(self, history: Sequence[dict[str, str]], include_la
112115
for h in reversed(history if include_last_turn else history[:-1]):
113116
history_text = """<|im_start|>user""" + "\n" + h["user"] + "\n" + """<|im_end|>""" + "\n" + """<|im_start|>assistant""" + "\n" + (h.get("bot", "") + """<|im_end|>""" if h.get("bot") else "") + "\n" + history_text
114117
if len(history_text) > approx_max_tokens*4:
115-
break
116-
return history_text
118+
break
119+
return history_text

app/backend/approaches/readretrieveread.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,22 @@
66
from langchain.callbacks.manager import CallbackManager, Callbacks
77
from langchain.chains import LLMChain
88
from langchain.agents import Tool, ZeroShotAgent, AgentExecutor
9-
from langchain.llms.openai import AzureOpenAI
109
from langchainadapters import HtmlCallbackHandler
1110
from text import nonewlines
1211
from lookuptool import CsvLookupTool
1312
from typing import Any
1413

15-
# Attempt to answer questions by iteratively evaluating the question to see what information is missing, and once all information
16-
# is present then formulate an answer. Each iteration consists of two parts: first use GPT to see if we need more information,
17-
# second if more data is needed use the requested "tool" to retrieve it. The last call to GPT answers the actual question.
18-
# This is inspired by the MKRL paper[1] and applied here using the implementation in Langchain.
19-
# [1] E. Karpas, et al. arXiv:2205.00445
2014
class ReadRetrieveReadApproach(Approach):
15+
"""
16+
Attempt to answer questions by iteratively evaluating the question to see what information is missing, and once all information
17+
is present then formulate an answer. Each iteration consists of two parts:
18+
1. use GPT to see if we need more information
19+
2. if more data is needed, use the requested "tool" to retrieve it.
20+
The last call to GPT answers the actual question.
21+
This is inspired by the MKRL paper[1] and applied here using the implementation in Langchain.
22+
23+
[1] E. Karpas, et al. arXiv:2205.00445
24+
"""
2125

2226
template_prefix = \
2327
"You are an intelligent assistant helping Contoso Inc employees with their healthcare plan questions and employee handbook questions. " \

app/backend/approaches/retrievethenread.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
from text import nonewlines
66
from typing import Any
77

8-
# Simple retrieve-then-read implementation, using the Cognitive Search and OpenAI APIs directly. It first retrieves
9-
# top documents from search, then constructs a prompt with them, and then uses OpenAI to generate an completion
10-
# (answer) with that prompt.
8+
119
class RetrieveThenReadApproach(Approach):
10+
"""
11+
Simple retrieve-then-read implementation, using the Cognitive Search and OpenAI APIs directly. It first retrieves
12+
top documents from search, then constructs a prompt with them, and then uses OpenAI to generate an completion
13+
(answer) with that prompt.
14+
"""
1215

1316
template = \
1417
"You are an intelligent assistant helping Contoso Inc employees with their healthcare plan questions and employee handbook questions. " + \

app/backend/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
azure-identity==1.13.0b3
1+
azure-identity==1.13.0
22
Flask==2.2.5
33
langchain==0.0.187
44
openai==0.26.4

app/start.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ if [ $? -ne 0 ]; then
1717
fi
1818

1919
echo 'Creating python virtual environment "backend/backend_env"'
20-
python -m venv backend/backend_env
20+
python3 -m venv backend/backend_env
2121

2222
echo ""
2323
echo "Restoring backend python packages"

infra/core/ai/cognitiveservices.bicep

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ resource deployment 'Microsoft.CognitiveServices/accounts/deployments@2023-05-01
3030
model: deployment.model
3131
raiPolicyName: contains(deployment, 'raiPolicyName') ? deployment.raiPolicyName : null
3232
}
33-
sku: {
33+
sku: contains(deployment, 'sku') ? deployment.sku : {
3434
name: 'Standard'
35-
capacity: deployment.capacity
35+
capacity: 20
3636
}
3737
}]
3838

infra/main.bicep

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ param formRecognizerResourceGroupLocation string = location
3737

3838
param formRecognizerSkuName string = 'S0'
3939

40-
param gptDeploymentName string = ''
40+
param gptDeploymentName string // Set in main.parameters.json
4141
param gptDeploymentCapacity int = 30
4242
param gptModelName string = 'text-davinci-003'
43-
param chatGptDeploymentName string = ''
43+
param chatGptDeploymentName string // Set in main.parameters.json
4444
param chatGptDeploymentCapacity int = 30
4545
param chatGptModelName string = 'gpt-35-turbo'
4646

@@ -50,8 +50,6 @@ param principalId string = ''
5050
var abbrs = loadJsonContent('abbreviations.json')
5151
var resourceToken = toLower(uniqueString(subscription().id, environmentName, location))
5252
var tags = { 'azd-env-name': environmentName }
53-
var gptDeployment = empty(gptDeploymentName) ? 'davinci' : gptDeploymentName
54-
var chatGptDeployment = empty(chatGptDeploymentName) ? 'chat' : chatGptDeploymentName
5553

5654
// Organize resources in a resource group
5755
resource resourceGroup 'Microsoft.Resources/resourceGroups@2021-04-01' = {
@@ -111,8 +109,8 @@ module backend 'core/host/appservice.bicep' = {
111109
AZURE_OPENAI_SERVICE: openAi.outputs.name
112110
AZURE_SEARCH_INDEX: searchIndexName
113111
AZURE_SEARCH_SERVICE: searchService.outputs.name
114-
AZURE_OPENAI_GPT_DEPLOYMENT: gptDeployment
115-
AZURE_OPENAI_CHATGPT_DEPLOYMENT: chatGptDeployment
112+
AZURE_OPENAI_GPT_DEPLOYMENT: gptDeploymentName
113+
AZURE_OPENAI_CHATGPT_DEPLOYMENT: chatGptDeploymentName
116114
}
117115
}
118116
}
@@ -129,22 +127,28 @@ module openAi 'core/ai/cognitiveservices.bicep' = {
129127
}
130128
deployments: [
131129
{
132-
name: gptDeployment
130+
name: gptDeploymentName
133131
model: {
134132
format: 'OpenAI'
135133
name: gptModelName
136134
version: '1'
137135
}
138-
capacity: gptDeploymentCapacity
136+
sku: {
137+
name: 'Standard'
138+
capacity: gptDeploymentCapacity
139+
}
139140
}
140141
{
141-
name: chatGptDeployment
142+
name: chatGptDeploymentName
142143
model: {
143144
format: 'OpenAI'
144145
name: chatGptModelName
145146
version: '0301'
146147
}
147-
capacity: chatGptDeploymentCapacity
148+
sku: {
149+
name: 'Standard'
150+
capacity: chatGptDeploymentCapacity
151+
}
148152
}
149153
]
150154
}
@@ -315,8 +319,8 @@ output AZURE_RESOURCE_GROUP string = resourceGroup.name
315319

316320
output AZURE_OPENAI_SERVICE string = openAi.outputs.name
317321
output AZURE_OPENAI_RESOURCE_GROUP string = openAiResourceGroup.name
318-
output AZURE_OPENAI_GPT_DEPLOYMENT string = gptDeployment
319-
output AZURE_OPENAI_CHATGPT_DEPLOYMENT string = chatGptDeployment
322+
output AZURE_OPENAI_GPT_DEPLOYMENT string = gptDeploymentName
323+
output AZURE_OPENAI_CHATGPT_DEPLOYMENT string = chatGptDeploymentName
320324

321325
output AZURE_FORMRECOGNIZER_SERVICE string = formRecognizer.outputs.name
322326
output AZURE_FORMRECOGNIZER_RESOURCE_GROUP string = formRecognizerResourceGroup.name

infra/main.parameters.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@
4545
"value": "${AZURE_STORAGE_RESOURCE_GROUP}"
4646
},
4747
"chatGptDeploymentName": {
48-
"value": "${AZURE_OPENAI_CHATGPT_DEPLOYMENT}"
48+
"value": "${AZURE_OPENAI_CHATGPT_DEPLOYMENT=chat}"
4949
},
5050
"gptDeploymentName": {
51-
"value": "${AZURE_OPENAI_GPT_DEPLOYMENT}"
51+
"value": "${AZURE_OPENAI_GPT_DEPLOYMENT=davinci}"
5252
}
5353
}
5454
}

0 commit comments

Comments
 (0)