Skip to content

Commit 56358c1

Browse files
committed
feat: 🎉 Added support for batch mode & batch querying
1 parent 8db4fbf commit 56358c1

4 files changed

Lines changed: 94 additions & 32 deletions

File tree

ai21/clients/studio/resources/studio_library.py

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import uuid
34
from os import PathLike
45
from typing import List, Optional
56

@@ -9,7 +10,7 @@
910
)
1011
from ai21.http_client.async_http_client import AsyncAI21HTTPClient
1112
from ai21.http_client.http_client import AI21HTTPClient
12-
from ai21.models import FileResponse
13+
from ai21.models import FileResponse, UploadMode, BatchStatusResponse
1314
from ai21.types import NOT_GIVEN, NotGiven
1415
from ai21.utils.typing import remove_not_given
1516

@@ -32,10 +33,21 @@ def create(
3233
path: Optional[str] | NotGiven = NOT_GIVEN,
3334
labels: Optional[List[str]] | NotGiven = NOT_GIVEN,
3435
public_url: Optional[str] | NotGiven = NOT_GIVEN,
36+
upload_mode: Optional[UploadMode] | NotGiven = NOT_GIVEN,
37+
batch_id: Optional[uuid.UUID] | NotGiven = NOT_GIVEN,
3538
**kwargs,
3639
) -> str:
3740
files = {"file": open(file_path, "rb")}
38-
body = remove_not_given({"path": path, "labels": labels, "publicUrl": public_url, **kwargs})
41+
body = remove_not_given(
42+
{
43+
"path": path,
44+
"labels": labels,
45+
"publicUrl": public_url,
46+
"upload_mode": upload_mode,
47+
"batch_id": batch_id,
48+
**kwargs,
49+
}
50+
)
3951

4052
raw_response = self._post(path=f"/{self._module_name}", files=files, body=body, response_cls=dict)
4153

