Skip to content

Commit 1e8e922

Browse files
authored
Update README.md
1 parent 3bf7334 commit 1e8e922

File tree

1 file changed

+190
-9
lines changed

1 file changed

+190
-9
lines changed

README.md

Lines changed: 190 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,224 @@
11
# Humanloop Python Library
22

33
[![fern shield](https://img.shields.io/badge/%F0%9F%8C%BF-SDK%20generated%20by%20Fern-brightgreen)](https://github.com/fern-api/fern)
4-
[![pypi](https://img.shields.io/pypi/v/humanloop)](https://pypi.python.org/pypi/humanloop)
54

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.
710

811
## Installation
912

10-
```sh
13+
Add this dependency to your project's build file:
14+
15+
```bash
1116
pip install humanloop
17+
# or
18+
poetry add humanloop
1219
```
1320

1421
## Usage
1522

16-
Instantiate and use the client with the following:
23+
Simply import `Humanloop` and start making calls to our API.
1724

1825
```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
1970
from humanloop.client import Humanloop
2071

2172
client = Humanloop(
2273
api_key="YOUR_API_KEY",
2374
)
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",
26100
)
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)
27122
```
28123

29124
## Async Client
30125

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.
32128

33129
```python
130+
import asyncio
131+
from humanloop import ChatMessage
34132
from humanloop.client import AsyncHumanloop
35133

36134
client = AsyncHumanloop(
37135
api_key="YOUR_API_KEY",
38136
)
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+
),
41222
)
42223
```
43224

0 commit comments

Comments
 (0)