Skip to content

Commit

Permalink
openai base url as settings
Browse files Browse the repository at this point in the history
  • Loading branch information
nbonamy committed Jun 29, 2024
1 parent 2ca557d commit 443c236
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 53 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ To use Internet search you need a [Tavily API key](https://app.tavily.com/home).

## DONE

- [x] OpenAI base URL as settings
- [x] DALL-E as tool
- [x] Google Gemini API
- [x] Prompt anywhere
Expand Down
2 changes: 2 additions & 0 deletions defaults/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"engines": {
"openai": {
"apiKey": "",
"baseURL": "https://api.openai.com/v1",
"models": {
"chat": [],
"image": []
Expand Down Expand Up @@ -93,6 +94,7 @@
}
},
"ollama": {
"baseURL": "https://api.ollama.com",
"models": {
"chat": [],
"image": []
Expand Down
76 changes: 26 additions & 50 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"@el3um4s/run-vbs": "^1.1.2",
"@excalidraw/markdown-to-text": "^0.1.2",
"@google/generative-ai": "^0.11.1",
"@iktakahiro/markdown-it-katex": "^4.0.1",
"@iktakahiro/markdown-it-katex": "^3.0.4",
"@mistralai/mistralai": "^0.1.3",
"applescript": "^1.0.0",
"bootstrap-icons-vue": "^1.11.3",
Expand All @@ -89,7 +89,7 @@
"openai-speech-stream-player": "^1.0.8",
"pdf2json": "^3.1.2",
"python-shell": "^5.0.0",
"sweetalert2": "^11.10.7",
"sweetalert2": "^11.6.13",
"update-electron-app": "^3.0.0",
"uuid": "^9.0.1",
"vue": "^3.4.21"
Expand Down
17 changes: 16 additions & 1 deletion src/main/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ const buildConfig = (defaults: anyDict, overrides: anyDict): Configuration => {
delete config.ollama
}

// nullify defaults
nullifyDefaults(config)

// done
return config as Configuration

Expand All @@ -75,6 +78,9 @@ export const loadSettings = (source: App|string): Configuration => {
export const saveSettings = (dest: App|string, config: anyDict) => {
try {

// nullify defaults
nullifyDefaults(config)

// remove instructions that are the same as the default
const settings = JSON.parse(JSON.stringify(config))
for (const instr in settings.instructions) {
Expand All @@ -90,4 +96,13 @@ export const saveSettings = (dest: App|string, config: anyDict) => {
} catch (error) {
console.log('Error saving settings data', error)
}
}
}

const nullifyDefaults = (settings: anyDict) => {
if (settings.engines.openai.baseURL == '' || settings.engines.openai.baseURL === defaultSettings.engines.openai.baseURL) {
delete settings.engines.openai.baseURL
}
if (settings.engines.ollama.baseURL == '' || settings.engines.ollama.baseURL === defaultSettings.engines.ollama.baseURL) {
delete settings.engines.ollama.baseURL
}
}
14 changes: 14 additions & 0 deletions src/services/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { Message } from '../types/index.d'
import { LLmCompletionPayload, LlmChunk, LlmCompletionOpts, LlmResponse, LlmStream, LlmToolCall, LlmEventCallback } from '../types/llm.d'
import { EngineConfig, Configuration } from '../types/config.d'
import defaults from '../../defaults/settings.json'
import LlmEngine from './engine'
import OpenAI from 'openai'
import { ChatCompletionChunk } from 'openai/resources'
Expand All @@ -24,6 +25,7 @@ export default class extends LlmEngine {
super(config)
this.client = new OpenAI({
apiKey: config.engines.openai.apiKey,
baseURL: config.engines.openai.baseURL || defaults.engines.openai.baseURL,
dangerouslyAllowBrowser: true
})
}
Expand Down Expand Up @@ -52,8 +54,17 @@ export default class extends LlmEngine {
}
}

private setBaseURL() {
if (this.client) {
this.client.baseURL = this.config.engines.openai.baseURL || defaults.engines.openai.baseURL
}
}

async complete(thread: Message[], opts: LlmCompletionOpts): Promise<LlmResponse> {

// set baseURL on client
this.setBaseURL()

// call
const model = opts?.model || this.config.engines.openai.model.chat
console.log(`[openai] prompting model ${model}`)
Expand All @@ -71,6 +82,9 @@ export default class extends LlmEngine {

async stream(thread: Message[], opts: LlmCompletionOpts): Promise<LlmStream> {

// set baseURL on client
this.setBaseURL()

// model: switch to vision if needed
this.currentModel = this.selectModel(thread, opts?.model || this.getChatModel())

Expand Down
8 changes: 8 additions & 0 deletions src/settings/SettingsOpenAI.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
</div>
<button @click.prevent="onRefresh">{{ refreshLabel }}</button>
</div>
<div class="group">
<label>API Base URL</label>
<input v-model="baseURL" :placeholder="defaults.engines.openai.baseURL" @keydown.enter.prevent="save" @change="save"/>
</div>
</div>
</template>

Expand All @@ -27,15 +31,18 @@
import { ref } from 'vue'
import { store } from '../services/store'
import { loadOpenAIModels } from '../services/llm'
import defaults from '../../defaults/settings.json'
import InputObfuscated from '../components/InputObfuscated.vue'
const apiKey = ref(null)
const baseURL = ref(null)
const refreshLabel = ref('Refresh')
const chat_model = ref(null)
const chat_models = ref([])
const load = () => {
apiKey.value = store.config.engines.openai?.apiKey || ''
baseURL.value = store.config.engines.openai?.baseURL || ''
chat_models.value = store.config.engines.openai?.models?.chat || []
chat_model.value = store.config.engines.openai?.model?.chat || ''
}
Expand Down Expand Up @@ -78,6 +85,7 @@ const onKeyChange = () => {
const save = () => {
store.config.engines.openai.apiKey = apiKey.value
store.config.engines.openai.baseURL = baseURL.value
store.config.engines.openai.model.chat = chat_model.value
store.saveSettings()
}
Expand Down
1 change: 1 addition & 0 deletions src/types/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ interface ShortcutsConfig {

interface EngineConfig {
apiKey?: string
baseURL?: string
models: ModelsConfig
model: ModelConfig
tts?: TTSConfig
Expand Down

0 comments on commit 443c236

Please sign in to comment.