|
1 | 1 | # Openlayer Python API library |
2 | 2 |
|
3 | | -[](https://pypi.org/project/openlayer/) |
| 3 | +[>)](https://pypi.org/project/openlayer/) |
4 | 4 |
|
5 | 5 | The Openlayer Python library provides convenient access to the Openlayer REST API from any Python 3.8+ |
6 | 6 | application. The library includes type definitions for all request params and response fields, |
@@ -100,6 +100,56 @@ asyncio.run(main()) |
100 | 100 |
|
101 | 101 | Functionality between the synchronous and asynchronous clients is otherwise identical. |
102 | 102 |
|
| 103 | +### With aiohttp |
| 104 | + |
| 105 | +By default, the async client uses `httpx` for HTTP requests. However, for improved concurrency performance you may also use `aiohttp` as the HTTP backend. |
| 106 | + |
| 107 | +You can enable this by installing `aiohttp`: |
| 108 | + |
| 109 | +```sh |
| 110 | +# install from PyPI |
| 111 | +pip install --pre openlayer[aiohttp] |
| 112 | +``` |
| 113 | + |
| 114 | +Then you can enable it by instantiating the client with `http_client=DefaultAioHttpClient()`: |
| 115 | + |
| 116 | +```python |
| 117 | +import os |
| 118 | +import asyncio |
| 119 | +from openlayer import DefaultAioHttpClient |
| 120 | +from openlayer import AsyncOpenlayer |
| 121 | + |
| 122 | + |
| 123 | +async def main() -> None: |
| 124 | + async with AsyncOpenlayer( |
| 125 | + api_key=os.environ.get("OPENLAYER_API_KEY"), # This is the default and can be omitted |
| 126 | + http_client=DefaultAioHttpClient(), |
| 127 | + ) as client: |
| 128 | + response = await client.inference_pipelines.data.stream( |
| 129 | + inference_pipeline_id="182bd5e5-6e1a-4fe4-a799-aa6d9a6ab26e", |
| 130 | + config={ |
| 131 | + "input_variable_names": ["user_query"], |
| 132 | + "output_column_name": "output", |
| 133 | + "num_of_token_column_name": "tokens", |
| 134 | + "cost_column_name": "cost", |
| 135 | + "timestamp_column_name": "timestamp", |
| 136 | + }, |
| 137 | + rows=[ |
| 138 | + { |
| 139 | + "user_query": "what is the meaning of life?", |
| 140 | + "output": "42", |
| 141 | + "tokens": 7, |
| 142 | + "cost": 0.02, |
| 143 | + "timestamp": 1610000000, |
| 144 | + } |
| 145 | + ], |
| 146 | + ) |
| 147 | + print(response.success) |
| 148 | + |
| 149 | + |
| 150 | +asyncio.run(main()) |
| 151 | +``` |
| 152 | + |
103 | 153 | ## Using types |
104 | 154 |
|
105 | 155 | Nested request parameters are [TypedDicts](https://docs.python.org/3/library/typing.html#typing.TypedDict). Responses are [Pydantic models](https://docs.pydantic.dev) which also provide helper methods for things like: |
@@ -227,7 +277,7 @@ client.with_options(max_retries=5).inference_pipelines.data.stream( |
227 | 277 | ### Timeouts |
228 | 278 |
|
229 | 279 | By default requests time out after 1 minute. You can configure this with a `timeout` option, |
230 | | -which accepts a float or an [`httpx.Timeout`](https://www.python-httpx.org/advanced/#fine-tuning-the-configuration) object: |
| 280 | +which accepts a float or an [`httpx.Timeout`](https://www.python-httpx.org/advanced/timeouts/#fine-tuning-the-configuration) object: |
231 | 281 |
|
232 | 282 | ```python |
233 | 283 | from openlayer import Openlayer |
|
0 commit comments