Perplexity Sonar
最后更新于
POST /v1/chat/completions
Content-Type: application/json
Accept: application/json
Authorization: Bearer $YOUR_API_KEY
{
"model": "sonar",
"messages": [
{
"role": "system",
"content": "Be precise and concise."
},
{
"role": "user",
"content": "How many stars are there in our galaxy?"
}
]
}curl https://gateway.theturbo.ai/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Accept: application/json" \
-H "Authorization: Bearer $YOUR_API_KEY" \
-d "{
\"model\": \"sonar\",
\"messages\": [{
\"role\": \"system\",
\"content\": \"Be precise and concise.\"
},
{
\"role\": \"user\",
\"content\": \"How many stars are there in our galaxy?\"
}
]
}"package main
import (
"context"
"fmt"
"github.com/openai/openai-go"
"github.com/openai/openai-go/option"
)
func main() {
apiKey := "sk-123456789012345678901234567890123456789012345678"
client := openai.NewClient(
option.WithAPIKey(apiKey),
option.WithBaseURL("https://gateway.theturbo.ai/v1"),
)
resp, err := client.Chat.Completions.New(
context.Background(),
openai.ChatCompletionNewParams{
Model: "sonar",
Messages: []openai.ChatCompletionMessageParamUnion{
openai.SystemMessage("Be precise and concise."),
openai.UserMessage("How many stars are there in our galaxy?"),
},
},
)
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(resp.Choices[0].Message.Content)
}
#!/usr/bin/env python3
from openai import OpenAI
def main():
api_key = "sk-123456789012345678901234567890123456789012345678"
client = OpenAI(
api_key=api_key,
base_url="https://gateway.theturbo.ai/v1"
)
response = client.chat.completions.create(
model="sonar",
messages=[
{"role": "system", "content": "Be precise and concise."},
{"role": "user", "content": "How many stars are there in our galaxy?"}
]
)
print(response.choices[0].message.content)
if __name__ == "__main__":
main()
{
"id": "3c90c3cc-0d44-4b50-8888-8dd25736052a",
"model": "sonar",
"object": "chat.completion",
"created": 1724369245,
"citations": [
"https://www.astronomy.com/science/astro-for-kids-how-many-stars-are-there-in-space/",
"https://www.esa.int/Science_Exploration/Space_Science/Herschel/How_many_stars_are_there_in_the_Universe",
"https://www.space.com/25959-how-many-stars-are-in-the-milky-way.html",
"https://www.space.com/26078-how-many-stars-are-there.html",
"https://en.wikipedia.org/wiki/Milky_Way",
"https://www.littlepassports.com/blog/space/how-many-stars-are-in-the-universe/?srsltid=AfmBOoqWVymRloolU4KZBI9-LotDIoTnzhKYKCw7vVkaIifhjrEU66_5"
],
"choices": [
{
"index": 0,
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": "The number of stars in the Milky Way galaxy is estimated to be between 100 billion and 400 billion stars. The most recent estimates from the Gaia mission suggest that there are approximately 100 to 400 billion stars in the Milky Way, with significant uncertainties remaining due to the difficulty in detecting faint red dwarfs and brown dwarfs."
},
"delta": {
"role": "assistant",
"content": ""
}
}
],
"usage": {
"prompt_tokens": 14,
"completion_tokens": 70,
"total_tokens": 84
}
}