Skip to content

Commit

Permalink
Merge branch 'dev' into dependabotchanges
Browse files Browse the repository at this point in the history
  • Loading branch information
Roopan-Microsoft committed Dec 30, 2024
2 parents 0cbbac0 + 56adb59 commit 61753fe
Show file tree
Hide file tree
Showing 26 changed files with 1,198 additions and 572 deletions.
22 changes: 14 additions & 8 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ AZURE_SEARCH_DATASOURCE_NAME=
# Azure OpenAI for generating the answer and computing the embedding of the documents
AZURE_OPENAI_RESOURCE=
AZURE_OPENAI_API_KEY=
AZURE_OPENAI_MODEL_INFO="{\"model\":\"gpt-35-turbo-16k\",\"modelName\":\"gpt-35-turbo-16k\",\"modelVersion\":\"0613\"}"
AZURE_OPENAI_EMBEDDING_MODEL_INFO="{\"model\":\"text-embedding-ada-002\",\"modelName\":\"text-embedding-ada-002\",\"modelVersion\":\"2\"}"
AZURE_OPENAI_MODEL=gpt-35-turbo
AZURE_OPENAI_MODEL_NAME=gpt-35-turbo
AZURE_OPENAI_EMBEDDING_MODEL=text-embedding-ada-002
AZURE_OPENAI_TEMPERATURE=0
AZURE_OPENAI_TOP_P=1.0
AZURE_OPENAI_MAX_TOKENS=1000
Expand All @@ -35,10 +36,12 @@ AZURE_OPENAI_STREAM=True
AzureWebJobsStorage=
BACKEND_URL=http://localhost:7071
DOCUMENT_PROCESSING_QUEUE_NAME=
# Azure Blob Storage for storing the original documents to be processed
AZURE_BLOB_STORAGE_INFO="{\"containerName\":\"documents\",\"accountName\":\"\",\"accountKey\":\"\"}"
AZURE_BLOB_ACCOUNT_NAME=
AZURE_BLOB_ACCOUNT_KEY=
AZURE_BLOB_CONTAINER_NAME=
# Azure Form Recognizer for extracting the text from the documents
AZURE_FORM_RECOGNIZER_INFO="{\"endpoint\":\"\",\"key\":\"\"}"
AZURE_FORM_RECOGNIZER_ENDPOINT=
AZURE_FORM_RECOGNIZER_KEY=
# Azure AI Content Safety for filtering out the inappropriate questions or answers
AZURE_CONTENT_SAFETY_ENDPOINT=
AZURE_CONTENT_SAFETY_KEY=
Expand All @@ -60,8 +63,11 @@ AZURE_KEY_VAULT_ENDPOINT=
# Chat conversation type to decide between custom or byod (bring your own data) conversation type
CONVERSATION_FLOW=
# Chat History CosmosDB Integration Settings
AZURE_COSMOSDB_INFO="{\"accountName\":\"cosmos-abc123\",\"databaseName\":\"db_conversation_history\",\"containerName\":\"conversations\"}"
AZURE_COSMOSDB_ACCOUNT_KEY=
AZURE_COSMOSDB_ACCOUNT_NAME=
AZURE_COSMOSDB_DATABASE_NAME=
AZURE_COSMOSDB_CONVERSATIONS_CONTAINER_NAME=
AZURE_COSMOSDB_ENABLE_FEEDBACK=
AZURE_POSTGRESQL_INFO="{\"user\":\"\",\"dbname\":\"postgres\",\"host\":\"\"}"
AZURE_POSTGRESQL_HOST_NAME=
AZURE_POSTGRESQL_DATABASE_NAME=
AZURE_POSTGRESQL_USER=
DATABASE_TYPE="CosmosDB"
11 changes: 4 additions & 7 deletions .github/workflows/build-docker-images.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
name: Build Docker Images

on:
workflow_run:
workflows: [Tests]
types: [completed]
push:
branches:
- main
- dev
Expand All @@ -22,7 +20,6 @@ on:

