From efc704e2813f94a0eb093ffa47f278f367850a84 Mon Sep 17 00:00:00 2001 From: Gato <115658935+CollectiveUnicorn@users.noreply.github.com> Date: Fri, 21 Jun 2024 15:28:15 -0700 Subject: [PATCH] bug(api): Fixes tools issue with modify asssistant (#682) * Fixes issue with modify assistant where the default values were defaults were not being correctly selected --- src/leapfrogai_api/backend/helpers.py | 7 +++ .../routers/openai/assistants.py | 43 ++++++++++--------- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/src/leapfrogai_api/backend/helpers.py b/src/leapfrogai_api/backend/helpers.py index 1ba448dff..d1677e07b 100644 --- a/src/leapfrogai_api/backend/helpers.py +++ b/src/leapfrogai_api/backend/helpers.py @@ -102,3 +102,10 @@ def read_chunks(file: BinaryIO, chunk_size: int) -> Iterator[lfai.AudioRequest]: if not chunk: break yield lfai.AudioRequest(chunk_data=chunk) + + +def object_or_default(obj: Any | None, _default: Any) -> Any: + if obj: + return obj + else: + return _default diff --git a/src/leapfrogai_api/routers/openai/assistants.py b/src/leapfrogai_api/routers/openai/assistants.py index b981116b7..f568efbe8 100644 --- a/src/leapfrogai_api/routers/openai/assistants.py +++ b/src/leapfrogai_api/routers/openai/assistants.py @@ -3,7 +3,9 @@ from fastapi import HTTPException, APIRouter, status from fastapi.security import HTTPBearer from openai.types.beta import Assistant, AssistantDeleted -from openai.types.beta.assistant import ToolResources, ToolResourcesCodeInterpreter +from openai.types.beta.assistant import ToolResourcesCodeInterpreter + +from leapfrogai_api.backend.helpers import object_or_default from leapfrogai_api.backend.types import ( CreateAssistantRequest, ListAssistantsResponse, @@ -166,30 +168,29 @@ async def modify_assistant( ) try: - new_tool_resources: ToolResources | None - - try: - new_tool_resources = ToolResources.model_validate( - getattr(request, "tool_resources", None) - ) - except Exception: - new_tool_resources = old_assistant.tool_resources - new_assistant = Assistant( id=assistant_id, created_at=old_assistant.created_at, - name=getattr(request, "name", old_assistant.name), - description=getattr(request, "description", old_assistant.description), - instructions=getattr(request, "instructions", old_assistant.instructions), - model=getattr(request, "model", old_assistant.model), + name=object_or_default(request.name, old_assistant.name), + description=object_or_default( + request.description, old_assistant.description + ), + instructions=object_or_default( + request.instructions, old_assistant.instructions + ), + model=object_or_default(request.model, old_assistant.model), object="assistant", - tools=getattr(request, "tools", old_assistant.tools), - tool_resources=new_tool_resources, - temperature=getattr(request, "temperature", old_assistant.temperature), - top_p=getattr(request, "top_p", old_assistant.top_p), - metadata=getattr(request, "metadata", old_assistant.metadata), - response_format=getattr( - request, "response_format", old_assistant.response_format + tools=object_or_default(request.tools, old_assistant.tools), + tool_resources=object_or_default( + request.tool_resources, old_assistant.tool_resources + ), + temperature=object_or_default( + request.temperature, old_assistant.temperature + ), + top_p=object_or_default(request.top_p, old_assistant.top_p), + metadata=object_or_default(request.metadata, old_assistant.metadata), + response_format=object_or_default( + request.response_format, old_assistant.response_format ), ) except Exception as exc: