|
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 OpenAI Assistant 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 OpenAI Assistant's 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: create_agent (post assistant API) |
| 19 | + APIServer-->>WebServer: return agent |
| 20 | +
|
| 21 | + User->>Browser: Open 'http://localhost:50505' |
| 22 | + Browser->>WebServer: /index |
| 23 | + WebServer-->>Browser: return HTML, JavaScript, CSS |
| 24 | +
|
| 25 | + User->>Browser: Type message and hit enter |
| 26 | + Browser->>WebServer: /chat |
| 27 | + WebServer->>APIServer: create_thread (post thread API) |
| 28 | + APIServer-->>WebServer: return thread |
| 29 | +
|
| 30 | + WebServer->>APIServer: create_message (post message API) |
| 31 | + APIServer-->>WebServer: return message |
| 32 | +
|
| 33 | + WebServer->>APIServer: create_stream (post run API) |
| 34 | + APIServer-->>WebServer: return chunk |
| 35 | + WebServer-->>Browser: return chunk (thread_id, agent_id in cookie) |
| 36 | +``` |
| 37 | + |
| 38 | +## Application Users and Thread Management |
| 39 | + |
| 40 | +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. |
| 41 | + |
| 42 | +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 cookie, the same thread will be used to serve the users. |
| 43 | + |
| 44 | + |
| 45 | +## Local development with Docker |
| 46 | + |
| 47 | +This sample includes a `docker-compose.yaml` for local development which creates a volume for the app code. That allows you to make changes to the code and see them instantly. |
| 48 | + |
| 49 | +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. |
| 50 | + |
| 51 | +2. Make sure that the `.env` file exists. |
| 52 | + |
| 53 | +3. Store a keys and endpoint information (Azure) for the OpenAI resource in the `.env` file. The key should be stored in the `.env` file as `PROJECT_CONNECTION_STRING`. This is necessary because Docker containers don't have access to your user Azure credentials. |
| 54 | + |
| 55 | +4. Start the services with this command: |
| 56 | + |
| 57 | + ```shell |
| 58 | + docker-compose up --build |
| 59 | + ``` |
| 60 | + |
| 61 | +5. Click 'http://localhost:50505' in the browser to run the application. |
| 62 | + |
| 63 | +## Example run |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | +## Deployment to Azure |
| 68 | + |
| 69 | +As mentioned earlier, please integrate this app using [template](https://github.com/Azure-Samples/openai-chat-app-quickstart) and following the Azure Container App deployment steps there. |
0 commit comments