|
1 | 1 | # Humanloop Python Library |
2 | 2 |
|
3 | 3 | [](https://github.com/fern-api/fern) |
| 4 | +[](https://pypi.python.org/pypi/humanloop) |
4 | 5 |
|
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. |
| 6 | +The Humanloop Python library provides convenient access to the Humanloop API from Python. |
10 | 7 |
|
11 | 8 | ## Installation |
12 | 9 |
|
13 | | -Add this dependency to your project's build file: |
14 | | - |
15 | | -```bash |
| 10 | +```sh |
16 | 11 | pip install humanloop |
17 | | -# or |
18 | | -poetry add humanloop |
19 | 12 | ``` |
20 | 13 |
|
21 | 14 | ## Usage |
22 | 15 |
|
23 | | -Simply import `Humanloop` and start making calls to our API. |
| 16 | +Instantiate and use the client with the following: |
24 | 17 |
|
25 | 18 | ```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 |
70 | 19 | from humanloop.client import Humanloop |
71 | 20 |
|
72 | 21 | client = Humanloop( |
73 | 22 | api_key="YOUR_API_KEY", |
74 | 23 | ) |
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 | | - ], |
| 24 | +client.prompts.create( |
| 25 | + model="model", |
85 | 26 | ) |
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", |
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) |
122 | 27 | ``` |
123 | 28 |
|
124 | 29 | ## Async Client |
125 | 30 |
|
126 | | -The SDK also exports an async client so that you can make non-blocking |
127 | | -calls to our API. |
| 31 | +The SDK also exports an `async` client so that you can make non-blocking calls to our API. |
128 | 32 |
|
129 | 33 | ```python |
130 | | -import asyncio |
131 | | -from humanloop import ChatMessage |
132 | 34 | from humanloop.client import AsyncHumanloop |
133 | 35 |
|
134 | 36 | client = AsyncHumanloop( |
135 | 37 | api_key="YOUR_API_KEY", |
136 | 38 | ) |
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()) |
| 39 | +await client.prompts.create( |
| 40 | + model="model", |
| 41 | +) |
149 | 42 | ``` |
150 | 43 |
|
151 | 44 | ## Exception Handling |
|
0 commit comments