forked from helallao/perplexity-ai
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_upload.py
More file actions
96 lines (76 loc) · 2.4 KB
/
Copy pathfile_upload.py
File metadata and controls
96 lines (76 loc) · 2.4 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""
File upload example.
This example demonstrates how to upload files with queries
for document analysis and Q&A.
"""
import perplexity
from pathlib import Path
def main():
"""Run file upload example."""
print("=" * 60)
print("Perplexity API - File Upload Example")
print("=" * 60)
# Note: File uploads require an account with cookies
# This example shows the API structure
print("\nNote: This example requires Perplexity account cookies")
print("See README for instructions on obtaining cookies\n")
# Example with cookies (replace with actual cookies)
cookies = {
# 'next-auth.csrf-token': 'your-token',
# 'next-auth.session-token': 'your-session-token',
}
if not cookies or not any(cookies.values()):
print("No cookies provided. Showing example code only.\n")
print("Example code:")
print("-" * 60)
print("""
# With actual cookies:
client = perplexity.Client(cookies)
# Upload a text file
with open('document.txt', 'r') as f:
response = client.search(
'Summarize this document',
files={'document.txt': f.read()}
)
# Upload a PDF (as bytes)
with open('report.pdf', 'rb') as f:
response = client.search(
'What are the key findings in this report?',
files={'report.pdf': f.read()}
)
# Multiple files
files = {
'doc1.txt': open('doc1.txt').read(),
'doc2.txt': open('doc2.txt').read(),
}
response = client.search(
'Compare these two documents',
files=files
)
""")
print("-" * 60)
return
# Actual implementation with cookies
client = perplexity.Client(cookies)
print(f"Client created with account")
print(f"File uploads available: {client.file_upload}")
# Create a sample file
sample_file = Path("sample.txt")
sample_file.write_text("This is a sample document about artificial intelligence.")
try:
with open(sample_file, "r") as f:
response = client.search("What is this document about?", files={"sample.txt": f.read()})
if "answer" in response:
print("\nResponse:")
print("-" * 60)
print(response["answer"])
print("-" * 60)
finally:
# Cleanup
if sample_file.exists():
sample_file.unlink()
print("\n" + "=" * 60)
print("File upload example completed!")
print("=" * 60)
if __name__ == "__main__":
main()