add gpt-4v support for aask and AN.fill

This commit is contained in:
better629 2024-01-30 11:59:53 +08:00
parent 9e49e2252d
commit 310687258e
8 changed files with 113 additions and 14 deletions

View file

@ -34,8 +34,26 @@ class BaseLLM(ABC):
def __init__(self, config: LLMConfig):
pass
def _user_msg(self, msg: str) -> dict[str, str]:
return {"role": "user", "content": msg}
def _user_msg(self, msg: str, images: Optional[Union[str, list[str]]] = None) -> dict[str, Union[str, dict]]:
if images:
# as gpt-4v, chat with image
return self._user_msg_with_imgs(msg, images)
else:
return {"role": "user", "content": msg}
def _user_msg_with_imgs(self, msg: str, images: Optional[Union[str, list[str]]]):
"""
images: can be list of http(s) url or base64
"""
if isinstance(images, str):
images = [images]
content = [{"type": "text", "text": msg}]
for image in images:
# image url or image base64
url = image if image.startswith("http") else f"data:image/jpeg;base64,{image}"
# it can with multiple-image inputs
content.append({"type": "image_url", "image_url": url})
return {"role": "user", "content": content}
def _assistant_msg(self, msg: str) -> dict[str, str]:
return {"role": "assistant", "content": msg}
@ -54,6 +72,7 @@ class BaseLLM(ABC):
msg: str,
system_msgs: Optional[list[str]] = None,
format_msgs: Optional[list[dict[str, str]]] = None,
images: Optional[Union[str, list[str]]] = None,
timeout=3,
stream=True,
) -> str:
@ -65,7 +84,7 @@ class BaseLLM(ABC):
message = []
if format_msgs:
message.extend(format_msgs)
message.append(self._user_msg(msg))
message.append(self._user_msg(msg, images=images))
logger.debug(message)
rsp = await self.acompletion_text(message, stream=stream, timeout=timeout)
return rsp

View file

@ -99,7 +99,7 @@ class OpenAILLM(BaseLLM):
"messages": messages,
"max_tokens": self._get_max_tokens(messages),
"n": 1,
"stop": None,
# "stop": None, # default it's None and gpt4-v can't have this one
"temperature": 0.3,
"model": self.model,
"timeout": max(self.config.timeout, timeout),