mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-04 20:28:10 +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":
|
if content_part.get("type") == "text":
|
||||||
content += f"\n{content_part['text']}"
|
content += f"\n{content_part['text']}"
|
||||||
elif content_part.get("type") == "image_url":
|
elif content_part.get("type") == "image_url":
|
||||||
if isinstance(content_part.get("image_url"), str):
|
image_url = None
|
||||||
image_url_components = content_part["image_url"].split(",")
|
temp_image_url = content_part.get("image_url")
|
||||||
# Support data:image/jpeg;base64,<image> format
|
if isinstance(temp_image_url, str):
|
||||||
# and base64 strings
|
image_url = content_part["image_url"]
|
||||||
if len(image_url_components) > 1:
|
elif (
|
||||||
images.append(image_url_components[1])
|
isinstance(temp_image_url, dict) and "url" in temp_image_url
|
||||||
else:
|
):
|
||||||
images.append(image_url_components[0])
|
image_url = temp_image_url
|
||||||
else:
|
else:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"Only string image_url " "content parts are supported."
|
"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:
|
else:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"Unsupported message content type. "
|
"Unsupported message content type. "
|
||||||
|
Loading…
Reference in New Issue
Block a user