everything working again after cleanup
This commit is contained in:
+61
-21
@@ -6,9 +6,11 @@ Allows custom endpoints for each of the above supported functions.
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from typing import TYPE_CHECKING, cast
|
||||
|
||||
import openai
|
||||
import requests
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from io import BufferedReader, BytesIO
|
||||
@@ -54,8 +56,12 @@ def chat_completion(
|
||||
model=model,
|
||||
messages=messages,
|
||||
max_tokens=max_tokens,
|
||||
timeout=60.0,
|
||||
)
|
||||
|
||||
if not response.choices:
|
||||
return ""
|
||||
|
||||
content = response.choices[0].message.content
|
||||
if content:
|
||||
return content.strip()
|
||||
@@ -101,8 +107,12 @@ def chat_completion_with_history(
|
||||
messages=messages,
|
||||
max_tokens=max_tokens,
|
||||
seed=-1,
|
||||
timeout=60.0,
|
||||
)
|
||||
|
||||
if not response.choices:
|
||||
return ""
|
||||
|
||||
content = response.choices[0].message.content
|
||||
if content:
|
||||
return content.strip()
|
||||
@@ -148,8 +158,12 @@ def chat_completion_instruct(
|
||||
messages=messages,
|
||||
max_tokens=max_tokens,
|
||||
seed=-1,
|
||||
timeout=60.0,
|
||||
)
|
||||
|
||||
if not response.choices:
|
||||
return ""
|
||||
|
||||
content = response.choices[0].message.content
|
||||
if content:
|
||||
return content.strip()
|
||||
@@ -158,8 +172,10 @@ def chat_completion_instruct(
|
||||
|
||||
def image_generation(
|
||||
prompt: str,
|
||||
*,
|
||||
openai_url: str,
|
||||
openai_api_key: str,
|
||||
model: str = "gen",
|
||||
n: int = 1,
|
||||
) -> str:
|
||||
"""Generate an image using the given prompt.
|
||||
@@ -168,19 +184,28 @@ def image_generation(
|
||||
prompt: The image generation prompt.
|
||||
openai_url: The OpenAI-compatible API URL.
|
||||
openai_api_key: The API key for authentication.
|
||||
model: The model to use for image generation.
|
||||
n: Number of images to generate.
|
||||
|
||||
Returns:
|
||||
The base64 encoded image data. Decode and write to a file.
|
||||
|
||||
"""
|
||||
client = openai.OpenAI(base_url=openai_url, api_key=openai_api_key)
|
||||
response = client.images.generate(
|
||||
prompt=prompt,
|
||||
n=n,
|
||||
size="1024x1024",
|
||||
model="gen",
|
||||
client = openai.OpenAI(
|
||||
base_url=openai_url,
|
||||
api_key=openai_api_key,
|
||||
max_retries=0,
|
||||
)
|
||||
try:
|
||||
response = client.images.generate(
|
||||
prompt=prompt,
|
||||
n=n,
|
||||
size="1024x1024",
|
||||
model=model,
|
||||
timeout=120.0,
|
||||
)
|
||||
except openai.APIConnectionError:
|
||||
return ""
|
||||
if response.data:
|
||||
return response.data[0].b64_json or ""
|
||||
return ""
|
||||
@@ -189,8 +214,10 @@ def image_generation(
|
||||
def image_edit(
|
||||
image: BufferedReader | BytesIO | list[BufferedReader] | list[BytesIO],
|
||||
prompt: str,
|
||||
*,
|
||||
openai_url: str,
|
||||
openai_api_key: str,
|
||||
model: str = "edit",
|
||||
n: int = 1,
|
||||
) -> str:
|
||||
"""Edit an existing image using a prompt.
|
||||
@@ -200,6 +227,7 @@ def image_edit(
|
||||
prompt: The edit instruction.
|
||||
openai_url: The OpenAI-compatible API URL.
|
||||
openai_api_key: The API key for authentication.
|
||||
model: The model to use for image editing.
|
||||
n: Number of edited images to generate.
|
||||
|
||||
Returns:
|
||||
@@ -212,7 +240,7 @@ def image_edit(
|
||||
prompt=prompt,
|
||||
n=n,
|
||||
size="1024x1024",
|
||||
model="edit",
|
||||
model=model,
|
||||
)
|
||||
if response.data:
|
||||
return response.data[0].b64_json or ""
|
||||
@@ -228,6 +256,9 @@ def embedding(
|
||||
) -> list[float]:
|
||||
"""Generate an embedding vector for the given text.
|
||||
|
||||
Uses a raw HTTP request to avoid the OpenAI SDK injecting
|
||||
unsupported parameters like encoding_format.
|
||||
|
||||
Args:
|
||||
text: The text to embed.
|
||||
openai_url: The OpenAI-compatible API URL.
|
||||
@@ -238,17 +269,26 @@ def embedding(
|
||||
The embedding vector as a list of floats, or an empty list on failure.
|
||||
|
||||
"""
|
||||
client = openai.OpenAI(base_url=openai_url, api_key=openai_api_key)
|
||||
response = client.embeddings.create(
|
||||
input=[text],
|
||||
model=model,
|
||||
encoding_format="float",
|
||||
)
|
||||
if response:
|
||||
data = response.data
|
||||
raw_data = data[0].embedding
|
||||
# The result could be an array of floats or a single float.
|
||||
if not isinstance(raw_data, float):
|
||||
return list(raw_data)
|
||||
return [raw_data]
|
||||
return []
|
||||
url = f"{openai_url.rstrip('/')}/embeddings"
|
||||
headers = {
|
||||
"Authorization": f"Bearer {openai_api_key}",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
payload = {"model": model, "input": [text]}
|
||||
|
||||
try:
|
||||
resp = requests.post(url, headers=headers, json=payload, timeout=30)
|
||||
resp.raise_for_status()
|
||||
except requests.RequestException:
|
||||
return []
|
||||
|
||||
data = resp.json()
|
||||
if not data.get("data"):
|
||||
return []
|
||||
|
||||
raw = data["data"][0].get("embedding")
|
||||
if isinstance(raw, str):
|
||||
raw = json.loads(raw)
|
||||
if not isinstance(raw, list):
|
||||
raw = list(raw)
|
||||
return raw
|
||||
|
||||
Reference in New Issue
Block a user