Skip to content

Commit

Permalink
Refacto: Configuration management
Browse files Browse the repository at this point in the history
  • Loading branch information
clemlesne committed Nov 29, 2023
1 parent c291768 commit 32e4417
Show file tree
Hide file tree
Showing 37 changed files with 785 additions and 795 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.version.cache

# Local app config file
config.toml
config.yaml

# Created by https://www.toptal.com/developers/gitignore/api/osx,linux,eclipse,windows,visualstudio,visualstudiocode,dotenv
# Edit at https://www.toptal.com/developers/gitignore?templates=osx,linux,eclipse,windows,visualstudio,visualstudiocode,dotenv
Expand Down
178 changes: 90 additions & 88 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,96 +113,98 @@ graph

### Run locally

Create a local configuration file, a file named `config.toml` at the root of the project. The TOML file can be placed anywhere in the folder or in any parent directory.
Create a local configuration file, a file named `config.yaml` at the root of the project. The TOML file can be placed anywhere in the folder or in any parent directory.

```toml
# config.toml
```yaml
# config.yaml
# /!\ All the file values are for example, you must change them
[api]
# root_path = "[api-path]"

[oidc]
algorithms = ["RS256"]
api_audience = "[aad_app_id]"
issuers = ["https://login.microsoftonline.com/[tenant_id]/v2.0"]
jwks = "https://login.microsoftonline.com/common/discovery/v2.0/keys"

[monitoring]

[monitoring.logging]
app_level = "DEBUG" # Enum: "NOSET", "DEBUG", "INFO", "WARN", "ERROR", "FATAL", "CRITICAL"
sys_level = "WARN" # Enum: "NOSET", "DEBUG", "INFO", "WARN", "ERROR", "FATAL", "CRITICAL"

[monitoring.azure_app_insights]
connection_str = "InstrumentationKey=[key];[...]"

[persistence]
cache = "redis" # Enum: "redis"
search = "qdrant" # Enum: "qdrant"
store = "cosmos" # Enum: "cache", "cosmos"
stream = "redis" # Enum: "redis"

[persistence.qdrant]
host = "[host]"

[persistence.redis]
db = 0
host = "[host]"

[persistence.cosmos]
# Containers "conversation" (/user_id), "message" (/conversation_id), "user" (/dummy), "usage" (/user_id) must exist
url = "https://[deployment].documents.azure.com:443"
database = "[db_name]"

[ai]

[ai.openai]
ada_deploy_id = "ada"
ada_max_tokens = 2049
api_base = "https://[deployment].openai.azure.com"
gpt_deploy_id = "gpt"
gpt_max_tokens = 4096

[ai.azure_content_safety]
api_base = "https://[deployment].cognitiveservices.azure.com"
api_token = "[api_token]"
max_length = 1000

[tools]

[tools.azure_form_recognizer]
api_base = "https://[deployment].cognitiveservices.azure.com"
api_token = "[api_token]"

[tools.bing]
search_url = "https://api.bing.microsoft.com/v7.0/search"
subscription_key = "[api_token]"

[tools.tmdb]
bearer_token = "[jwt_token]"

[tools.news]
api_key = "[api_token]"

[tools.listen_notes]
api_key = "[api_token]"

[tools.open_weather_map]
api_key = "[api_token]"

[tools.google_places]
api_key = "[api_token]"

[[tools.azure_cognitive_search]]
api_key = "[api_token]"
content_key = "content"
displayed_name = "My search engine"
index_name = "[index_name]"
language = "en-US"
semantic_configuration = "default"
service_name = "[service_name]"
top_k = 10
usage = "Useful when you need legal content contained in one of the codes (incl. civil, commercial, labor, electoral) derived from French legislation. Use this in priority for legal content in France."
api: {}
# root_path: API_PATH

oidc:
algorithms: ["RS256"]
api_audience: e9d5f20f-7f14-4204-a9a2-0d91d6af5c82
issuers:
[
"https://login.microsoftonline.com/72f988bf-86f1-41af-91ab-2d7cd011db47/v2.0",
"https://login.microsoftonline.com/9188040d-6c67-4c5b-b112-36a304b66dad/v2.0",
]
jwks: https://login.microsoftonline.com/common/discovery/v2.0/keys

monitoring:
logging:
app_level: DEBUG
sys_level: WARN
azure_app_insights:
connection_str: InstrumentationKey=XXX;IngestionEndpoint=https://westeurope-5.in.applicationinsights.azure.com/;LiveEndpoint=https://westeurope.livediagnostics.monitor.azure.com

persistence:
cache:
type: redis
config:
db: 0
host: localhost
search:
type: qdrant
config:
host: localhost
store:
type: cosmos
config:
url: https://private-gpt.documents.azure.com:443
database: private-gpt
stream:
type: redis
config:
db: 0
host: localhost

ai:
openai:
ada_deploy_id: ada
ada_max_tokens: 2049
api_base: https://private-gpt-fr.openai.azure.com
gpt_deploy_id: gpt
gpt_max_tokens: 16384
azure_content_safety:
api_base: https://moaw-dev-clesne-moaw-search-acs.cognitiveservices.azure.com
api_token: XXX
max_length: 1000

