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

fix: Allows clients to pass in an HTTP Session #59

Merged
merged 1 commit into from
Dec 10, 2024
Merged
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
17 changes: 2 additions & 15 deletions cohere/compass/clients/compass.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,9 @@ class RetryResult:
error: Optional[str] = None


_DEFAULT_TIMEOUT = 30


logger = logging.getLogger(__name__)


class SessionWithDefaultTimeout(requests.Session):
def __init__(self, timeout: int):
self._timeout = timeout
super().__init__()

def request(self, *args: Any, **kwargs: Any):
kwargs.setdefault("timeout", self._timeout)
return super().request(*args, **kwargs)


class CompassClient:
def __init__(
self,
Expand All @@ -89,7 +76,7 @@ def __init__(
username: Optional[str] = None,
password: Optional[str] = None,
bearer_token: Optional[str] = None,
default_timeout: int = _DEFAULT_TIMEOUT,
http_session: Optional[requests.Session] = None,
):
"""
A compass client to interact with the Compass API
Expand All @@ -100,7 +87,7 @@ def __init__(
self.index_url = index_url
self.username = username or os.getenv("COHERE_COMPASS_USERNAME")
self.password = password or os.getenv("COHERE_COMPASS_PASSWORD")
self.session = SessionWithDefaultTimeout(default_timeout)
self.session = http_session or requests.Session()
self.bearer_token = bearer_token

self.api_method = {
Expand Down
Loading