friendli 0.1.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- friendli-0.1.2/PKG-INFO +646 -0
- friendli-0.1.2/README-PYPI.md +624 -0
- friendli-0.1.2/py.typed +1 -0
- friendli-0.1.2/pyproject.toml +54 -0
- friendli-0.1.2/src/friendli/__init__.py +9 -0
- friendli-0.1.2/src/friendli/_hooks/__init__.py +5 -0
- friendli-0.1.2/src/friendli/_hooks/registration.py +13 -0
- friendli-0.1.2/src/friendli/_hooks/sdkhooks.py +76 -0
- friendli-0.1.2/src/friendli/_hooks/types.py +94 -0
- friendli-0.1.2/src/friendli/_version.py +12 -0
- friendli-0.1.2/src/friendli/basesdk.py +334 -0
- friendli-0.1.2/src/friendli/httpclient.py +78 -0
- friendli-0.1.2/src/friendli/inference.py +865 -0
- friendli-0.1.2/src/friendli/models/__init__.py +322 -0
- friendli-0.1.2/src/friendli/models/assistantmessage.py +81 -0
- friendli-0.1.2/src/friendli/models/chatcompletionchoice.py +102 -0
- friendli-0.1.2/src/friendli/models/chatcompletionop.py +58 -0
- friendli-0.1.2/src/friendli/models/chatcompletionrequestbody.py +385 -0
- friendli-0.1.2/src/friendli/models/chatcompletionresponse.py +24 -0
- friendli-0.1.2/src/friendli/models/completionchoice.py +31 -0
- friendli-0.1.2/src/friendli/models/completionop.py +52 -0
- friendli-0.1.2/src/friendli/models/completionrequestbody.py +22 -0
- friendli-0.1.2/src/friendli/models/completionrequestbodywithprompt.py +338 -0
- friendli-0.1.2/src/friendli/models/completionrequestbodywithtokens.py +338 -0
- friendli-0.1.2/src/friendli/models/completionresponse.py +19 -0
- friendli-0.1.2/src/friendli/models/detokenizationop.py +32 -0
- friendli-0.1.2/src/friendli/models/detokenizationrequestbody.py +21 -0
- friendli-0.1.2/src/friendli/models/detokenizationresponse.py +20 -0
- friendli-0.1.2/src/friendli/models/filebuiltintool.py +28 -0
- friendli-0.1.2/src/friendli/models/function.py +76 -0
- friendli-0.1.2/src/friendli/models/functiontool.py +26 -0
- friendli-0.1.2/src/friendli/models/logprobs.py +55 -0
- friendli-0.1.2/src/friendli/models/message.py +30 -0
- friendli-0.1.2/src/friendli/models/otherbuiltintool.py +27 -0
- friendli-0.1.2/src/friendli/models/sdkerror.py +22 -0
- friendli-0.1.2/src/friendli/models/security.py +25 -0
- friendli-0.1.2/src/friendli/models/streamedchatcompletionchoice.py +130 -0
- friendli-0.1.2/src/friendli/models/streamedchatcompletionresponse.py +39 -0
- friendli-0.1.2/src/friendli/models/streamedcompletionresponse.py +38 -0
- friendli-0.1.2/src/friendli/models/streamedcompletiontokencomplete.py +31 -0
- friendli-0.1.2/src/friendli/models/streamedcompletiontokensampled.py +37 -0
- friendli-0.1.2/src/friendli/models/streamedtoolassistedchatcompletionresponse.py +35 -0
- friendli-0.1.2/src/friendli/models/systemmessage.py +33 -0
- friendli-0.1.2/src/friendli/models/textresponseformat.py +65 -0
- friendli-0.1.2/src/friendli/models/tokenizationop.py +32 -0
- friendli-0.1.2/src/friendli/models/tokenizationrequestbody.py +20 -0
- friendli-0.1.2/src/friendli/models/tokenizationresponse.py +20 -0
- friendli-0.1.2/src/friendli/models/tokensequence.py +16 -0
- friendli-0.1.2/src/friendli/models/tool.py +26 -0
- friendli-0.1.2/src/friendli/models/toolassistedchatcompletionop.py +65 -0
- friendli-0.1.2/src/friendli/models/toolassistedcompletionrequestbody.py +325 -0
- friendli-0.1.2/src/friendli/models/toolfortoolassistedchat.py +25 -0
- friendli-0.1.2/src/friendli/models/toolmessage.py +38 -0
- friendli-0.1.2/src/friendli/models/usage.py +25 -0
- friendli-0.1.2/src/friendli/models/usermessage.py +33 -0
- friendli-0.1.2/src/friendli/py.typed +1 -0
- friendli-0.1.2/src/friendli/sdk.py +103 -0
- friendli-0.1.2/src/friendli/sdkconfiguration.py +50 -0
- friendli-0.1.2/src/friendli/serverless.py +252 -0
- friendli-0.1.2/src/friendli/types/__init__.py +21 -0
- friendli-0.1.2/src/friendli/types/basemodel.py +39 -0
- friendli-0.1.2/src/friendli/utils/__init__.py +99 -0
- friendli-0.1.2/src/friendli/utils/annotations.py +30 -0
- friendli-0.1.2/src/friendli/utils/enums.py +34 -0
- friendli-0.1.2/src/friendli/utils/eventstreaming.py +178 -0
- friendli-0.1.2/src/friendli/utils/forms.py +208 -0
- friendli-0.1.2/src/friendli/utils/headers.py +136 -0
- friendli-0.1.2/src/friendli/utils/logger.py +27 -0
- friendli-0.1.2/src/friendli/utils/metadata.py +118 -0
- friendli-0.1.2/src/friendli/utils/queryparams.py +205 -0
- friendli-0.1.2/src/friendli/utils/requestbodies.py +66 -0
- friendli-0.1.2/src/friendli/utils/retries.py +217 -0
- friendli-0.1.2/src/friendli/utils/security.py +192 -0
- friendli-0.1.2/src/friendli/utils/serializers.py +215 -0
- friendli-0.1.2/src/friendli/utils/url.py +155 -0
- friendli-0.1.2/src/friendli/utils/values.py +134 -0
friendli-0.1.2/PKG-INFO
ADDED
|
@@ -0,0 +1,646 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: friendli
|
|
3
|
+
Version: 0.1.2
|
|
4
|
+
Summary: Python Client SDK Generated by Speakeasy.
|
|
5
|
+
Author: Speakeasy
|
|
6
|
+
Requires-Python: >=3.8,<4.0
|
|
7
|
+
Classifier: Programming Language :: Python :: 3
|
|
8
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
14
|
+
Requires-Dist: eval-type-backport (>=0.2.0,<0.3.0)
|
|
15
|
+
Requires-Dist: httpx (>=0.27.0,<0.28.0)
|
|
16
|
+
Requires-Dist: jsonpath-python (>=1.0.6,<2.0.0)
|
|
17
|
+
Requires-Dist: pydantic (>=2.9.2,<2.10.0)
|
|
18
|
+
Requires-Dist: python-dateutil (==2.8.2)
|
|
19
|
+
Requires-Dist: typing-inspect (>=0.9.0,<0.10.0)
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
|
|
22
|
+
# friendli
|
|
23
|
+
|
|
24
|
+
Developer-friendly & type-safe Python SDK specifically catered to leverage *friendli* API.
|
|
25
|
+
|
|
26
|
+
<div align="left">
|
|
27
|
+
<a href="https://www.speakeasy.com/?utm_source=friendli&utm_campaign=python"><img src="https://custom-icon-badges.demolab.com/badge/-Built%20By%20Speakeasy-212015?style=for-the-badge&logoColor=FBE331&logo=speakeasy&labelColor=545454" /></a>
|
|
28
|
+
<a href="https://opensource.org/licenses/MIT">
|
|
29
|
+
<img src="https://img.shields.io/badge/License-MIT-blue.svg" style="width: 100px; height: 28px;" />
|
|
30
|
+
</a>
|
|
31
|
+
</div>
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
<br /><br />
|
|
35
|
+
> [!IMPORTANT]
|
|
36
|
+
> This SDK is not yet ready for production use. To complete setup please follow the steps outlined in your [workspace](https://app.speakeasy.com/org/friendliai/friendliai). Delete this section before > publishing to a package manager.
|
|
37
|
+
|
|
38
|
+
<!-- Start Summary [summary] -->
|
|
39
|
+
## Summary
|
|
40
|
+
|
|
41
|
+
Friendli Endpoints API Reference: This is an OpenAPI reference of Friendli Endpoints API.
|
|
42
|
+
<!-- End Summary [summary] -->
|
|
43
|
+
|
|
44
|
+
<!-- Start Table of Contents [toc] -->
|
|
45
|
+
## Table of Contents
|
|
46
|
+
|
|
47
|
+
* [SDK Installation](#sdk-installation)
|
|
48
|
+
* [IDE Support](#ide-support)
|
|
49
|
+
* [SDK Example Usage](#sdk-example-usage)
|
|
50
|
+
* [Available Resources and Operations](#available-resources-and-operations)
|
|
51
|
+
* [Server-sent event streaming](#server-sent-event-streaming)
|
|
52
|
+
* [Retries](#retries)
|
|
53
|
+
* [Error Handling](#error-handling)
|
|
54
|
+
* [Server Selection](#server-selection)
|
|
55
|
+
* [Custom HTTP Client](#custom-http-client)
|
|
56
|
+
* [Authentication](#authentication)
|
|
57
|
+
* [Debugging](#debugging)
|
|
58
|
+
<!-- End Table of Contents [toc] -->
|
|
59
|
+
|
|
60
|
+
<!-- Start SDK Installation [installation] -->
|
|
61
|
+
## SDK Installation
|
|
62
|
+
|
|
63
|
+
The SDK can be installed with either *pip* or *poetry* package managers.
|
|
64
|
+
|
|
65
|
+
### PIP
|
|
66
|
+
|
|
67
|
+
*PIP* is the default package installer for Python, enabling easy installation and management of packages from PyPI via the command line.
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
pip install git+<UNSET>.git
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Poetry
|
|
74
|
+
|
|
75
|
+
*Poetry* is a modern tool that simplifies dependency management and package publishing by using a single `pyproject.toml` file to handle project metadata and dependencies.
|
|
76
|
+
|
|
77
|
+
```bash
|
|
78
|
+
poetry add git+<UNSET>.git
|
|
79
|
+
```
|
|
80
|
+
<!-- End SDK Installation [installation] -->
|
|
81
|
+
|
|
82
|
+
<!-- Start IDE Support [idesupport] -->
|
|
83
|
+
## IDE Support
|
|
84
|
+
|
|
85
|
+
### PyCharm
|
|
86
|
+
|
|
87
|
+
Generally, the SDK will work well with most IDEs out of the box. However, when using PyCharm, you can enjoy much better integration with Pydantic by installing an additional plugin.
|
|
88
|
+
|
|
89
|
+
- [PyCharm Pydantic Plugin](https://docs.pydantic.dev/latest/integrations/pycharm/)
|
|
90
|
+
<!-- End IDE Support [idesupport] -->
|
|
91
|
+
|
|
92
|
+
<!-- Start SDK Example Usage [usage] -->
|
|
93
|
+
## SDK Example Usage
|
|
94
|
+
|
|
95
|
+
### Example
|
|
96
|
+
|
|
97
|
+
```python
|
|
98
|
+
# Synchronous Example
|
|
99
|
+
import friendli
|
|
100
|
+
from friendli import Friendli
|
|
101
|
+
import os
|
|
102
|
+
|
|
103
|
+
s = Friendli(
|
|
104
|
+
bearer_auth=os.getenv("FRIENDLI_BEARER_AUTH", ""),
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
res = s.inference.chat_completion(chat_completion_request_body={
|
|
108
|
+
"model": "meta-llama-3.1-8b-instruct",
|
|
109
|
+
"messages": [
|
|
110
|
+
{
|
|
111
|
+
"role": friendli.Role.SYSTEM,
|
|
112
|
+
"content": "You are a helpful assistant.",
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
"role": friendli.UserMessageRole.USER,
|
|
116
|
+
"content": "Hello!",
|
|
117
|
+
},
|
|
118
|
+
],
|
|
119
|
+
"max_tokens": 200,
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
if res is not None:
|
|
123
|
+
for event in res:
|
|
124
|
+
# handle event
|
|
125
|
+
print(event, flush=True)
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
</br>
|
|
129
|
+
|
|
130
|
+
The same SDK client can also be used to make asychronous requests by importing asyncio.
|
|
131
|
+
```python
|
|
132
|
+
# Asynchronous Example
|
|
133
|
+
import asyncio
|
|
134
|
+
import friendli
|
|
135
|
+
from friendli import Friendli
|
|
136
|
+
import os
|
|
137
|
+
|
|
138
|
+
async def main():
|
|
139
|
+
s = Friendli(
|
|
140
|
+
bearer_auth=os.getenv("FRIENDLI_BEARER_AUTH", ""),
|
|
141
|
+
)
|
|
142
|
+
res = await s.inference.chat_completion_async(chat_completion_request_body={
|
|
143
|
+
"model": "meta-llama-3.1-8b-instruct",
|
|
144
|
+
"messages": [
|
|
145
|
+
{
|
|
146
|
+
"role": friendli.Role.SYSTEM,
|
|
147
|
+
"content": "You are a helpful assistant.",
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
"role": friendli.UserMessageRole.USER,
|
|
151
|
+
"content": "Hello!",
|
|
152
|
+
},
|
|
153
|
+
],
|
|
154
|
+
"max_tokens": 200,
|
|
155
|
+
})
|
|
156
|
+
if res is not None:
|
|
157
|
+
for event in res:
|
|
158
|
+
# handle event
|
|
159
|
+
print(event, flush=True)
|
|
160
|
+
|
|
161
|
+
asyncio.run(main())
|
|
162
|
+
```
|
|
163
|
+
<!-- End SDK Example Usage [usage] -->
|
|
164
|
+
|
|
165
|
+
<!-- Start Available Resources and Operations [operations] -->
|
|
166
|
+
## Available Resources and Operations
|
|
167
|
+
|
|
168
|
+
<details open>
|
|
169
|
+
<summary>Available methods</summary>
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
### [inference](docs/sdks/inference/README.md)
|
|
173
|
+
|
|
174
|
+
* [chat_completion](docs/sdks/inference/README.md#chat_completion) - Chat completion
|
|
175
|
+
* [completion](docs/sdks/inference/README.md#completion) - Completion
|
|
176
|
+
* [tokenization](docs/sdks/inference/README.md#tokenization) - Tokenization
|
|
177
|
+
* [detokenization](docs/sdks/inference/README.md#detokenization) - Detokenization
|
|
178
|
+
|
|
179
|
+
### [serverless](docs/sdks/serverless/README.md)
|
|
180
|
+
|
|
181
|
+
* [tool_assisted_chat_completion](docs/sdks/serverless/README.md#tool_assisted_chat_completion) - Tool assisted chat completion
|
|
182
|
+
|
|
183
|
+
</details>
|
|
184
|
+
<!-- End Available Resources and Operations [operations] -->
|
|
185
|
+
|
|
186
|
+
<!-- Start Server-sent event streaming [eventstream] -->
|
|
187
|
+
## Server-sent event streaming
|
|
188
|
+
|
|
189
|
+
[Server-sent events][mdn-sse] are used to stream content from certain
|
|
190
|
+
operations. These operations will expose the stream as [Generator][generator] that
|
|
191
|
+
can be consumed using a simple `for` loop. The loop will
|
|
192
|
+
terminate when the server no longer has any events to send and closes the
|
|
193
|
+
underlying connection.
|
|
194
|
+
|
|
195
|
+
```python
|
|
196
|
+
import friendli
|
|
197
|
+
from friendli import Friendli
|
|
198
|
+
import os
|
|
199
|
+
|
|
200
|
+
s = Friendli(
|
|
201
|
+
bearer_auth=os.getenv("FRIENDLI_BEARER_AUTH", ""),
|
|
202
|
+
)
|
|
203
|
+
|
|
204
|
+
res = s.inference.chat_completion(chat_completion_request_body={
|
|
205
|
+
"model": "meta-llama-3.1-8b-instruct",
|
|
206
|
+
"messages": [
|
|
207
|
+
{
|
|
208
|
+
"role": friendli.Role.SYSTEM,
|
|
209
|
+
"content": "You are a helpful assistant.",
|
|
210
|
+
},
|
|
211
|
+
{
|
|
212
|
+
"role": friendli.UserMessageRole.USER,
|
|
213
|
+
"content": "Hello!",
|
|
214
|
+
},
|
|
215
|
+
],
|
|
216
|
+
"max_tokens": 200,
|
|
217
|
+
})
|
|
218
|
+
|
|
219
|
+
if res is not None:
|
|
220
|
+
for event in res:
|
|
221
|
+
# handle event
|
|
222
|
+
print(event, flush=True)
|
|
223
|
+
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
[mdn-sse]: https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events
|
|
227
|
+
[generator]: https://wiki.python.org/moin/Generators
|
|
228
|
+
<!-- End Server-sent event streaming [eventstream] -->
|
|
229
|
+
|
|
230
|
+
<!-- Start Retries [retries] -->
|
|
231
|
+
## Retries
|
|
232
|
+
|
|
233
|
+
Some of the endpoints in this SDK support retries. If you use the SDK without any configuration, it will fall back to the default retry strategy provided by the API. However, the default retry strategy can be overridden on a per-operation basis, or across the entire SDK.
|
|
234
|
+
|
|
235
|
+
To change the default retry strategy for a single API call, simply provide a `RetryConfig` object to the call:
|
|
236
|
+
```python
|
|
237
|
+
import friendli
|
|
238
|
+
from friendli import Friendli
|
|
239
|
+
from friendli.utils import BackoffStrategy, RetryConfig
|
|
240
|
+
import os
|
|
241
|
+
|
|
242
|
+
s = Friendli(
|
|
243
|
+
bearer_auth=os.getenv("FRIENDLI_BEARER_AUTH", ""),
|
|
244
|
+
)
|
|
245
|
+
|
|
246
|
+
res = s.inference.chat_completion(chat_completion_request_body={
|
|
247
|
+
"model": "meta-llama-3.1-8b-instruct",
|
|
248
|
+
"messages": [
|
|
249
|
+
{
|
|
250
|
+
"role": friendli.Role.SYSTEM,
|
|
251
|
+
"content": "You are a helpful assistant.",
|
|
252
|
+
},
|
|
253
|
+
{
|
|
254
|
+
"role": friendli.UserMessageRole.USER,
|
|
255
|
+
"content": "Hello!",
|
|
256
|
+
},
|
|
257
|
+
],
|
|
258
|
+
"max_tokens": 200,
|
|
259
|
+
},
|
|
260
|
+
RetryConfig("backoff", BackoffStrategy(1, 50, 1.1, 100), False))
|
|
261
|
+
|
|
262
|
+
if res is not None:
|
|
263
|
+
for event in res:
|
|
264
|
+
# handle event
|
|
265
|
+
print(event, flush=True)
|
|
266
|
+
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
If you'd like to override the default retry strategy for all operations that support retries, you can use the `retry_config` optional parameter when initializing the SDK:
|
|
270
|
+
```python
|
|
271
|
+
import friendli
|
|
272
|
+
from friendli import Friendli
|
|
273
|
+
from friendli.utils import BackoffStrategy, RetryConfig
|
|
274
|
+
import os
|
|
275
|
+
|
|
276
|
+
s = Friendli(
|
|
277
|
+
retry_config=RetryConfig("backoff", BackoffStrategy(1, 50, 1.1, 100), False),
|
|
278
|
+
bearer_auth=os.getenv("FRIENDLI_BEARER_AUTH", ""),
|
|
279
|
+
)
|
|
280
|
+
|
|
281
|
+
res = s.inference.chat_completion(chat_completion_request_body={
|
|
282
|
+
"model": "meta-llama-3.1-8b-instruct",
|
|
283
|
+
"messages": [
|
|
284
|
+
{
|
|
285
|
+
"role": friendli.Role.SYSTEM,
|
|
286
|
+
"content": "You are a helpful assistant.",
|
|
287
|
+
},
|
|
288
|
+
{
|
|
289
|
+
"role": friendli.UserMessageRole.USER,
|
|
290
|
+
"content": "Hello!",
|
|
291
|
+
},
|
|
292
|
+
],
|
|
293
|
+
"max_tokens": 200,
|
|
294
|
+
})
|
|
295
|
+
|
|
296
|
+
if res is not None:
|
|
297
|
+
for event in res:
|
|
298
|
+
# handle event
|
|
299
|
+
print(event, flush=True)
|
|
300
|
+
|
|
301
|
+
```
|
|
302
|
+
<!-- End Retries [retries] -->
|
|
303
|
+
|
|
304
|
+
<!-- Start Error Handling [errors] -->
|
|
305
|
+
## Error Handling
|
|
306
|
+
|
|
307
|
+
Handling errors in this SDK should largely match your expectations. All operations return a response object or raise an exception.
|
|
308
|
+
|
|
309
|
+
By default, an API error will raise a models.SDKError exception, which has the following properties:
|
|
310
|
+
|
|
311
|
+
| Property | Type | Description |
|
|
312
|
+
|-----------------|------------------|-----------------------|
|
|
313
|
+
| `.status_code` | *int* | The HTTP status code |
|
|
314
|
+
| `.message` | *str* | The error message |
|
|
315
|
+
| `.raw_response` | *httpx.Response* | The raw HTTP response |
|
|
316
|
+
| `.body` | *str* | The response content |
|
|
317
|
+
|
|
318
|
+
When custom error responses are specified for an operation, the SDK may also raise their associated exceptions. You can refer to respective *Errors* tables in SDK docs for more details on possible exception types for each operation. For example, the `chat_completion_async` method may raise the following exceptions:
|
|
319
|
+
|
|
320
|
+
| Error Type | Status Code | Content Type |
|
|
321
|
+
| --------------- | --------------- | --------------- |
|
|
322
|
+
| models.SDKError | 4XX, 5XX | \*/\* |
|
|
323
|
+
|
|
324
|
+
### Example
|
|
325
|
+
|
|
326
|
+
```python
|
|
327
|
+
import friendli
|
|
328
|
+
from friendli import Friendli, models
|
|
329
|
+
import os
|
|
330
|
+
|
|
331
|
+
s = Friendli(
|
|
332
|
+
bearer_auth=os.getenv("FRIENDLI_BEARER_AUTH", ""),
|
|
333
|
+
)
|
|
334
|
+
|
|
335
|
+
res = None
|
|
336
|
+
try:
|
|
337
|
+
res = s.inference.chat_completion(chat_completion_request_body={
|
|
338
|
+
"model": "meta-llama-3.1-8b-instruct",
|
|
339
|
+
"messages": [
|
|
340
|
+
{
|
|
341
|
+
"role": friendli.Role.SYSTEM,
|
|
342
|
+
"content": "You are a helpful assistant.",
|
|
343
|
+
},
|
|
344
|
+
{
|
|
345
|
+
"role": friendli.UserMessageRole.USER,
|
|
346
|
+
"content": "Hello!",
|
|
347
|
+
},
|
|
348
|
+
],
|
|
349
|
+
"max_tokens": 200,
|
|
350
|
+
})
|
|
351
|
+
|
|
352
|
+
if res is not None:
|
|
353
|
+
for event in res:
|
|
354
|
+
# handle event
|
|
355
|
+
print(event, flush=True)
|
|
356
|
+
|
|
357
|
+
except models.SDKError as e:
|
|
358
|
+
# handle exception
|
|
359
|
+
raise(e)
|
|
360
|
+
```
|
|
361
|
+
<!-- End Error Handling [errors] -->
|
|
362
|
+
|
|
363
|
+
<!-- Start Server Selection [server] -->
|
|
364
|
+
## Server Selection
|
|
365
|
+
|
|
366
|
+
### Select Server by Index
|
|
367
|
+
|
|
368
|
+
You can override the default server globally by passing a server index to the `server_idx: int` optional parameter when initializing the SDK client instance. The selected server will then be used as the default on the operations that use it. This table lists the indexes associated with the available servers:
|
|
369
|
+
|
|
370
|
+
| # | Server | Variables |
|
|
371
|
+
| - | ------ | --------- |
|
|
372
|
+
| 0 | `https://inference.friendli.ai` | None |
|
|
373
|
+
| 1 | `https://inference.friendli.ai/dedicated` | None |
|
|
374
|
+
|
|
375
|
+
#### Example
|
|
376
|
+
|
|
377
|
+
```python
|
|
378
|
+
import friendli
|
|
379
|
+
from friendli import Friendli
|
|
380
|
+
import os
|
|
381
|
+
|
|
382
|
+
s = Friendli(
|
|
383
|
+
server_idx=1,
|
|
384
|
+
bearer_auth=os.getenv("FRIENDLI_BEARER_AUTH", ""),
|
|
385
|
+
)
|
|
386
|
+
|
|
387
|
+
res = s.inference.chat_completion(chat_completion_request_body={
|
|
388
|
+
"model": "meta-llama-3.1-8b-instruct",
|
|
389
|
+
"messages": [
|
|
390
|
+
{
|
|
391
|
+
"role": friendli.Role.SYSTEM,
|
|
392
|
+
"content": "You are a helpful assistant.",
|
|
393
|
+
},
|
|
394
|
+
{
|
|
395
|
+
"role": friendli.UserMessageRole.USER,
|
|
396
|
+
"content": "Hello!",
|
|
397
|
+
},
|
|
398
|
+
],
|
|
399
|
+
"max_tokens": 200,
|
|
400
|
+
})
|
|
401
|
+
|
|
402
|
+
if res is not None:
|
|
403
|
+
for event in res:
|
|
404
|
+
# handle event
|
|
405
|
+
print(event, flush=True)
|
|
406
|
+
|
|
407
|
+
```
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
### Override Server URL Per-Client
|
|
411
|
+
|
|
412
|
+
The default server can also be overridden globally by passing a URL to the `server_url: str` optional parameter when initializing the SDK client instance. For example:
|
|
413
|
+
```python
|
|
414
|
+
import friendli
|
|
415
|
+
from friendli import Friendli
|
|
416
|
+
import os
|
|
417
|
+
|
|
418
|
+
s = Friendli(
|
|
419
|
+
server_url="https://inference.friendli.ai",
|
|
420
|
+
bearer_auth=os.getenv("FRIENDLI_BEARER_AUTH", ""),
|
|
421
|
+
)
|
|
422
|
+
|
|
423
|
+
res = s.inference.chat_completion(chat_completion_request_body={
|
|
424
|
+
"model": "meta-llama-3.1-8b-instruct",
|
|
425
|
+
"messages": [
|
|
426
|
+
{
|
|
427
|
+
"role": friendli.Role.SYSTEM,
|
|
428
|
+
"content": "You are a helpful assistant.",
|
|
429
|
+
},
|
|
430
|
+
{
|
|
431
|
+
"role": friendli.UserMessageRole.USER,
|
|
432
|
+
"content": "Hello!",
|
|
433
|
+
},
|
|
434
|
+
],
|
|
435
|
+
"max_tokens": 200,
|
|
436
|
+
})
|
|
437
|
+
|
|
438
|
+
if res is not None:
|
|
439
|
+
for event in res:
|
|
440
|
+
# handle event
|
|
441
|
+
print(event, flush=True)
|
|
442
|
+
|
|
443
|
+
```
|
|
444
|
+
|
|
445
|
+
### Override Server URL Per-Operation
|
|
446
|
+
|
|
447
|
+
The server URL can also be overridden on a per-operation basis, provided a server list was specified for the operation. For example:
|
|
448
|
+
```python
|
|
449
|
+
import friendli
|
|
450
|
+
from friendli import Friendli
|
|
451
|
+
import os
|
|
452
|
+
|
|
453
|
+
s = Friendli(
|
|
454
|
+
bearer_auth=os.getenv("FRIENDLI_BEARER_AUTH", ""),
|
|
455
|
+
)
|
|
456
|
+
|
|
457
|
+
res = s.serverless.tool_assisted_chat_completion(tool_assisted_completion_request_body={
|
|
458
|
+
"model": "meta-llama-3.1-8b-instruct",
|
|
459
|
+
"messages": [
|
|
460
|
+
{
|
|
461
|
+
"role": friendli.Role.SYSTEM,
|
|
462
|
+
"content": "You are a helpful assistant.",
|
|
463
|
+
},
|
|
464
|
+
{
|
|
465
|
+
"role": friendli.UserMessageRole.USER,
|
|
466
|
+
"content": "Hello!",
|
|
467
|
+
},
|
|
468
|
+
],
|
|
469
|
+
"max_tokens": 200,
|
|
470
|
+
"tools": [
|
|
471
|
+
{
|
|
472
|
+
"type": friendli.OtherBuiltInToolType.MATH_CALCULATOR,
|
|
473
|
+
},
|
|
474
|
+
{
|
|
475
|
+
"type": friendli.OtherBuiltInToolType.WEB_URL,
|
|
476
|
+
},
|
|
477
|
+
],
|
|
478
|
+
}, server_url="https://inference.friendli.ai")
|
|
479
|
+
|
|
480
|
+
if res is not None:
|
|
481
|
+
for event in res:
|
|
482
|
+
# handle event
|
|
483
|
+
print(event, flush=True)
|
|
484
|
+
|
|
485
|
+
```
|
|
486
|
+
<!-- End Server Selection [server] -->
|
|
487
|
+
|
|
488
|
+
<!-- Start Custom HTTP Client [http-client] -->
|
|
489
|
+
## Custom HTTP Client
|
|
490
|
+
|
|
491
|
+
The Python SDK makes API calls using the [httpx](https://www.python-httpx.org/) HTTP library. In order to provide a convenient way to configure timeouts, cookies, proxies, custom headers, and other low-level configuration, you can initialize the SDK client with your own HTTP client instance.
|
|
492
|
+
Depending on whether you are using the sync or async version of the SDK, you can pass an instance of `HttpClient` or `AsyncHttpClient` respectively, which are Protocol's ensuring that the client has the necessary methods to make API calls.
|
|
493
|
+
This allows you to wrap the client with your own custom logic, such as adding custom headers, logging, or error handling, or you can just pass an instance of `httpx.Client` or `httpx.AsyncClient` directly.
|
|
494
|
+
|
|
495
|
+
For example, you could specify a header for every request that this sdk makes as follows:
|
|
496
|
+
```python
|
|
497
|
+
from friendli import Friendli
|
|
498
|
+
import httpx
|
|
499
|
+
|
|
500
|
+
http_client = httpx.Client(headers={"x-custom-header": "someValue"})
|
|
501
|
+
s = Friendli(client=http_client)
|
|
502
|
+
```
|
|
503
|
+
|
|
504
|
+
or you could wrap the client with your own custom logic:
|
|
505
|
+
```python
|
|
506
|
+
from friendli import Friendli
|
|
507
|
+
from friendli.httpclient import AsyncHttpClient
|
|
508
|
+
import httpx
|
|
509
|
+
|
|
510
|
+
class CustomClient(AsyncHttpClient):
|
|
511
|
+
client: AsyncHttpClient
|
|
512
|
+
|
|
513
|
+
def __init__(self, client: AsyncHttpClient):
|
|
514
|
+
self.client = client
|
|
515
|
+
|
|
516
|
+
async def send(
|
|
517
|
+
self,
|
|
518
|
+
request: httpx.Request,
|
|
519
|
+
*,
|
|
520
|
+
stream: bool = False,
|
|
521
|
+
auth: Union[
|
|
522
|
+
httpx._types.AuthTypes, httpx._client.UseClientDefault, None
|
|
523
|
+
] = httpx.USE_CLIENT_DEFAULT,
|
|
524
|
+
follow_redirects: Union[
|
|
525
|
+
bool, httpx._client.UseClientDefault
|
|
526
|
+
] = httpx.USE_CLIENT_DEFAULT,
|
|
527
|
+
) -> httpx.Response:
|
|
528
|
+
request.headers["Client-Level-Header"] = "added by client"
|
|
529
|
+
|
|
530
|
+
return await self.client.send(
|
|
531
|
+
request, stream=stream, auth=auth, follow_redirects=follow_redirects
|
|
532
|
+
)
|
|
533
|
+
|
|
534
|
+
def build_request(
|
|
535
|
+
self,
|
|
536
|
+
method: str,
|
|
537
|
+
url: httpx._types.URLTypes,
|
|
538
|
+
*,
|
|
539
|
+
content: Optional[httpx._types.RequestContent] = None,
|
|
540
|
+
data: Optional[httpx._types.RequestData] = None,
|
|
541
|
+
files: Optional[httpx._types.RequestFiles] = None,
|
|
542
|
+
json: Optional[Any] = None,
|
|
543
|
+
params: Optional[httpx._types.QueryParamTypes] = None,
|
|
544
|
+
headers: Optional[httpx._types.HeaderTypes] = None,
|
|
545
|
+
cookies: Optional[httpx._types.CookieTypes] = None,
|
|
546
|
+
timeout: Union[
|
|
547
|
+
httpx._types.TimeoutTypes, httpx._client.UseClientDefault
|
|
548
|
+
] = httpx.USE_CLIENT_DEFAULT,
|
|
549
|
+
extensions: Optional[httpx._types.RequestExtensions] = None,
|
|
550
|
+
) -> httpx.Request:
|
|
551
|
+
return self.client.build_request(
|
|
552
|
+
method,
|
|
553
|
+
url,
|
|
554
|
+
content=content,
|
|
555
|
+
data=data,
|
|
556
|
+
files=files,
|
|
557
|
+
json=json,
|
|
558
|
+
params=params,
|
|
559
|
+
headers=headers,
|
|
560
|
+
cookies=cookies,
|
|
561
|
+
timeout=timeout,
|
|
562
|
+
extensions=extensions,
|
|
563
|
+
)
|
|
564
|
+
|
|
565
|
+
s = Friendli(async_client=CustomClient(httpx.AsyncClient()))
|
|
566
|
+
```
|
|
567
|
+
<!-- End Custom HTTP Client [http-client] -->
|
|
568
|
+
|
|
569
|
+
<!-- Start Authentication [security] -->
|
|
570
|
+
## Authentication
|
|
571
|
+
|
|
572
|
+
### Per-Client Security Schemes
|
|
573
|
+
|
|
574
|
+
This SDK supports the following security scheme globally:
|
|
575
|
+
|
|
576
|
+
| Name | Type | Scheme | Environment Variable |
|
|
577
|
+
| ---------------------- | ---------------------- | ---------------------- | ---------------------- |
|
|
578
|
+
| `bearer_auth` | http | HTTP Bearer | `FRIENDLI_BEARER_AUTH` |
|
|
579
|
+
|
|
580
|
+
To authenticate with the API the `bearer_auth` parameter must be set when initializing the SDK client instance. For example:
|
|
581
|
+
```python
|
|
582
|
+
import friendli
|
|
583
|
+
from friendli import Friendli
|
|
584
|
+
import os
|
|
585
|
+
|
|
586
|
+
s = Friendli(
|
|
587
|
+
bearer_auth=os.getenv("FRIENDLI_BEARER_AUTH", ""),
|
|
588
|
+
)
|
|
589
|
+
|
|
590
|
+
res = s.inference.chat_completion(chat_completion_request_body={
|
|
591
|
+
"model": "meta-llama-3.1-8b-instruct",
|
|
592
|
+
"messages": [
|
|
593
|
+
{
|
|
594
|
+
"role": friendli.Role.SYSTEM,
|
|
595
|
+
"content": "You are a helpful assistant.",
|
|
596
|
+
},
|
|
597
|
+
{
|
|
598
|
+
"role": friendli.UserMessageRole.USER,
|
|
599
|
+
"content": "Hello!",
|
|
600
|
+
},
|
|
601
|
+
],
|
|
602
|
+
"max_tokens": 200,
|
|
603
|
+
})
|
|
604
|
+
|
|
605
|
+
if res is not None:
|
|
606
|
+
for event in res:
|
|
607
|
+
# handle event
|
|
608
|
+
print(event, flush=True)
|
|
609
|
+
|
|
610
|
+
```
|
|
611
|
+
<!-- End Authentication [security] -->
|
|
612
|
+
|
|
613
|
+
<!-- Start Debugging [debug] -->
|
|
614
|
+
## Debugging
|
|
615
|
+
|
|
616
|
+
You can setup your SDK to emit debug logs for SDK requests and responses.
|
|
617
|
+
|
|
618
|
+
You can pass your own logger class directly into your SDK.
|
|
619
|
+
```python
|
|
620
|
+
from friendli import Friendli
|
|
621
|
+
import logging
|
|
622
|
+
|
|
623
|
+
logging.basicConfig(level=logging.DEBUG)
|
|
624
|
+
s = Friendli(debug_logger=logging.getLogger("friendli"))
|
|
625
|
+
```
|
|
626
|
+
|
|
627
|
+
You can also enable a default debug logger by setting an environment variable `FRIENDLI_DEBUG` to true.
|
|
628
|
+
<!-- End Debugging [debug] -->
|
|
629
|
+
|
|
630
|
+
<!-- Placeholder for Future Speakeasy SDK Sections -->
|
|
631
|
+
|
|
632
|
+
# Development
|
|
633
|
+
|
|
634
|
+
## Maturity
|
|
635
|
+
|
|
636
|
+
This SDK is in beta, and there may be breaking changes between versions without a major version update. Therefore, we recommend pinning usage
|
|
637
|
+
to a specific package version. This way, you can install the same version each time without breaking changes unless you are intentionally
|
|
638
|
+
looking for the latest version.
|
|
639
|
+
|
|
640
|
+
## Contributions
|
|
641
|
+
|
|
642
|
+
While we value open-source contributions to this SDK, this library is generated programmatically. Any manual changes added to internal files will be overwritten on the next generation.
|
|
643
|
+
We look forward to hearing your feedback. Feel free to open a PR or an issue with a proof of concept and we'll do our best to include it in a future release.
|
|
644
|
+
|
|
645
|
+
### SDK Created by [Speakeasy](https://www.speakeasy.com/?utm_source=friendli&utm_campaign=python)
|
|
646
|
+
|