forked from open-webui/open-webui
Add workaround for gpt-4-vision-preview model
This commit is contained in:
parent
f079cb6b56
commit
8662437a9f
1 changed files with 32 additions and 4 deletions
|
@ -84,9 +84,37 @@ async def proxy(path: str, request: Request, user=Depends(get_current_user)):
|
||||||
raise HTTPException(status_code=401,
|
raise HTTPException(status_code=401,
|
||||||
detail=ERROR_MESSAGES.API_KEY_NOT_FOUND)
|
detail=ERROR_MESSAGES.API_KEY_NOT_FOUND)
|
||||||
|
|
||||||
body = await request.body()
|
# Try to decode the body of the request from bytes to a UTF-8 string (Require add max_token to fix gpt-4-vision)
|
||||||
# headers = dict(request.headers)
|
try:
|
||||||
# print(headers)
|
body_str = (await request.body()).decode('utf-8')
|
||||||
|
except UnicodeDecodeError as e:
|
||||||
|
print("Error decoding request body:", e)
|
||||||
|
raise HTTPException(status_code=400, detail="Invalid request body")
|
||||||
|
# Check if the body is not empty
|
||||||
|
if body_str:
|
||||||
|
try:
|
||||||
|
|
||||||
|
body_dict = json.loads(body_str)
|
||||||
|
except json.JSONDecodeError as e:
|
||||||
|
print("Error loading request body into a dictionary:", e)
|
||||||
|
raise HTTPException(status_code=400, detail="Invalid JSON in request body")
|
||||||
|
|
||||||
|
# Check if the model is "gpt-4-vision-preview" and set "max_tokens" to 10000
|
||||||
|
# This is a workaround until OpenAI fixes the issue with this model
|
||||||
|
if body_dict.get("model") == "gpt-4-vision-preview":
|
||||||
|
body_dict["max_tokens"] = 10000
|
||||||
|
print("Modified body_dict:", body_dict)
|
||||||
|
|
||||||
|
# Try to convert the modified body back to JSON
|
||||||
|
try:
|
||||||
|
# Convert the modified body back to JSON
|
||||||
|
body_json = json.dumps(body_dict)
|
||||||
|
except TypeError as e:
|
||||||
|
print("Error converting modified body to JSON:", e)
|
||||||
|
raise HTTPException(status_code=500, detail="Internal server error")
|
||||||
|
else:
|
||||||
|
body_json = body_str # If the body is empty, use it as is
|
||||||
|
|
||||||
|
|
||||||
headers = {}
|
headers = {}
|
||||||
headers["Authorization"] = f"Bearer {app.state.OPENAI_API_KEY}"
|
headers["Authorization"] = f"Bearer {app.state.OPENAI_API_KEY}"
|
||||||
|
@ -96,7 +124,7 @@ async def proxy(path: str, request: Request, user=Depends(get_current_user)):
|
||||||
r = requests.request(
|
r = requests.request(
|
||||||
method=request.method,
|
method=request.method,
|
||||||
url=target_url,
|
url=target_url,
|
||||||
data=body,
|
data=body_json,
|
||||||
headers=headers,
|
headers=headers,
|
||||||
stream=True,
|
stream=True,
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue