Skip to content

Commit

Permalink
#407 make OpenAI api endpoint url configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
ken107 committed Jun 26, 2024
1 parent 17b2db8 commit e45d79d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
1 change: 1 addition & 0 deletions custom-voices.html
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ <h3 class="mt-3">Enable Custom Voices</h3>
<div class="form-group">
<div class="input-group">
<input type="text" class="form-control" id="openai-api-key" placeholder="API Key">
<input type="text" class="form-control" id="openai-url" placeholder="[Optional] API URL">
<div class="input-group-append">
<button type="button" class="btn btn-primary" id="openai-save-button">Save</button>
</div>
Expand Down
11 changes: 4 additions & 7 deletions js/custom-voices.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ $(function() {
}
if (items.openaiCreds) {
$("#openai-api-key").val(obfuscate(items.openaiCreds.apiKey))
$("#openai-url").val(items.openaiCreds.url || "")
}
if (items.azureCreds) {
$("#azure-region").val(items.azureCreds.region)
Expand Down Expand Up @@ -198,11 +199,12 @@ function testRiva(url) {
async function openaiSave() {
$(".status").hide()
const apiKey = $("#openai-api-key").val().trim()
const url = $("#openai-url").val().trim()
if (apiKey) {
$("#openai-progress").show()
try {
await testOpenai(apiKey)
await updateSettings({openaiCreds: {apiKey: apiKey}})
await openaiTtsEngine.test(apiKey, url)
await updateSettings({openaiCreds: {apiKey, url}})
$("#openai-success").text("ChatGPT voices are enabled.").show()
$("#openai-api-key").val(obfuscate(apiKey))
}
Expand All @@ -219,11 +221,6 @@ async function openaiSave() {
}
}

async function testOpenai(apiKey) {
const res = await fetch("https://api.openai.com/v1/models", {headers: {"Authorization": "Bearer " + apiKey}})
if (!res.ok) throw await res.json().then(x => x.error)
}



async function azureSave() {
Expand Down
12 changes: 11 additions & 1 deletion js/tts-engines.js
Original file line number Diff line number Diff line change
Expand Up @@ -1141,8 +1141,18 @@ function PhoneTtsEngine() {


function OpenaiTtsEngine() {
const defaultApiUrl = "https://api.openai.com/v1"
var audio, prefetchAudio
var isSpeaking = false
this.test = async function(apiKey, url) {
const res = await fetch((url || defaultApiUrl) + "/models", {
headers: {"Authorization": "Bearer " + apiKey}
})
if (!res.ok) {
const {error} = await res.json()
throw error
}
}
this.speak = function(utterance, options, onEvent) {
const urlPromise = Promise.resolve()
.then(() => {
Expand Down Expand Up @@ -1192,7 +1202,7 @@ function OpenaiTtsEngine() {
const matches = voice.voiceName.match(/^ChatGPT .* \((\w+)\)$/)
const voiceName = matches[1]
const {openaiCreds} = await getSettings(["openaiCreds"])
const res = await fetch("https://api.openai.com/v1/audio/speech", {
const res = await fetch((openaiCreds.url || defaultApiUrl) + "/audio/speech", {
method: "POST",
headers: {
"Content-Type": "application/json",
Expand Down

0 comments on commit e45d79d

Please sign in to comment.