Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing of response_format to Assistant constructor #957

Merged
merged 2 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/marvin/beta/assistants/assistants.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pathlib import Path
from typing import TYPE_CHECKING, Any, Callable, Optional, Union
from typing import TYPE_CHECKING, Any, Callable, Literal, Optional, Union

from openai import AsyncAssistantEventHandler
from prompt_toolkit import PromptSession
Expand All @@ -13,7 +13,7 @@
import marvin.utilities.tools
from marvin.beta.assistants.handlers import PrintHandler
from marvin.tools.assistants import AssistantTool
from marvin.types import Tool
from marvin.types import AssistantResponseFormat, Tool
from marvin.utilities.asyncio import (
ExposeSyncMethodsMixin,
expose_sync_method,
Expand Down Expand Up @@ -64,6 +64,7 @@ class Assistant(BaseModel, ExposeSyncMethodsMixin):
tools: list[Union[AssistantTool, Callable]] = []
tool_resources: dict[str, Any] = {}
metadata: dict[str, str] = {}
response_format: Optional[Union[Literal["auto"], AssistantResponseFormat]] = "auto"
# context level tracks nested assistant contexts
_context_level: int = PrivateAttr(0)

Expand Down Expand Up @@ -173,6 +174,7 @@ async def create_async(self, _auto_delete: bool = False):
"metadata",
"tool_resources",
"metadata",
"response_format",
}
),
tools=[tool.model_dump() for tool in self.get_tools()],
Expand Down
12 changes: 12 additions & 0 deletions src/marvin/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ class FunctionCall(MarvinType):
name: str


class AssistantResponseFormat(MarvinType):
type: Literal["json_object", "text"]


class JsonObjectAssistantResponseFormat(AssistantResponseFormat):
type: Literal["json_object"] = "json_object"


class TextAssistantResponseFormat(AssistantResponseFormat):
type: Literal["text"] = "text"


class ImageUrl(MarvinType):
url: str = Field(
description="URL of the image to be sent or a base64 encoded image."
Expand Down