Skip to content

feat(api): updates from question grouping #93

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

Merged
merged 1 commit into from
Apr 2, 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
6 changes: 3 additions & 3 deletions .stats.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
configured_endpoints: 37
openapi_spec_hash: 6993cffe14e021af26bf142f5b557263
config_hash: 0529917e0e61d8cd6366481b77eff77e
configured_endpoints: 36
openapi_spec_hash: 4e7cb2cd6132c29f60a87a958f617a41
config_hash: adbedb6317fca6f566f54564cc341846
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ from codex import Codex

client = Codex()

all_entries = []
all_clusters = []
# Automatically fetches more pages as needed.
for entry in client.projects.entries.list(
for cluster in client.projects.clusters.list(
project_id="182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e",
):
# Do something with entry here
all_entries.append(entry)
print(all_entries)
# Do something with cluster here
all_clusters.append(cluster)
print(all_clusters)
```

Or, asynchronously:
Expand All @@ -107,13 +107,13 @@ client = AsyncCodex()


async def main() -> None:
all_entries = []
all_clusters = []
# Iterate through items across all pages, issuing requests as needed.
async for entry in client.projects.entries.list(
async for cluster in client.projects.clusters.list(
project_id="182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e",
):
all_entries.append(entry)
print(all_entries)
all_clusters.append(cluster)
print(all_clusters)


asyncio.run(main())
Expand All @@ -122,25 +122,25 @@ asyncio.run(main())
Alternatively, you can use the `.has_next_page()`, `.next_page_info()`, or `.get_next_page()` methods for more granular control working with pages:

```python
first_page = await client.projects.entries.list(
first_page = await client.projects.clusters.list(
project_id="182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e",
)
if first_page.has_next_page():
print(f"will fetch next page using these details: {first_page.next_page_info()}")
next_page = await first_page.get_next_page()
print(f"number of items we just fetched: {len(next_page.entries)}")
print(f"number of items we just fetched: {len(next_page.clusters)}")

# Remove `await` for non-async usage.
```

Or just work directly with the returned data:

```python
first_page = await client.projects.entries.list(
first_page = await client.projects.clusters.list(
project_id="182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e",
)
for entry in first_page.entries:
print(entry.id)
for cluster in first_page.clusters:
print(cluster.id)

# Remove `await` for non-async usage.
```
Expand Down
20 changes: 15 additions & 5 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Methods:

- <code title="get /api/health/">client.health.<a href="./src/codex/resources/health.py">check</a>() -> <a href="./src/codex/types/health_check_response.py">HealthCheckResponse</a></code>
- <code title="get /api/health/db">client.health.<a href="./src/codex/resources/health.py">db</a>() -> <a href="./src/codex/types/health_check_response.py">HealthCheckResponse</a></code>
- <code title="get /api/health/weaviate">client.health.<a href="./src/codex/resources/health.py">weaviate</a>() -> <a href="./src/codex/types/health_check_response.py">HealthCheckResponse</a></code>

# Organizations

Expand Down Expand Up @@ -175,18 +174,29 @@ Methods:
Types:

```python
from codex.types.projects import Entry
from codex.types.projects import Entry, EntryQueryResponse
```

Methods:

- <code title="post /api/projects/{project_id}/entries/">client.projects.entries.<a href="./src/codex/resources/projects/entries.py">create</a>(project_id, \*\*<a href="src/codex/types/projects/entry_create_params.py">params</a>) -> <a href="./src/codex/types/projects/entry.py">Entry</a></code>
- <code title="get /api/projects/{project_id}/entries/{entry_id}">client.projects.entries.<a href="./src/codex/resources/projects/entries.py">retrieve</a>(entry_id, \*, project_id) -> <a href="./src/codex/types/projects/entry.py">Entry</a></code>
- <code title="put /api/projects/{project_id}/entries/{entry_id}">client.projects.entries.<a href="./src/codex/resources/projects/entries.py">update</a>(entry_id, \*, project_id, \*\*<a href="src/codex/types/projects/entry_update_params.py">params</a>) -> <a href="./src/codex/types/projects/entry.py">Entry</a></code>
- <code title="get /api/projects/{project_id}/entries/">client.projects.entries.<a href="./src/codex/resources/projects/entries.py">list</a>(project_id, \*\*<a href="src/codex/types/projects/entry_list_params.py">params</a>) -> <a href="./src/codex/types/projects/entry.py">SyncOffsetPageEntries[Entry]</a></code>
- <code title="delete /api/projects/{project_id}/entries/{entry_id}">client.projects.entries.<a href="./src/codex/resources/projects/entries.py">delete</a>(entry_id, \*, project_id) -> None</code>
- <code title="post /api/projects/{project_id}/entries/add_question">client.projects.entries.<a href="./src/codex/resources/projects/entries.py">add_question</a>(project_id, \*\*<a href="src/codex/types/projects/entry_add_question_params.py">params</a>) -> <a href="./src/codex/types/projects/entry.py">Entry</a></code>
- <code title="post /api/projects/{project_id}/entries/query">client.projects.entries.<a href="./src/codex/resources/projects/entries.py">query</a>(project_id, \*\*<a href="src/codex/types/projects/entry_query_params.py">params</a>) -> <a href="./src/codex/types/projects/entry.py">Optional[Entry]</a></code>
- <code title="post /api/projects/{project_id}/entries/query">client.projects.entries.<a href="./src/codex/resources/projects/entries.py">query</a>(project_id, \*\*<a href="src/codex/types/projects/entry_query_params.py">params</a>) -> <a href="./src/codex/types/projects/entry_query_response.py">EntryQueryResponse</a></code>

## Clusters

Types:

```python
from codex.types.projects import ClusterListResponse, ClusterListVariantsResponse
```

Methods:

- <code title="get /api/projects/{project_id}/entries/clusters">client.projects.clusters.<a href="./src/codex/resources/projects/clusters.py">list</a>(project_id, \*\*<a href="src/codex/types/projects/cluster_list_params.py">params</a>) -> <a href="./src/codex/types/projects/cluster_list_response.py">SyncOffsetPageClusters[ClusterListResponse]</a></code>
- <code title="get /api/projects/{project_id}/entries/clusters/{representative_entry_id}">client.projects.clusters.<a href="./src/codex/resources/projects/clusters.py">list_variants</a>(representative_entry_id, \*, project_id) -> <a href="./src/codex/types/projects/cluster_list_variants_response.py">ClusterListVariantsResponse</a></code>

# Tlm

Expand Down
62 changes: 62 additions & 0 deletions src/codex/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
__all__ = [
"SyncMyOffsetPageTopLevelArray",
"AsyncMyOffsetPageTopLevelArray",
"SyncOffsetPageClusters",
"AsyncOffsetPageClusters",
"SyncOffsetPageEntries",
"AsyncOffsetPageEntries",
]
Expand Down Expand Up @@ -83,6 +85,66 @@ def build(cls: Type[_BaseModelT], *, response: Response, data: object) -> _BaseM
)


class SyncOffsetPageClusters(BaseSyncPage[_T], BasePage[_T], Generic[_T]):
clusters: List[_T]
total_count: Optional[int] = None

@override
def _get_page_items(self) -> List[_T]:
clusters = self.clusters
if not clusters:
return []
return clusters

@override
def next_page_info(self) -> Optional[PageInfo]:
offset = self._options.params.get("offset") or 0
if not isinstance(offset, int):
raise ValueError(f'Expected "offset" param to be an integer but got {offset}')

length = len(self._get_page_items())
current_count = offset + length

total_count = self.total_count
if total_count is None:
return None

if current_count < total_count:
return PageInfo(params={"offset": current_count})

return None


class AsyncOffsetPageClusters(BaseAsyncPage[_T], BasePage[_T], Generic[_T]):
clusters: List[_T]
total_count: Optional[int] = None

@override
def _get_page_items(self) -> List[_T]:
clusters = self.clusters
if not clusters:
return []
return clusters

@override
def next_page_info(self) -> Optional[PageInfo]:
offset = self._options.params.get("offset") or 0
if not isinstance(offset, int):
raise ValueError(f'Expected "offset" param to be an integer but got {offset}')

length = len(self._get_page_items())
current_count = offset + length

total_count = self.total_count
if total_count is None:
return None

if current_count < total_count:
return PageInfo(params={"offset": current_count})

return None


class SyncOffsetPageEntries(BaseSyncPage[_T], BasePage[_T], Generic[_T]):
entries: List[_T]
total_count: Optional[int] = None
Expand Down
50 changes: 0 additions & 50 deletions src/codex/resources/health.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,6 @@ def db(
cast_to=HealthCheckResponse,
)

def weaviate(
self,
*,
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
# The extra values given here take precedence over values defined on the client or passed to this method.
extra_headers: Headers | None = None,
extra_query: Query | None = None,
extra_body: Body | None = None,
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
) -> HealthCheckResponse:
"""Check the weaviate connection."""
return self._get(
"/api/health/weaviate",
options=make_request_options(
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
),
cast_to=HealthCheckResponse,
)


class AsyncHealthResource(AsyncAPIResource):
@cached_property
Expand Down Expand Up @@ -155,25 +136,6 @@ async def db(
cast_to=HealthCheckResponse,
)

async def weaviate(
self,
*,
# Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs.
# The extra values given here take precedence over values defined on the client or passed to this method.
extra_headers: Headers | None = None,
extra_query: Query | None = None,
extra_body: Body | None = None,
timeout: float | httpx.Timeout | None | NotGiven = NOT_GIVEN,
) -> HealthCheckResponse:
"""Check the weaviate connection."""
return await self._get(
"/api/health/weaviate",
options=make_request_options(
extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout
),
cast_to=HealthCheckResponse,
)


class HealthResourceWithRawResponse:
def __init__(self, health: HealthResource) -> None:
Expand All @@ -185,9 +147,6 @@ def __init__(self, health: HealthResource) -> None:
self.db = to_raw_response_wrapper(
health.db,
)
self.weaviate = to_raw_response_wrapper(
health.weaviate,
)


class AsyncHealthResourceWithRawResponse:
Expand All @@ -200,9 +159,6 @@ def __init__(self, health: AsyncHealthResource) -> None:
self.db = async_to_raw_response_wrapper(
health.db,
)
self.weaviate = async_to_raw_response_wrapper(
health.weaviate,
)


class HealthResourceWithStreamingResponse:
Expand All @@ -215,9 +171,6 @@ def __init__(self, health: HealthResource) -> None:
self.db = to_streamed_response_wrapper(
health.db,
)
self.weaviate = to_streamed_response_wrapper(
health.weaviate,
)


class AsyncHealthResourceWithStreamingResponse:
Expand All @@ -230,6 +183,3 @@ def __init__(self, health: AsyncHealthResource) -> None:
self.db = async_to_streamed_response_wrapper(
health.db,
)
self.weaviate = async_to_streamed_response_wrapper(
health.weaviate,
)
14 changes: 14 additions & 0 deletions src/codex/resources/projects/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@
EntriesResourceWithStreamingResponse,
AsyncEntriesResourceWithStreamingResponse,
)
from .clusters import (
ClustersResource,
AsyncClustersResource,
ClustersResourceWithRawResponse,
AsyncClustersResourceWithRawResponse,
ClustersResourceWithStreamingResponse,
AsyncClustersResourceWithStreamingResponse,
)
from .projects import (
ProjectsResource,
AsyncProjectsResource,
Expand Down Expand Up @@ -38,6 +46,12 @@
"AsyncEntriesResourceWithRawResponse",
"EntriesResourceWithStreamingResponse",
"AsyncEntriesResourceWithStreamingResponse",
"ClustersResource",
"AsyncClustersResource",
"ClustersResourceWithRawResponse",
"AsyncClustersResourceWithRawResponse",
"ClustersResourceWithStreamingResponse",
"AsyncClustersResourceWithStreamingResponse",
"ProjectsResource",
"AsyncProjectsResource",
"ProjectsResourceWithRawResponse",
Expand Down
Loading