-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #231 from AI21Labs/assistant
feat: ✨ add support for Assistant resource
- Loading branch information
Showing
9 changed files
with
269 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
from __future__ import annotations | ||
|
||
from abc import ABC, abstractmethod | ||
from typing import Any, Dict, List | ||
|
||
from ai21.models.responses.assistant_response import ( | ||
AssistantResponse, | ||
Optimization, | ||
ToolResources, | ||
Tool, | ||
ListAssistantResponse, | ||
) | ||
from ai21.types import NotGiven, NOT_GIVEN | ||
from ai21.utils.typing import remove_not_given | ||
|
||
|
||
class Assistant(ABC): | ||
_module_name = "assistants" | ||
|
||
@abstractmethod | ||
def create( | ||
self, | ||
name: str, | ||
*, | ||
description: str | NotGiven = NOT_GIVEN, | ||
optimization: Optimization | NotGiven = NOT_GIVEN, | ||
avatar: str | NotGiven = NOT_GIVEN, | ||
models: List[str] | NotGiven = NOT_GIVEN, | ||
tools: List[Tool] | NotGiven = NOT_GIVEN, | ||
tool_resources: ToolResources | NotGiven = NOT_GIVEN, | ||
**kwargs, | ||
) -> AssistantResponse: | ||
pass | ||
|
||
def _create_body( | ||
self, | ||
*, | ||
name: str, | ||
description: str | NotGiven, | ||
optimization: str | NotGiven, | ||
avatar: str | NotGiven, | ||
models: List[str] | NotGiven, | ||
tools: List[str] | NotGiven, | ||
tool_resources: dict | NotGiven, | ||
**kwargs, | ||
) -> Dict[str, Any]: | ||
return remove_not_given( | ||
{ | ||
"name": name, | ||
"description": description, | ||
"optimization": optimization, | ||
"avatar": avatar, | ||
"models": models, | ||
"tools": tools, | ||
"tool_resources": tool_resources, | ||
**kwargs, | ||
} | ||
) | ||
|
||
@abstractmethod | ||
def list(self) -> ListAssistantResponse: | ||
pass | ||
|
||
@abstractmethod | ||
def get(self, assistant_id: str) -> AssistantResponse: | ||
pass | ||
|
||
@abstractmethod | ||
def modify( | ||
self, | ||
assistant_id: str, | ||
*, | ||
name: str | NotGiven = NOT_GIVEN, | ||
description: str | NotGiven = NOT_GIVEN, | ||
optimization: Optimization | NotGiven = NOT_GIVEN, | ||
avatar: str | NotGiven = NOT_GIVEN, | ||
is_archived: bool | NotGiven = NOT_GIVEN, | ||
models: List[str] | NotGiven = NOT_GIVEN, | ||
tools: List[Tool] | NotGiven = NOT_GIVEN, | ||
tool_resources: ToolResources | NotGiven = NOT_GIVEN, | ||
) -> AssistantResponse: | ||
pass |
Empty file.
134 changes: 134 additions & 0 deletions
134
ai21/clients/studio/resources/assistant/studio_assistant.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
from __future__ import annotations | ||
|
||
from typing import List | ||
|
||
from ai21.clients.common.assistant.assistant import Assistant | ||
from ai21.clients.studio.resources.studio_resource import ( | ||
AsyncStudioResource, | ||
StudioResource, | ||
) | ||
from ai21.models.responses.assistant_response import ( | ||
AssistantResponse, | ||
Tool, | ||
ToolResources, | ||
ListAssistantResponse, | ||
) | ||
from ai21.types import NotGiven, NOT_GIVEN | ||
|
||
|
||
class StudioAssistant(StudioResource, Assistant): | ||
def create( | ||
self, | ||
name: str, | ||
*, | ||
description: str | NotGiven = NOT_GIVEN, | ||
optimization: str | NotGiven = NOT_GIVEN, | ||
avatar: str | NotGiven = NOT_GIVEN, | ||
models: List[str] | NotGiven = NOT_GIVEN, | ||
tools: List[Tool] | NotGiven = NOT_GIVEN, | ||
tool_resources: ToolResources | NotGiven = NOT_GIVEN, | ||
**kwargs, | ||
) -> AssistantResponse: | ||
body = self._create_body( | ||
name=name, | ||
description=description, | ||
optimization=optimization, | ||
avatar=avatar, | ||
models=models, | ||
tools=tools, | ||
tool_resources=tool_resources, | ||
**kwargs, | ||
) | ||
|
||
return self._post(path=f"/{self._module_name}", body=body, response_cls=AssistantResponse) | ||
|
||
def get(self, assistant_id: str) -> AssistantResponse: | ||
return self._get(path=f"/{self._module_name}/{assistant_id}", response_cls=AssistantResponse) | ||
|
||
def list(self) -> ListAssistantResponse: | ||
return self._get(path=f"/{self._module_name}", response_cls=ListAssistantResponse) | ||
|
||
def modify( | ||
self, | ||
assistant_id: str, | ||
*, | ||
name: str | NotGiven = NOT_GIVEN, | ||
description: str | NotGiven = NOT_GIVEN, | ||
optimization: str | NotGiven = NOT_GIVEN, | ||
avatar: str | NotGiven = NOT_GIVEN, | ||
is_archived: bool | NotGiven = NOT_GIVEN, | ||
models: List[str] | NotGiven = NOT_GIVEN, | ||
tools: List[Tool] | NotGiven = NOT_GIVEN, | ||
tool_resources: ToolResources | NotGiven = NOT_GIVEN, | ||
) -> AssistantResponse: | ||
body = self._create_body( | ||
name=name, | ||
description=description, | ||
optimization=optimization, | ||
avatar=avatar, | ||
is_archived=is_archived, | ||
models=models, | ||
tools=tools, | ||
tool_resources=tool_resources, | ||
) | ||
|
||
return self._patch(path=f"/{self._module_name}/{assistant_id}", body=body, response_cls=AssistantResponse) | ||
|
||
|
||
class AsyncStudioAssistant(AsyncStudioResource, Assistant): | ||
async def create( | ||
self, | ||
name: str, | ||
*, | ||
description: str | NotGiven = NOT_GIVEN, | ||
optimization: str | NotGiven = NOT_GIVEN, | ||
avatar: str | NotGiven = NOT_GIVEN, | ||
models: List[str] | NotGiven = NOT_GIVEN, | ||
tools: List[Tool] | NotGiven = NOT_GIVEN, | ||
tool_resources: ToolResources | NotGiven = NOT_GIVEN, | ||
**kwargs, | ||
) -> AssistantResponse: | ||
body = self._create_body( | ||
name=name, | ||
description=description, | ||
optimization=optimization, | ||
avatar=avatar, | ||
models=models, | ||
tools=tools, | ||
tool_resources=tool_resources, | ||
**kwargs, | ||
) | ||
|
||
return self._post(path=f"/{self._module_name}", body=body, response_cls=AssistantResponse) | ||
|
||
async def get(self, assistant_id: str) -> AssistantResponse: | ||
return await self._get(path=f"/{self._module_name}/{assistant_id}", response_cls=AssistantResponse) | ||
|
||
async def list(self) -> ListAssistantResponse: | ||
return await self._get(path=f"/{self._module_name}", response_cls=ListAssistantResponse) | ||
|
||
async def modify( | ||
self, | ||
assistant_id: str, | ||
*, | ||
name: str | NotGiven = NOT_GIVEN, | ||
description: str | NotGiven = NOT_GIVEN, | ||
optimization: str | NotGiven = NOT_GIVEN, | ||
avatar: str | NotGiven = NOT_GIVEN, | ||
is_archived: bool | NotGiven = NOT_GIVEN, | ||
models: List[str] | NotGiven = NOT_GIVEN, | ||
tools: List[Tool] | NotGiven = NOT_GIVEN, | ||
tool_resources: ToolResources | NotGiven = NOT_GIVEN, | ||
) -> AssistantResponse: | ||
body = self._create_body( | ||
name=name, | ||
description=description, | ||
optimization=optimization, | ||
avatar=avatar, | ||
is_archived=is_archived, | ||
models=models, | ||
tools=tools, | ||
tool_resources=tool_resources, | ||
) | ||
|
||
return await self._patch(path=f"/{self._module_name}/{assistant_id}", body=body, response_cls=AssistantResponse) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from datetime import datetime | ||
from typing import Optional, List, Literal | ||
|
||
from typing_extensions import TypedDict | ||
|
||
from ai21.models.ai21_base_model import AI21BaseModel | ||
|
||
|
||
Optimization = Literal["cost", "latency"] | ||
Tool = Literal["rag", "internet_research", "plan_approval"] | ||
|
||
|
||
class ToolResources(TypedDict, total=False): | ||
rag: Optional[dict] | ||
internet_research: Optional[dict] | ||
plan_approval: Optional[dict] | ||
|
||
|
||
class AssistantResponse(AI21BaseModel): | ||
id: str | ||
created_at: datetime | ||
updated_at: datetime | ||
object: str | ||
name: str | ||
description: Optional[str] = None | ||
optimization: str | ||
organization_id: str | ||
user_id: str | ||
avatar: Optional[str] = None | ||
is_archived: bool = False | ||
models: Optional[List[str]] = None | ||
tools: Optional[List[str]] = None | ||
tool_resources: Optional[ToolResources] = None | ||
|
||
|
||
class ListAssistantResponse(AI21BaseModel): | ||
results: List[AssistantResponse] |
Empty file.