|
| 1 | +import app from "../../perplexity.app.mjs"; |
| 2 | + |
| 3 | +export default { |
| 4 | + key: "perplexity-chat-completions-advanced", |
| 5 | + name: "Chat Completions (Advanced)", |
| 6 | + description: "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", |
| 7 | + version: "0.0.1", |
| 8 | + annotations: { |
| 9 | + destructiveHint: false, |
| 10 | + openWorldHint: true, |
| 11 | + readOnlyHint: false, |
| 12 | + }, |
| 13 | + type: "action", |
| 14 | + props: { |
| 15 | + app, |
| 16 | + model: { |
| 17 | + propDefinition: [ |
| 18 | + app, |
| 19 | + "model", |
| 20 | + ], |
| 21 | + }, |
| 22 | + messages: { |
| 23 | + type: "string[]", |
| 24 | + label: "Messages (JSON strings or UI collection)", |
| 25 | + description: "Array of message objects: [{ role: 'system'|'user'|'assistant', content: '...' }, ...]. If provided, 'role' and 'content' are ignored.", |
| 26 | + optional: true, |
| 27 | + }, |
| 28 | + system: { |
| 29 | + type: "string", |
| 30 | + label: "System", |
| 31 | + description: "Optional system prompt injected as the first message", |
| 32 | + optional: true, |
| 33 | + }, |
| 34 | + role: { |
| 35 | + propDefinition: [ |
| 36 | + app, |
| 37 | + "role", |
| 38 | + ], |
| 39 | + optional: true, |
| 40 | + }, |
| 41 | + content: { |
| 42 | + propDefinition: [ |
| 43 | + app, |
| 44 | + "content", |
| 45 | + ], |
| 46 | + optional: true, |
| 47 | + }, |
| 48 | + temperature: { |
| 49 | + type: "string", |
| 50 | + label: "Temperature", |
| 51 | + description: "Sampling temperature. Higher values = more diverse output.", |
| 52 | + optional: true, |
| 53 | + }, |
| 54 | + topP: { |
| 55 | + type: "string", |
| 56 | + label: "Top-p", |
| 57 | + description: "Nucleus sampling probability mass. Use either temperature or top_p.", |
| 58 | + optional: true, |
| 59 | + }, |
| 60 | + maxTokens: { |
| 61 | + type: "integer", |
| 62 | + label: "Max Output Tokens", |
| 63 | + description: "Cap response length (Sonar Pro practical max output ~8k).", |
| 64 | + optional: true, |
| 65 | + }, |
| 66 | + stream: { |
| 67 | + type: "boolean", |
| 68 | + label: "Stream", |
| 69 | + description: "Enable server-side streaming. This action will still buffer and return the final text.", |
| 70 | + optional: true, |
| 71 | + default: false, |
| 72 | + }, |
| 73 | + searchDomainFilter: { |
| 74 | + type: "string[]", |
| 75 | + label: "Search Domain Filter", |
| 76 | + description: "Limit web search to these domains (e.g., ['sec.gov','ft.com'])", |
| 77 | + optional: true, |
| 78 | + }, |
| 79 | + searchRecencyFilter: { |
| 80 | + type: "string", |
| 81 | + label: "Search Recency Filter", |
| 82 | + description: "Prefer recent sources (e.g., 'day', 'week', 'month', 'year')", |
| 83 | + optional: true, |
| 84 | + }, |
| 85 | + topK: { |
| 86 | + type: "integer", |
| 87 | + label: "Top K", |
| 88 | + description: "Restrict number of retrieved items considered", |
| 89 | + optional: true, |
| 90 | + }, |
| 91 | + returnImages: { |
| 92 | + type: "boolean", |
| 93 | + label: "Return Images", |
| 94 | + description: "Ask API to include images when applicable", |
| 95 | + optional: true, |
| 96 | + default: false, |
| 97 | + }, |
| 98 | + returnRelatedQuestions: { |
| 99 | + type: "boolean", |
| 100 | + label: "Return Related Questions", |
| 101 | + description: "Ask API to include related questions in response", |
| 102 | + optional: true, |
| 103 | + default: false, |
| 104 | + }, |
| 105 | + }, |
| 106 | + |
| 107 | + async run({ $ }) { |
| 108 | + // Build messages array |
| 109 | + let messages = []; |
| 110 | + const provided = this.messages && this.messages.length > 0; |
| 111 | + |
| 112 | + if (provided) { |
| 113 | + // Accept messages as array of either objects or JSON strings |
| 114 | + messages = this.messages.map((m) => |
| 115 | + typeof m === "string" |
| 116 | + ? JSON.parse(m) |
| 117 | + : m); |
| 118 | + } else { |
| 119 | + if (!this.content) { |
| 120 | + throw new Error("Either provide `messages` or `content`."); |
| 121 | + } |
| 122 | + // Back-compat single-turn |
| 123 | + if (this.system) { |
| 124 | + messages.push({ |
| 125 | + role: "system", |
| 126 | + content: this.system, |
| 127 | + }); |
| 128 | + } |
| 129 | + messages.push({ |
| 130 | + role: this.role || "user", |
| 131 | + content: this.content, |
| 132 | + }); |
| 133 | + } |
| 134 | + |
| 135 | + const data = { |
| 136 | + model: this.model, |
| 137 | + messages, |
| 138 | + // Generation knobs |
| 139 | + ...(this.temperature != null && { |
| 140 | + temperature: +this.temperature, |
| 141 | + }), |
| 142 | + ...(this.topP != null && { |
| 143 | + top_p: +this.topP, |
| 144 | + }), |
| 145 | + ...(this.maxTokens != null && { |
| 146 | + max_tokens: this.maxTokens, |
| 147 | + }), |
| 148 | + ...(this.stream != null && { |
| 149 | + stream: this.stream, |
| 150 | + }), |
| 151 | + // Perplexity search controls |
| 152 | + ...(this.searchDomainFilter && { |
| 153 | + search_domain_filter: this.searchDomainFilter, |
| 154 | + }), |
| 155 | + ...(this.searchRecencyFilter && { |
| 156 | + search_recency_filter: this.searchRecencyFilter, |
| 157 | + }), |
| 158 | + ...(this.topK != null && { |
| 159 | + top_k: this.topK, |
| 160 | + }), |
| 161 | + ...(this.returnImages != null && { |
| 162 | + return_images: this.returnImages, |
| 163 | + }), |
| 164 | + ...(this.returnRelatedQuestions != null && { |
| 165 | + return_related_questions: this.returnRelatedQuestions, |
| 166 | + }), |
| 167 | + }; |
| 168 | + |
| 169 | + const response = await this.app.chatCompletions({ |
| 170 | + $, |
| 171 | + data, |
| 172 | + }); |
| 173 | + |
| 174 | + $.export( |
| 175 | + "$summary", |
| 176 | + `Perplexity ${this.model} responded${ |
| 177 | + this.stream |
| 178 | + ? " (streaming buffered)" |
| 179 | + : "" |
| 180 | + }`, |
| 181 | + ); |
| 182 | + |
| 183 | + return response; |
| 184 | + }, |
| 185 | +}; |
0 commit comments