Skip to content

Commit

Permalink
Custom ChatGPT prompts enabled in beta.
Browse files Browse the repository at this point in the history
  • Loading branch information
igoramadas committed Nov 7, 2023
1 parent 5bbb1a7 commit 303a43d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
13 changes: 12 additions & 1 deletion pages/account/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
</n-link>
</div>
<div class="mt-n1">
<h3 class="mb-2">{{ user.isPro ? "FTP auto update" : "FTP auto update (PRO only)" }}</h3>
<h3 class="mb-2">FTP auto update{{ user.isPro ? "" : " (PRO only)" }}</h3>
<div class="body-2">Strautomator can automatically update your cycling FTP and your estimated fitness level based on your recent activities.</div>
<v-switch class="mt-2" title="FTP auto-update" v-model="ftpAutoUpdate" :disabled="!user.isPro" :label="ftpAutoUpdate ? 'Yes, auto-update my Strava FTP' : 'No, leave my Strava FTP alone'"></v-switch>
</div>
Expand Down Expand Up @@ -158,6 +158,14 @@
</v-radio-group>
</div>
</div>
<div class="mt-4" v-if="$store.state.beta">
<h3 class="mb-2">ChatGPT custom prompt{{ user.isPro ? "" : " (PRO only)" }}</h3>
<div class="body-2">
You can enhance the generated activity names with ChatGPT by using your own custom prompt, that will be appended to the default one.
<n-link to="/help?q=chatgpt prompt" title="More details about the privacy mode" nuxt>More details...</n-link>
</div>
<v-text-field v-model="chatGptPrompt" class="mt-2" label="Prompt" maxlength="100" @blur="delaySavePreferences()" outlined rounded></v-text-field>
</div>
</v-card-text>
</v-card>
<h3 class="mt-5 mb-3">Status: {{ $store.state.user.isPro ? "PRO" : "Free" }} account</h3>
Expand Down Expand Up @@ -365,6 +373,7 @@ export default {
const noSuffixes = preferences.noSuffixes || false
const ftpAutoUpdate = preferences.ftpAutoUpdate || false
const language = preferences.language || "en"
const chatGptPrompt = preferences.chatGptPrompt || ""
const weatherProvider = user.isPro ? preferences.weatherProvider || null : null
const weatherUnit = preferences.weatherUnit || "c"
const windSpeedUnit = preferences.windSpeedUnit ? preferences.windSpeedUnit : weatherUnit == "f" ? "mph" : "kph"
Expand Down Expand Up @@ -420,6 +429,7 @@ export default {
minDateReset: this.$dayjs().format(dateFormat),
maxDateReset: this.$dayjs().add(1, "year").format(dateFormat),
language: language,
chatGptPrompt: chatGptPrompt,
weatherProvider: weatherProvider,
weatherUnit: weatherUnit,
windSpeedUnit: windSpeedUnit,
Expand Down Expand Up @@ -623,6 +633,7 @@ export default {
weatherUnit: this.weatherUnit,
windSpeedUnit: this.windSpeedUnit,
language: this.language,
chatGptPrompt: this.chatGptPrompt,
dateResetCounter: this.resetCounter ? arrDate.join("-") : false
}
Expand Down
25 changes: 19 additions & 6 deletions src/routes/api/users.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Strautomator API: Users

import {logHelper, gdpr, recipes, subscriptions, strava, users, RecipeData, RecipeStatsData, UserData, UserPreferences} from "strautomator-core"
import {logHelper, gdpr, openai, recipes, subscriptions, strava, users, RecipeData, RecipeStatsData, UserData, UserPreferences} from "strautomator-core"
import auth from "../auth"
import dayjs from "../../dayjs"
import _ from "lodash"
Expand Down Expand Up @@ -149,16 +149,16 @@ router.post("/:userId/preferences", async (req: express.Request, res: express.Re
preferences.weatherProvider = req.body.weatherProvider
}

// Make sure linksOn is valid.
if (preferenceChanged("linksOn")) {
preferences.linksOn = req.body.linksOn
}

// Make sure ftpAutoUpdate is valid.
if (preferenceChanged("ftpAutoUpdate") && user.isPro) {
preferences.ftpAutoUpdate = req.body.ftpAutoUpdate ? true : false
}

// Make sure linksOn is valid.
if (preferenceChanged("linksOn")) {
preferences.linksOn = req.body.linksOn
}

// Make sure weather unit is valid.
if (preferenceChanged("weatherUnit")) {
preferences.weatherUnit = req.body.weatherUnit != "c" ? "f" : "c"
Expand Down Expand Up @@ -214,6 +214,19 @@ router.post("/:userId/preferences", async (req: express.Request, res: express.Re
}
}

// Set the ChatGPT custom prompt? Make sure it ends with a period and passes the OpenAI moderation.
if (preferenceChanged("chatGptPrompt") && user.isPro && user.isBeta) {
const lastChar = req.body.chatGptPrompt.substring(req.body.chatGptPrompt.length - 1)
if (![".", "!", "?"].includes(lastChar)) {
req.body.chatGptPrompt += "."
}
const failedCategories = await openai.validatePrompt(user, req.body.chatGptPrompt)
if (failedCategories?.length > 0) {
throw new Error(`ChatGPt prompt failed moderation: ${failedCategories.join(", ")}`)
}
preferences.chatGptPrompt = req.body.chatGptPrompt.substring(0, 100).trim()
}

// User details to be updated.
const data: Partial<UserData> = {
id: user.id,
Expand Down

0 comments on commit 303a43d

Please sign in to comment.