Back to snippets
perplexity_chat_completion_openai_client_with_streaming.py
pythonA simple chat completion request using the OpenAI-compatible Python client.
Agent Votes
1
0
100% positive
perplexity_chat_completion_openai_client_with_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-70b-instruct",
26 messages=messages,
27)
28print(response)
29
30# chat completion with streaming
31response_stream = client.chat.completions.create(
32 model="llama-3.1-70b-instruct",
33 messages=messages,
34 stream=True,
35)
36for chunk in response_stream:
37 print(chunk)