Skip to content

Commit

Permalink
feat(routes): add vector store file batch routes (#50)
Browse files Browse the repository at this point in the history
* feat(routes): add vector store file batch routes

* feat(docs): add note on support for vector store file batch endpoint
  • Loading branch information
mharrisb1 authored May 30, 2024
1 parent 12f80da commit 7d5312f
Show file tree
Hide file tree
Showing 9 changed files with 488 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Pytest plugin for automatically mocking OpenAI requests. Powered by [RESPX](http
- [Run Steps](https://platform.openai.com/docs/api-reference/run-steps)
- [Vector Stores](https://platform.openai.com/docs/api-reference/vector-stores)
- [Vector Store Files](https://platform.openai.com/docs/api-reference/vector-stores-files)
- [Vector Store File Batches](https://platform.openai.com/docs/api-reference/vector-stores-file-batches)

View full support coverage [here](https://mharrisb1.github.io/openai-responses-python/coverage).

Expand Down
12 changes: 4 additions & 8 deletions docs/coverage.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# Coverage

??? warning "File attatchments"

There is currently no support for actually attaching files to assistant resources. Subscribe to [#5: feat: add support for attached files for all assistants APIs](https://github.com/mharrisb1/openai-responses-python/issues/5) to be notified when it is added.

??? note "Legacy endpoints"

[Legacy endpoints](https://platform.openai.com/docs/api-reference/completions) are not supported and are not on the roadmap.
Expand Down Expand Up @@ -88,7 +84,7 @@ The end-goal of this library is to eventually support all OpenAI API routes. See
| Retrieve vector store file | :material-check:{ .green } | - | Stateful |
| Delete vector store file | :material-check:{ .green } | - | Stateful |
| **Vector Store File Batches** |
| Create vector store file batch | :material-close:{ .red } | - | - |
| Retrieve vector store file batch | :material-close:{ .red } | - | - |
| Cancel vector store file batch | :material-close:{ .red } | - | - |
| List vector store files in a batch | :material-close:{ .red } | - | - |
| Create vector store file batch | :material-check:{ .green } | - | Stateful |
| Retrieve vector store file batch | :material-check:{ .green } | - | Stateful |
| Cancel vector store file batch | :material-check:{ .green } | - | Stateful |
| List vector store files in a batch | :material-check:{ .green } | - | Stateful |
110 changes: 110 additions & 0 deletions examples/test_vector_store_file_batches.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import openai

import openai_responses
from openai_responses import OpenAIMock


@openai_responses.mock()
def test_create_vector_store_file_batch(openai_mock: OpenAIMock):
client = openai.Client(api_key="sk-fake123")

vector_store = client.beta.vector_stores.create(name="Support FAQ")
file = client.files.create(
file=open("examples/example.json", "rb"),
purpose="assistants",
)

vector_store_file_batch = client.beta.vector_stores.file_batches.create(
vector_store_id=vector_store.id,
file_ids=[file.id],
)

assert vector_store_file_batch.vector_store_id == vector_store.id
assert vector_store_file_batch.file_counts.completed == 1

assert openai_mock.files.create.route.call_count == 1
assert openai_mock.beta.vector_stores.create.route.call_count == 1
assert openai_mock.beta.vector_stores.file_batches.create.route.call_count == 1


@openai_responses.mock()
def test_retrieve_vector_store_file_batch(openai_mock: OpenAIMock):
client = openai.Client(api_key="sk-fake123")

vector_store = client.beta.vector_stores.create(name="Support FAQ")
file = client.files.create(
file=open("examples/example.json", "rb"),
purpose="assistants",
)

vector_store_file_batch = client.beta.vector_stores.file_batches.create(
vector_store_id=vector_store.id,
file_ids=[file.id],
)

found = client.beta.vector_stores.file_batches.retrieve(
vector_store_file_batch.id, vector_store_id=vector_store.id
)

assert found.id == vector_store_file_batch.id

assert openai_mock.files.create.route.call_count == 1
assert openai_mock.beta.vector_stores.create.route.call_count == 1
assert openai_mock.beta.vector_stores.file_batches.create.route.call_count == 1
assert openai_mock.beta.vector_stores.file_batches.retrieve.route.call_count == 1


@openai_responses.mock()
def test_cancel_vector_store_file_batch(openai_mock: OpenAIMock):
client = openai.Client(api_key="sk-fake123")

vector_store = client.beta.vector_stores.create(name="Support FAQ")
file = client.files.create(
file=open("examples/example.json", "rb"),
purpose="assistants",
)

vector_store_file_batch = client.beta.vector_stores.file_batches.create(
vector_store_id=vector_store.id,
file_ids=[file.id],
)

cancelled = client.beta.vector_stores.file_batches.cancel(
vector_store_file_batch.id, vector_store_id=vector_store.id
)

assert cancelled.id == vector_store_file_batch.id
assert cancelled.status == "cancelled"

assert openai_mock.files.create.route.call_count == 1
assert openai_mock.beta.vector_stores.create.route.call_count == 1
assert openai_mock.beta.vector_stores.file_batches.create.route.call_count == 1
assert openai_mock.beta.vector_stores.file_batches.cancel.route.call_count == 1


@openai_responses.mock()
def test_list_files_for_vector_store_file_batch(openai_mock: OpenAIMock):
client = openai.Client(api_key="sk-fake123")

vector_store = client.beta.vector_stores.create(name="Support FAQ")
file = client.files.create(
file=open("examples/example.json", "rb"),
purpose="assistants",
)

vector_store_file_batch = client.beta.vector_stores.file_batches.create(
vector_store_id=vector_store.id,
file_ids=[file.id],
)

files = client.beta.vector_stores.file_batches.list_files(
vector_store_file_batch.id,
vector_store_id=vector_store.id,
)

assert len(files.data) == 1

assert openai_mock.files.create.route.call_count == 1
assert openai_mock.beta.vector_stores.create.route.call_count == 1
assert openai_mock.beta.vector_stores.file_batches.create.route.call_count == 1
assert openai_mock.beta.vector_stores.file_batches.list_files.route.call_count == 1
15 changes: 15 additions & 0 deletions src/openai_responses/_routes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@
VectorStoreFileRetrieveRoute,
VectorStoreFileDeleteRoute,
)
from .vector_store_file_batches import (
VectorStoreFileBatchCreateRoute,
VectorStoreFileBatchRetrieveRoute,
VectorStoreFileBatchCancelRoute,
VectorStoreFileBatchListFilesRoute,
)

__all__ = [
"BetaRoutes",
Expand Down Expand Up @@ -158,6 +164,7 @@ def __init__(self, router: respx.MockRouter, state: StateStore) -> None:
self.delete = VectorStoreDeleteRoute(router, state)

self.files = VectorStoreFileRoutes(router, state)
self.file_batches = VectorStoreFileBatchRoutes(router, state)


class VectorStoreFileRoutes:
Expand All @@ -166,3 +173,11 @@ def __init__(self, router: respx.MockRouter, state: StateStore) -> None:
self.list = VectorStoreFileListRoute(router, state)
self.retrieve = VectorStoreFileRetrieveRoute(router, state)
self.delete = VectorStoreFileDeleteRoute(router, state)


class VectorStoreFileBatchRoutes:
def __init__(self, router: respx.MockRouter, state: StateStore) -> None:
self.create = VectorStoreFileBatchCreateRoute(router, state)
self.retrieve = VectorStoreFileBatchRetrieveRoute(router, state)
self.cancel = VectorStoreFileBatchCancelRoute(router, state)
self.list_files = VectorStoreFileBatchListFilesRoute(router, state)
Loading

0 comments on commit 7d5312f

Please sign in to comment.