# Chat Completions (Advanced) — Perplexity

> Generates a model's response for the given chat conversation with multi-message support and Perplexity search controls. Docs: https://docs.perplexity.ai/api-reference/chat-completions-post

- Key: `perplexity-chat-completions-advanced`
- Type: Action (Write)
- Version: 0.0.4
- App: Perplexity (`perplexity`) — https://pipedream.com/apps/perplexity.md
- This page (HTML): https://pipedream.com/apps/perplexity/actions/chat-completions-advanced
- Hints: open-world
- Source: https://github.com/PipedreamHQ/pipedream/blob/master/components/perplexity/actions/chat-completions-advanced/chat-completions-advanced.mjs

## Props

| Prop | Type | Required | Description |
|---|---|---|---|
| `model` | `string` | Yes | The name of the model that will complete your prompt |
| `messages` | `string[]` | No | Array of message objects: [{ role: 'system'\|'user'\|'assistant', content: '...' }, ...]. If provided, 'role' and 'content' are ignored. |
| `system` | `string` | No | Optional system prompt injected as the first message |
| `role` | `string` | No | The role of the speaker in this turn of conversation. After the (optional) system message, user and assistant roles should alternate with 'user' then 'assistant', ending in 'user'. |
| `content` | `string` | No | The contents of the message in this turn of conversation |
| `temperature` | `string` | No | Sampling temperature. Higher values = more diverse output. |
| `topP` | `string` | No | Nucleus sampling probability mass. Use either temperature or top_p. |
| `maxTokens` | `integer` | No | Cap response length (Sonar Pro practical max output ~8k). |
| `stream` | `boolean` | No | Enable server-side streaming. This action will still buffer and return the final text. |
| `searchDomainFilter` | `string[]` | No | Limit web search to these domains (e.g., ['sec.gov','ft.com']) |
| `searchRecencyFilter` | `string` | No | Prefer recent sources (e.g., 'day', 'week', 'month', 'year') |
| `topK` | `integer` | No | Restrict number of retrieved items considered |
| `returnImages` | `boolean` | No | Ask API to include images when applicable |
| `returnRelatedQuestions` | `boolean` | No | Ask API to include related questions in response |

## Run it

**MCP**

```ts
import { Client } from "@modelcontextprotocol/sdk/client/index.js"
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"
import { PipedreamClient } from "@pipedream/sdk"

const pd = new PipedreamClient({
  projectId: process.env.PIPEDREAM_PROJECT_ID!,
  clientId: process.env.PIPEDREAM_CLIENT_ID!,
  clientSecret: process.env.PIPEDREAM_CLIENT_SECRET!,
  projectEnvironment: "production",
})

const accessToken = await pd.rawAccessToken

const transport = new StreamableHTTPClientTransport(
  new URL("https://remote.mcp.pipedream.net/v3"),
  {
    requestInit: {
      headers: {
        Authorization: `Bearer ${accessToken}`,
        "x-pd-project-id": process.env.PIPEDREAM_PROJECT_ID!,
        "x-pd-environment": "production",
        "x-pd-external-user-id": "{external_user_id}", // any stable ID for this user in your system
        "x-pd-app-slug": "perplexity",
      },
    },
  },
)

const mcp = new Client({ name: "my-agent", version: "1.0.0" })
await mcp.connect(transport)

const { tools } = await mcp.listTools()

// listTools() hands your model this tool's input schema, so it can
// fill the arguments itself:
const result = await mcp.callTool({
  name: "perplexity-chat-completions-advanced",
  arguments: {
    model: "Model",
    messages: ["Messages (JSON strings or UI collection)"],
  },
})
```

**TypeScript**

```ts
import { PipedreamClient } from "@pipedream/sdk"

const pd = new PipedreamClient({
  projectId: process.env.PIPEDREAM_PROJECT_ID!,
  clientId: process.env.PIPEDREAM_CLIENT_ID!,
  clientSecret: process.env.PIPEDREAM_CLIENT_SECRET!,
  projectEnvironment: "production",
})

const result = await pd.actions.run({
  id: "perplexity-chat-completions-advanced",
  externalUserId: "{external_user_id}", // any stable ID for this user in your system
  configuredProps: {
    perplexity: { authProvisionId: "apn_xxxxxxx" },
    model: "Model",
    messages: ["Messages (JSON strings or UI collection)"],
  },
})

console.log(result)
```

**cURL**

```bash
curl -X POST https://api.pipedream.com/v1/connect/{project_id}/actions/run \
  -H "Content-Type: application/json" \
  -H "X-PD-Environment: production" \
  -H "Authorization: Bearer {access_token}" \
  -d '{
    "external_user_id": "{external_user_id}",
    "id": "perplexity-chat-completions-advanced",
    "configured_props": {
      "perplexity": { "authProvisionId": "apn_xxxxxxx" },
      "model": "Model",
      "messages": ["Messages (JSON strings or UI collection)"]
    }
  }'
```

---

- App: https://pipedream.com/apps/perplexity.md · All apps: https://pipedream.com/apps