jobs:
docker-build:
if: ${{ github.event_name != 'workflow_run' || github.event.workflow_run.conclusion == 'success' }}
strategy:
matrix:
include:
Expand All @@ -34,9 +31,9 @@ jobs:
dockerfile: docker/Frontend.Dockerfile
uses: ./.github/workflows/build-docker.yml
with:
registry: ${{ github.event.workflow_run.head_branch == 'main' && 'fruoccopublic.azurecr.io' || 'cwydcontainerreg.azurecr.io'}}
username: ${{ github.event.workflow_run.head_branch == 'main' && 'fruoccopublic' || 'cwydcontainerreg'}}
registry: ${{ github.ref_name == 'main' && 'fruoccopublic.azurecr.io' || 'cwydcontainerreg.azurecr.io'}}
username: ${{ github.ref_name == 'main' && 'fruoccopublic' || 'cwydcontainerreg'}}
app_name: ${{ matrix.app_name }}
dockerfile: ${{ matrix.dockerfile }}
push: ${{ github.event.workflow_run.head_branch == 'main' || github.event.workflow_run.head_branch == 'dev' || github.event.workflow_run.head_branch == 'demo' }}
push: ${{ github.ref_name == 'main' || github.ref_name == 'dev' || github.ref_name == 'demo' }}
secrets: inherit
5 changes: 2 additions & 3 deletions .github/workflows/build-docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ jobs:
docker-build:
runs-on: ubuntu-latest
steps:

- name: Checkout
uses: actions/checkout@v4

Expand Down Expand Up @@ -61,7 +60,7 @@ jobs:
context: .
file: ${{ inputs.dockerfile }}
push: ${{ inputs.push }}
cache-from: type=registry,ref=${{ inputs.registry }}/${{ inputs.app_name}}:${{ github.ref_name == 'main' && 'latest' || github.ref_name == 'dev' && 'dev' || github.ref_name == 'demo' && 'demo' || 'latest' }}
cache-from: type=registry,ref=${{ inputs.registry }}/${{ inputs.app_name}}:${{ github.ref_name == 'main' && 'latest' || github.ref_name == 'dev' && 'dev' || github.ref_name == 'demo' && 'demo' || github.head_ref }}
tags: |
${{ inputs.registry }}/${{ inputs.app_name}}:${{ github.ref_name == 'main' && 'latest' || github.ref_name == 'dev' && 'dev' || github.ref_name == 'demo' && 'demo' || 'latest' }}
${{ inputs.registry }}/${{ inputs.app_name}}:${{ github.ref_name == 'main' && 'latest' || github.ref_name == 'dev' && 'dev' || github.ref_name == 'demo' && 'demo' || github.head_ref }}
${{ inputs.registry }}/${{ inputs.app_name}}:${{ steps.date.outputs.date }}_${{ github.run_number }}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Welcome to the *Chat with your data* Solution accelerator repository! The *Chat




### About this repo

This repository provides an end-to-end solution for users who want to query their data with natural language. It includes a well designed ingestion mechanism for multiple file types, an easy deployment, and a support team for maintenance. The accelerator demonstrates both Push or Pull Ingestion; the choice of orchestration (Semantic Kernel, LangChain, OpenAI Functions or [Prompt Flow](docs/prompt_flow.md)) and should be the minimum components needed to implement a RAG pattern. It is not intended to be put into Production as-is without experimentation or evaluation of your data. It provides the following features:
Expand Down
6 changes: 6 additions & 0 deletions azure.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ metadata:
hooks:
postprovision:
run: ./infra/prompt-flow/create-prompt-flow.sh
posix:
shell: sh
run: ./scripts/parse_env.sh
windows:
shell: pwsh
run: ./scripts/parse_env.ps1
services:
web:
project: ./code
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def get_container_sas(self):
user_delegation_key=self.user_delegation_key,
account_key=self.account_key,
permission="r",
expiry=datetime.utcnow() + timedelta(hours=1),
expiry=datetime.utcnow() + timedelta(days=365 * 5),
)

def get_blob_sas(self, file_name):
Expand Down
10 changes: 5 additions & 5 deletions code/backend/batch/utilities/helpers/config/config_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ def __init__(self, config: dict):
)
self.enable_chat_history = config["enable_chat_history"]
self.database_type = config.get("database_type", self.env_helper.DATABASE_TYPE)
self.conversational_flow = config.get(
"conversational_flow", self.env_helper.CONVERSATION_FLOW
)

