Skip to content
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

draft: compass async sdk #90

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open

Conversation

raphael-cohere
Copy link
Contributor

@raphael-cohere raphael-cohere commented Feb 12, 2025

Add an async client to the compass sdk. Was playing around with the code, there is probably more logic that can be extracted. If you do not feel this is the right direction please provide guidance how you would like this to look like.

@raphael-cohere raphael-cohere requested a review from a team as a code owner February 12, 2025 15:36

@retry(
stop=stop_after_attempt(compass_request["max_retries"]),
wait=wait_fixed(compass_request["sleep_retry_seconds"]),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope for async it is using asyncio.sleep but should probably double check


def __init__(
self,
*,
index_url: str,
bearer_token: Optional[str] = None,
http_session: Optional[requests.Session] = None,
default_max_retries: int = DEFAULT_MAX_RETRIES,
default_sleep_retry_seconds: int = DEFAULT_SLEEP_RETRY_SECONDS,
):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious why you removed the docstring instead of changing it to fit the new BaseCompassClient.

)
self.session = http_session or aiohttp.ClientSession()

async def search_documents(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you only implemented the search APIs. Do you plan to implement the rest of the async methods?

Copy link
Contributor Author

@raphael-cohere raphael-cohere Feb 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this PR is more of a draft than anything else. Just wanted to make sure you guys are okay with this change before I commit to implementing the rest.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks.

I am wondering whether there is a way to automate this, somehow, so we don't have to keep adding/modifying APIs in two places. The only difference really between the two clients is how the _send_request method is implemented, so I can suggest the following, though it is an abstract idea that I have not tested:

  1. Define all the API methods in the base class and let them simply just call the abstract method _send_request defined in the base class.
  2. The sync and async clients will define the _send_request method differently.
  3. To support type annotation, the base client class can be made generic with a generic argument for the return type of _send_request, e.g.
class BaseClient(T):
    def _send_request(...) -> T
        pass
  1. The Sync and Async client specifies the expected return type, e.g.:
class CompassClient(BaseClient[RetryResponse]):
    pass
class CompassAsyncClient(BaseClient[Awaitable[RetryResponse]]):
    pass

Thought?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice idea, let me tinker with this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I tested this idea. It works well if we do not parse the response but can just pass it along. Then we can hoist the method to the base class. So it doesn't work for all methods but should cut down on duplication

@corafid
Copy link
Member

corafid commented Feb 17, 2025

Looks good overall, but you would need to run the pre-commits to fix the linting issues. We also need some tests for the async client.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants