Skip to content

Commit

Permalink
bug(api): Fixes tools issue with modify asssistant (#682)
Browse files Browse the repository at this point in the history
* Fixes issue with modify assistant where the default values were defaults were not being correctly selected
  • Loading branch information
CollectiveUnicorn authored Jun 21, 2024
1 parent 518e5fc commit efc704e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
7 changes: 7 additions & 0 deletions src/leapfrogai_api/backend/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
43 changes: 22 additions & 21 deletions src/leapfrogai_api/routers/openai/assistants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit efc704e

Please sign in to comment.