diff --git a/backend/apps/images/main.py b/backend/apps/images/main.py index 5f7ce388b..2059ac3c0 100644 --- a/backend/apps/images/main.py +++ b/backend/apps/images/main.py @@ -35,8 +35,8 @@ from config import ( ENABLE_IMAGE_GENERATION, AUTOMATIC1111_BASE_URL, COMFYUI_BASE_URL, - IMAGE_OPENAI_API_BASE_URL, - IMAGE_OPENAI_API_KEY, + IMAGES_OPENAI_API_BASE_URL, + IMAGES_OPENAI_API_KEY, ) @@ -58,8 +58,8 @@ app.add_middleware( app.state.ENGINE = "" app.state.ENABLED = ENABLE_IMAGE_GENERATION -app.state.OPENAI_API_BASE_URL = IMAGE_OPENAI_API_BASE_URL -app.state.OPENAI_API_KEY = IMAGE_OPENAI_API_KEY +app.state.OPENAI_API_BASE_URL = IMAGES_OPENAI_API_BASE_URL +app.state.OPENAI_API_KEY = IMAGES_OPENAI_API_KEY app.state.MODEL = "" @@ -135,27 +135,33 @@ async def update_engine_url( } -class OpenAIKeyUpdateForm(BaseModel): +class OpenAIConfigUpdateForm(BaseModel): + url: str key: str -@app.get("/key") -async def get_openai_key(user=Depends(get_admin_user)): - return {"OPENAI_API_KEY": app.state.OPENAI_API_KEY} +@app.get("/openai/config") +async def get_openai_config(user=Depends(get_admin_user)): + return { + "OPENAI_API_BASE_URL": app.state.OPENAI_API_BASE_URL, + "OPENAI_API_KEY": app.state.OPENAI_API_KEY, + } -@app.post("/key/update") -async def update_openai_key( - form_data: OpenAIKeyUpdateForm, user=Depends(get_admin_user) +@app.post("/openai/config/update") +async def update_openai_config( + form_data: OpenAIConfigUpdateForm, user=Depends(get_admin_user) ): - if form_data.key == "": raise HTTPException(status_code=400, detail=ERROR_MESSAGES.API_KEY_NOT_FOUND) + app.state.OPENAI_API_BASE_URL = form_data.url app.state.OPENAI_API_KEY = form_data.key + return { - "OPENAI_API_KEY": app.state.OPENAI_API_KEY, "status": True, + "OPENAI_API_BASE_URL": app.state.OPENAI_API_BASE_URL, + "OPENAI_API_KEY": app.state.OPENAI_API_KEY, } diff --git a/src/lib/apis/images/index.ts b/src/lib/apis/images/index.ts index aadfafd14..3f624704e 100644 --- a/src/lib/apis/images/index.ts +++ b/src/lib/apis/images/index.ts @@ -72,10 +72,10 @@ export const updateImageGenerationConfig = async ( return res; }; -export const getOpenAIKey = async (token: string = '') => { +export const getOpenAIConfig = async (token: string = '') => { let error = null; - const res = await fetch(`${IMAGES_API_BASE_URL}/key`, { + const res = await fetch(`${IMAGES_API_BASE_URL}/openai/config`, { method: 'GET', headers: { Accept: 'application/json', @@ -101,13 +101,13 @@ export const getOpenAIKey = async (token: string = '') => { throw error; } - return res.OPENAI_API_KEY; + return res; }; -export const updateOpenAIKey = async (token: string = '', key: string) => { +export const updateOpenAIConfig = async (token: string = '', url: string, key: string) => { let error = null; - const res = await fetch(`${IMAGES_API_BASE_URL}/key/update`, { + const res = await fetch(`${IMAGES_API_BASE_URL}/openai/config/update`, { method: 'POST', headers: { Accept: 'application/json', @@ -115,6 +115,7 @@ export const updateOpenAIKey = async (token: string = '', key: string) => { ...(token && { authorization: `Bearer ${token}` }) }, body: JSON.stringify({ + url: url, key: key }) }) @@ -136,7 +137,7 @@ export const updateOpenAIKey = async (token: string = '', key: string) => { throw error; } - return res.OPENAI_API_KEY; + return res; }; export const getImageGenerationEngineUrls = async (token: string = '') => { diff --git a/src/lib/components/chat/Settings/Images.svelte b/src/lib/components/chat/Settings/Images.svelte index 7282c184a..b1a31f8b9 100644 --- a/src/lib/components/chat/Settings/Images.svelte +++ b/src/lib/components/chat/Settings/Images.svelte @@ -15,8 +15,8 @@ updateImageSize, getImageSteps, updateImageSteps, - getOpenAIKey, - updateOpenAIKey + getOpenAIConfig, + updateOpenAIConfig } from '$lib/apis/images'; import { getBackendConfig } from '$lib/apis'; const dispatch = createEventDispatcher(); @@ -33,6 +33,7 @@ let AUTOMATIC1111_BASE_URL = ''; let COMFYUI_BASE_URL = ''; + let OPENAI_API_BASE_URL = ''; let OPENAI_API_KEY = ''; let selectedModel = ''; @@ -131,7 +132,10 @@ AUTOMATIC1111_BASE_URL = URLS.AUTOMATIC1111_BASE_URL; COMFYUI_BASE_URL = URLS.COMFYUI_BASE_URL; - OPENAI_API_KEY = await getOpenAIKey(localStorage.token); + const config = await getOpenAIConfig(localStorage.token); + + OPENAI_API_KEY = config.OPENAI_API_KEY; + OPENAI_API_BASE_URL = config.OPENAI_API_BASE_URL; imageSize = await getImageSize(localStorage.token); steps = await getImageSteps(localStorage.token); @@ -149,7 +153,7 @@ loading = true; if (imageGenerationEngine === 'openai') { - await updateOpenAIKey(localStorage.token, OPENAI_API_KEY); + await updateOpenAIConfig(localStorage.token, OPENAI_API_BASE_URL, OPENAI_API_KEY); } await updateDefaultImageGenerationModel(localStorage.token, selectedModel); @@ -300,13 +304,22 @@ {:else if imageGenerationEngine === 'openai'} -