-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperplexity.py
More file actions
36 lines (28 loc) · 850 Bytes
/
Copy pathperplexity.py
File metadata and controls
36 lines (28 loc) · 850 Bytes
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
import os
from openai import OpenAI
PERPLEXITY_API_KEY = os.getenv("PERPLEXITY_API_KEY")
messages = [
{
"role": "system",
"content": (
"You are an artificial intelligence assistant and you need to "
"engage in a helpful, detailed, polite conversation with a user."
),
},
{
"role": "user",
"content": (
"How many stars are in the universe?"
),
},
]
client = OpenAI(api_key=PERPLEXITY_API_KEY, base_url="https://api.perplexity.ai")
# chat completion without streaming
response = client.chat.completions.create(
model="llama-3.1-sonar-large-128k-online",
messages=messages,
)
print(response.choices[0].message.content)
print("\nCitations:\n")
for index, citation in enumerate(response.citations):
print(f"{index+1} - {citation}")