def get_available_document_types(self) -> list[str]:
document_types = {
Expand Down Expand Up @@ -247,11 +250,7 @@ def get_default_config():
logger.info("Loading default config from %s", config_file_path)
ConfigHelper._default_config = json.loads(
Template(f.read()).substitute(
ORCHESTRATION_STRATEGY=(
OrchestrationStrategy.SEMANTIC_KERNEL.value
if env_helper.DATABASE_TYPE == DatabaseType.POSTGRESQL.value
else env_helper.ORCHESTRATION_STRATEGY
),
ORCHESTRATION_STRATEGY=env_helper.ORCHESTRATION_STRATEGY,
LOG_USER_INTERACTIONS=(
False
if env_helper.DATABASE_TYPE == DatabaseType.POSTGRESQL.value
Expand All @@ -262,6 +261,7 @@ def get_default_config():
if env_helper.DATABASE_TYPE == DatabaseType.POSTGRESQL.value
else True
),
CONVERSATION_FLOW=env_helper.CONVERSATION_FLOW,
DATABASE_TYPE=env_helper.DATABASE_TYPE,
)
)
Expand Down
2 changes: 1 addition & 1 deletion code/backend/batch/utilities/helpers/config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"enable_post_answering_prompt": false,
"ai_assistant_type": "default",
"enable_content_safety": true,
"conversational_flow": "custom"
"conversational_flow": "${CONVERSATION_FLOW}"
},
"example": {
"documents": "{\n \"retrieved_documents\": [\n {\n \"[doc1]\": {\n \"content\": \"Dual Transformer Encoder (DTE) DTE (https://dev.azure.com/TScience/TSciencePublic/_wiki/wikis/TSciencePublic.wiki/82/Dual-Transformer-Encoder) DTE is a general pair-oriented sentence representation learning framework based on transformers. It provides training, inference and evaluation for sentence similarity models. Model Details DTE can be used to train a model for sentence similarity with the following features: - Build upon existing transformer-based text representations (e.g.TNLR, BERT, RoBERTa, BAG-NLR) - Apply smoothness inducing technology to improve the representation robustness - SMART (https://arxiv.org/abs/1911.03437) SMART - Apply NCE (Noise Contrastive Estimation) based similarity learning to speed up training of 100M pairs We use pretrained DTE model\"\n }\n },\n {\n \"[doc2]\": {\n \"content\": \"trained on internal data. You can find more details here - Models.md (https://dev.azure.com/TScience/_git/TSciencePublic?path=%2FDualTransformerEncoder%2FMODELS.md&version=GBmaster&_a=preview) Models.md DTE-pretrained for In-context Learning Research suggests that finetuned transformers can be used to retrieve semantically similar exemplars for e.g. KATE (https://arxiv.org/pdf/2101.06804.pdf) KATE . They show that finetuned models esp. tuned on related tasks give the maximum boost to GPT-3 in-context performance. DTE have lot of pretrained models that are trained on intent classification tasks. We can use these model embedding to find natural language utterances which are similar to our test utterances at test time. The steps are: 1. Embed\"\n }\n },\n {\n \"[doc3]\": {\n \"content\": \"train and test utterances using DTE model 2. For each test embedding, find K-nearest neighbors. 3. Prefix the prompt with nearest embeddings. The following diagram from the above paper (https://arxiv.org/pdf/2101.06804.pdf) the above paper visualizes this process: DTE-Finetuned This is an extension of DTE-pretrained method where we further finetune the embedding models for prompt crafting task. In summary, we sample random prompts from our training data and use them for GPT-3 inference for the another part of training data. Some prompts work better and lead to right results whereas other prompts lead\"\n }\n },\n {\n \"[doc4]\": {\n \"content\": \"to wrong completions. We finetune the model on the downstream task of whether a prompt is good or not based on whether it leads to right or wrong completion. This approach is similar to this paper: Learning To Retrieve Prompts for In-Context Learning (https://arxiv.org/pdf/2112.08633.pdf) this paper: Learning To Retrieve Prompts for In-Context Learning . This method is very general but it may require a lot of data to actually finetune a model to learn how to retrieve examples suitable for the downstream inference model like GPT-3.\"\n }\n }\n ]\n}",
Expand Down
50 changes: 38 additions & 12 deletions code/backend/batch/utilities/helpers/env_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
from dotenv import load_dotenv
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
from azure.keyvault.secrets import SecretClient

from ..orchestrator.orchestration_strategy import OrchestrationStrategy
from ..helpers.config.conversation_flow import ConversationFlow
from ..helpers.config.database_type import DatabaseType

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -97,11 +100,24 @@ def __load_config(self, **kwargs) -> None:
# Cosmos DB configuration
if self.DATABASE_TYPE == DatabaseType.COSMOSDB.value:
azure_cosmosdb_info = self.get_info_from_env("AZURE_COSMOSDB_INFO", "")
self.AZURE_COSMOSDB_DATABASE = azure_cosmosdb_info.get("databaseName", "")
self.AZURE_COSMOSDB_ACCOUNT = azure_cosmosdb_info.get("accountName", "")
self.AZURE_COSMOSDB_CONVERSATIONS_CONTAINER = azure_cosmosdb_info.get(
"containerName", ""
)
if azure_cosmosdb_info:
self.AZURE_COSMOSDB_DATABASE = azure_cosmosdb_info.get(
"databaseName", ""
)
self.AZURE_COSMOSDB_ACCOUNT = azure_cosmosdb_info.get("accountName", "")
self.AZURE_COSMOSDB_CONVERSATIONS_CONTAINER = azure_cosmosdb_info.get(
"containerName", ""
)
else:
self.AZURE_COSMOSDB_DATABASE = os.getenv(
"AZURE_COSMOSDB_DATABASE_NAME", ""
)
self.AZURE_COSMOSDB_ACCOUNT = os.getenv(
"AZURE_COSMOSDB_ACCOUNT_NAME", ""
)
self.AZURE_COSMOSDB_CONVERSATIONS_CONTAINER = os.getenv(
"AZURE_COSMOSDB_CONVERSATIONS_CONTAINER_NAME", ""
)
self.AZURE_COSMOSDB_ACCOUNT_KEY = self.secretHelper.get_secret(
"AZURE_COSMOSDB_ACCOUNT_KEY"
)
Expand All @@ -114,18 +130,32 @@ def __load_config(self, **kwargs) -> None:
self.USE_ADVANCED_IMAGE_PROCESSING = self.get_env_var_bool(
"USE_ADVANCED_IMAGE_PROCESSING", "False"
)
self.CONVERSATION_FLOW = os.getenv("CONVERSATION_FLOW", "custom")
# Orchestration Settings
self.ORCHESTRATION_STRATEGY = os.getenv(
"ORCHESTRATION_STRATEGY", "openai_function"
)
# PostgreSQL configuration
elif self.DATABASE_TYPE == DatabaseType.POSTGRESQL.value:
self.AZURE_POSTGRES_SEARCH_TOP_K = self.get_env_var_int(
"AZURE_POSTGRES_SEARCH_TOP_K", 5
)
azure_postgresql_info = self.get_info_from_env("AZURE_POSTGRESQL_INFO", "")
self.POSTGRESQL_USER = azure_postgresql_info.get("user", "")
self.POSTGRESQL_DATABASE = azure_postgresql_info.get("dbname", "")
self.POSTGRESQL_HOST = azure_postgresql_info.get("host", "")
if azure_postgresql_info:
self.POSTGRESQL_USER = azure_postgresql_info.get("user", "")
self.POSTGRESQL_DATABASE = azure_postgresql_info.get("dbname", "")
self.POSTGRESQL_HOST = azure_postgresql_info.get("host", "")
else:
self.POSTGRESQL_USER = os.getenv("AZURE_POSTGRESQL_USER", "")
self.POSTGRESQL_DATABASE = os.getenv(
"AZURE_POSTGRESQL_DATABASE_NAME", ""
)
self.POSTGRESQL_HOST = os.getenv("AZURE_POSTGRESQL_HOST_NAME", "")
# Ensure integrated vectorization is disabled for PostgreSQL
self.AZURE_SEARCH_USE_INTEGRATED_VECTORIZATION = False
self.USE_ADVANCED_IMAGE_PROCESSING = False
self.CONVERSATION_FLOW = ConversationFlow.CUSTOM.value
self.ORCHESTRATION_STRATEGY = OrchestrationStrategy.SEMANTIC_KERNEL.value
else:
raise ValueError(
"Unsupported DATABASE_TYPE. Please set DATABASE_TYPE to 'CosmosDB' or 'PostgreSQL'."
Expand Down Expand Up @@ -305,10 +335,6 @@ def __load_config(self, **kwargs) -> None:
self.AZURE_CONTENT_SAFETY_KEY = self.secretHelper.get_secret(
"AZURE_CONTENT_SAFETY_KEY"
)
# Orchestration Settings
self.ORCHESTRATION_STRATEGY = os.getenv(
"ORCHESTRATION_STRATEGY", "openai_function"
)
# Speech Service
self.AZURE_SPEECH_SERVICE_NAME = os.getenv("AZURE_SPEECH_SERVICE_NAME", "")
self.AZURE_SPEECH_SERVICE_REGION = os.getenv("AZURE_SPEECH_SERVICE_REGION")
Expand Down
23 changes: 14 additions & 9 deletions code/frontend/src/pages/chat/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const Chat = () => {
const lastQuestionRef = useRef<string>("");
const chatMessageStreamEnd = useRef<HTMLDivElement | null>(null);
const [isLoading, setIsLoading] = useState<boolean>(false);
const [isGenerating, setIsGenerating] = useState<boolean>(false); // Add this state
const [showLoadingMessage, setShowLoadingMessage] = useState<boolean>(false);
const [isAssistantAPILoading, setIsAssistantAPILoading] = useState(false);
const [isSendButtonDisabled, setSendButtonDisabled] = useState(false);
Expand Down Expand Up @@ -185,7 +186,7 @@ const Chat = () => {
text: "Clear all chat history",
disabled:
!chatHistory.length ||
isLoading ||
isGenerating ||
fetchingConvMessages ||
fetchingChatHistory,
iconProps: { iconName: "Delete" },
Expand All @@ -194,7 +195,7 @@ const Chat = () => {
const makeApiRequest = async (question: string) => {
lastQuestionRef.current = question;

setIsLoading(true);
setIsGenerating(true);
setShowLoadingMessage(true);
const abortController = new AbortController();
abortFuncs.current.unshift(abortController);
Expand Down Expand Up @@ -274,7 +275,7 @@ const Chat = () => {
}
setAnswers([...answers, userMessage]);
} finally {
setIsLoading(false);
setIsGenerating(false);
setShowLoadingMessage(false);
abortFuncs.current = abortFuncs.current.filter(
(a) => a !== abortController
Expand Down Expand Up @@ -371,7 +372,7 @@ const Chat = () => {
const stopGenerating = () => {
abortFuncs.current.forEach((a) => a.abort());
setShowLoadingMessage(false);
setIsLoading(false);
setIsGenerating(false);
};

useEffect(() => {
Expand Down Expand Up @@ -485,6 +486,10 @@ const Chat = () => {
};

const onSelectConversation = async (id: string) => {
if (isGenerating) {
// If response is being generated, prevent switching threads
return;
}
if (!id) {
console.error("No conversation Id found");
return;
Expand Down Expand Up @@ -623,7 +628,7 @@ const Chat = () => {
) : (
<div
className={styles.chatMessageStream}
style={{ marginBottom: isLoading ? "40px" : "0px" }}
style={{ marginBottom: isGenerating ? "40px" : "0px" }}
>
{fetchingConvMessages && (
<div className={styles.fetchMessagesSpinner}>
Expand Down Expand Up @@ -697,7 +702,7 @@ const Chat = () => {
</div>

<Stack horizontal className={styles.chatInput}>
{isLoading && (
{isGenerating && (
<Stack
horizontal
className={styles.stopGeneratingContainer}
Expand Down Expand Up @@ -725,10 +730,10 @@ const Chat = () => {
className={`${styles.clearChatBroom} ${styles.mobileclearChatBroom}`}
style={{
background:
isLoading || answers.length === 0
isGenerating || answers.length === 0
? "#BDBDBD"
: "radial-gradient(109.81% 107.82% at 100.1% 90.19%, #0F6CBD 33.63%, #2D87C3 70.31%, #8DDDD8 100%)",
cursor: isLoading || answers.length === 0 ? "" : "pointer",
cursor: isGenerating || answers.length === 0 ? "" : "pointer",
}}
onClick={clearChat}
onKeyDown={(e) =>
Expand All @@ -741,7 +746,7 @@ const Chat = () => {
<QuestionInput
clearOnSend
placeholder="Type a new question..."
disabled={isLoading}
disabled={isGenerating}
onSend={(question) => makeApiRequest(question)}
recognizedText={recognizedText}
isSendButtonDisabled={isSendButtonDisabled}
Expand Down
Loading

0 comments on commit 61753fe

Please sign in to comment.