EN

Perplexity AI API 指南 2026:为 AI 应用提供实时网络搜索

使用 Perplexity 搜索 API 为 AI 应用注入实时网络知识

返回教程列表🌐 Read in English
入门20 分钟

Perplexity AI API 指南 2026:为 AI 应用提供实时网络搜索

使用 Perplexity 搜索 API 为 AI 应用注入实时网络知识

完整的 Perplexity API 指南。涵盖 sonar 模型、引用、流式输出、多轮研究、竞争情报以及混合网络+私有知识搜索。

Perplexity AI API 指南 2026:为 AI 应用提供实时网络搜索

Perplexity 将 LLM 与实时网络搜索相结合,解决了数据陈旧的问题。

Perplexity 与自定义 RAG 对比

使用场景Perplexity自定义 RAG

时事新闻最佳陈旧 私有文档不支持你的数据 设置时间分钟级小时级 维护成本无持续投入

快速开始

python

Perplexity 使用兼容 OpenAI 的 API

from openai import OpenAI

client = OpenAI( api_key='pplx-your-key', base_url='https://api.perplexity.ai' )

基本搜索查询

python
r = client.chat.completions.create(
    model='sonar-pro',
    messages=[
        {'role': 'system', 'content': '请精确回答并引用来源。'},
        {'role': 'user', 'content': '2026 年最佳 AI 智能体框架有哪些?'}
    ]
)
print(r.choices[0].message.content)

模型

  • sonar:快速,约 $1/百万 token
  • sonar-pro:更高质量 + 引用,约 $3/百万
  • sonar-reasoning:逐步推理 + 搜索
  • sonar-reasoning-pro:最强推理 + 搜索
  • 流式输出

    python
    stream = client.chat.completions.create(
        model='sonar-pro',
        messages=[{'role': 'user', 'content': '2026 年 AI 芯片新闻'}],
        stream=True
    )
    for chunk in stream:
        if chunk.choices[0].delta.content:
            print(chunk.choices[0].delta.content, end='', flush=True)
    

    多轮研究助手

    python
    class ResearchBot:
        def __init__(self):
            self.history = []
        
        def ask(self, q: str) -> str:
            self.history.append({'role': 'user', 'content': q})
            r = client.chat.completions.create(
                model='sonar-pro',
                messages=[{'role': 'system', 'content': '研究助手。'}] + self.history
            )
            ans = r.choices[0].message.content
            self.history.append({'role': 'assistant', 'content': ans})
            return ans

    bot = ResearchBot() print(bot.ask('2026 年顶级向量数据库有哪些?')) print(bot.ask('比较这些数据库的定价')) # 记住上下文

    竞争情报

    python
    from datetime import datetime

    def check_competitors(companies): return { c: client.chat.completions.create( model='sonar', messages=[{'role': 'user', 'content': f'{c} 本周的最新消息?'}] ).choices[0].message.content for c in companies }

    report = check_competitors(['OpenAI', 'Anthropic', 'Mistral', 'Google DeepMind'])

    结论

    Perplexity 是构建需要最新信息的 AI 应用的最快方式。对于市场研究和竞争情报,它省去了向量数据库的维护工作。

    相关工具

    perplexityopenaipython