Skip to content

Commit

Permalink
Restore V1 functionality, add V2 endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
monst committed Oct 12, 2023
1 parent c7c3cad commit 394de2e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
18 changes: 18 additions & 0 deletions app/models/dtos.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ class ContentType(str, Enum):
TEXT = "text"


# V1 API only
class Content(BaseModel):
text_content: str = Field(..., alias="textContent")
type: ContentType


class SendMessageRequest(BaseModel):
class Template(BaseModel):
id: int
Expand All @@ -23,7 +29,19 @@ class Template(BaseModel):
parameters: dict


# V1 API only
class SendMessageResponse(BaseModel):
class Message(BaseModel):
sent_at: datetime = Field(
alias="sentAt", default_factory=datetime.utcnow
)
content: list[Content]

used_model: str = Field(..., alias="usedModel")
message: Message


class SendMessageResponseV2(BaseModel):
used_model: str = Field(..., alias="usedModel")
sent_at: datetime = Field(alias="sentAt", default_factory=datetime.utcnow)
content: dict
Expand Down
50 changes: 43 additions & 7 deletions app/routes/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@
InvalidModelException,
)
from app.dependencies import TokenPermissionsValidator
from app.models.dtos import SendMessageRequest, SendMessageResponse
from app.models.dtos import (
SendMessageRequest,
SendMessageResponse,
Content,
ContentType,
SendMessageResponseV2,
)
from app.services.circuit_breaker import CircuitBreaker
from app.services.guidance_wrapper import GuidanceWrapper
from app.config import settings

router = APIRouter(tags=["messages"])


@router.post(
"/api/v1/messages", dependencies=[Depends(TokenPermissionsValidator())]
)
def send_message(body: SendMessageRequest) -> SendMessageResponse:
def execute_call(body: SendMessageRequest) -> dict:
try:
model = settings.pyris.llms[body.preferred_model]
except ValueError as e:
Expand All @@ -35,7 +38,7 @@ def send_message(body: SendMessageRequest) -> SendMessageResponse:
)

try:
content = CircuitBreaker.protected_call(
return CircuitBreaker.protected_call(
func=guidance.query,
cache_key=body.preferred_model,
accepted_exceptions=(
Expand All @@ -52,8 +55,41 @@ def send_message(body: SendMessageRequest) -> SendMessageResponse:
except Exception as e:
raise InternalServerException(str(e))


@router.post(
"/api/v1/messages", dependencies=[Depends(TokenPermissionsValidator())]
)
def send_message(body: SendMessageRequest) -> SendMessageResponse:
generated_vars = execute_call(body)

# Restore the old behavior of only returning the 'response' variable for the v1 API
if "response" not in generated_vars:
raise InternalServerException(
str(ValueError("The handlebars do not generate 'response'"))
)

return SendMessageResponse(
usedModel=body.preferred_model,
message=SendMessageResponse.Message(
sentAt=datetime.now(timezone.utc),
content=[
Content(
type=ContentType.TEXT,
textContent=generated_vars["response"],
)
],
),
)


@router.post(
"/api/v2/messages", dependencies=[Depends(TokenPermissionsValidator())]
)
def send_message_v2(body: SendMessageRequest) -> SendMessageResponseV2:
generated_vars = execute_call(body)

return SendMessageResponseV2(
usedModel=body.preferred_model,
sentAt=datetime.now(timezone.utc),
content=content,
content=generated_vars,
)

0 comments on commit 394de2e

Please sign in to comment.