tools:
azure_form_recognizer:
api_base: https://private-gpt.cognitiveservices.azure.com
api_token: XXX
bing:
search_url: https://api.bing.microsoft.com/v7.0/search
subscription_key: XXX
tmdb:
bearer_token: XXX
news:
api_key: XXX
listen_notes:
api_key: XXX
open_weather_map:
api_key: XXX
google_places:
api_key: XXX
azure_cognitive_search:
- api_key: XXX
displayed_name: Légifrance
index_name: legifrance-xml
language: fr-FR
semantic_configuration: default
service_name: samples-v2
top_k: 10
usage: Useful when you need legal content contained in one of the codes (incl. civil, commercial, labor, electoral) derived from French legislation. Use this in priority for France legal related data.
- api_key: XXX
displayed_name: Open Food Facts
index_name: openfoodfacts-json-v2
language: en-US
semantic_configuration: default
service_name: samples-v2
top_k: 10
usage: Useful when you need details about a food product (incl. nutrient levels, allergens, additives, stores, generic name). Use this in priority food related data.
```
Now, you can either run the application as container or with live reload. For development, it is recommended to use live reload. For demo, it is recommended to use the container.
Expand Down
2 changes: 1 addition & 1 deletion cicd/helm/private-gpt/Chart.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ dependencies:
repository: oci://registry-1.docker.io/bitnamicharts
version: 18.0.4
digest: sha256:d2b04f80cb92a439086247637917406f435e287a10c81d4ff421a5aa66c4c44b
generated: "2023-11-21T13:21:45.838475+01:00"
generated: "2023-11-28T18:30:15.805929+01:00"
3 changes: 1 addition & 2 deletions cicd/helm/private-gpt/templates/conversation-api-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ metadata:
{{- include "private-gpt.labels" . | nindent 4 }}
app.kubernetes.io/component: conversation-api
data:
config.toml: |
config.yaml: |
[oidc]
algorithms = ["{{ join "\",\"" .Values.oidc.algorithms | required "A value for .Values.oidc.algorithms is required" }}"]
api_audience = {{ .Values.oidc.api_audience | quote | required "A value for .Values.oidc.api_audience is required" }}
Expand Down Expand Up @@ -85,7 +85,6 @@ data:
# Tool {{ .displayed_name | required "A value for .displayed_name is required" }}
[[tools.azure_cognitive_search]]
api_key = {{ .api_key | quote | required "A value for .api_key is required" }}
content_key = {{ .content_key | quote | required "A value for .content_key is required" }}
displayed_name = {{ .displayed_name | quote | required "A value for .displayed_name is required" }}
index_name = {{ .index_name | quote | required "A value for .index_name is required" }}
service_name = {{ .service_name | quote | required "A value for .service_name is required" }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ spec:
port: http
volumeMounts:
- name: config
mountPath: /app/config.toml
subPath: config.toml
mountPath: /app/config.yaml
subPath: config.yaml
- name: tmp
mountPath: /tmp
env:
Expand Down
1 change: 0 additions & 1 deletion cicd/helm/private-gpt/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ tools:
azure_cognitive_search:
[]
# - api_key: xxx
# content_key: content
# displayed_name: Légifrance
# index_name: legifrance-xml
# service_name: samples-v2
Expand Down
3 changes: 2 additions & 1 deletion src/conversation-api/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ start:
--no-server-header \
--port 8081 \
--proxy-headers \
--reload
--reload \


build:
$(docker) build \
Expand Down
27 changes: 5 additions & 22 deletions src/conversation-api/ai/contentsafety.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,21 @@
# Import utils
from utils import build_logger, get_config

# Import misc
from azure.core.credentials import AzureKeyCredential
from fastapi import HTTPException, status
from helpers.config import CONFIG
from helpers.logging import build_logger
from tenacity import retry, stop_after_attempt, wait_random_exponential
import azure.ai.contentsafety as azure_cs
import azure.core.exceptions as azure_exceptions


###
# Init misc
###

_logger = build_logger(__name__)

###
# Init Azure Content Safety
###

# Score are following: 0 - Safe, 2 - Low, 4 - Medium, 6 - High
# See: https://review.learn.microsoft.com/en-us/azure/cognitive-services/content-safety/concepts/harm-categories?branch=release-build-content-safety#severity-levels
ACS_SEVERITY_THRESHOLD = 2
ACS_API_BASE = get_config(
["ai", "azure_content_safety"], "api_base", str, required=True
)
ACS_API_TOKEN = get_config(
["ai", "azure_content_safety"], "api_token", str, required=True
)
ACS_MAX_LENGTH = get_config(
["ai", "azure_content_safety"], "max_length", int, required=True
)
acs_client = azure_cs.ContentSafetyClient(
ACS_API_BASE, AzureKeyCredential(ACS_API_TOKEN)
credential=AzureKeyCredential(CONFIG.ai.azure_content_safety.api_token.get_secret_value()),
endpoint=CONFIG.ai.azure_content_safety.api_base,
)


Expand All @@ -45,7 +28,7 @@ class ContentSafety:
async def is_moderated(self, prompt: str) -> bool:
_logger.debug(f"Checking moderation for text: {prompt}")

if len(prompt) > ACS_MAX_LENGTH:
if len(prompt) > CONFIG.ai.azure_content_safety.max_length:
_logger.info(f"Message ({len(prompt)}) too long for moderation")
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
Expand Down
Loading

0 comments on commit 32e4417

Please sign in to comment.