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

chore: ♻️ thread messages should be optional #259

Merged
merged 1 commit into from
Jan 20, 2025
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
16 changes: 13 additions & 3 deletions ai21/clients/common/beta/assistant/threads.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from __future__ import annotations

from abc import ABC, abstractmethod
from typing import List
from typing import List, Optional

from ai21.clients.common.beta.assistant.messages import BaseMessages
from ai21.models.assistant.message import Message
from ai21.models.assistant.message import Message, modify_message_content
from ai21.models.responses.thread_response import ThreadResponse
from ai21.types import NOT_GIVEN, NotGiven
from ai21.utils.typing import remove_not_given


class BaseThreads(ABC):
Expand All @@ -15,11 +17,19 @@ class BaseThreads(ABC):
@abstractmethod
def create(
self,
messages: List[Message],
messages: List[Message] | NotGiven = NOT_GIVEN,
**kwargs,
) -> ThreadResponse:
pass

def _create_body(self, messages: List[Message] | NotGiven, **kwargs) -> Optional[dict]:
body = remove_not_given({"messages": messages, **kwargs})

if "messages" in body:
body["messages"] = [modify_message_content(message) for message in body["messages"]]

return body

@abstractmethod
def retrieve(self, thread_id: str) -> ThreadResponse:
pass
11 changes: 6 additions & 5 deletions ai21/clients/studio/resources/beta/assistant/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
from ai21.clients.studio.resources.studio_resource import StudioResource, AsyncStudioResource
from ai21.http_client.async_http_client import AsyncAI21HTTPClient
from ai21.http_client.http_client import AI21HTTPClient
from ai21.models.assistant.message import Message, modify_message_content
from ai21.models.assistant.message import Message
from ai21.models.responses.thread_response import ThreadResponse
from ai21.types import NOT_GIVEN, NotGiven


class Threads(StudioResource, BaseThreads):
Expand All @@ -21,10 +22,10 @@ def __init__(self, client: AI21HTTPClient):

def create(
self,
messages: List[Message],
messages: List[Message] | NotGiven = NOT_GIVEN,
**kwargs,
) -> ThreadResponse:
body = dict(messages=[modify_message_content(message) for message in messages])
body = self._create_body(messages=messages, **kwargs)

return self._post(path=f"/{self._module_name}", body=body, response_cls=ThreadResponse)

Expand All @@ -41,10 +42,10 @@ def __init__(self, client: AsyncAI21HTTPClient):

async def create(
self,
messages: List[Message],
messages: List[Message] | NotGiven = NOT_GIVEN,
**kwargs,
) -> ThreadResponse:
body = dict(messages=[modify_message_content(message) for message in messages])
body = self._create_body(messages=messages, **kwargs)

return await self._post(path=f"/{self._module_name}", body=body, response_cls=ThreadResponse)

Expand Down
Loading