|
1 |
| -# azure-ai-projects-file-search |
| 1 | +# Sample Application using Agents from Azure AI Projects and File Search tool (Python) |
| 2 | + |
| 3 | +This sample includes a simple Python [Quart](https://quart.palletsprojects.com/en/latest/) app that streams responses from Azure AI Agents to an HTML/JS frontend using Server-Sent Events (SSEs). The application is configured to upload two documents under the `files` folder for use with the Azure AI Agents' File Search tool. |
| 4 | + |
| 5 | +The sample is designed for use with [Docker containers](https://www.docker.com/), both for local development and Azure deployment. For Azure deployment to [Azure Container Apps](https://learn.microsoft.com/azure/container-apps/overview), please use this [template](https://github.com/Azure-Samples/openai-chat-app-quickstart) and replace the `src` folder content with this application. |
| 6 | + |
| 7 | +## Application Flow |
| 8 | + |
| 9 | +This application utilizes agents from the azure-ai-projects SDK to interact with the Azure ML agents API. The following sequence diagram describes the interaction between each component in the system. More comprehensive logic related to thread management will be discussed in the next section: |
| 10 | + |
| 11 | +```mermaid |
| 12 | +sequenceDiagram |
| 13 | + participant User |
| 14 | + participant Browser |
| 15 | + participant WebServer |
| 16 | + participant APIServer |
| 17 | +
|
| 18 | + WebServer->>APIServer: upload_file_poll (post file API and poll by get) |
| 19 | + APIServer-->>WebServer: return file |
| 20 | + WebServer->>APIServer: create_vector_store_and_poll (post vector store API and poll by get) |
| 21 | + APIServer-->>WebServer: return vector store |
| 22 | + WebServer->>APIServer: create_agent (post assistant API) |
| 23 | + APIServer-->>WebServer: return agent |
| 24 | +
|
| 25 | + User->>Browser: Open 'http://localhost:50505' |
| 26 | + Browser->>WebServer: /index |
| 27 | + WebServer-->>Browser: return HTML, JavaScript, CSS |
| 28 | +
|
| 29 | + User->>Browser: Type message and hit enter |
| 30 | + Browser->>WebServer: /chat |
| 31 | + WebServer->>APIServer: create_thread (post thread API) |
| 32 | + APIServer-->>WebServer: return thread |
| 33 | +
|
| 34 | + WebServer->>APIServer: create_message (post message API) |
| 35 | + APIServer-->>WebServer: return message |
| 36 | +
|
| 37 | + WebServer->>APIServer: create_stream (post run API) |
| 38 | + APIServer-->>WebServer: return chunk |
| 39 | + WebServer-->>Browser: return chunk (thread_id, agent_id in cookie) |
| 40 | +``` |
| 41 | + |
| 42 | +## Application Users and Thread Management |
| 43 | + |
| 44 | +As a web application, it is designed to serve multiple users on multiple browsers. This application uses cookies to ensure that the same thread is reused for conversations across multiple tabs in the same browser. If the browser is restarted, the old thread will continue to serve the user. However, if the application has a new agent after a server restart or a thread is deleted, a new thread will be created without requiring a browser refresh or signaling to the users. |
| 45 | + |
| 46 | +To achieve this, when users submit a message to the web server, the web server will create an agent, thread, and stream back a reply. The response contains `agent_id` and `thread_id` in cookies. As a result, each subsequent message sent to the web server will also contain these IDs. As long as the same agent is being used in the system and the thread can be retrieved in the cookie, the same thread will be used to serve the users. |
| 47 | + |
| 48 | +## Local Development |
| 49 | + |
| 50 | +1. Run `pip install -r requirements.txt`. |
| 51 | + |
| 52 | +2. Make sure that the `.env` file exists. |
| 53 | + |
| 54 | +3. Store the Azure AI Projects connection string in the `.env` file as `PROJECT_CONNECTION_STRING`. |
| 55 | + |
| 56 | +4. Run `az login`. |
| 57 | + |
| 58 | +5. Start the services with this command: |
| 59 | + |
| 60 | + ```shell |
| 61 | + python -m quart --app src.quartapp run --port 50505 --reload |
| 62 | + ``` |
| 63 | + |
| 64 | +6. Click 'http://localhost:50505' in the browser to run the application. |
| 65 | + |
| 66 | + |
| 67 | +## Example Run |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +## Deployment to Azure |
| 72 | + |
| 73 | +Follow these steps for deployment: |
| 74 | +1. Install [Docker Desktop](https://www.docker.com/products/docker-desktop/). If you opened this inside GitHub Codespaces or a Dev Container in VS Code, installation is not needed. ⚠️ If you're on an Apple M1/M2, you won't be able to run `docker` commands inside a Dev Container; either use Codespaces or do not open the Dev Container. |
| 75 | +2. Integrate this app using [template](https://github.com/Azure-Samples/openai-chat-app-quickstart) and follow the Azure Container App deployment steps there. |
| 76 | +3. Add an entry of environment variable, `PROJECT_CONNECTION_STRING` in `infra\aca.bicep`. |
| 77 | +4. When you visit the Azure Container App, you will see the deployment has a permission error in the `Console Log`. |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +To resolve this permission issue, you need to assign `Contributor` and `Cognitive Services OpenAI User` roles for this `object id` in the `Resource Group` of the `Azure AI Projects`. |
0 commit comments