Skip to content

Commit 945f7c2

Browse files
feat(api): add /chat/completions and /async/chat/completions
1 parent 2cda817 commit 945f7c2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2539
-57
lines changed

.stats.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
configured_endpoints: 2
2-
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/perplexity-ai%2Fperplexity-185831f484c464517fc6618626a9df301132e9985cbf4b96f1db7d2d0efbe49c.yml
3-
openapi_spec_hash: 45730bd4915f28dea6ab7a945bbc3f25
4-
config_hash: 0ce8ec3ce8abe4cf5d15af7506ca235c
1+
configured_endpoints: 6
2+
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/perplexity-ai%2Fperplexity-0a20b2c1a864613083f602f3589f85caa0e39b04c29c0a37db76f6398dfd2f50.yml
3+
openapi_spec_hash: 5c8d07ba17d180b472679f2b85def126
4+
config_hash: d9165b5fe0f6ad59bc12cb11dfbb17a8

README.md

Lines changed: 89 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,16 @@ client = Perplexity(
3232
api_key=os.environ.get("PERPLEXITY_API_KEY"), # This is the default and can be omitted
3333
)
3434

35-
search = client.search.create(
36-
query="string",
35+
completion = client.chat.completions.create(
36+
messages=[
37+
{
38+
"role": "user",
39+
"content": "Tell me about the latest developments in AI",
40+
}
41+
],
42+
model="sonar",
3743
)
38-
print(search.id)
44+
print(completion.id)
3945
```
4046

4147
While you can provide an `api_key` keyword argument,
@@ -58,10 +64,16 @@ client = AsyncPerplexity(
5864

5965

6066
async def main() -> None:
61-
search = await client.search.create(
62-
query="string",
67+
completion = await client.chat.completions.create(
68+
messages=[
69+
{
70+
"role": "user",
71+
"content": "Tell me about the latest developments in AI",
72+
}
73+
],
74+
model="sonar",
6375
)
64-
print(search.id)
76+
print(completion.id)
6577

6678

6779
asyncio.run(main())
@@ -93,10 +105,16 @@ async def main() -> None:
93105
api_key="My API Key",
94106
http_client=DefaultAioHttpClient(),
95107
) as client:
96-
search = await client.search.create(
97-
query="string",
108+
completion = await client.chat.completions.create(
109+
messages=[
110+
{
111+
"role": "user",
112+
"content": "Tell me about the latest developments in AI",
113+
}
114+
],
115+
model="sonar",
98116
)
99-
print(search.id)
117+
print(completion.id)
100118

101119

102120
asyncio.run(main())
@@ -111,6 +129,28 @@ Nested request parameters are [TypedDicts](https://docs.python.org/3/library/typ
111129

112130
Typed requests and responses provide autocomplete and documentation within your editor. If you would like to see type errors in VS Code to help catch bugs earlier, set `python.analysis.typeCheckingMode` to `basic`.
113131

132+
## Nested params
133+
134+
Nested parameters are dictionaries, typed using `TypedDict`, for example:
135+
136+
```python
137+
from perplexity import Perplexity
138+
139+
client = Perplexity()
140+
141+
completion = client.chat.completions.create(
142+
messages=[
143+
{
144+
"content": "string",
145+
"role": "system",
146+
}
147+
],
148+
model="sonar",
149+
web_search_options={},
150+
)
151+
print(completion.web_search_options)
152+
```
153+
114154
## Handling errors
115155

116156
When the library is unable to connect to the API (for example, due to network connection problems or a timeout), a subclass of `perplexity.APIConnectionError` is raised.
@@ -127,8 +167,14 @@ from perplexity import Perplexity
127167
client = Perplexity()
128168

129169
try:
130-
client.search.create(
131-
query="string",
170+
client.chat.completions.create(
171+
messages=[
172+
{
173+
"role": "user",
174+
"content": "What is the capital of France?",
175+
}
176+
],
177+
model="sonar",
132178
)
133179
except perplexity.APIConnectionError as e:
134180
print("The server could not be reached")
@@ -172,8 +218,14 @@ client = Perplexity(
172218
)
173219

174220
# Or, configure per-request:
175-
client.with_options(max_retries=5).search.create(
176-
query="string",
221+
client.with_options(max_retries=5).chat.completions.create(
222+
messages=[
223+
{
224+
"role": "user",
225+
"content": "What is the capital of France?",
226+
}
227+
],
228+
model="sonar",
177229
)
178230
```
179231

@@ -197,8 +249,14 @@ client = Perplexity(
197249
)
198250

199251
# Override per-request:
200-
client.with_options(timeout=5.0).search.create(
201-
query="string",
252+
client.with_options(timeout=5.0).chat.completions.create(
253+
messages=[
254+
{
255+
"role": "user",
256+
"content": "What is the capital of France?",
257+
}
258+
],
259+
model="sonar",
202260
)
203261
```
204262

@@ -240,13 +298,17 @@ The "raw" Response object can be accessed by prefixing `.with_raw_response.` to
240298
from perplexity import Perplexity
241299

242300
client = Perplexity()
243-
response = client.search.with_raw_response.create(
244-
query="string",
301+
response = client.chat.completions.with_raw_response.create(
302+
messages=[{
303+
"role": "user",
304+
"content": "What is the capital of France?",
305+
}],
306+
model="sonar",
245307
)
246308
print(response.headers.get('X-My-Header'))
247309

248-
search = response.parse() # get the object that `search.create()` would have returned
249-
print(search.id)
310+
completion = response.parse() # get the object that `chat.completions.create()` would have returned
311+
print(completion.id)
250312
```
251313

252314
These methods return an [`APIResponse`](https://github.com/ppl-ai/perplexity-py/tree/main/src/perplexity/_response.py) object.
@@ -260,8 +322,14 @@ The above interface eagerly reads the full response body when you make the reque
260322
To stream the response body, use `.with_streaming_response` instead, which requires a context manager and only reads the response body once you call `.read()`, `.text()`, `.json()`, `.iter_bytes()`, `.iter_text()`, `.iter_lines()` or `.parse()`. In the async client, these are async methods.
261323

262324
```python
263-
with client.search.with_streaming_response.create(
264-
query="string",
325+
with client.chat.completions.with_streaming_response.create(
326+
messages=[
327+
{
328+
"role": "user",
329+
"content": "What is the capital of France?",
330+
}
331+
],
332+
model="sonar",
265333
) as response:
266334
print(response.headers.get("X-My-Header"))
267335

api.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,45 @@
1+
# Shared Types
2+
3+
```python
4+
from perplexity.types import ChatChoice, ChatMessage, SearchResult, UsageInfo
5+
```
6+
7+
# Chat
8+
9+
## Completions
10+
11+
Types:
12+
13+
```python
14+
from perplexity.types.chat import CompletionCreateResponse
15+
```
16+
17+
Methods:
18+
19+
- <code title="post /chat/completions">client.chat.completions.<a href="./src/perplexity/resources/chat/completions.py">create</a>(\*\*<a href="src/perplexity/types/chat/completion_create_params.py">params</a>) -> <a href="./src/perplexity/types/chat/completion_create_response.py">CompletionCreateResponse</a></code>
20+
21+
# Async
22+
23+
## Chat
24+
25+
### Completions
26+
27+
Types:
28+
29+
```python
30+
from perplexity.types.async_.chat import (
31+
CompletionCreateResponse,
32+
CompletionListResponse,
33+
CompletionGetResponse,
34+
)
35+
```
36+
37+
Methods:
38+
39+
- <code title="post /async/chat/completions">client.async*.chat.completions.<a href="./src/perplexity/resources/async*/chat/completions.py">create</a>(\*\*<a href="src/perplexity/types/async_/chat/completion_create_params.py">params</a>) -> <a href="./src/perplexity/types/async_/chat/completion_create_response.py">CompletionCreateResponse</a></code>
40+
- <code title="get /async/chat/completions">client.async*.chat.completions.<a href="./src/perplexity/resources/async*/chat/completions.py">list</a>(\*\*<a href="src/perplexity/types/async_/chat/completion_list_params.py">params</a>) -> <a href="./src/perplexity/types/async_/chat/completion_list_response.py">CompletionListResponse</a></code>
41+
- <code title="get /async/chat/completions/{request_id}">client.async*.chat.completions.<a href="./src/perplexity/resources/async*/chat/completions.py">get</a>(request*id) -> <a href="./src/perplexity/types/async*/chat/completion_get_response.py">CompletionGetResponse</a></code>
42+
143
# Search
244

345
Types:

src/perplexity/_client.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
SyncAPIClient,
3030
AsyncAPIClient,
3131
)
32+
from .resources.chat import chat
33+
from .resources.async_ import async_
3234

3335
__all__ = [
3436
"Timeout",
@@ -43,6 +45,8 @@
4345

4446

4547
class Perplexity(SyncAPIClient):
48+
chat: chat.ChatResource
49+
async_: async_.AsyncResource
4650
search: search.SearchResource
4751
content: content.ContentResource
4852
with_raw_response: PerplexityWithRawResponse
@@ -102,6 +106,8 @@ def __init__(
102106
_strict_response_validation=_strict_response_validation,
103107
)
104108

109+
self.chat = chat.ChatResource(self)
110+
self.async_ = async_.AsyncResource(self)
105111
self.search = search.SearchResource(self)
106112
self.content = content.ContentResource(self)
107113
self.with_raw_response = PerplexityWithRawResponse(self)
@@ -213,6 +219,8 @@ def _make_status_error(
213219

214220

215221
class AsyncPerplexity(AsyncAPIClient):
222+
chat: chat.AsyncChatResource
223+
async_: async_.AsyncAsyncResource
216224
search: search.AsyncSearchResource
217225
content: content.AsyncContentResource
218226
with_raw_response: AsyncPerplexityWithRawResponse
@@ -272,6 +280,8 @@ def __init__(
272280
_strict_response_validation=_strict_response_validation,
273281
)
274282

283+
self.chat = chat.AsyncChatResource(self)
284+
self.async_ = async_.AsyncAsyncResource(self)
275285
self.search = search.AsyncSearchResource(self)
276286
self.content = content.AsyncContentResource(self)
277287
self.with_raw_response = AsyncPerplexityWithRawResponse(self)
@@ -384,24 +394,32 @@ def _make_status_error(
384394

385395
class PerplexityWithRawResponse:
386396
def __init__(self, client: Perplexity) -> None:
397+
self.chat = chat.ChatResourceWithRawResponse(client.chat)
398+
self.async_ = async_.AsyncResourceWithRawResponse(client.async_)
387399
self.search = search.SearchResourceWithRawResponse(client.search)
388400
self.content = content.ContentResourceWithRawResponse(client.content)
389401

390402

391403
class AsyncPerplexityWithRawResponse:
392404
def __init__(self, client: AsyncPerplexity) -> None:
405+
self.chat = chat.AsyncChatResourceWithRawResponse(client.chat)
406+
self.async_ = async_.AsyncAsyncResourceWithRawResponse(client.async_)
393407
self.search = search.AsyncSearchResourceWithRawResponse(client.search)
394408
self.content = content.AsyncContentResourceWithRawResponse(client.content)
395409

396410

397411
class PerplexityWithStreamedResponse:
398412
def __init__(self, client: Perplexity) -> None:
413+
self.chat = chat.ChatResourceWithStreamingResponse(client.chat)
414+
self.async_ = async_.AsyncResourceWithStreamingResponse(client.async_)
399415
self.search = search.SearchResourceWithStreamingResponse(client.search)
400416
self.content = content.ContentResourceWithStreamingResponse(client.content)
401417

402418

403419
class AsyncPerplexityWithStreamedResponse:
404420
def __init__(self, client: AsyncPerplexity) -> None:
421+
self.chat = chat.AsyncChatResourceWithStreamingResponse(client.chat)
422+
self.async_ = async_.AsyncAsyncResourceWithStreamingResponse(client.async_)
405423
self.search = search.AsyncSearchResourceWithStreamingResponse(client.search)
406424
self.content = content.AsyncContentResourceWithStreamingResponse(client.content)
407425

src/perplexity/resources/__init__.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

3+
from .chat import (
4+
ChatResource,
5+
AsyncChatResource,
6+
ChatResourceWithRawResponse,
7+
AsyncChatResourceWithRawResponse,
8+
ChatResourceWithStreamingResponse,
9+
AsyncChatResourceWithStreamingResponse,
10+
)
11+
from .async_ import (
12+
AsyncResource,
13+
AsyncAsyncResource,
14+
AsyncResourceWithRawResponse,
15+
AsyncAsyncResourceWithRawResponse,
16+
AsyncResourceWithStreamingResponse,
17+
AsyncAsyncResourceWithStreamingResponse,
18+
)
319
from .search import (
420
SearchResource,
521
AsyncSearchResource,
@@ -18,6 +34,18 @@
1834
)
1935

2036
__all__ = [
37+
"ChatResource",
38+
"AsyncChatResource",
39+
"ChatResourceWithRawResponse",
40+
"AsyncChatResourceWithRawResponse",
41+
"ChatResourceWithStreamingResponse",
42+
"AsyncChatResourceWithStreamingResponse",
43+
"AsyncResource",
44+
"AsyncAsyncResource",
45+
"AsyncResourceWithRawResponse",
46+
"AsyncAsyncResourceWithRawResponse",
47+
"AsyncResourceWithStreamingResponse",
48+
"AsyncAsyncResourceWithStreamingResponse",
2149
"SearchResource",
2250
"AsyncSearchResource",
2351
"SearchResourceWithRawResponse",
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2+
3+
from .chat import (
4+
ChatResource,
5+
AsyncChatResource,
6+
ChatResourceWithRawResponse,
7+
AsyncChatResourceWithRawResponse,
8+
ChatResourceWithStreamingResponse,
9+
AsyncChatResourceWithStreamingResponse,
10+
)
11+
from .async_ import (
12+
AsyncResource,
13+
AsyncAsyncResource,
14+
AsyncResourceWithRawResponse,
15+
AsyncAsyncResourceWithRawResponse,
16+
AsyncResourceWithStreamingResponse,
17+
AsyncAsyncResourceWithStreamingResponse,
18+
)
19+
20+
__all__ = [
21+
"ChatResource",
22+
"AsyncChatResource",
23+
"ChatResourceWithRawResponse",
24+
"AsyncChatResourceWithRawResponse",
25+
"ChatResourceWithStreamingResponse",
26+
"AsyncChatResourceWithStreamingResponse",
27+
"AsyncResource",
28+
"AsyncAsyncResource",
29+
"AsyncResourceWithRawResponse",
30+
"AsyncAsyncResourceWithRawResponse",
31+
"AsyncResourceWithStreamingResponse",
32+
"AsyncAsyncResourceWithStreamingResponse",
33+
]

0 commit comments

Comments
 (0)