You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+89-21Lines changed: 89 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,10 +32,16 @@ client = Perplexity(
32
32
api_key=os.environ.get("PERPLEXITY_API_KEY"), # This is the default and can be omitted
33
33
)
34
34
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",
37
43
)
38
-
print(search.id)
44
+
print(completion.id)
39
45
```
40
46
41
47
While you can provide an `api_key` keyword argument,
@@ -58,10 +64,16 @@ client = AsyncPerplexity(
58
64
59
65
60
66
asyncdefmain() -> 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",
63
75
)
64
-
print(search.id)
76
+
print(completion.id)
65
77
66
78
67
79
asyncio.run(main())
@@ -93,10 +105,16 @@ async def main() -> None:
93
105
api_key="My API Key",
94
106
http_client=DefaultAioHttpClient(),
95
107
) 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",
98
116
)
99
-
print(search.id)
117
+
print(completion.id)
100
118
101
119
102
120
asyncio.run(main())
@@ -111,6 +129,28 @@ Nested request parameters are [TypedDicts](https://docs.python.org/3/library/typ
111
129
112
130
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`.
113
131
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
+
114
154
## Handling errors
115
155
116
156
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
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)
250
312
```
251
313
252
314
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
260
322
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.
261
323
262
324
```python
263
-
with client.search.with_streaming_response.create(
264
-
query="string",
325
+
with client.chat.completions.with_streaming_response.create(
0 commit comments