forked from open-webui/open-webui
		
	Merge pull request #355 from ollama-webui/openai-api-fix
fix: openai issue
This commit is contained in:
		
						commit
						df206a233d
					
				
					 5 changed files with 108 additions and 21 deletions
				
			
		|  | @ -1,7 +1,7 @@ | ||||||
| from fastapi import FastAPI, Depends | from fastapi import FastAPI, Depends | ||||||
| from fastapi.routing import APIRoute | from fastapi.routing import APIRoute | ||||||
| from fastapi.middleware.cors import CORSMiddleware | from fastapi.middleware.cors import CORSMiddleware | ||||||
| from apps.web.routers import auths, users, chats, modelfiles, utils | from apps.web.routers import auths, users, chats, modelfiles, configs, utils | ||||||
| from config import WEBUI_VERSION, WEBUI_AUTH | from config import WEBUI_VERSION, WEBUI_AUTH | ||||||
| 
 | 
 | ||||||
| app = FastAPI() | app = FastAPI() | ||||||
|  | @ -9,6 +9,7 @@ app = FastAPI() | ||||||
| origins = ["*"] | origins = ["*"] | ||||||
| 
 | 
 | ||||||
| app.state.ENABLE_SIGNUP = True | app.state.ENABLE_SIGNUP = True | ||||||
|  | app.state.DEFAULT_MODELS = None | ||||||
| 
 | 
 | ||||||
