Skip to content

Commit

Permalink
Merge tag 'v0.5.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
clemlesne committed Jan 12, 2024
2 parents 504c764 + 0ad1a72 commit 8fcab96
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 33 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/pipeline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ jobs:

- name: Container meta
id: meta
uses: docker/metadata-action@v5.0.0
uses: docker/metadata-action@v5.5.0
with:
images: ${{ env.CONTAINER_REGISTRY_GHCR }}/${{ env.CONTAINER_NAME }}
tags: |
Expand All @@ -125,7 +125,7 @@ jobs:
echo "tag=$tag" >> $GITHUB_OUTPUT
- name: Build/push container
uses: docker/build-push-action@v5.0.0
uses: docker/build-push-action@v5.1.0
with:
build-args: |
VERSION=${{ needs.init.outputs.VERSION_FULL }}
Expand Down Expand Up @@ -159,7 +159,7 @@ jobs:
run: semgrep ci --sarif --output=semgrep.sarif

- name: Upload results to GitHub Security
uses: github/codeql-action/upload-sarif@v2.22.8
uses: github/codeql-action/upload-sarif@v3.23.0
with:
sarif_file: semgrep.sarif

Expand Down
2 changes: 1 addition & 1 deletion .python-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
claimaiphonebot311
claimaiphonebot312
12 changes: 5 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
# Base container
FROM docker.io/library/python:3.11-slim-bullseye@sha256:9f35f3a6420693c209c11bba63dcf103d88e47ebe0b205336b5168c122967edf AS base

RUN rm -f /etc/apt/apt.conf.d/docker-clean \
&& echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache
RUN --mount=target=/var/lib/apt/lists,type=cache,sharing=locked \
apt-get update -q
FROM docker.io/library/python:3.12-slim-bookworm@sha256:448eb6cade8c9edfa66b0840829744a10a0131f6e9ef95052913170c8291a348 AS base

# Build container
FROM base AS build

RUN rm -f /etc/apt/apt.conf.d/docker-clean \
&& echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache
RUN --mount=target=/var/lib/apt/lists,type=cache,sharing=locked --mount=target=/root/.cache/pip,type=cache,sharing=locked \
apt-get install -y -q --no-install-recommends \
apt-get update -q \
&& apt-get install -y -q --no-install-recommends \
gcc \
python3-dev \
&& python3 -m pip install --upgrade \
Expand Down
2 changes: 1 addition & 1 deletion helpers/config_models/monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


class LoggingLevel(str, Enum):
# Copied from https://docs.python.org/3.11/library/logging.html#logging-levels
# Copied from https://docs.python.org/3.12/library/logging.html#logging-levels
CRITICAL = "CRITICAL"
DEBUG = "DEBUG"
ERROR = "ERROR"
Expand Down
4 changes: 2 additions & 2 deletions helpers/config_models/openai.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pydantic import BaseModel, HttpUrl
from pydantic import BaseModel


class OpenAiModel(BaseModel):
endpoint: HttpUrl
endpoint: str
gpt_deployment: str
gpt_model: str
52 changes: 34 additions & 18 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
from contextlib import asynccontextmanager
from datetime import datetime
from enum import Enum
from fastapi import FastAPI, status, Request
from fastapi import FastAPI, status, Request, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse, Response
from fastapi.responses import JSONResponse
from helpers.config import CONFIG
from helpers.logging import build_logger
from helpers.prompts import LLM as LLMPrompt, TTS as TTSPrompt
Expand Down Expand Up @@ -71,7 +71,9 @@
CONFIG.communication_service.access_key.get_secret_value()
),
)
sms_client = SmsClient(credential=AZ_CREDENTIAL, endpoint=CONFIG.communication_service.endpoint)
sms_client = SmsClient(
credential=AZ_CREDENTIAL, endpoint=CONFIG.communication_service.endpoint
)
db = sqlite3.connect(".local.sqlite", check_same_thread=False)

EVENTS_DOMAIN = environ.get("EVENTS_DOMAIN").strip("/")
Expand Down Expand Up @@ -170,10 +172,14 @@ def eventgrid_unregister() -> None:
description="Liveness healthckeck, always returns 204, used to check if the API is up.",
)
async def health_liveness_get() -> None:
return None
pass


