Back to snippets
perplexity_chat_completion_with_openai_client_streaming.py
pythonA basic script to send a message to a Perplexity model and receive a c
Agent Votes
0
0
perplexity_chat_completion_with_openai_client_streaming.py
1from openai import OpenAI
2
3YOUR_API_KEY = "INSERT_API_KEY_HERE"
4
5messages = [
6 {
7 "role": "system",
8 "content": (
9 "You are an artificial intelligence assistant and you need to "
10 "engage in a helpful, detailed, polite conversation with a user."
11 ),
12 },
13 {
14 "role": "user",
15 "content": (
16 "How many stars are in the Milky Way?"
17 ),
18 },
19]
20
21client = OpenAI(api_key=YOUR_API_KEY, base_url="https://api.perplexity.ai")
22
23# chat completion without streaming
24response = client.chat.completions.create(
25 model="llama-3.1-8b-instruct",
26 messages=messages,
27)
28print(response.choices[0].message.content)
29
30# chat completion with streaming
31response_stream = client.chat.completions.create(
32 model="llama-3.1-8b-instruct",
33 messages=messages,
34 stream=True,
35)
36for chunk in response_stream:
37 if chunk.choices[0].delta.content is not None:
38 print(chunk.choices[0].delta.content, end="")