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

Add support for prepending images to raw prompts and examples #392

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
41 changes: 41 additions & 0 deletions examples/multimodal-raw-generate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import sys
import random
import httpx

from ollama import generate

latest = httpx.get('https://xkcd.com/info.0.json')
latest.raise_for_status()

if len(sys.argv) > 1:
num = int(sys.argv[1])
else:
num = random.randint(1, latest.json().get('num'))

comic = httpx.get(f'https://xkcd.com/{num}/info.0.json')
comic.raise_for_status()

print(f'xkcd #{comic.json().get("num")}: {comic.json().get("alt")}')
print(f'link: https://xkcd.com/{num}')
print('---')

raw = httpx.get(comic.json().get('img'))
raw.raise_for_status()

for response in generate(model='llama3.2-vision',
prompt='explain this comic:',
raw=True,
prepend_images_to_raw_prompt=True,
images=[raw.content],
stream=True):
print(response['response'], end='', flush=True)

## Or to manually place image in specific location in prompt, use the below.
## note: the `[img-0]<image>` tag can be placed anywhere in the prompt, and the `0` corresponds to the index of the image in the `images` array

# for response in generate(model='llama3.2-vision',
# prompt='[img-0]<image>explain this comic:',
# raw=True,
# images=[raw.content],
# stream=True):
# print(response['response'], end='', flush=True)
4 changes: 4 additions & 0 deletions ollama/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ def generate(
context: Optional[Sequence[int]] = None,
stream: Literal[False] = False,
raw: bool = False,
prepend_images_to_raw_prompt: Optional[bool] = False,
format: Optional[Union[Literal['', 'json'], JsonSchemaValue]] = None,
images: Optional[Sequence[Union[str, bytes]]] = None,
options: Optional[Union[Mapping[str, Any], Options]] = None,
Expand All @@ -206,6 +207,7 @@ def generate(
context: Optional[Sequence[int]] = None,
stream: Literal[True] = True,
raw: bool = False,
prepend_images_to_raw_prompt: Optional[bool] = False,
format: Optional[Union[Literal['', 'json'], JsonSchemaValue]] = None,
images: Optional[Sequence[Union[str, bytes]]] = None,
options: Optional[Union[Mapping[str, Any], Options]] = None,
Expand All @@ -223,6 +225,7 @@ def generate(
context: Optional[Sequence[int]] = None,
stream: bool = False,
raw: Optional[bool] = None,
prepend_images_to_raw_prompt: Optional[bool] = False,
format: Optional[Union[Literal['', 'json'], JsonSchemaValue]] = None,
images: Optional[Sequence[Union[str, bytes]]] = None,
options: Optional[Union[Mapping[str, Any], Options]] = None,
Expand Down Expand Up @@ -251,6 +254,7 @@ def generate(
context=context,
stream=stream,
raw=raw,
prepend_images_to_raw_prompt=prepend_images_to_raw_prompt,
format=format,
images=[Image(value=image) for image in images] if images else None,
options=options,
Expand Down
3 changes: 3 additions & 0 deletions ollama/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ class GenerateRequest(BaseGenerateRequest):

raw: Optional[bool] = None

prepend_images_to_raw_prompt: Optional[bool] = None
'Whether to automatically add properly formatted and necessary image placeholders to top of raw prompt'

images: Optional[Sequence[Image]] = None
'Image data for multimodal models.'

Expand Down