Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added the ability to call Ollama client seamlessly for image description #265

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ dependencies = [
"pathvalidate",
"charset-normalizer",
"openai",
"ollama"
]

[project.urls]
Expand Down
51 changes: 35 additions & 16 deletions src/markitdown/_markitdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import tempfile
import traceback
import zipfile
from http.client import responses
from xml.dom import minidom
from typing import Any, Dict, List, Optional, Union
from pathlib import Path
Expand Down Expand Up @@ -1085,24 +1086,42 @@ def _get_llm_description(self, local_path, extension, client, model, prompt=None
content_type = "image/jpeg"
image_base64 = base64.b64encode(image_file.read()).decode("utf-8")
data_uri = f"data:{content_type};base64,{image_base64}"
# check if Ollama client
if str(type(client)) == "<class 'ollama._client.Client'>":
messages = [
{
"role": "user",
"content": prompt,
'images': [local_path]

messages = [
{
"role": "user",
"content": [
{"type": "text", "text": prompt},
{
"type": "image_url",
"image_url": {
"url": data_uri,
},
},
],
}
]
}
]

response = client.chat(
model = model,
messages = messages,

response = client.chat.completions.create(model=model, messages=messages)
return response.choices[0].message.content
)

return response.message.content

else:# use openai
messages = [
{
"role": "user",
"content": [
{"type": "text", "text": prompt},
{
"type": "image_url",
"image_url": {
"url": data_uri,
},
},
],
}
]
response = client.chat.completions.create(model=model, messages=messages)
return response.choices[0].message.content


class OutlookMsgConverter(DocumentConverter):
Expand Down