data
Perplexity Lua API for KosmoKrator Agents
Agent-facing Lua documentation and function reference for the Perplexity KosmoKrator integration.Lua Namespace
Agents call this integration through app.integrations.perplexity.*.
Use lua_read_doc("integrations.perplexity") inside KosmoKrator to discover the same reference at runtime.
Call Lua from the Headless CLI
Use kosmo integrations:lua when a shell script, CI job, cron job, or another coding CLI should run a deterministic
Perplexity workflow without starting an interactive agent session.
kosmo integrations:lua --eval 'dump(app.integrations.perplexity.sonar_chat({messages = "example_messages", model = "example_model", temperature = 1, top_p = 1, max_tokens = 1, stop = "example_stop", response_format = "example_response_format", web_search_options = "example_web_search_options"}))' --json kosmo integrations:lua --eval 'print(docs.read("perplexity"))' --json
kosmo integrations:lua --eval 'print(docs.read("perplexity.sonar_chat"))' --json Workflow file
Put repeatable logic in a Lua file, then execute it with JSON output for the calling process.
local perplexity = app.integrations.perplexity
local result = perplexity.sonar_chat({messages = "example_messages", model = "example_model", temperature = 1, top_p = 1, max_tokens = 1, stop = "example_stop", response_format = "example_response_format", web_search_options = "example_web_search_options"})
dump(result) kosmo integrations:lua workflow.lua --json
kosmo integrations:lua workflow.lua --force --json integrations:lua exposes app.integrations.perplexity, app.mcp.*, docs.*, json.*, and regex.*. Use app.integrations.perplexity.default.* or app.integrations.perplexity.work.* when you configured named credential accounts.
MCP-only Lua
If the script only needs configured MCP servers and does not need Perplexity, use the narrower mcp:lua command.
# Use mcp:lua for MCP-only scripts; use integrations:lua for this integration namespace.
kosmo mcp:lua --eval 'dump(mcp.servers())' --json Agent-Facing Lua Docs
This is the rendered version of the full Lua documentation exposed to agents when they inspect the integration namespace.
Perplexity — Lua API Reference
Namespace: app.integrations.perplexity
This integration uses the current Perplexity APIs:
- Sonar chat:
POST /v1/sonar - Search:
POST /search - Async Sonar:
POST /v1/async/sonar,GET /v1/async/sonar,GET /v1/async/sonar/{id} - Agent responses:
POST /v1/agent - Embeddings:
POST /v1/embeddings,POST /v1/contextualizedembeddings - Models:
GET /v1/models
ask is a convenience wrapper over Sonar chat. It is not a separate upstream /ask endpoint.
chat
Create a Sonar chat completion.
Required:
messages: array of{ role = "system" | "user" | "assistant", content = "..." }
Common optional fields:
model: defaults tosonartemperature,top_p,max_tokensresponse_formatweb_search_options: pass Perplexity’s current web search options object- convenience search options:
search_mode,search_domain_filter,search_language_filter,search_recency_filter,return_images,return_related_questions,disable_search reasoning_effort,language_preference
local result = app.integrations.perplexity.chat({
messages = {
{ role = "user", content = "Summarize the current Perplexity Sonar API." }
},
model = "sonar-pro",
search_domain_filter = { "docs.perplexity.ai" },
return_related_questions = true
})
print(result.content)
for _, source in ipairs(result.citations or {}) do
print(source)
end
Normalized output includes id, model, content, role, finish_reason, usage, citations, search_results, images, and related_questions when present.
ask
Ask a one-shot Sonar question. This builds a single user message and calls the same Sonar chat endpoint as chat.
local result = app.integrations.perplexity.ask({
query = "What changed in the latest public Sonar API shape?",
model = "sonar",
search_recency_filter = "month"
})
local content = result.choices[1].message.content
print(content)
ask returns the raw Sonar response so agents can inspect the full choices, citations, search_results, usage, images, and related_questions payload.
search
Search the web and retrieve relevant page results without generating an answer.
local result = app.integrations.perplexity.search({
query = "Perplexity embeddings API",
max_results = 5,
search_domain_filter = { "docs.perplexity.ai" }
})
for _, item in ipairs(result.results or {}) do
print(item.title .. " " .. item.url)
end
create_async_sonar
Submit a long-running Sonar request and poll it later.
local created = app.integrations.perplexity.create_async_sonar({
query = "Create a detailed research brief on search-grounded LLM APIs.",
model = "sonar-deep-research",
idempotency_key = "research-brief-example-001"
})
print(created.id)
print(created.status)
You can pass messages instead of query for multi-turn requests.
list_async_sonar
List async Sonar requests for the configured account.
local result = app.integrations.perplexity.list_async_sonar({})
for _, request in ipairs(result.requests or {}) do
print(request.id .. " " .. request.status)
end
get_async_sonar
Retrieve one async Sonar request.
local result = app.integrations.perplexity.get_async_sonar({
request_id = "req_example"
})
print(result.status)
if result.response then
print(result.response.choices[1].message.content)
end
agent
Create a Perplexity Agent API response.
local result = app.integrations.perplexity.agent({
input = "Find three sources about agentic search APIs and summarize the tradeoffs.",
model = "perplexity/sonar"
})
for _, output in ipairs(result.output or {}) do
for _, content in ipairs(output.content or {}) do
if content.text then
print(content.text)
end
end
end
Use list_models for Agent API model ids.
embeddings
Create embeddings for a string or array of strings.
local result = app.integrations.perplexity.embeddings({
input = { "First document chunk", "Second document chunk" },
model = "pplx-embed-v1-0.6b",
encoding_format = "base64_int8"
})
print(result.model)
print(#(result.data or {}))
Embeddings are returned in Perplexity’s encoded format, usually base64-encoded compact vectors.
contextualized_embeddings
Create contextualized embeddings for chunks grouped by source document.
local result = app.integrations.perplexity.contextualized_embeddings({
input = {
{ "Document A chunk 1", "Document A chunk 2" },
{ "Document B chunk 1" }
},
model = "pplx-embed-context-v1-0.6b"
})
print(result.model)
The input is nested: each inner array is one document’s chunks.
list_models
List Perplexity Agent API models.
local result = app.integrations.perplexity.list_models({})
for _, model in ipairs(result.data or {}) do
print(model.id .. " " .. (model.owned_by or ""))
end
Multi-Account Usage
app.integrations.perplexity.chat({...})
app.integrations.perplexity.default.chat({...})
app.integrations.perplexity.research.chat({...})
The same tool names are available on each account namespace.
Raw agent markdown
# Perplexity — Lua API Reference
Namespace: `app.integrations.perplexity`
This integration uses the current Perplexity APIs:
- Sonar chat: `POST /v1/sonar`
- Search: `POST /search`
- Async Sonar: `POST /v1/async/sonar`, `GET /v1/async/sonar`, `GET /v1/async/sonar/{id}`
- Agent responses: `POST /v1/agent`
- Embeddings: `POST /v1/embeddings`, `POST /v1/contextualizedembeddings`
- Models: `GET /v1/models`
`ask` is a convenience wrapper over Sonar chat. It is not a separate upstream `/ask` endpoint.
## chat
Create a Sonar chat completion.
Required:
- `messages`: array of `{ role = "system" | "user" | "assistant", content = "..." }`
Common optional fields:
- `model`: defaults to `sonar`
- `temperature`, `top_p`, `max_tokens`
- `response_format`
- `web_search_options`: pass Perplexity's current web search options object
- convenience search options: `search_mode`, `search_domain_filter`, `search_language_filter`, `search_recency_filter`, `return_images`, `return_related_questions`, `disable_search`
- `reasoning_effort`, `language_preference`
```lua
local result = app.integrations.perplexity.chat({
messages = {
{ role = "user", content = "Summarize the current Perplexity Sonar API." }
},
model = "sonar-pro",
search_domain_filter = { "docs.perplexity.ai" },
return_related_questions = true
})
print(result.content)
for _, source in ipairs(result.citations or {}) do
print(source)
end
```
Normalized output includes `id`, `model`, `content`, `role`, `finish_reason`, `usage`, `citations`, `search_results`, `images`, and `related_questions` when present.
## ask
Ask a one-shot Sonar question. This builds a single user message and calls the same Sonar chat endpoint as `chat`.
```lua
local result = app.integrations.perplexity.ask({
query = "What changed in the latest public Sonar API shape?",
model = "sonar",
search_recency_filter = "month"
})
local content = result.choices[1].message.content
print(content)
```
`ask` returns the raw Sonar response so agents can inspect the full `choices`, `citations`, `search_results`, `usage`, `images`, and `related_questions` payload.
## search
Search the web and retrieve relevant page results without generating an answer.
```lua
local result = app.integrations.perplexity.search({
query = "Perplexity embeddings API",
max_results = 5,
search_domain_filter = { "docs.perplexity.ai" }
})
for _, item in ipairs(result.results or {}) do
print(item.title .. " " .. item.url)
end
```
## create_async_sonar
Submit a long-running Sonar request and poll it later.
```lua
local created = app.integrations.perplexity.create_async_sonar({
query = "Create a detailed research brief on search-grounded LLM APIs.",
model = "sonar-deep-research",
idempotency_key = "research-brief-example-001"
})
print(created.id)
print(created.status)
```
You can pass `messages` instead of `query` for multi-turn requests.
## list_async_sonar
List async Sonar requests for the configured account.
```lua
local result = app.integrations.perplexity.list_async_sonar({})
for _, request in ipairs(result.requests or {}) do
print(request.id .. " " .. request.status)
end
```
## get_async_sonar
Retrieve one async Sonar request.
```lua
local result = app.integrations.perplexity.get_async_sonar({
request_id = "req_example"
})
print(result.status)
if result.response then
print(result.response.choices[1].message.content)
end
```
## agent
Create a Perplexity Agent API response.
```lua
local result = app.integrations.perplexity.agent({
input = "Find three sources about agentic search APIs and summarize the tradeoffs.",
model = "perplexity/sonar"
})
for _, output in ipairs(result.output or {}) do
for _, content in ipairs(output.content or {}) do
if content.text then
print(content.text)
end
end
end
```
Use `list_models` for Agent API model ids.
## embeddings
Create embeddings for a string or array of strings.
```lua
local result = app.integrations.perplexity.embeddings({
input = { "First document chunk", "Second document chunk" },
model = "pplx-embed-v1-0.6b",
encoding_format = "base64_int8"
})
print(result.model)
print(#(result.data or {}))
```
Embeddings are returned in Perplexity's encoded format, usually base64-encoded compact vectors.
## contextualized_embeddings
Create contextualized embeddings for chunks grouped by source document.
```lua
local result = app.integrations.perplexity.contextualized_embeddings({
input = {
{ "Document A chunk 1", "Document A chunk 2" },
{ "Document B chunk 1" }
},
model = "pplx-embed-context-v1-0.6b"
})
print(result.model)
```
The input is nested: each inner array is one document's chunks.
## list_models
List Perplexity Agent API models.
```lua
local result = app.integrations.perplexity.list_models({})
for _, model in ipairs(result.data or {}) do
print(model.id .. " " .. (model.owned_by or ""))
end
```
## Multi-Account Usage
```lua
app.integrations.perplexity.chat({...})
app.integrations.perplexity.default.chat({...})
app.integrations.perplexity.research.chat({...})
```
The same tool names are available on each account namespace. local result = app.integrations.perplexity.sonar_chat({messages = "example_messages", model = "example_model", temperature = 1, top_p = 1, max_tokens = 1, stop = "example_stop", response_format = "example_response_format", web_search_options = "example_web_search_options"})
print(result) Functions
sonar_chat Read
Create a Perplexity Sonar chat completion. Supports multi-turn conversations, web search controls, citations, images, and related questions.
- Lua path
app.integrations.perplexity.sonar_chat- Full name
perplexity.perplexity_chat
| Parameter | Type | Required | Description |
|---|---|---|---|
messages | array | yes | Array of message objects, each with "role" (system, user, or assistant) and "content" (string). Example: [{"role": "user", "content": "What is AI?"}]. |
model | string | no | Sonar model to use. Common values: "sonar", "sonar-pro", "sonar-deep-research", "sonar-reasoning-pro". Defaults to "sonar". |
temperature | number | no | Sampling temperature (0.0–2.0). Lower values are more focused, higher values more creative. Defaults to 0.2. |
top_p | number | no | Nucleus sampling value between 0 and 1. |
max_tokens | integer | no | Maximum number of tokens in the response. |
stop | array | no | Stop sequence string or array of stop sequences. |
response_format | object | no | Optional response format object, including JSON schema formats supported by Sonar. |
web_search_options | object | no | Current Perplexity web search options object. Use this for advanced search/image/date filters. |
search_mode | string | no | Convenience web search option: "web", "academic", or "sec". |
search_domain_filter | array | no | Convenience web search option: domains to limit search results to (e.g., ["wikipedia.org"]). |
search_language_filter | array | no | Convenience web search option: ISO 639-1 language codes. |
search_recency_filter | string | no | Convenience web search option: "hour", "day", "week", "month", or "year". |
return_images | boolean | no | Convenience web search option: include image results. |
return_related_questions | boolean | no | Convenience web search option: include related questions. |
disable_search | boolean | no | Convenience web search option: disable web search. |
reasoning_effort | string | no | Reasoning effort for supported models: "minimal", "low", "medium", or "high". |
language_preference | string | no | Preferred response language as an ISO 639-1 code. |
ask Read
Ask Perplexity a one-shot question through the Sonar chat endpoint and get an answer with citations and optional search metadata.
- Lua path
app.integrations.perplexity.ask- Full name
perplexity.perplexity_ask
| Parameter | Type | Required | Description |
|---|---|---|---|
query | string | yes | The question or prompt to ask Perplexity AI. |
model | string | no | Sonar model to use. Common values: "sonar", "sonar-pro", "sonar-deep-research", "sonar-reasoning-pro". Defaults to "sonar". |
temperature | number | no | Sampling temperature (0.0–2.0). Lower values are more focused. Defaults to 0.2. |
top_p | number | no | Nucleus sampling value between 0 and 1. |
max_tokens | integer | no | Maximum number of tokens in the response. |
response_format | object | no | Optional response format object, including JSON schema formats supported by Sonar. |
web_search_options | object | no | Current Perplexity web search options object. Use this for advanced search/image/date filters. |
search_mode | string | no | Convenience web search option: "web", "academic", or "sec". |
search_domain_filter | array | no | List of domains to limit search results to (e.g., ["wikipedia.org"]). |
search_language_filter | array | no | Convenience web search option: ISO 639-1 language codes. |
return_images | boolean | no | Whether to return images in the response. Defaults to false. |
return_related_questions | boolean | no | Whether to return related questions. Defaults to false. |
search_recency_filter | string | no | Filter search results by recency: "hour", "day", "week", "month", or "year". |
disable_search | boolean | no | Convenience web search option: disable web search. |
reasoning_effort | string | no | Reasoning effort for supported models: "minimal", "low", "medium", or "high". |
language_preference | string | no | Preferred response language as an ISO 639-1 code. |
search_web Read
Search the web with Perplexity Search API and return relevant page results. Use this when you need sources/content, not a generated answer.
- Lua path
app.integrations.perplexity.search_web- Full name
perplexity.perplexity_search
| Parameter | Type | Required | Description |
|---|---|---|---|
query | string | yes | Search query string or array of query strings. |
country | string | no | Optional ISO 3166-1 alpha-2 country code. |
max_results | integer | no | Maximum number of results to return, from 1 to 20. |
max_tokens | integer | no | Maximum total context tokens. |
max_tokens_per_page | integer | no | Maximum tokens per returned page. |
search_language_filter | array | no | ISO 639-1 language codes. |
search_domain_filter | array | no | Domains to limit search results to. |
last_updated_after_filter | string | no | Only return pages updated after this date in MM/DD/YYYY format. |
last_updated_before_filter | string | no | Only return pages updated before this date in MM/DD/YYYY format. |
search_after_date_filter | string | no | Only return pages published after this date in MM/DD/YYYY format. |
search_before_date_filter | string | no | Only return pages published before this date in MM/DD/YYYY format. |
create_async_sonar Read
Submit an asynchronous Sonar chat completion request and return the request id/status for later polling.
- Lua path
app.integrations.perplexity.create_async_sonar- Full name
perplexity.perplexity_create_async_sonar
| Parameter | Type | Required | Description |
|---|---|---|---|
messages | array | no | Array of message objects. Required unless query is provided. |
query | string | no | Convenience single user query. Used only when messages is omitted. |
model | string | no | Sonar model to use. Defaults to "sonar-deep-research". |
idempotency_key | string | no | Optional unique key to prevent duplicate async requests. |
max_tokens | integer | no | Maximum number of completion tokens. |
temperature | number | no | Sampling temperature between 0 and 2. |
web_search_options | object | no | Current Perplexity web search options object. |
reasoning_effort | string | no | Reasoning effort for supported models: "minimal", "low", "medium", or "high". |
list_async_sonar Read
List asynchronous Sonar chat completion requests for the configured Perplexity account.
- Lua path
app.integrations.perplexity.list_async_sonar- Full name
perplexity.perplexity_list_async_sonar
| Parameter | Type | Required | Description |
|---|---|---|---|
| No parameters. | |||
get_async_sonar Read
Retrieve status and response data for a Perplexity asynchronous Sonar request.
- Lua path
app.integrations.perplexity.get_async_sonar- Full name
perplexity.perplexity_get_async_sonar
| Parameter | Type | Required | Description |
|---|---|---|---|
request_id | string | yes | Async Sonar request id returned by create_async_sonar. |
agent_response Read
Create a Perplexity Agent API response for an input prompt with optional model and tool configuration.
- Lua path
app.integrations.perplexity.agent_response- Full name
perplexity.perplexity_agent
| Parameter | Type | Required | Description |
|---|---|---|---|
input | string | yes | Input prompt for the Agent API. |
model | string | no | Optional Agent API model id from list_models. |
instructions | string | no | Optional high-level instructions for the agent response. |
tools | array | no | Optional Agent API tools array. |
temperature | number | no | Optional sampling temperature. |
max_output_tokens | integer | no | Optional maximum output tokens. |
embeddings Read
Create Perplexity embeddings for a string or array of strings.
- Lua path
app.integrations.perplexity.embeddings- Full name
perplexity.perplexity_embeddings
| Parameter | Type | Required | Description |
|---|---|---|---|
input | string | yes | Text string or array of text strings to embed. |
model | string | no | Embedding model. Defaults to "pplx-embed-v1-0.6b". |
dimensions | integer | no | Optional output dimensions. |
encoding_format | string | no | Embedding encoding format: "base64_int8" or "base64_binary". |
contextualized_embeddings Read
Create contextualized embeddings for document chunks grouped by source document.
- Lua path
app.integrations.perplexity.contextualized_embeddings- Full name
perplexity.perplexity_contextualized_embeddings
| Parameter | Type | Required | Description |
|---|---|---|---|
input | array | yes | Nested array where each inner array contains chunks from one document. |
model | string | no | Contextualized embedding model. Defaults to "pplx-embed-context-v1-0.6b". |
dimensions | integer | no | Optional output dimensions. |
encoding_format | string | no | Embedding encoding format: "base64_int8" or "base64_binary". |
list_models Read
List Perplexity Agent API models. Returns model IDs in OpenAI-compatible list format.
- Lua path
app.integrations.perplexity.list_models- Full name
perplexity.perplexity_list_models
| Parameter | Type | Required | Description |
|---|---|---|---|
| No parameters. | |||