making image code more robust

This commit is contained in:
Joshua Vial 2023-12-11 22:21:24 +13:00
parent f9ba8e7b41
commit 9ceaf97f08
3 changed files with 9 additions and 6 deletions

View file

@ -10,7 +10,7 @@ from aider import prompts, voice
from .dump import dump # noqa: F401 from .dump import dump # noqa: F401
from aider.utils import is_image_file from aider.utils import is_image_file, is_gpt4_with_openai_base_url
class Commands: class Commands:
voice = None voice = None
@ -171,11 +171,12 @@ class Commands:
self.io.tool_output("=" * (width + cost_width + 1)) self.io.tool_output("=" * (width + cost_width + 1))
self.io.tool_output(f"${total_cost:5.2f} {fmt(total)} tokens total") self.io.tool_output(f"${total_cost:5.2f} {fmt(total)} tokens total")
# Set image_in_chat to False unless is_gpt4_with_openai_base_url returns True # only switch to image model token count if gpt4 and openai and image in files
image_in_chat = False image_in_chat = False
if utils.is_gpt4_with_openai_base_url(self.coder.main_model.name, self.coder.client): if is_gpt4_with_openai_base_url(self.coder.main_model.name, self.coder.client):
image_in_chat = any(is_image_file(relative_fname) for relative_fname in self.coder.get_inchat_relative_files()) image_in_chat = any(is_image_file(relative_fname) for relative_fname in self.coder.get_inchat_relative_files())
limit = 128000 if image_in_chat else self.coder.main_model.max_context_tokens limit = 128000 if image_in_chat else self.coder.main_model.max_context_tokens
remaining = limit - total remaining = limit - total
if remaining > 1024: if remaining > 1024:
self.io.tool_output(f"{cost_pad}{fmt(remaining)} tokens remaining in context window") self.io.tool_output(f"{cost_pad}{fmt(remaining)} tokens remaining in context window")
@ -327,7 +328,9 @@ class Commands:
if abs_file_path in self.coder.abs_fnames: if abs_file_path in self.coder.abs_fnames:
self.io.tool_error(f"{matched_file} is already in the chat") self.io.tool_error(f"{matched_file} is already in the chat")
else: else:
#TODO put in guard to stop images being added to non openai / gpt-4 if is_image_file(matched_file) and not is_gpt4_with_openai_base_url(self.coder.main_model.name, self.coder.client):
self.io.tool_error(f"Cannot add image file {matched_file} as the model does not support image files")
continue
content = self.io.read_text(abs_file_path) content = self.io.read_text(abs_file_path)
if content is None: if content is None:
self.io.tool_error(f"Unable to read {matched_file}") self.io.tool_error(f"Unable to read {matched_file}")

View file

@ -8,6 +8,7 @@ import openai
# from diskcache import Cache # from diskcache import Cache
from openai import APIConnectionError, InternalServerError, RateLimitError from openai import APIConnectionError, InternalServerError, RateLimitError
from aider.utils import is_gpt4_with_openai_base_url
from aider.dump import dump # noqa: F401 from aider.dump import dump # noqa: F401
CACHE_PATH = "~/.aider.send.cache.v1" CACHE_PATH = "~/.aider.send.cache.v1"
@ -41,7 +42,6 @@ def send_with_retries(client, model_name, messages, functions, stream):
if functions is not None: if functions is not None:
kwargs["functions"] = functions kwargs["functions"] = functions
from aider.utils import is_gpt4_with_openai_base_url
# Check conditions to switch to gpt-4-vision-preview or strip out image_url messages # Check conditions to switch to gpt-4-vision-preview or strip out image_url messages
if client and is_gpt4_with_openai_base_url(model_name, client): if client and is_gpt4_with_openai_base_url(model_name, client):

View file

@ -1,5 +1,4 @@
from pathlib import Path from pathlib import Path
from openai import OpenAIError
# Set of image file extensions # Set of image file extensions
IMAGE_EXTENSIONS = {'.png', '.jpg', '.jpeg', '.gif', '.bmp', '.tiff', '.webp'} IMAGE_EXTENSIONS = {'.png', '.jpg', '.jpeg', '.gif', '.bmp', '.tiff', '.webp'}
@ -42,6 +41,7 @@ def show_messages(messages, title=None, functions=None):
if functions: if functions:
dump(functions) dump(functions)
def is_gpt4_with_openai_base_url(model_name, client): def is_gpt4_with_openai_base_url(model_name, client):
""" """
Check if the model_name starts with 'gpt-4' and the client base URL includes 'api.openai.com'. Check if the model_name starts with 'gpt-4' and the client base URL includes 'api.openai.com'.