@api.get("/call/initiate", description="Initiate an outbound call to a phone number.")
@api.get(
"/call/initiate",
status_code=status.HTTP_204_NO_CONTENT,
description="Initiate an outbound call to a phone number.",
)
def call_initiate_get(phone_number: str) -> None:
_logger.info(f"Initiating outbound call to {phone_number}")
call_connection_properties = call_automation_client.create_call(
Expand All @@ -185,7 +191,6 @@ def call_initiate_get(phone_number: str) -> None:
_logger.info(
f"Created call with connection id: {call_connection_properties.call_connection_id}"
)
return Response(status_code=status.HTTP_204_NO_CONTENT)


@api.post(
Expand All @@ -204,7 +209,7 @@ async def call_inbound_post(request: Request):
_logger.info(f"Validating Event Grid subscription ({validation_code})")
return JSONResponse(
content={"validationResponse": event.data["validationCode"]},
status_code=200,
status_code=status.HTTP_200_OK,
)

elif event_type == SystemEventNames.AcsIncomingCallEventName:
Expand Down Expand Up @@ -234,14 +239,19 @@ async def call_inbound_post(request: Request):
# See: https://github.com/MicrosoftDocs/azure-docs/blob/main/articles/communication-services/how-tos/call-automation/secure-webhook-endpoint.md
async def call_event_post(request: Request, call_id: UUID) -> None:
for event_dict in await request.json():
event = CloudEvent.from_dict(event_dict)
call = get_call_by_id(call_id)
if not call:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Call {call_id} not found",
)

event = CloudEvent.from_dict(event_dict)
connection_id = event.data["callConnectionId"]
operation_context = event.data.get("operationContext", None)
client = call_automation_client.get_call_connection(
call_connection_id=connection_id
)
call = get_call_by_id(call_id)
event_type = event.type

_logger.debug(f"Call event received {event_type} for call {call}")
Expand Down Expand Up @@ -376,7 +386,7 @@ async def call_event_post(request: Request, call_id: UUID) -> None:
elif (
event_type == "Microsoft.Communication.CallTransferFailed"
): # Call transfer failed
_logger.debig(f"Call transfer failed event ({call.id})")
_logger.debug(f"Call transfer failed event ({call.id})")
result_information = event.data["resultInformation"]
sub_code = result_information["subCode"]
_logger.info(f"Error during call transfer, subCode {sub_code} ({call.id})")
Expand All @@ -390,9 +400,7 @@ async def call_event_post(request: Request, call_id: UUID) -> None:
save_call(call)


async def intelligence(
call: CallModel, client: CallConnectionClient
) -> None:
async def intelligence(call: CallModel, client: CallConnectionClient) -> None:
chat_res = await gpt_chat(call)
_logger.info(f"Chat ({call.id}): {chat_res}")

Expand All @@ -412,7 +420,11 @@ async def intelligence(
text=TTSPrompt.GOODBYE,
)

elif chat_res.intent in (IndentAction.NEW_CLAIM, IndentAction.UPDATED_CLAIM, IndentAction.NEW_OR_UPDATED_REMINDER):
elif chat_res.intent in (
IndentAction.NEW_CLAIM,
IndentAction.UPDATED_CLAIM,
IndentAction.NEW_OR_UPDATED_REMINDER,
):
await handle_play(
call=call,
client=client,
Expand Down Expand Up @@ -877,10 +889,14 @@ async def handle_hangup(client: CallConnectionClient, call: CallModel) -> None:
)
response = responses[0]

if (response.successful):
_logger.info(f"SMS report sent {response.message_id} to {response.to} ({call.id})")
if response.successful:
_logger.info(
f"SMS report sent {response.message_id} to {response.to} ({call.id})"
)
else:
_logger.warn(f"Failed SMS to {response.to}, status {response.http_status_code}, error {response.error_message} ({call.id})")
_logger.warn(
f"Failed SMS to {response.to}, status {response.http_status_code}, error {response.error_message} ({call.id})"
)

except Exception:
_logger.warn(f"SMS error ({call.id})", exc_info=True)
Expand Down Expand Up @@ -932,7 +948,7 @@ def save_call(call: CallModel):
db.commit()


def get_call_by_id(call_id: UUID) -> CallModel:
def get_call_by_id(call_id: UUID) -> Optional[CallModel]:
cursor = db.execute(
"SELECT data FROM calls WHERE id = ?",
(call_id.hex,),
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ azure-eventgrid==4.16.0
azure-identity==1.15.0
azure-mgmt-eventgrid==10.2.0
fastapi==0.108.0
openai==1.7.0
openai==1.7.1
phonenumbers==8.13.27
pydantic-extra-types==2.4.0
python-dotenv==1.0.0
Expand Down

0 comments on commit 8fcab96

Please sign in to comment.