Skip to content

Send a phone call from AI agent, in an API call. Or, directly call the bot from the configured phone number!

Python

perf: Enable Content Safety async moderation

Clémence Lesné30e506805c85
diff --git a/bicep/app.bicep b/bicep/app.bicep
index ae78a1e..8f63308 100644
--- a/bicep/app.bicep
+++ b/bicep/app.bicep
@@ -538,7 +538,7 @@ resource contentfilter 'Microsoft.CognitiveServices/accounts/raiPolicies@2024-04
tags: tags
properties: {
basePolicyName: 'Microsoft.Default'
- mode: 'Default'
+ mode: 'Deferred' // Async moderation
contentFilters: [
// Indirect attacks
{
diff --git a/helpers/llm_worker.py b/helpers/llm_worker.py
index c996872..99acc4d 100644
--- a/helpers/llm_worker.py
+++ b/helpers/llm_worker.py
@@ -188,19 +188,18 @@ async def _completion_stream_worker(
): # Skip empty choices, happens sometimes with GPT-4 Turbo
continue
choice = choices[0]
+ delta = choice.delta
if (
choice.finish_reason == "content_filter"
): # Azure OpenAI content filter
- raise SafetyCheckError(
- f"Issue detected in text: {choice.delta.content}"
- )
+ raise SafetyCheckError(f"Issue detected in text: {delta.content}")
if choice.finish_reason == "length":
logger.warning(
f"Maximum tokens reached {max_tokens}, should be fixed"
)
maximum_tokens_reached = True
- delta = choice.delta
- yield delta
+ if delta:
+ yield delta
else: # Non-streaming, emulate streaming with a single completion
completion: ChatCompletion = await client.chat.completions.create(