Drop text-davinci-003 code paths from openai_utils

Remove the legacy completion handling now that the model is gone:
- drop text-davinci-003 from the __init__ assert
- remove the legacy completions.create branches in send_message and
  send_message_stream
- delete the now-unused _generate_prompt and _count_tokens_from_prompt
  helpers

All remaining paths use the chat completions API.
This commit is contained in:
Father Bot
2026-02-14 12:05:18 +03:00
parent 6b90a74567
commit e37eb4a248
+1 -53
View File
@@ -27,7 +27,7 @@ OPENAI_COMPLETION_OPTIONS = {
class ChatGPT:
def __init__(self, model="gpt-4o-mini"):
assert model in {"text-davinci-003", "gpt-3.5-turbo-16k", "gpt-3.5-turbo", "gpt-4", "gpt-4o", "gpt-4o-mini", "gpt-4-1106-preview", "gpt-4-vision-preview"}, f"Unknown model: {model}"
assert model in {"gpt-3.5-turbo-16k", "gpt-3.5-turbo", "gpt-4", "gpt-4o", "gpt-4o-mini", "gpt-4-1106-preview", "gpt-4-vision-preview"}, f"Unknown model: {model}"
self.model = model
async def send_message(self, message, dialog_messages=[], chat_mode="assistant"):
@@ -47,14 +47,6 @@ class ChatGPT:
**OPENAI_COMPLETION_OPTIONS
)
answer = r.choices[0].message.content
elif self.model == "text-davinci-003":
prompt = self._generate_prompt(message, dialog_messages, chat_mode)
r = await openai_client.completions.create(
model=self.model,
prompt=prompt,
**OPENAI_COMPLETION_OPTIONS
)
answer = r.choices[0].text
else:
raise ValueError(f"Unknown model: {self.model}")
@@ -102,25 +94,6 @@ class ChatGPT:
yield "not_finished", answer, (n_input_tokens, n_output_tokens), n_first_dialog_messages_removed
elif self.model == "text-davinci-003":
prompt = self._generate_prompt(message, dialog_messages, chat_mode)
r_gen = await openai_client.completions.create(
model=self.model,
prompt=prompt,
stream=True,
**OPENAI_COMPLETION_OPTIONS
)
answer = ""
async for r_item in r_gen:
if len(r_item.choices) == 0:
continue
answer += r_item.choices[0].text
n_input_tokens, n_output_tokens = self._count_tokens_from_prompt(prompt, answer, model=self.model)
n_first_dialog_messages_removed = n_dialog_messages_before - len(dialog_messages)
yield "not_finished", answer, (n_input_tokens, n_output_tokens), n_first_dialog_messages_removed
answer = self._postprocess_answer(answer)
except BadRequestError as e: # too many tokens
@@ -237,23 +210,6 @@ class ChatGPT:
n_output_tokens,
), n_first_dialog_messages_removed
def _generate_prompt(self, message, dialog_messages, chat_mode):
prompt = config.chat_modes[chat_mode]["prompt_start"]
prompt += "\n\n"
# add chat context
if len(dialog_messages) > 0:
prompt += "Chat:\n"
for dialog_message in dialog_messages:
prompt += f"User: {dialog_message['user']}\n"
prompt += f"Assistant: {dialog_message['bot']}\n"
# current message
prompt += f"User: {message}\n"
prompt += "Assistant: "
return prompt
def _encode_image(self, image_buffer: BytesIO) -> bytes:
return base64.b64encode(image_buffer.read()).decode("utf-8")
@@ -349,14 +305,6 @@ class ChatGPT:
return n_input_tokens, n_output_tokens
def _count_tokens_from_prompt(self, prompt, answer, model="text-davinci-003"):
encoding = tiktoken.encoding_for_model(model)
n_input_tokens = len(encoding.encode(prompt)) + 1
n_output_tokens = len(encoding.encode(answer))
return n_input_tokens, n_output_tokens
async def transcribe_audio(audio_file) -> str:
r = await openai_client.audio.transcriptions.create(model="whisper-1", file=audio_file)