mirror of
https://github.com/hwchase17/langchain
synced 2024-11-18 09:25:54 +00:00
Ollama vision support (#22734)
**Description:** Ollama vision with messages in OpenAI-style support `{ "image_url": { "url": ... } }` **Issue:** #22460 Added flexible solution for ChatOllama to support chat messages with images. Works when you provide either `image_url` as a string or as a dict with "url" inside (like OpenAI does). So it makes available to use tuples with `ChatPromptTemplate.from_messages()` --------- Co-authored-by: Bagatur <baskaryan@gmail.com>
This commit is contained in:
parent
0908b01cb2
commit
912751e268
@ -137,18 +137,28 @@ class ChatOllama(BaseChatModel, _OllamaCommon):
|
||||
if content_part.get("type") == "text":
|
||||
content += f"\n{content_part['text']}"
|
||||
elif content_part.get("type") == "image_url":
|
||||
if isinstance(content_part.get("image_url"), str):
|
||||
image_url_components = content_part["image_url"].split(",")
|
||||
image_url = None
|
||||
temp_image_url = content_part.get("image_url")
|
||||
if isinstance(temp_image_url, str):
|
||||
image_url = content_part["image_url"]
|
||||
elif (
|
||||
isinstance(temp_image_url, dict) and "url" in temp_image_url
|
||||
):
|
||||
image_url = temp_image_url
|
||||
else:
|
||||
raise ValueError(
|
||||
"Only string image_url or dict with string 'url' "
|
||||
"inside content parts are supported."
|
||||
)
|
||||
|
||||
image_url_components = image_url.split(",")
|
||||
# Support data:image/jpeg;base64,<image> format
|
||||
# and base64 strings
|
||||
if len(image_url_components) > 1:
|
||||
images.append(image_url_components[1])
|
||||
else:
|
||||
images.append(image_url_components[0])
|
||||
else:
|
||||
raise ValueError(
|
||||
"Only string image_url " "content parts are supported."
|
||||
)
|
||||
|
||||
else:
|
||||
raise ValueError(
|
||||
"Unsupported message content type. "
|
||||
|
Loading…
Reference in New Issue
Block a user