forked from helallao/perplexity-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_usage.py
More file actions
76 lines (54 loc) · 1.88 KB
/
async_usage.py
File metadata and controls
76 lines (54 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""
Async usage example.
This example demonstrates how to use the async API
for non-blocking operations and concurrent queries.
"""
import asyncio
import perplexity_async
async def single_query():
"""Make a single async query."""
print("\n[Example 1] Single async query")
print("-" * 60)
client = await perplexity_async.Client()
response = await client.search("What is machine learning?")
if "answer" in response:
print(response["answer"][:200] + "...")
async def multiple_queries():
"""Make multiple concurrent queries."""
print("\n[Example 2] Multiple concurrent queries")
print("-" * 60)
client = await perplexity_async.Client()
queries = ["What is AI?", "What is ML?", "What is deep learning?"]
# Execute concurrently
tasks = [client.search(q) for q in queries]
responses = await asyncio.gather(*tasks)
for query, response in zip(queries, responses):
print(f"\nQ: {query}")
if "answer" in response:
print(f"A: {response['answer'][:100]}...")
async def streaming_async():
"""Stream response asynchronously."""
print("\n[Example 3] Async streaming")
print("-" * 60)
client = await perplexity_async.Client()
response_gen = await client.search("Explain neural networks", stream=True)
chunk_count = 0
async for chunk in response_gen:
chunk_count += 1
if "answer" in chunk:
print(f"\nReceived answer in chunk {chunk_count}")
print(chunk["answer"][:150] + "...")
break
async def main():
"""Run all async examples."""
print("=" * 60)
print("Perplexity API - Async Examples")
print("=" * 60)
await single_query()
await multiple_queries()
await streaming_async()
print("\n" + "=" * 60)
print("Async examples completed!")
print("=" * 60)
if __name__ == "__main__":
asyncio.run(main())