Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Teams enhancement #312

Merged
merged 4 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ AZURE_OPENAI_TOP_P=1.0
AZURE_OPENAI_MAX_TOKENS=1000
AZURE_OPENAI_STOP_SEQUENCE=
AZURE_OPENAI_SYSTEM_MESSAGE=You are an AI assistant that helps people find information.
AZURE_OPENAI_API_VERSION=2023-07-01-preview
AZURE_OPENAI_API_VERSION=2023-12-01-preview
AZURE_OPENAI_STREAM=True
# Backend for processing the documents and application logging in the app
AzureWebJobsStorage=
Expand Down
8 changes: 4 additions & 4 deletions code/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def get_config():
"You are an AI assistant that helps people find information.",
)
AZURE_OPENAI_API_VERSION = os.environ.get(
"AZURE_OPENAI_API_VERSION", "2023-06-01-preview"
"AZURE_OPENAI_API_VERSION", "2023-12-01-preview"
)
AZURE_OPENAI_STREAM = os.environ.get("AZURE_OPENAI_STREAM", "true")
AZURE_OPENAI_MODEL_NAME = os.environ.get(
Expand Down Expand Up @@ -144,9 +144,9 @@ def prepare_body_headers_with_data(request):

chatgpt_url = f"https://{AZURE_OPENAI_RESOURCE}.openai.azure.com/openai/deployments/{AZURE_OPENAI_MODEL}"
if is_chat_model():
chatgpt_url += "/chat/completions?api-version=2023-03-15-preview"
chatgpt_url += "/chat/completions?api-version=2023-12-01-preview"
else:
chatgpt_url += "/completions?api-version=2023-03-15-preview"
chatgpt_url += "/completions?api-version=2023-12-01-preview"

headers = {
"Content-Type": "application/json",
Expand Down Expand Up @@ -243,7 +243,7 @@ def stream_without_data(response):
def conversation_without_data(request):
openai.api_type = "azure"
openai.api_base = f"https://{AZURE_OPENAI_RESOURCE}.openai.azure.com/"
openai.api_version = "2023-03-15-preview"
openai.api_version = "2023-12-01-preview"
openai.api_key = AZURE_OPENAI_KEY

request_messages = request.json["messages"]
Expand Down
58 changes: 58 additions & 0 deletions code/batch/GetConversationResponse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import azure.functions as func
import logging
import json
import os
import sys
from utilities.helpers.OrchestratorHelper import Orchestrator

sys.path.append("..")

bp_get_conversation_response = func.Blueprint()

@bp_get_conversation_response.route(route="GetConversationResponse")
def get_conversation_response(req: func.HttpRequest) -> func.HttpResponse:
logging.info("Python HTTP trigger function processed a request.")


message_orchestrator = Orchestrator()

try:
req_body = req.get_json()
user_message = req_body["messages"][-1]["content"]
conversation_id = req_body["conversation_id"]
user_assistant_messages = list(
filter(
lambda x: x["role"] in ("user", "assistant"), req_body["messages"][0:-1]
)
)
chat_history = []
for i, k in enumerate(user_assistant_messages):
if i % 2 == 0:
chat_history.append(
(
user_assistant_messages[i]["content"],
user_assistant_messages[i + 1]["content"],
)
)
from utilities.helpers.ConfigHelper import ConfigHelper

messages = message_orchestrator.handle_message(
user_message=user_message,
chat_history=chat_history,
conversation_id=conversation_id,
orchestrator=ConfigHelper.get_active_config_or_default().orchestrator,
)

response_obj = {
"id": "response.id",
"model": os.getenv("AZURE_OPENAI_MODEL"),
"created": "response.created",
"object": "response.object",
"choices": [{"messages": messages}],
}

return func.HttpResponse(json.dumps(response_obj), status_code=200)

except Exception as e:
logging.exception("Exception in /api/GetConversationResponse")
return func.HttpResponse(json.dumps({"error": str(e)}), status_code=500)
2 changes: 2 additions & 0 deletions code/batch/function_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
from AddURLEmbeddings import bp_add_url_embeddings
from BatchPushResults import bp_batch_push_results
from BatchStartProcessing import bp_batch_start_processing
from GetConversationResponse import bp_get_conversation_response

app = func.FunctionApp(
http_auth_level=func.AuthLevel.FUNCTION
) # change to ANONYMOUS for local debugging
app.register_functions(bp_add_url_embeddings)
app.register_functions(bp_batch_push_results)
app.register_functions(bp_batch_start_processing)
app.register_functions(bp_get_conversation_response)
2 changes: 1 addition & 1 deletion code/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ beautifulsoup4==4.12.3
fake-useragent==1.4.0
chardet==5.2.0
--extra-index-url https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-python/pypi/simple/
azure-search-documents==11.4.0b8
azure-search-documents==11.4.0
opencensus-ext-azure==1.1.13
pandas==2.2.0
python-docx==1.1.0
4 changes: 2 additions & 2 deletions docs/LOCAL_DEPLOYMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ docker push YOUR_DOCKER_REGISTRY/YOUR_DOCKER_IMAGE
|AZURE_OPENAI_MAX_TOKENS|1000|The maximum number of tokens allowed for the generated answer.|
|AZURE_OPENAI_STOP_SEQUENCE||Up to 4 sequences where the API will stop generating further tokens. Represent these as a string joined with "|", e.g. `"stop1|stop2|stop3"`|
|AZURE_OPENAI_SYSTEM_MESSAGE|You are an AI assistant that helps people find information.|A brief description of the role and tone the model should use|
|AZURE_OPENAI_API_VERSION|2023-06-01-preview|API version when using Azure OpenAI on your data|
|AZURE_OPENAI_API_VERSION|2023-12-01-preview|API version when using Azure OpenAI on your data|
|AzureWebJobsStorage||The connection string to the Azure Blob Storage for the Azure Functions Batch processing|
|BACKEND_URL||The URL for the Backend Batch Azure Function. Use http://localhost:7071 for local execution and http://backend for docker compose|
|DOCUMENT_PROCESSING_QUEUE_NAME|doc-processing|The name of the Azure Queue to handle the Batch processing|
Expand All @@ -202,4 +202,4 @@ docker push YOUR_DOCKER_REGISTRY/YOUR_DOCKER_IMAGE
|AZURE_CONTENT_SAFETY_KEY | | The key of the Azure AI Content Safety service|
|AZURE_SPEECH_SERVICE_KEY | | The key of the Azure Speech service|
|AZURE_SPEECH_SERVICE_REGION | | The region (location) of the Azure Speech service|
|AZURE_AUTH_TYPE | rbac | Change the value to 'keys' to authenticate using AZURE API keys. For more information refer to section [Authenticate using RBAC](#authenticate-using-rbac)
|AZURE_AUTH_TYPE | rbac | Change the value to 'keys' to authenticate using AZURE API keys. For more information refer to section [Authenticate using RBAC](#authenticate-using-rbac)
44 changes: 11 additions & 33 deletions docs/TEAMS_EXTENSION.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
[Back to *Chat with your data* README](../README.md)

# Teams extension
[**USER STORY**](#user-story) | [**ONE-CLICK DEPLOY**](#one-click-deploy) | [**SUPPORTING DOCUMENTATION**](#supporting-documentation)
[**USER STORY**](#user-story) | [**TEAMS DEPLOY**](#teams-deploy) | [**SUPPORTING DOCUMENTATION**](#supporting-documentation)
\
\
![User Story](/media/userStory.png)
## User story
This extension enables users to experience Chat with your data within Teams, without having to switch platforms. It allows them to stay within their existing workflow and get the answers they need. Instead of building the Chat with your data solution accelerator from scratch within Teams, the same underlying backend used for the web application is leveraged within Teams.
\
\
![One-click Deploy](/media/oneClickDeploy.png)
## One-click deploy

## Deployment to Teams
**IMPORTANT**: Before you proceed, installation and configuration of the [Chat with your data with speech-to-text deployment](../README.md) is required.

### Pre-requisites
Expand All @@ -22,31 +20,6 @@ This extension enables users to experience Chat with your data within Teams, wit
- [Enable custom Teams apps and turn on custom app uploading](https://learn.microsoft.com/en-us/microsoftteams/platform/concepts/build-and-test/prepare-your-o365-tenant#enable-custom-teams-apps-and-turn-on-custom-app-uploading) (optional: Teams extension only)
- In order to publish the App to the Teams Store, the Teams Administrator role is required.

### Deploy backend Azure Function

[![Deploy to Azure](https://aka.ms/deploytoazurebutton)](https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2FAzure-Samples%2Fchat-with-your-data-solution-accelerator%2Fmain%2Fextensions%2Finfrastructure%2Fmain.json)

Note: The (p) suffix on the App Setting (below) means that you should use the same resources and services deployed during the [Chat with your data with speech-to-text deployment](../README.md)

| App Setting | Note |
| --- | ------------- |
|Resource group | The resource group that will contain the resources for this accelerator. You can select Create new to create a new group or use the existing resource group created with [Speech-to-text deployment](#speech-to-text-deployment). |
|Resource prefix | A text string that will be appended to each resource that gets created, and used as the website name for the web app. This name cannot contain spaces or special characters. |
|App Insights Connection String (p) | The Application Insights connection string to store the application logs. |
|Azure AI Search (p) | The **name** of your Azure AI Search resource. e.g. https://<**name**>.search.windows.net. |
|Azure Search Index (p) | The name of your Azure AI Search Index. |
|Azure Search Key (p) | An admin key for your Azure AI Search resource. |
|Azure OpenAI resource (p) | The name of your Azure OpenAI resource. This resource must have already been created previously. |
|Azure OpenAI key (p) | The access key is associated with your Azure OpenAI resource. |
|Orchestration strategy (p) | Use Azure OpenAI Functions (openai_functions) or LangChain (langchain) for messages orchestration. If you are using a new model version 0613 select "openai_functions" (or "langchain"), if you are using a model version 0314 select "langchain". |
|Azure Form Recognizer Endpoint (p) | The name of the Azure Form Recognizer for extracting the text from the documents. |
|Azure Form Recognizer Key (p) | The key of the Azure Form Recognizer for extracting the text from the documents. |
|Azure Blob Account Name (p) | The name of the Azure Blob Storage for storing the original documents to be processed. |
|Azure Blob Account Key (p) | The key of the Azure Blob Storage for storing the original documents to be processed. |
|Azure Blob Container Name (p) | The name of the Container in the Azure Blob Storage for storing the original documents to be processed. |

You can find the [ARM template](/extensions/infrastructure/main.json) used, along with a [Bicep file](/extensions/infrastructure/main.bicep) for deploying this accelerator in the /infrastructure directory.

### Deploy Teams application
1. Clone this GitHub repo.
2. Open the “extensions/teams” folder with Visual Studio Code
Expand All @@ -57,8 +30,13 @@ You can find the [ARM template](/extensions/infrastructure/main.json) used, alon

![ENV](/media/teams-1.png)

4. Locate the environment variable AZURE_FUNCTION_URL.
5. Replace the <YOUR AZURE FUNCTION NAME> with the name of your Function App resource (created in previous section)
4. Locate the environment variable _AZURE_FUNCTION_URL_.
5. Replace the `<DEPLOYMENT_PREFIX>` and `<FUNCTION_APP_CLIENT_KEY>` with the name of your Function App resource and its clientKey (created in previous section)
```env
AZURE_FUNCTION_URL=https://<DEPLOYMENT_PREFIX>-backend.azurewebsites.net/api/GetConversationResponse?code=<FUNCTION_APP_CLIENT_KEY>&clientId=clientKey

```
![Env](/media/teams-deploy-env.png)
6. Save the file.
7. Select Teams Toolkit from the navigation panel.

Expand Down Expand Up @@ -166,4 +144,4 @@ To customize the accelerator or run it locally, first, copy the .env.sample file
## Supporting documentation
### Resource links for Teams extension
This solution accelerator deploys the following resources. It's crucial to comprehend the functionality of each. Below are the links to their respective documentation:
- [Bots in Microsoft Teams - Teams | Microsoft Learn](https://learn.microsoft.com/en-us/microsoftteams/platform/bots/what-are-bots)
- [Bots in Microsoft Teams - Teams | Microsoft Learn](https://learn.microsoft.com/en-us/microsoftteams/platform/bots/what-are-bots)
22 changes: 9 additions & 13 deletions docs/TEAMS_LOCAL_DEPLOYMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,26 @@ First, install [Azure Functions Core Tools](https://learn.microsoft.com/en-us/az


```shell
cd extensions
cd backend
cd code\batch
func start
```

Or use the [Azure Functions VS Code extension](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azurefunctions).

#### Building the Teams Backend Docker image

```shell
docker build -f extensions\docker\Backend.Dockerfile -t YOUR_DOCKER_REGISTRY/YOUR_DOCKER_IMAGE .
docker run --env-file .env -p 7071:80 YOUR_DOCKER_REGISTRY/YOUR_DOCKER_IMAGE
docker push YOUR_DOCKER_REGISTRY/YOUR_DOCKER_IMAGE
```
### Develop & run the Teams Frontend
1. Open the “extensions/teams” folder with Visual Studio Code

![Teams](/media/teams.png)

2. Open the file env\\.env.local
3. Locate the environment variable AZURE_FUNCTION_URL.
4. Replace the <YOUR AZURE FUNCTION NAME> with your local Teams Backend URL (i.e., http://localhost:7071/api/http_cwyod)
3. Locate the environment variable _AZURE_FUNCTION_URL_.

![Env](/media/teams-local-3.png)
4. Replace the `<AZURE_FUNCTION_URL>` with your local Teams Backend URL (i.e., http://localhost:7071/api/GetConversationResponse)
```env
AZURE_FUNCTION_URL=http://localhost:7071/api/GetConversationResponse
```
![Env](/media/teams-local-3.png)

5. Save the file.
6. Select Teams Toolkit from the navigation panel.
Expand Down Expand Up @@ -76,7 +72,7 @@ docker push YOUR_DOCKER_REGISTRY/YOUR_DOCKER_IMAGE
|AZURE_OPENAI_MAX_TOKENS|1000|The maximum number of tokens allowed for the generated answer.|
|AZURE_OPENAI_STOP_SEQUENCE||Up to 4 sequences where the API will stop generating further tokens. Represent these as a string joined with "|", e.g. `"stop1|stop2|stop3"`|
|AZURE_OPENAI_SYSTEM_MESSAGE|You are an AI assistant that helps people find information.|A brief description of the role and tone the model should use|
|AZURE_OPENAI_API_VERSION|2023-06-01-preview|API version when using Azure OpenAI on your data|
|AZURE_OPENAI_API_VERSION|2023-12-01-preview|API version when using Azure OpenAI on your data|
|AzureWebJobsStorage||The connection string to the Azure Blob Storage for the Azure Functions Batch processing|
|BACKEND_URL||The URL for the Backend Batch Azure Function. Use http://localhost:7071 for local execution and http://backend for docker compose|
|DOCUMENT_PROCESSING_QUEUE_NAME|doc-processing|The name of the Azure Queue to handle the Batch processing|
Expand All @@ -90,4 +86,4 @@ docker push YOUR_DOCKER_REGISTRY/YOUR_DOCKER_IMAGE
|AZURE_CONTENT_SAFETY_ENDPOINT | | The endpoint of the Azure AI Content Safety service |
|AZURE_CONTENT_SAFETY_KEY | | The key of the Azure AI Content Safety service|
|AZURE_SPEECH_SERVICE_KEY | | The key of the Azure Speech service|
|AZURE_SPEECH_SERVICE_REGION | | The region (location) of the Azure Speech service|
|AZURE_SPEECH_SERVICE_REGION | | The region (location) of the Azure Speech service|
6 changes: 3 additions & 3 deletions extensions/infrastructure/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ param AzureOpenAIStopSequence string = '\n'
param AzureOpenAISystemMessage string = 'You are an AI assistant that helps people find information.'

@description('Azure OpenAI Api Version - Created during the "Chat with your data" Solution Accelerator')
param AzureOpenAIApiVersion string = '2023-07-01-preview'
param AzureOpenAIApiVersion string = '2023-12-01-preview'

@description('Whether or not to stream responses from Azure OpenAI - Created during the "Chat with your data" Solution Accelerator')
param AzureOpenAIStream string = 'true'
Expand All @@ -129,7 +129,7 @@ param AzureBlobAccountKey string
@description('Storage Account Container Name - Created during the "Chat with your data" Solution Accelerator')
param AzureBlobContainerName string

var BackendImageName = 'DOCKER|fruoccopublic.azurecr.io/cwyod_backend'
var BackendImageName = 'DOCKER|fruoccopublic.azurecr.io/rag-backend'

resource HostingPlan 'Microsoft.Web/serverfarms@2020-06-01' = {
name: HostingPlanName
Expand Down Expand Up @@ -214,4 +214,4 @@ resource WaitFunctionDeploymentSection 'Microsoft.Resources/deploymentScripts@20
dependsOn: [
Function
]
}
}
4 changes: 2 additions & 2 deletions extensions/infrastructure/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@
},
"AzureOpenAIApiVersion": {
"type": "string",
"defaultValue": "2023-07-01-preview",
"defaultValue": "2023-12-01-preview",
"metadata": {
"description": "Azure OpenAI Api Version - Created during the \"Chat with your data\" Solution Accelerator"
}
Expand Down Expand Up @@ -268,7 +268,7 @@
}
},
"variables": {
"BackendImageName": "DOCKER|fruoccopublic.azurecr.io/cwyod_backend"
"BackendImageName": "DOCKER|fruoccopublic.azurecr.io//rag-backend"
},
"resources": [
{
Expand Down
2 changes: 1 addition & 1 deletion extensions/teams/appPackage/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://developer.microsoft.com/en-us/json-schemas/teams/v1.16/MicrosoftTeams.schema.json",
"manifestVersion": "1.16",
"version": "1.0.0",
"version": "1.1.4",
"id": "${{TEAMS_APP_ID}}",
"packageName": "com.microsoft.teams.extension",
"developer": {
Expand Down
Loading
Loading