Skip to content

Commit d4beb22

Browse files
benshukbenshuk
andauthored
feat: ✨ add support for Run resource (#236)
* feat: ✨ add support for Run resource * fix: 🏷️ typing backwards compatability * refactor: 🚚 rename classes & types and such * refactor: 🔥 remove `avatar` option * fix: 🏷️ typing yay * feat: ✨ add examples * fix: 🏷️ make `description` optional * docs: 📝 add assistants info to README also, add examples * test: ✅ add assistants to tests * docs: 📝 update docs about Assistants * docs: 📝 better example * chore: 🚚 move files * docs: 📝 examples fixes --------- Co-authored-by: benshuk <bens@ai21.com>
1 parent abcc2f9 commit d4beb22

File tree

21 files changed

+424
-66
lines changed

21 files changed

+424
-66
lines changed

README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
- [Installation](#Installation) 💿
2727
- [Usage - Chat Completions](#Usage)
2828
- [Conversational RAG (Beta)](#Conversational-RAG-Beta)
29+
- [Assistants (Beta)](#Assistants-Beta)
2930
- [Older Models Support Usage](#Older-Models-Support-Usage)
3031
- [More Models](#More-Models)
3132
- [Streaming](#Streaming)
@@ -388,6 +389,41 @@ For a more detailed example, see the chat [sync](examples/studio/conversational_
388389

389390
---
390391

392+
### Assistants (Beta)
393+
394+
Create assistants to help you with your tasks.
395+
396+
```python
397+
from time import sleep
398+
from ai21 import AI21Client
399+
from ai21.models.assistant.message import Message
400+
401+
messages = [
402+
Message(content={"type": "text", "text": "Youre message here"}, role="user"),
403+
]
404+
405+
client = AI21Client()
406+
407+
assistant = client.beta.assistants.create(name="My assistant")
408+
thread = client.beta.threads.create(messages=messages)
409+
run = client.beta.threads.runs.create(thread_id=thread.id, assistant_id=assistant.id)
410+
411+
while run.status == "in_progress":
412+
run = client.beta.threads.runs.get(thread_id=thread.id, run_id=run.id)
413+
sleep(1)
414+
415+
if run.status == "completed":
416+
messages = client.beta.threads.messages.list(thread_id=thread.id)
417+
print(messages)
418+
else:
419+
# handle error or required action
420+
pass
421+
```
422+
423+
For a more detailed example, see [sync](examples/studio/assistant/assistant.py) and [async](examples/studio/assistant/async_assistant.py) examples.
424+
425+
---
426+
391427
### File Upload
392428

393429
```python
File renamed without changes.

ai21/clients/common/assistant/assistants.py renamed to ai21/clients/common/beta/assistant/assistants.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typing import Any, Dict, List
55

66
from ai21.models.assistant.assistant import Optimization, Tool, ToolResources
7-
from ai21.models.responses.assistant_response import Assistant, ListAssistant
7+
from ai21.models.responses.assistant_response import AssistantResponse, ListAssistant
88
from ai21.types import NotGiven, NOT_GIVEN
99
from ai21.utils.typing import remove_not_given
1010

@@ -19,12 +19,11 @@ def create(
1919
*,
2020
description: str | NotGiven = NOT_GIVEN,
2121
optimization: Optimization | NotGiven = NOT_GIVEN,
22-
avatar: str | NotGiven = NOT_GIVEN,
2322
models: List[str] | NotGiven = NOT_GIVEN,
2423
tools: List[Tool] | NotGiven = NOT_GIVEN,
2524
tool_resources: ToolResources | NotGiven = NOT_GIVEN,
2625
**kwargs,
27-
) -> Assistant:
26+
) -> AssistantResponse:
2827
pass
2928

3029
def _create_body(
@@ -33,7 +32,6 @@ def _create_body(
3332
name: str,
3433
description: str | NotGiven,
3534
optimization: str | NotGiven,
36-
avatar: str | NotGiven,
3735
models: List[str] | NotGiven,
3836
tools: List[str] | NotGiven,
3937
tool_resources: dict | NotGiven,
@@ -44,7 +42,6 @@ def _create_body(
4442
"name": name,
4543
"description": description,
4644
"optimization": optimization,
47-
"avatar": avatar,
4845
"models": models,
4946
"tools": tools,
5047
"tool_resources": tool_resources,
@@ -57,7 +54,7 @@ def list(self) -> ListAssistant:
5754
pass
5855

5956
@abstractmethod
60-
def retrieve(self, assistant_id: str) -> Assistant:
57+
def retrieve(self, assistant_id: str) -> AssistantResponse:
6158
pass
6259

6360
@abstractmethod
@@ -68,10 +65,9 @@ def modify(
6865
name: str | NotGiven = NOT_GIVEN,
6966
description: str | NotGiven = NOT_GIVEN,
7067
optimization: Optimization | NotGiven = NOT_GIVEN,
71-
avatar: str | NotGiven = NOT_GIVEN,
7268
is_archived: bool | NotGiven = NOT_GIVEN,
7369
models: List[str] | NotGiven = NOT_GIVEN,
7470
tools: List[Tool] | NotGiven = NOT_GIVEN,
7571
tool_resources: ToolResources | NotGiven = NOT_GIVEN,
76-
) -> Assistant:
72+
) -> AssistantResponse:
7773
pass
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from __future__ import annotations
2+
3+
from abc import ABC, abstractmethod
4+
from typing import List
5+
6+
from ai21.models.assistant.assistant import Optimization
7+
from ai21.models.assistant.run import ToolOutput
8+
from ai21.models.responses.run_response import RunResponse
9+
from ai21.types import NOT_GIVEN, NotGiven
10+
from ai21.utils.typing import remove_not_given
11+
12+
13+
class Runs(ABC):
14+
_module_name = "runs"
15+
16+
@abstractmethod
17+
def create(
18+
self,
19+
*,
20+
thread_id: str,
21+
assistant_id: str,
22+
description: str | NotGiven = NOT_GIVEN,
23+
optimization: Optimization | NotGiven = NOT_GIVEN,
24+
**kwargs,
25+
) -> RunResponse:
26+
pass
27+
28+
def _create_body(
29+
self,
30+
*,
31+
thread_id: str,
32+
assistant_id: str,
33+
description: str | NotGiven,
34+
optimization: str | NotGiven,
35+
**kwargs,
36+
) -> dict:
37+
return remove_not_given(
38+
{
39+
"thread_id": thread_id,
40+
"assistant_id": assistant_id,
41+
"description": description,
42+
"optimization": optimization,
43+
**kwargs,
44+
}
45+
)
46+
47+
@abstractmethod
48+
def retrieve(
49+
self,
50+
*,
51+
thread_id: str,
52+
run_id: str,
53+
) -> RunResponse:
54+
pass
55+
56+
@abstractmethod
57+
def cancel(
58+
self,
59+
*,
60+
thread_id: str,
61+
run_id: str,
62+
) -> RunResponse:
63+
pass
64+
65+
@abstractmethod
66+
def submit_tool_outputs(self, *, thread_id: str, run_id: str, tool_outputs: List[ToolOutput]) -> RunResponse:
67+
pass

ai21/clients/common/assistant/threads.py renamed to ai21/clients/common/beta/assistant/threads.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
from abc import ABC, abstractmethod
44
from typing import List
55

6-
from ai21.clients.common.assistant.messages import Messages
6+
from ai21.clients.common.beta.assistant.messages import Messages
77
from ai21.models.assistant.message import Message
8-
from ai21.models.responses.thread_response import Thread
8+
from ai21.models.responses.thread_response import ThreadResponse
99

1010

1111
class Threads(ABC):
@@ -17,9 +17,9 @@ def create(
1717
self,
1818
messages: List[Message],
1919
**kwargs,
20-
) -> Thread:
20+
) -> ThreadResponse:
2121
pass
2222

2323
@abstractmethod
24-
def retrieve(self, thread_id: str) -> Thread:
24+
def retrieve(self, thread_id: str) -> ThreadResponse:
2525
pass

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

Whitespace-only changes.

ai21/clients/studio/resources/assistant/studio_assistant.py renamed to ai21/clients/studio/resources/beta/assistant/assistant.py

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,42 @@
22

33
from typing import List
44

5-
from ai21.clients.common.assistant.assistants import Assistants
5+
from ai21.clients.common.beta.assistant.assistants import Assistants
66
from ai21.clients.studio.resources.studio_resource import (
77
AsyncStudioResource,
88
StudioResource,
99
)
1010
from ai21.models.assistant.assistant import Tool, ToolResources
11-
from ai21.models.responses.assistant_response import Assistant, ListAssistant
11+
from ai21.models.responses.assistant_response import AssistantResponse, ListAssistant
1212
from ai21.types import NotGiven, NOT_GIVEN
1313

1414

15-
class StudioAssistant(StudioResource, Assistants):
15+
class Assistant(StudioResource, Assistants):
1616
def create(
1717
self,
1818
name: str,
1919
*,
2020
description: str | NotGiven = NOT_GIVEN,
2121
optimization: str | NotGiven = NOT_GIVEN,
22-
avatar: str | NotGiven = NOT_GIVEN,
2322
models: List[str] | NotGiven = NOT_GIVEN,
2423
tools: List[Tool] | NotGiven = NOT_GIVEN,
2524
tool_resources: ToolResources | NotGiven = NOT_GIVEN,
2625
**kwargs,
27-
) -> Assistant:
26+
) -> AssistantResponse:
2827
body = self._create_body(
2928
name=name,
3029
description=description,
3130
optimization=optimization,
32-
avatar=avatar,
3331
models=models,
3432
tools=tools,
3533
tool_resources=tool_resources,
3634
**kwargs,
3735
)
3836

39-
return self._post(path=f"/{self._module_name}", body=body, response_cls=Assistant)
37+
return self._post(path=f"/{self._module_name}", body=body, response_cls=AssistantResponse)
4038

41-
def retrieve(self, assistant_id: str) -> Assistant:
42-
return self._get(path=f"/{self._module_name}/{assistant_id}", response_cls=Assistant)
39+
def retrieve(self, assistant_id: str) -> AssistantResponse:
40+
return self._get(path=f"/{self._module_name}/{assistant_id}", response_cls=AssistantResponse)
4341

4442
def list(self) -> ListAssistant:
4543
return self._get(path=f"/{self._module_name}", response_cls=ListAssistant)
@@ -51,54 +49,50 @@ def modify(
5149
name: str | NotGiven = NOT_GIVEN,
5250
description: str | NotGiven = NOT_GIVEN,
5351
optimization: str | NotGiven = NOT_GIVEN,
54-
avatar: str | NotGiven = NOT_GIVEN,
5552
is_archived: bool | NotGiven = NOT_GIVEN,
5653
models: List[str] | NotGiven = NOT_GIVEN,
5754
tools: List[Tool] | NotGiven = NOT_GIVEN,
5855
tool_resources: ToolResources | NotGiven = NOT_GIVEN,
59-
) -> Assistant:
56+
) -> AssistantResponse:
6057
body = self._create_body(
6158
name=name,
6259
description=description,
6360
optimization=optimization,
64-
avatar=avatar,
6561
is_archived=is_archived,
6662
models=models,
6763
tools=tools,
6864
tool_resources=tool_resources,
6965
)
7066

71-
return self._patch(path=f"/{self._module_name}/{assistant_id}", body=body, response_cls=Assistant)
67+
return self._patch(path=f"/{self._module_name}/{assistant_id}", body=body, response_cls=AssistantResponse)
7268

7369

74-
class AsyncStudioAssistant(AsyncStudioResource, Assistants):
70+
class AsyncAssistant(AsyncStudioResource, Assistants):
7571
async def create(
7672
self,
7773
name: str,
7874
*,
7975
description: str | NotGiven = NOT_GIVEN,
8076
optimization: str | NotGiven = NOT_GIVEN,
81-
avatar: str | NotGiven = NOT_GIVEN,
8277
models: List[str] | NotGiven = NOT_GIVEN,
8378
tools: List[Tool] | NotGiven = NOT_GIVEN,
8479
tool_resources: ToolResources | NotGiven = NOT_GIVEN,
8580
**kwargs,
86-
) -> Assistant:
81+
) -> AssistantResponse:
8782
body = self._create_body(
8883
name=name,
8984
description=description,
9085
optimization=optimization,
91-
avatar=avatar,
9286
models=models,
9387
tools=tools,
9488
tool_resources=tool_resources,
9589
**kwargs,
9690
)
9791

98-
return await self._post(path=f"/{self._module_name}", body=body, response_cls=Assistant)
92+
return await self._post(path=f"/{self._module_name}", body=body, response_cls=AssistantResponse)
9993

100-
async def retrieve(self, assistant_id: str) -> Assistant:
101-
return await self._get(path=f"/{self._module_name}/{assistant_id}", response_cls=Assistant)
94+
async def retrieve(self, assistant_id: str) -> AssistantResponse:
95+
return await self._get(path=f"/{self._module_name}/{assistant_id}", response_cls=AssistantResponse)
10296

10397
async def list(self) -> ListAssistant:
10498
return await self._get(path=f"/{self._module_name}", response_cls=ListAssistant)
@@ -110,21 +104,19 @@ async def modify(
110104
name: str | NotGiven = NOT_GIVEN,
111105
description: str | NotGiven = NOT_GIVEN,
112106
optimization: str | NotGiven = NOT_GIVEN,
113-
avatar: str | NotGiven = NOT_GIVEN,
114107
is_archived: bool | NotGiven = NOT_GIVEN,
115108
models: List[str] | NotGiven = NOT_GIVEN,
116109
tools: List[Tool] | NotGiven = NOT_GIVEN,
117110
tool_resources: ToolResources | NotGiven = NOT_GIVEN,
118-
) -> Assistant:
111+
) -> AssistantResponse:
119112
body = self._create_body(
120113
name=name,
121114
description=description,
122115
optimization=optimization,
123-
avatar=avatar,
124116
is_archived=is_archived,
125117
models=models,
126118
tools=tools,
127119
tool_resources=tool_resources,
128120
)
129121

130-
return await self._patch(path=f"/{self._module_name}/{assistant_id}", body=body, response_cls=Assistant)
122+
return await self._patch(path=f"/{self._module_name}/{assistant_id}", body=body, response_cls=AssistantResponse)

0 commit comments

Comments
 (0)