@@ -44,6 +56,9 @@ def create(
4456
def get(self, file_id: str) -> FileResponse:
4557
return self._get(path=f"/{self._module_name}/{file_id}", response_cls=FileResponse)
4658

59+
def get_batch_status(self, batch_id: uuid.UUID) -> BatchStatusResponse:
60+
return self._get(path=f"/library/batches/{batch_id}/status", response_cls=BatchStatusResponse)
61+
4762
def list(
4863
self,
4964
*,
@@ -94,10 +109,21 @@ async def create(
94109
path: Optional[str] | NotGiven = NOT_GIVEN,
95110
labels: Optional[List[str]] | NotGiven = NOT_GIVEN,
96111
public_url: Optional[str] | NotGiven = NOT_GIVEN,
112+
upload_mode: Optional[UploadMode] | NotGiven = NOT_GIVEN,
113+
batch_id: Optional[uuid.UUID] | NotGiven = NOT_GIVEN,
97114
**kwargs,
98115
) -> str:
99116
files = {"file": open(file_path, "rb")}
100-
body = remove_not_given({"path": path, "labels": labels, "publicUrl": public_url, **kwargs})
117+
body = remove_not_given(
118+
{
119+
"path": path,
120+
"labels": labels,
121+
"publicUrl": public_url,
122+
"upload_mode": upload_mode,
123+
"batch_id": batch_id,
124+
**kwargs,
125+
}
126+
)
101127

102128
raw_response = await self._post(path=f"/{self._module_name}", files=files, body=body, response_cls=dict)
103129

@@ -106,6 +132,9 @@ async def create(
106132
async def get(self, file_id: str) -> FileResponse:
107133
return await self._get(path=f"/{self._module_name}/{file_id}", response_cls=FileResponse)
108134

135+
async def get_batch_status(self, batch_id: uuid.UUID) -> BatchStatusResponse:
136+
return await self._get(path=f"/library/batches/{batch_id}/status", response_cls=BatchStatusResponse)
137+
109138
async def list(
110139
self,
111140
*,

ai21/models/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
ConversationalRagSource,
99
)
1010
from ai21.models.responses.file_response import FileResponse
11+
from ai21.models.upload_mode import UploadMode, BatchStatusResponse
1112

1213
__all__ = [
1314
"ChatMessage",
@@ -20,4 +21,6 @@
2021
"FileResponse",
2122
"ConversationalRagResponse",
2223
"ConversationalRagSource",
24+
"UploadMode",
25+
"BatchStatusResponse",
2326
]

ai21/models/upload_mode.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from enum import StrEnum
2+
from typing import List
3+
4+
from pydantic import BaseModel, Field
5+
6+
7+
class UploadMode(StrEnum):
8+
IMMEDIATE = "immediate"
9+
BATCH = "batch"
10+
11+
12+
class BatchStatusCount(BaseModel):
13+
status: str
14+
count: int
15+
16+
17+
class BatchStatusResponse(BaseModel):
18+
batch_id: str = Field(description="The UUID of the batch")
19+
total_documents: int = Field(description="Total number of documents in the batch")
20+
statuses: List[BatchStatusCount] = Field(description="List of document counts by status")

init.sh

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,48 @@
1-
#!/bin/bash
2-
3-
cd "$(dirname "$0")" || exit
1+
#!/usr/bin/env bash
42

53
# create .git folder
64
if [[ ! -d .git ]]; then
75
git init
86
fi
97

10-
# install the python version specified in .python-version, if not already installed
11-
if pyenv --version; then
12-
pyenv install --skip-existing
13-
fi
14-
15-
# install poetry if not already installed
16-
if ! poetry --version; then
17-
brew install poetry
18-
fi
19-
20-
# poetry needs to create the venv with the same python version
21-
poetry env use "$(cat .python-version)"
22-
23-
# update lock file
24-
poetry lock --no-update
25-
26-
# install dependencies
27-
poetry install
8+
PYTHON_VERSION=$(cat .python-version)
289

29-
# install pre-commit if not already installed
30-
if ! pre-commit --version; then
31-
brew install pre-commit
10+
# install the python version specified in .python-version, if not already installed
11+
if ! pyenv versions --bare | grep -q "^${PYTHON_VERSION}"; then
12+
pyenv install "${PYTHON_VERSION}" --skip-existing
3213
fi
3314

34-
# install pre-commit hooks
35-
pre-commit install --install-hooks -t pre-commit -t commit-msg
36-
37-
# shellcheck source=/dev/null
38-
source .venv/bin/activate
15+
{ [[ -d .venv ]] || {
16+
echo 'creating virtualenv...'
17+
python -m venv .venv
18+
}; } && {
19+
# shellcheck disable=SC1091
20+
. .venv/bin/activate
21+
} && {
22+
# install poetry if not already installed, or upgrade to version 2.x
23+
poetry_version_installed=$(poetry --version 2> /dev/null || true)
24+
poetry_major_version=$(echo "$poetry_version_installed" | awk '{print $3}' | cut -d '.' -f1)
25+
26+
# Check if poetry_major_version is a number and if it's >= 2
27+
if [[ -z $poetry_version_installed || ! $poetry_major_version =~ ^[0-9]+$ || $poetry_major_version -lt 2 ]]; then
28+
echo "Poetry >=2 is not installed. Installing/upgrading..."
29+
pip install --upgrade poetry || brew install poetry
30+
fi
31+
} && {
32+
# install keyring
33+
poetry self add keyrings-google-artifactregistry-auth@1.1.2
34+
} && {
35+
# update lock file
36+
poetry lock
37+
} && {
38+
# install dependencies
39+
poetry install --no-root && poetry sync
40+
}
41+
42+
{
43+
# install pre-commit if not already installed
44+
pre-commit --version || brew install pre-commit
45+
} && {
46+
# install pre-commit hooks
47+
pre-commit install --install-hooks -t pre-commit -t pre-push -t commit-msg
48+
}

0 commit comments

Comments
 (0)