Skip to content

Commit 62566cd

Browse files
author
benshuk
committed
feat: ✨ add support for Thread resource
1 parent 2cf1a85 commit 62566cd

File tree

7 files changed

+106
-4
lines changed

7 files changed

+106
-4
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from __future__ import annotations
2+
3+
from abc import ABC, abstractmethod
4+
from typing import List
5+
6+
from ai21.models.responses.thread_response import ThreadResponse, CreateMessagePayload
7+
8+
9+
class Thread(ABC):
10+
_module_name = "threads"
11+
12+
@abstractmethod
13+
def create(
14+
self,
15+
messages: List[CreateMessagePayload],
16+
**kwargs,
17+
) -> ThreadResponse:
18+
pass
19+
20+
@abstractmethod
21+
def get(self, thread_id: str) -> ThreadResponse:
22+
pass

ai21/clients/studio/resources/assistant/studio_assistant.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ async def create(
9999
**kwargs,
100100
)
101101

102-
return self._post(path=f"/{self._module_name}", body=body, response_cls=AssistantResponse)
102+
return await self._post(path=f"/{self._module_name}", body=body, response_cls=AssistantResponse)
103103

104104
async def get(self, assistant_id: str) -> AssistantResponse:
105105
return await self._get(path=f"/{self._module_name}/{assistant_id}", response_cls=AssistantResponse)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from __future__ import annotations
2+
3+
from typing import List
4+
5+
from ai21.clients.common.assistant.thread import Thread
6+
from ai21.clients.studio.resources.studio_resource import StudioResource, AsyncStudioResource
7+
from ai21.models.responses.thread_response import CreateMessagePayload, ThreadResponse
8+
9+
10+
class StudioThread(StudioResource, Thread):
11+
def create(
12+
self,
13+
messages: List[CreateMessagePayload],
14+
**kwargs,
15+
) -> ThreadResponse:
16+
body = dict(messages=messages)
17+
18+
return self._post(path=f"/{self._module_name}", body=body, response_cls=ThreadResponse)
19+
20+
def get(self, thread_id: str) -> ThreadResponse:
21+
return self._get(path=f"/{self._module_name}/{thread_id}", response_cls=ThreadResponse)
22+
23+
24+
class AsyncStudioThread(AsyncStudioResource, Thread):
25+
async def create(
26+
self,
27+
messages: List[CreateMessagePayload],
28+
**kwargs,
29+
) -> ThreadResponse:
30+
body = dict(messages=messages)
31+
32+
return await self._post(path=f"/{self._module_name}", body=body, response_cls=ThreadResponse)
33+
34+
async def get(self, thread_id: str) -> ThreadResponse:
35+
return await self._get(path=f"/{self._module_name}/{thread_id}", response_cls=ThreadResponse)

ai21/clients/studio/resources/beta/async_beta.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from ai21.clients.studio.resources.assistant.studio_assistant import AsyncStudioAssistant
2+
from ai21.clients.studio.resources.assistant.studio_thread import AsyncStudioThread
23
from ai21.clients.studio.resources.studio_conversational_rag import AsyncStudioConversationalRag
34
from ai21.clients.studio.resources.studio_resource import AsyncStudioResource
45
from ai21.http_client.async_http_client import AsyncAI21HTTPClient
@@ -8,5 +9,6 @@ class AsyncBeta(AsyncStudioResource):
89
def __init__(self, client: AsyncAI21HTTPClient):
910
super().__init__(client)
1011

11-
self.conversational_rag = AsyncStudioConversationalRag(client)
1212
self.assistants = AsyncStudioAssistant(client)
13+
self.conversational_rag = AsyncStudioConversationalRag(client)
14+
self.threads = AsyncStudioThread(client)

ai21/clients/studio/resources/beta/beta.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from ai21.clients.studio.resources.assistant.studio_assistant import StudioAssistant
2+
from ai21.clients.studio.resources.assistant.studio_thread import StudioThread
23
from ai21.clients.studio.resources.studio_conversational_rag import StudioConversationalRag
34
from ai21.clients.studio.resources.studio_resource import StudioResource
45
from ai21.http_client.http_client import AI21HTTPClient
@@ -8,5 +9,6 @@ class Beta(StudioResource):
89
def __init__(self, client: AI21HTTPClient):
910
super().__init__(client)
1011

11-
self.conversational_rag = StudioConversationalRag(client)
1212
self.assistants = StudioAssistant(client)
13+
self.conversational_rag = StudioConversationalRag(client)
14+
self.threads = StudioThread(client)

ai21/models/responses/assistant_response.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class AssistantResponse(AI21BaseModel):
2020
id: str
2121
created_at: datetime
2222
updated_at: datetime
23-
object: str
23+
object: Literal["assistant"] = "assistant"
2424
name: str
2525
description: Optional[str] = None
2626
optimization: str
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from datetime import datetime
2+
from typing import Optional, List, Literal
3+
4+
from typing_extensions import TypedDict
5+
6+
from ai21.models.ai21_base_model import AI21BaseModel
7+
8+
9+
MessageRole = Literal["assistant", "user"]
10+
11+
12+
class MessageContentText(TypedDict):
13+
type: Literal["text"]
14+
text: str
15+
16+
17+
class CreateMessagePayload(TypedDict):
18+
role: MessageRole
19+
content: MessageContentText
20+
21+
22+
class ThreadMessageResponse(AI21BaseModel):
23+
id: str
24+
created_at: datetime
25+
updated_at: datetime
26+
object: Literal["message"] = "message"
27+
role: MessageRole
28+
content: MessageContentText
29+
run_id: Optional[str] = None
30+
assistant_id: Optional[str] = None
31+
32+
33+
class ThreadResponse(AI21BaseModel):
34+
id: str
35+
created_at: datetime
36+
updated_at: datetime
37+
object: Literal["thread"] = "thread"
38+
39+
40+
class ListThreadResponse(AI21BaseModel):
41+
results: List[ThreadResponse]

0 commit comments

Comments
 (0)