diff --git a/components/perplexity/actions/chat-completions/chat-completions.mjs b/components/perplexity/actions/chat-completions/chat-completions.mjs new file mode 100644 index 0000000000000..96e44c3ef154f --- /dev/null +++ b/components/perplexity/actions/chat-completions/chat-completions.mjs @@ -0,0 +1,48 @@ +import app from "../../perplexity.app.mjs"; + +export default { + key: "perplexity-chat-completions", + name: "Chat Completions", + description: "Generates a model's response for the given chat conversation. [See the documentation](https://docs.perplexity.ai/reference/post_chat_completions)", + version: "0.0.1", + type: "action", + props: { + app, + model: { + propDefinition: [ + app, + "model", + ], + }, + content: { + propDefinition: [ + app, + "content", + ], + }, + role: { + propDefinition: [ + app, + "role", + ], + }, + }, + async run({ $ }) { + const response = await this.app.chatCompletions({ + $, + data: { + model: this.model, + messages: [ + { + role: this.role, + content: this.content, + }, + ], + }, + }); + + $.export("$summary", "Successfully generated a response from the selected model"); + + return response; + }, +}; diff --git a/components/perplexity/common/constants.mjs b/components/perplexity/common/constants.mjs new file mode 100644 index 0000000000000..7c1bf16783ff4 --- /dev/null +++ b/components/perplexity/common/constants.mjs @@ -0,0 +1,15 @@ +export default { + MODELS: [ + "sonar-small-chat", + "sonar-small-online", + "sonar-medium-chat", + "sonar-medium-online", + "mistral-7b-instruct", + "mixtral-8x7b-instruct", + ], + ROLES: [ + "system", + "user", + "assistant", + ], +}; diff --git a/components/perplexity/package.json b/components/perplexity/package.json index 9e6ad06f0b0bd..4caff5d7319f1 100644 --- a/components/perplexity/package.json +++ b/components/perplexity/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/perplexity", - "version": "0.0.1", + "version": "0.1.0", "description": "Pipedream Perplexity Components", "main": "perplexity.app.mjs", "keywords": [ @@ -11,5 +11,8 @@ "author": "Pipedream (https://pipedream.com/)", "publishConfig": { "access": "public" + }, + "dependencies": { + "@pipedream/platform": "^1.6.5" } -} \ No newline at end of file +} diff --git a/components/perplexity/perplexity.app.mjs b/components/perplexity/perplexity.app.mjs index 8cb047fa417ed..b7cba4a78ee95 100644 --- a/components/perplexity/perplexity.app.mjs +++ b/components/perplexity/perplexity.app.mjs @@ -1,11 +1,54 @@ +import { axios } from "@pipedream/platform"; +import constants from "./common/constants.mjs"; + export default { type: "app", app: "perplexity", - propDefinitions: {}, + propDefinitions: { + model: { + type: "string", + label: "Model", + description: "The name of the model that will complete your prompt", + options: constants.MODELS, + }, + content: { + type: "string", + label: "Content", + description: "The contents of the message in this turn of conversation", + }, + role: { + type: "string", + label: "Role", + description: "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'.", + options: constants.ROLES, + }, + }, methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); + _baseUrl() { + return "https://api.perplexity.ai"; + }, + async _makeRequest(opts = {}) { + const { + $ = this, + path, + headers, + ...otherOpts + } = opts; + return axios($, { + ...otherOpts, + url: this._baseUrl() + path, + headers: { + ...headers, + Authorization: `Bearer ${this.$auth.api_key}`, + }, + }); + }, + async chatCompletions(args = {}) { + return this._makeRequest({ + method: "post", + path: "/chat/completions", + ...args, + }); }, }, -}; \ No newline at end of file +}; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b1a2e33b0415c..22164e8b5d512 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -6080,7 +6080,10 @@ importers: '@pipedream/platform': 1.6.2 components/perplexity: - specifiers: {} + specifiers: + '@pipedream/platform': ^1.6.5 + dependencies: + '@pipedream/platform': 1.6.5 components/perry_github_test: specifiers: {}