| app.add_middleware( | app.add_middleware( | ||||||
|     CORSMiddleware, |     CORSMiddleware, | ||||||
|  | @ -19,13 +20,18 @@ app.add_middleware( | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| app.include_router(auths.router, prefix="/auths", tags=["auths"]) | app.include_router(auths.router, prefix="/auths", tags=["auths"]) | ||||||
| 
 |  | ||||||
| app.include_router(users.router, prefix="/users", tags=["users"]) | app.include_router(users.router, prefix="/users", tags=["users"]) | ||||||
| app.include_router(chats.router, prefix="/chats", tags=["chats"]) | app.include_router(chats.router, prefix="/chats", tags=["chats"]) | ||||||
| app.include_router(modelfiles.router, prefix="/modelfiles", tags=["modelfiles"]) | app.include_router(modelfiles.router, prefix="/modelfiles", tags=["modelfiles"]) | ||||||
|  | app.include_router(configs.router, prefix="/configs", tags=["configs"]) | ||||||
| app.include_router(utils.router, prefix="/utils", tags=["utils"]) | app.include_router(utils.router, prefix="/utils", tags=["utils"]) | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| @app.get("/") | @app.get("/") | ||||||
| async def get_status(): | async def get_status(): | ||||||
|     return {"status": True, "version": WEBUI_VERSION, "auth": WEBUI_AUTH} |     return { | ||||||
|  |         "status": True, | ||||||
|  |         "version": WEBUI_VERSION, | ||||||
|  |         "auth": WEBUI_AUTH, | ||||||
|  |         "default_models": app.state.DEFAULT_MODELS, | ||||||
|  |     } | ||||||
|  |  | ||||||
							
								
								
									
										41
									
								
								backend/apps/web/routers/configs.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								backend/apps/web/routers/configs.py
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,41 @@ | ||||||
|  | from fastapi import Response, Request | ||||||
|  | from fastapi import Depends, FastAPI, HTTPException, status | ||||||
|  | from datetime import datetime, timedelta | ||||||
|  | from typing import List, Union | ||||||
|  | 
 | ||||||
|  | from fastapi import APIRouter | ||||||
|  | from pydantic import BaseModel | ||||||
|  | import time | ||||||
|  | import uuid | ||||||
|  | 
 | ||||||
|  | from apps.web.models.users import Users | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | from utils.utils import get_password_hash, get_current_user, create_token | ||||||
|  | from utils.misc import get_gravatar_url, validate_email_format | ||||||
|  | from constants import ERROR_MESSAGES | ||||||
|  | 
 | ||||||
|  | router = APIRouter() | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | class SetDefaultModelsForm(BaseModel): | ||||||
|  |     models: str | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | ############################ | ||||||
|  | # SetDefaultModels | ||||||
|  | ############################ | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | @router.post("/default/models", response_model=str) | ||||||
|  | async def set_global_default_models( | ||||||
|  |     request: Request, form_data: SetDefaultModelsForm, user=Depends(get_current_user) | ||||||
|  | ): | ||||||
|  |     if user.role == "admin": | ||||||
|  |         request.app.state.DEFAULT_MODELS = form_data.models | ||||||
|  |         return request.app.state.DEFAULT_MODELS | ||||||
|  |     else: | ||||||
|  |         raise HTTPException( | ||||||
|  |             status_code=status.HTTP_403_FORBIDDEN, | ||||||
|  |             detail=ERROR_MESSAGES.ACCESS_PROHIBITED, | ||||||
|  |         ) | ||||||
							
								
								
									
										31
									
								
								src/lib/apis/configs/index.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								src/lib/apis/configs/index.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,31 @@ | ||||||
|  | import { WEBUI_API_BASE_URL } from '$lib/constants'; | ||||||
|  | 
 | ||||||
|  | export const setDefaultModels = async (token: string, models: string) => { | ||||||
|  | 	let error = null; | ||||||
|  | 
 | ||||||
|  | 	const res = await fetch(`${WEBUI_API_BASE_URL}/configs/default/models`, { | ||||||
|  | 		method: 'GET', | ||||||
|  | 		headers: { | ||||||
|  | 			'Content-Type': 'application/json', | ||||||
|  | 			Authorization: `Bearer ${token}` | ||||||
|  | 		}, | ||||||
|  | 		body: JSON.stringify({ | ||||||
|  | 			models: models | ||||||
|  | 		}) | ||||||
|  | 	}) | ||||||
|  | 		.then(async (res) => { | ||||||
|  | 			if (!res.ok) throw await res.json(); | ||||||
|  | 			return res.json(); | ||||||
|  | 		}) | ||||||
|  | 		.catch((err) => { | ||||||
|  | 			console.log(err); | ||||||
|  | 			error = err.detail; | ||||||
|  | 			return null; | ||||||
|  | 		}); | ||||||
|  | 
 | ||||||
|  | 	if (error) { | ||||||
|  | 		throw error; | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	return res; | ||||||
|  | }; | ||||||
|  | @ -6,7 +6,7 @@ | ||||||
| 	import { goto } from '$app/navigation'; | 	import { goto } from '$app/navigation'; | ||||||
| 	import { page } from '$app/stores'; | 	import { page } from '$app/stores'; | ||||||
| 
 | 
 | ||||||
| 	import { models, modelfiles, user, settings, chats, chatId } from '$lib/stores'; | 	import { models, modelfiles, user, settings, chats, chatId, config } from '$lib/stores'; | ||||||
| 	import { OLLAMA_API_BASE_URL } from '$lib/constants'; | 	import { OLLAMA_API_BASE_URL } from '$lib/constants'; | ||||||
| 
 | 
 | ||||||
| 	import { generateChatCompletion, generateTitle } from '$lib/apis/ollama'; | 	import { generateChatCompletion, generateTitle } from '$lib/apis/ollama'; | ||||||
|  | @ -90,9 +90,18 @@ | ||||||
| 			messages: {}, | 			messages: {}, | ||||||
| 			currentId: null | 			currentId: null | ||||||
| 		}; | 		}; | ||||||
| 		selectedModels = $page.url.searchParams.get('models') | 
 | ||||||
| 			? $page.url.searchParams.get('models')?.split(',') | 		console.log($config); | ||||||
| 			: $settings.models ?? ['']; | 
 | ||||||
|  | 		if ($page.url.searchParams.get('models')) { | ||||||
|  | 			selectedModels = $page.url.searchParams.get('models')?.split(','); | ||||||
|  | 		} else if ($settings?.models) { | ||||||
|  | 			selectedModels = $settings?.models; | ||||||
|  | 		} else if ($config?.default_models) { | ||||||
|  | 			selectedModels = $config?.default_models.split(','); | ||||||
|  | 		} else { | ||||||
|  | 			selectedModels = ['']; | ||||||
|  | 		} | ||||||
| 
 | 
 | ||||||
| 		let _settings = JSON.parse(localStorage.getItem('settings') ?? '{}'); | 		let _settings = JSON.parse(localStorage.getItem('settings') ?? '{}'); | ||||||
| 		settings.set({ | 		settings.set({ | ||||||
|  | @ -383,13 +392,13 @@ | ||||||
| 										  } | 										  } | ||||||
| 										: { content: message.content }) | 										: { content: message.content }) | ||||||
| 								})), | 								})), | ||||||
| 							seed: $settings.options.seed ?? undefined, | 							seed: $settings?.options?.seed ?? undefined, | ||||||
| 							stop: $settings.options.stop ?? undefined, | 							stop: $settings?.options?.stop ?? undefined, | ||||||
| 							temperature: $settings.options.temperature ?? undefined, | 							temperature: $settings?.options?.temperature ?? undefined, | ||||||
| 							top_p: $settings.options.top_p ?? undefined, | 							top_p: $settings?.options?.top_p ?? undefined, | ||||||
| 							num_ctx: $settings.options.num_ctx ?? undefined, | 							num_ctx: $settings?.options?.num_ctx ?? undefined, | ||||||
| 							frequency_penalty: $settings.options.repeat_penalty ?? undefined, | 							frequency_penalty: $settings?.options?.repeat_penalty ?? undefined, | ||||||
| 							max_tokens: $settings.options.num_predict ?? undefined | 							max_tokens: $settings?.options?.num_predict ?? undefined | ||||||
| 						}) | 						}) | ||||||
| 					} | 					} | ||||||
| 				).catch((err) => { | 				).catch((err) => { | ||||||
|  |  | ||||||
|  | @ -409,13 +409,13 @@ | ||||||
| 										  } | 										  } | ||||||
| 										: { content: message.content }) | 										: { content: message.content }) | ||||||
| 								})), | 								})), | ||||||
| 							seed: $settings.options.seed ?? undefined, | 							seed: $settings?.options?.seed ?? undefined, | ||||||
| 							stop: $settings.options.stop ?? undefined, | 							stop: $settings?.options?.stop ?? undefined, | ||||||
| 							temperature: $settings.options.temperature ?? undefined, | 							temperature: $settings?.options?.temperature ?? undefined, | ||||||
| 							top_p: $settings.options.top_p ?? undefined, | 							top_p: $settings?.options?.top_p ?? undefined, | ||||||
| 							num_ctx: $settings.options.num_ctx ?? undefined, | 							num_ctx: $settings?.options?.num_ctx ?? undefined, | ||||||
| 							frequency_penalty: $settings.options.repeat_penalty ?? undefined, | 							frequency_penalty: $settings?.options?.repeat_penalty ?? undefined, | ||||||
| 							max_tokens: $settings.options.num_predict ?? undefined | 							max_tokens: $settings?.options?.num_predict ?? undefined | ||||||
| 						}) | 						}) | ||||||
| 					} | 					} | ||||||
| 				).catch((err) => { | 				).catch((err) => { | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Timothy Jaeryang Baek
						Timothy Jaeryang Baek