PerplexityAPI

diegosouzapw2026-10-28查看 328复制 750

Perplexity API 集成,用于构建具备实时网络搜索和引用的 AI 驱动应用。在使用 Perplexity Sonar 模型(sonar、sonar-pro、sonar-reasoning-pro、sonar-deep-research)、Chat Completions(/chat/completions 和 /async/chat/completions)、Agentic Research(/responses 及 fast-search/pro-search/deep-research 等预设)、Search API(/search)、web_search 选项(search_domain_filter、search_recency_filter、search_mode、search_context_size、user_locatio...

Download (.md)

📘如何使用这个技能

  1. 1
    复制技能内容·点击上方「Copy SKILL.md」复制全部内容,或 Download 下载到本地
  2. 2
    打开你的 AI 客户端·支持 Claude Code、Trae、Cursor、ChatGPT、DeepSeek、Kimi 等任意 AI 助手
  3. 3
    粘贴为系统提示/知识文件·Claude Code:放入 .claude/skills/ 目录;普通对话:作为首条消息发送并说「请严格按这份技能文档执行」
  4. 4
    直接提出你的需求·AI 会按技能定义的工作流、格式和约束来处理,无需再解释背景

💡 提示:技能生效后,每次对话只需一句需求,AI 会按 SKILL.md 的流程执行。

SKILL.md3894 chars
---
name: perplexity-api
description: "Perplexity API integration for building AI-powered applications with real-time web search and citations. Use when working with Perplexity Sonar models (sonar, sonar-pro, sonar-reasoning-pro, sonar-deep-research), Chat Completions (/chat/completions and /async/chat/completions), Agentic Research (/responses and presets like fast-search/pro-search/deep-research), Search API (/search), web_search options (search_domain_filter, search_recency_filter, search_mode, search_context_size, user_location), file/image attachments (file_url, image_url), returning images/videos, structured outputs (json_schema or regex), streaming (SSE), OpenAI-compatible usage via base URL https://api.perplexity.ai/v2, or Perplexity SDKs (Python: pip install perplexityai; TypeScript: @perplexity-ai/perplexity_ai). Triggers on Perplexity, Sonar, citations, web-grounded chat, pro-search, deep research, /responses, /chat/completions, or PERPLEXITY_API_KEY."
---

# Perplexity API

Integrate Perplexity's APIs (search-grounded chat, agentic research, and web search) without leaking API keys to the browser.

## Choose an API (fast routing)

- Use **Chat Completions** for "answer with citations" Q&A and lightweight research: `POST https://api.perplexity.ai/v2/chat/completions`.
- Use **Async Chat Completions** for long-running deep research with `sonar-deep-research`: `POST https://api.perplexity.ai/v2/async/chat/completions`.
- Use **Agentic Research (Responses)** for multi-step tool-using research flows: `POST https://api.perplexity.ai/v2/responses`.
- Use **Search API** when you want ranked results without synthesis: `POST https://api.perplexity.ai/v2/search`.

## Quick start (SDK-first)

### Installation

```bash
# Python
pip install perplexityai

# TypeScript/Node.js
npm install @perplexity-ai/perplexity_ai
```

### Client Setup

**TypeScript (Node.js):**
```ts
import Perplexity from "@perplexity-ai/perplexity_ai";

const apiKey = process.env["PERPLEXITY_API_KEY"];
if (!apiKey) throw new Error("Missing PERPLEXITY_API_KEY");

const client = new Perplexity({ apiKey });
```

**Python:**
```py
from perplexity import Perplexity

client = Perplexity()  # uses PERPLEXITY_API_KEY env var
```

## Chat Completions (Sonar + citations)

Basic "answer with sources" request:

**Python:**
```py
completion = client.chat.completions.create(
    model="sonar-pro",
    messages=[
        {"role": "system", "content": "Be precise and concise."},
        {"role": "user", "content": "What happened in tech news today?"}
    ],
    web_search_options={"search_context_size": "medium"},
)
print(completion.choices[0].message.content)

# Access citations (URLs of sources used)
for citation in completion.citations:
    print(f"Source: {citation}")
```

**TypeScript:**
```ts
const chunk = await client.chat.completions.create({
  model: "sonar-pro",
  messages: [
    { role: "system", content: "Be precise and concise." },
    { role: "user", content: "What happened in tech news today?" },
  ],
  web_search_options: { searchContextSize: "medium" },
});
console.log(chunk.content);
```

## OpenAI compatibility (when you already use OpenAI SDKs)

- Set the OpenAI SDK base URL to `https://api.perplexity.ai/v2`.
- Use **Chat Completions** (`/chat/completions`) for Sonar models and **Responses** (`/responses`) for Agentic Research.
- Avoid TypeScript `any` casts; prefer the Perplexity SDKs or a typed `fetch` wrapper when you need Perplexity-only request fields.

## References

- Chat Completions (models, web_search_options, structured outputs, streaming, attachments, return_images/videos): `references/chat-completions.md`
- Agentic Research / Responses (presets, tools web_search/fetch_url, streaming events): `references/agentic-research.md`
- Search API: `references/search-api.md`
- API key management (auth tokens/groups): `references/key-management.md`
来源:skillsdirectory

评论0

登录后参与评论

还没有评论,来抢沙发吧