|
1 | 1 | # Humanloop Python Library |
2 | 2 |
|
3 | 3 | [](https://github.com/fern-api/fern) |
4 | | -[](https://pypi.python.org/pypi/humanloop) |
5 | 4 |
|
6 | | -The Humanloop Python library provides convenient access to the Humanloop API from Python. |
| 5 | +The Humanloop Python Library provides convenient access to the Humanloop API from |
| 6 | +applications written in Python. |
| 7 | + |
| 8 | +The library includes type definitions for all |
| 9 | +request and response fields, and offers both synchronous and asynchronous clients powered by httpx. |
7 | 10 |
|
8 | 11 | ## Installation |
9 | 12 |
|
10 | | -```sh |
| 13 | +Add this dependency to your project's build file: |
| 14 | + |
| 15 | +```bash |
11 | 16 | pip install humanloop |
| 17 | +# or |
| 18 | +poetry add humanloop |
12 | 19 | ``` |
13 | 20 |
|
14 | 21 | ## Usage |
15 | 22 |
|
16 | | -Instantiate and use the client with the following: |
| 23 | +Simply import `Humanloop` and start making calls to our API. |
17 | 24 |
|
18 | 25 | ```python |
| 26 | +from humanloop import ChatMessage |
| 27 | +from humanloop.client import Humanloop |
| 28 | + |
| 29 | +client = Humanloop( |
| 30 | + api_key="YOUR_API_KEY", # Defaults to HUMANLOOP_API_KEY |
| 31 | +) |
| 32 | + |
| 33 | +client.prompts.call( |
| 34 | + prompt_id="prompt_id", |
| 35 | + messages=[ |
| 36 | + ChatMessage( |
| 37 | + content="What is the day today?", |
| 38 | + role="user", |
| 39 | + ) |
| 40 | + ], |
| 41 | +) |
| 42 | +``` |
| 43 | + |
| 44 | +### Typing |
| 45 | + |
| 46 | +To construct payloads you can either use the dedicated types like `ChatMessage` or construct directly from a dictionary like so: |
| 47 | + |
| 48 | +```python |
| 49 | +from humanloop import ChatMessage |
| 50 | +from humanloop.client import Humanloop |
| 51 | + |
| 52 | +client.prompts.call( |
| 53 | + prompt_id="prompt_id", |
| 54 | + messages=[ |
| 55 | + { |
| 56 | + content="Tell me a funny joke", |
| 57 | + role="user", |
| 58 | + } |
| 59 | + ], |
| 60 | +) |
| 61 | +``` |
| 62 | + |
| 63 | +### Streaming |
| 64 | + |
| 65 | +The SDK supports streaming endpoints. To take advantage of this feature for `prompts.call`, simply |
| 66 | +pass in `stream=True` in the request. The response will be a generator that you can loop over. |
| 67 | + |
| 68 | +```Python |
| 69 | +from humanloop import ChatMessage |
19 | 70 | from humanloop.client import Humanloop |
20 | 71 |
|
21 | 72 | client = Humanloop( |
22 | 73 | api_key="YOUR_API_KEY", |
23 | 74 | ) |
24 | | -client.prompts.create( |
25 | | - model="model", |
| 75 | + |
| 76 | +stream = client.prompts.call( |
| 77 | + prompt_id="prompt_id", |
| 78 | + stream=True, |
| 79 | + messages=[ |
| 80 | + ChatMessage( |
| 81 | + content="Tell me a funny joke", |
| 82 | + role="user", |
| 83 | + ) |
| 84 | + ], |
| 85 | +) |
| 86 | + |
| 87 | +for message in stream: |
| 88 | + print(message) |
| 89 | +``` |
| 90 | + |
| 91 | +### Pagination |
| 92 | + |
| 93 | +Paginated requests will return a `SyncPager` or `AsyncPager`, which can be used as generators for the underlying object. For example, `evaluations.list` will return a generator over `EvaluationResponse` and handle the pagination behind the scenes: |
| 94 | + |
| 95 | +```python |
| 96 | +import humanloop.client |
| 97 | + |
| 98 | +client = HumanloopClient( |
| 99 | + api_key="YOUR_API_KEY", |
26 | 100 | ) |
| 101 | + |
| 102 | +for evaluation in client.evaluations.list(file_id="id"): |
| 103 | + print(evaluation) |
| 104 | +``` |
| 105 | + |
| 106 | +you could also iterate page-by-page: |
| 107 | + |
| 108 | +```python |
| 109 | +for page in client.evaluations.list(file_id="id").iter_pages(): |
| 110 | + print(page.items) |
| 111 | +``` |
| 112 | + |
| 113 | +or manually: |
| 114 | + |
| 115 | +```python |
| 116 | +pager = client.evaluations.list(project_id="id") |
| 117 | +# First page |
| 118 | +print(pager.items) |
| 119 | +# Second page |
| 120 | +pager = pager.next_page() |
| 121 | +print(pager.items) |
27 | 122 | ``` |
28 | 123 |
|
29 | 124 | ## Async Client |
30 | 125 |
|
31 | | -The SDK also exports an `async` client so that you can make non-blocking calls to our API. |
| 126 | +The SDK also exports an async client so that you can make non-blocking |
| 127 | +calls to our API. |
32 | 128 |
|
33 | 129 | ```python |
| 130 | +import asyncio |
| 131 | +from humanloop import ChatMessage |
34 | 132 | from humanloop.client import AsyncHumanloop |
35 | 133 |
|
36 | 134 | client = AsyncHumanloop( |
37 | 135 | api_key="YOUR_API_KEY", |
38 | 136 | ) |
39 | | -await client.prompts.create( |
40 | | - model="model", |
| 137 | + |
| 138 | +async def main() -> None: |
| 139 | + await client.prompts.call( |
| 140 | + prompt_id="prompt_id", |
| 141 | + messages=[ |
| 142 | + ChatMessage( |
| 143 | + content="What is the day today?", |
| 144 | + role="user", |
| 145 | + ) |
| 146 | + ], |
| 147 | + ) |
| 148 | +asyncio.run(main()) |
| 149 | +``` |
| 150 | + |
| 151 | +## Exception Handling |
| 152 | + |
| 153 | +All errors thrown by the SDK will be subclasses of [`ApiError`](./src/schematic/core/api_error.py). |
| 154 | + |
| 155 | +```python |
| 156 | +import humanloop |
| 157 | + |
| 158 | +try: |
| 159 | + client.prompts.call(...) |
| 160 | +except humanloop.core.ApiError as e: # Handle all errors |
| 161 | + print(e.status_code) |
| 162 | + print(e.body) |
| 163 | +``` |
| 164 | + |
| 165 | +## Advanced |
| 166 | + |
| 167 | +### Timeouts |
| 168 | + |
| 169 | +By default, requests time out after 60 seconds. You can configure this with a |
| 170 | +timeout option at the client or request level. |
| 171 | + |
| 172 | +```python |
| 173 | +from humanloop.client import Humanloop |
| 174 | + |
| 175 | +client = Humanloop( |
| 176 | + ..., |
| 177 | + # All timeouts are 20 seconds |
| 178 | + timeout=20.0, |
| 179 | +) |
| 180 | + |
| 181 | +# Override timeout for a specific method |
| 182 | +client.prompts.call(..., { |
| 183 | + timeout_in_seconds=20.0 |
| 184 | +}) |
| 185 | +``` |
| 186 | + |
| 187 | +### Retries |
| 188 | + |
| 189 | +The SDK is instrumented with automatic retries with exponential backoff. A request will be |
| 190 | +retried as long as the request is deemed retriable and the number of retry attempts has not grown larger |
| 191 | +than the configured retry limit (default: 2). |
| 192 | + |
| 193 | +A request is deemed retriable when any of the following HTTP status codes is returned: |
| 194 | + |
| 195 | +- [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) (Timeout) |
| 196 | +- [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) (Too Many Requests) |
| 197 | +- [5XX](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) (Internal Server Errors) |
| 198 | + |
| 199 | +Use the `max_retries` request option to configure this behavior. |
| 200 | + |
| 201 | +```python |
| 202 | +client.prompts.call(..., { |
| 203 | + max_retries=1 |
| 204 | +}) |
| 205 | +``` |
| 206 | + |
| 207 | +### Custom HTTP client |
| 208 | + |
| 209 | +You can override the httpx client to customize it for your use-case. Some common use-cases |
| 210 | +include support for proxies and transports. |
| 211 | + |
| 212 | +```python |
| 213 | +import httpx |
| 214 | + |
| 215 | +from humanloop.client import Humanloop |
| 216 | + |
| 217 | +client = Humanloop(..., |
| 218 | + http_client=httpx.Client( |
| 219 | + proxies="http://my.test.proxy.example.com", |
| 220 | + transport=httpx.HTTPTransport(local_address="0.0.0.0"), |
| 221 | + ), |
41 | 222 | ) |
42 | 223 | ``` |
43 | 224 |
|
|
0 commit comments