mirror of
https://github.com/hwchase17/langchain.git
synced 2026-06-09 10:17:00 +00:00
Thank you for contributing to LangChain! Follow these steps to mark your
pull request as ready for review. **If any of these steps are not
completed, your PR will not be considered for review.**
- [x] **PR title**: Follows the format: {TYPE}({SCOPE}): {DESCRIPTION}
- [x] **PR message**: ***Delete this entire checklist*** and replace
with
fix #30146
- [x] **Add tests and docs**: If you're adding a new integration, you
must include:
- [x] **Lint and test**: Run `make format`, `make lint` and `make test`
from the root of the package(s) you've modified. **We will not consider
a PR unless these three are passing in CI.** See [contribution
guidelines](https://python.langchain.com/docs/contributing/) for more.
Additional guidelines:
- Make sure optional dependencies are imported within a function.
- Please do not add dependencies to `pyproject.toml` files (even
optional ones) unless they are **required** for unit tests.
- Most PRs should not touch more than one package.
- Changes should be backwards compatible.
---------
Co-authored-by: Mason Daugherty <mason@langchain.dev>
Co-authored-by: Mason Daugherty <github@mdrxy.com>
84 lines
2.1 KiB
Python
84 lines
2.1 KiB
Python
"""Helpers for creating Anthropic API clients.
|
|
|
|
This module allows for the caching of httpx clients to avoid creating new instances
|
|
for each instance of ChatAnthropic.
|
|
|
|
Logic is largely replicated from anthropic._base_client.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import os
|
|
from functools import lru_cache
|
|
from typing import Any, Optional
|
|
|
|
import anthropic
|
|
|
|
_NOT_GIVEN: Any = object()
|
|
|
|
|
|
class _SyncHttpxClientWrapper(anthropic.DefaultHttpxClient):
|
|
"""Borrowed from anthropic._base_client."""
|
|
|
|
def __del__(self) -> None:
|
|
if self.is_closed:
|
|
return
|
|
|
|
try:
|
|
self.close()
|
|
except Exception: # noqa: S110
|
|
pass
|
|
|
|
|
|
class _AsyncHttpxClientWrapper(anthropic.DefaultAsyncHttpxClient):
|
|
"""Borrowed from anthropic._base_client."""
|
|
|
|
def __del__(self) -> None:
|
|
if self.is_closed:
|
|
return
|
|
|
|
try:
|
|
# TODO(someday): support non asyncio runtimes here
|
|
asyncio.get_running_loop().create_task(self.aclose())
|
|
except Exception: # noqa: S110
|
|
pass
|
|
|
|
|
|
@lru_cache
|
|
def _get_default_httpx_client(
|
|
*,
|
|
base_url: Optional[str],
|
|
timeout: Any = _NOT_GIVEN,
|
|
anthropic_proxy: Optional[str] = None,
|
|
) -> _SyncHttpxClientWrapper:
|
|
kwargs: dict[str, Any] = {
|
|
"base_url": base_url
|
|
or os.environ.get("ANTHROPIC_BASE_URL")
|
|
or "https://api.anthropic.com",
|
|
}
|
|
if timeout is not _NOT_GIVEN:
|
|
kwargs["timeout"] = timeout
|
|
if anthropic_proxy is not None:
|
|
kwargs["proxy"] = anthropic_proxy
|
|
return _SyncHttpxClientWrapper(**kwargs)
|
|
|
|
|
|
@lru_cache
|
|
def _get_default_async_httpx_client(
|
|
*,
|
|
base_url: Optional[str],
|
|
timeout: Any = _NOT_GIVEN,
|
|
anthropic_proxy: Optional[str] = None,
|
|
) -> _AsyncHttpxClientWrapper:
|
|
kwargs: dict[str, Any] = {
|
|
"base_url": base_url
|
|
or os.environ.get("ANTHROPIC_BASE_URL")
|
|
or "https://api.anthropic.com",
|
|
}
|
|
if timeout is not _NOT_GIVEN:
|
|
kwargs["timeout"] = timeout
|
|
if anthropic_proxy is not None:
|
|
kwargs["proxy"] = anthropic_proxy
|
|
return _AsyncHttpxClientWrapper(**kwargs)
|