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

Multilingual support #470

Merged
merged 8 commits into from
Jan 24, 2024
42 changes: 28 additions & 14 deletions app/backend/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import urllib.parse
from datetime import datetime, timedelta
from fastapi.staticfiles import StaticFiles
import openai
from fastapi import FastAPI, HTTPException, Request
from fastapi.responses import RedirectResponse
import openai
from approaches.chatreadretrieveread import ChatReadRetrieveReadApproach
from approaches.approach import Approaches
from azure.core.credentials import AzureKeyCredential
Expand Down Expand Up @@ -65,7 +65,10 @@
"COSMOSDB_TAGS_CONTAINER_NAME": "tagscontainer",
"QUERY_TERM_LANGUAGE": "English",
"TARGET_EMBEDDINGS_MODEL": "BAAI/bge-small-en-v1.5",
"ENRICHMENT_APPSERVICE_NAME": "enrichment"
"ENRICHMENT_APPSERVICE_NAME": "enrichment",
"TARGET_TRANSLATION_LANGUAGE": "en",
"ENRICHMENT_ENDPOINT": None,
"ENRICHMENT_KEY": None
}

for key, value in ENV.items():
Expand Down Expand Up @@ -97,10 +100,16 @@

# Setup StatusLog to allow access to CosmosDB for logging
statusLog = StatusLog(
ENV["COSMOSDB_URL"], ENV["COSMOSDB_KEY"], ENV["COSMOSDB_LOG_DATABASE_NAME"], ENV["COSMOSDB_LOG_CONTAINER_NAME"]
ENV["COSMOSDB_URL"],
ENV["COSMOSDB_KEY"],
ENV["COSMOSDB_LOG_DATABASE_NAME"],
ENV["COSMOSDB_LOG_CONTAINER_NAME"]
)
tagsHelper = TagsHelper(
ENV["COSMOSDB_URL"], ENV["COSMOSDB_KEY"], ENV["COSMOSDB_TAGS_DATABASE_NAME"], ENV["COSMOSDB_TAGS_CONTAINER_NAME"]
ENV["COSMOSDB_URL"],
ENV["COSMOSDB_KEY"],
ENV["COSMOSDB_TAGS_DATABASE_NAME"],
ENV["COSMOSDB_TAGS_CONTAINER_NAME"]
)

# Comment these two lines out if using keys, set your API key in the OPENAI_API_KEY environment variable instead
Expand All @@ -123,7 +132,7 @@
model_name = ''
model_version = ''

if (str_to_bool.get(ENV["IS_GOV_CLOUD_DEPLOYMENT"])):
if str_to_bool.get(ENV["IS_GOV_CLOUD_DEPLOYMENT"]):
model_name = ENV["AZURE_OPENAI_CHATGPT_MODEL_NAME"]
model_version = ENV["AZURE_OPENAI_CHATGPT_MODEL_VERSION"]
embedding_model_name = ENV["AZURE_OPENAI_EMBEDDINGS_MODEL_NAME"]
Expand Down Expand Up @@ -171,7 +180,10 @@
model_version,
str_to_bool.get(ENV["IS_GOV_CLOUD_DEPLOYMENT"]),
ENV["TARGET_EMBEDDINGS_MODEL"],
ENV["ENRICHMENT_APPSERVICE_NAME"]
ENV["ENRICHMENT_APPSERVICE_NAME"],
ENV["TARGET_TRANSLATION_LANGUAGE"],
ENV["ENRICHMENT_ENDPOINT"],
ENV["ENRICHMENT_KEY"]
)
}

Expand All @@ -186,6 +198,7 @@

@app.get("/", include_in_schema=False, response_class=RedirectResponse)
async def root():
"""Redirect to the index.html page"""
return RedirectResponse(url="/index.html")


Expand Down Expand Up @@ -220,7 +233,7 @@ async def chat(request: Request):

except Exception as ex:
log.error(f"Error in chat:: {ex}")
raise HTTPException(status_code=500, detail=str(ex))
raise HTTPException(status_code=500, detail=str(ex)) from ex

@app.get("/getblobclienturl")
async def get_blob_client_url():
Expand Down Expand Up @@ -268,7 +281,7 @@ async def get_all_upload_status(request: Request):
results = statusLog.read_files_status_by_timeframe(timeframe, State[state])
except Exception as ex:
log.exception("Exception in /getalluploadstatus")
raise HTTPException(status_code=500, detail=str(ex))
raise HTTPException(status_code=500, detail=str(ex)) from ex
return results

@app.post("/logstatus")
Expand All @@ -280,7 +293,8 @@ async def logstatus(request: Request):
- request: Request object containing the HTTP request data.

Returns:
- A dictionary with the status code 200 if successful, or an error message with status code 500 if an exception occurs.
- A dictionary with the status code 200 if successful, or an error
message with status code 500 if an exception occurs.
"""
try:
json_body = await request.json()
Expand All @@ -295,10 +309,10 @@ async def logstatus(request: Request):
state=state,
fresh_start=True)
statusLog.save_document(document_path=path)

except Exception as ex:
log.exception("Exception in /logstatus")
raise HTTPException(status_code=500, detail=str(ex))
raise HTTPException(status_code=500, detail=str(ex)) from ex
raise HTTPException(status_code=200, detail="Success")

# Return AZURE_OPENAI_CHATGPT_DEPLOYMENT
Expand Down Expand Up @@ -365,7 +379,7 @@ async def get_citation(request: Request):
results = json.loads(decoded_text)
except Exception as ex:
log.exception("Exception in /getalluploadstatus")
raise HTTPException(status_code=500, detail=str(ex))
raise HTTPException(status_code=500, detail=str(ex)) from ex
return results

# Return APPLICATION_TITLE
Expand Down Expand Up @@ -393,7 +407,7 @@ async def get_all_tags():
results = tagsHelper.get_all_tags()
except Exception as ex:
log.exception("Exception in /getalltags")
raise HTTPException(status_code=500, detail=str(ex))
raise HTTPException(status_code=500, detail=str(ex)) from ex
return results

@app.post("/retryFile")
Expand All @@ -415,7 +429,7 @@ async def retryFile(request: Request):

except Exception as ex:
logging.exception("Exception in /retryFile")
raise HTTPException(status_code=500, detail=str(ex))
raise HTTPException(status_code=500, detail=str(ex)) from ex
return {"status": 200}


Expand Down
7 changes: 5 additions & 2 deletions app/backend/approaches/approach.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ class Approach:
An approach is a method for answering a question from a query and a set of
documents.
"""
# Chat roles
SYSTEM = "system"
USER = "user"
ASSISTANT = "assistant"

async def run(self, history: list[dict], overrides: dict) -> any:
"""
Expand All @@ -28,14 +32,13 @@ async def run(self, history: list[dict], overrides: dict) -> any:
"""
raise NotImplementedError

#Aparmar. Custom method to construct Chat History as opposed to single string of chat History.
def get_messages_from_history(
self,
system_prompt: str,
model_id: str,
history: Sequence[dict[str, str]],
user_conv: str,
few_shots = [],
few_shots = [dict[str, str]],
max_tokens: int = 4096,
) -> []:
"""
Expand Down
Loading
Loading