Skip to content

Commit 5c84d6d

Browse files
made specific to OpenAI backend
1 parent 542b9f1 commit 5c84d6d

File tree

1 file changed

+77
-12
lines changed

1 file changed

+77
-12
lines changed

test/stdlib_basics/test_vision.py renamed to test/stdlib_basics/test_vision_openai.py

Lines changed: 77 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from mellea.backends import ModelOption
1111
from mellea.stdlib.base import ImageBlock, ModelOutputThunk
1212
from mellea.stdlib.chat import Message
13+
from mellea.stdlib.instruction import Instruction
1314

1415

1516
@pytest.fixture(scope="module")
@@ -24,8 +25,10 @@ def m_session(gh_run):
2425
)
2526
else:
2627
m = start_session(
27-
"ollama",
28+
"openai",
2829
model_id="granite3.2-vision",
30+
base_url=f"http://{os.environ.get('OLLAMA_HOST', 'localhost:11434')}/v1",
31+
api_key="ollama",
2932
model_options={ModelOption.MAX_NEW_TOKENS: 5},
3033
)
3134
yield m
@@ -69,14 +72,46 @@ def test_image_block_in_instruction(m_session: MelleaSession, pil_image: Image.I
6972
if not gh_run == 1:
7073
assert "yes" in instr.value.lower() or "no" in instr.value.lower()
7174

72-
# make sure you get the last prompt
73-
lp = m_session.last_prompt()
74-
assert isinstance(lp, list)
75-
assert "images" in lp[0]
75+
# make sure you get the last action
76+
_, log = m_session.ctx.last_output_and_logs()
77+
last_action = log.action
78+
assert isinstance(last_action, Instruction)
79+
assert len(last_action._images) > 0
7680

7781
# first image in image list should be the same as the image block
78-
image0_str = lp[0]["images"][0]
79-
assert image0_str == image_block._value
82+
image0 = last_action._images[0]
83+
assert image0 == image_block
84+
85+
# get prompt message
86+
lp = log.prompt
87+
assert isinstance(lp, list)
88+
assert len(lp) == 1
89+
90+
# prompt message is a dict
91+
prompt_msg = lp[0]
92+
assert isinstance(prompt_msg, dict)
93+
94+
# ### OPENAI SPECIFIC TEST ####
95+
96+
# get content
97+
content_list = prompt_msg.get("content", None)
98+
assert isinstance(content_list, list)
99+
assert len(content_list) == 2
100+
101+
# get the image content
102+
content_img = content_list[1]
103+
assert isinstance(content_img, dict)
104+
assert content_img.get("type") == "image_url"
105+
106+
# image url
107+
image_url = content_img.get("image_url")
108+
assert image_url is not None
109+
assert "url" in image_url
110+
111+
# check that the image is in the url content
112+
assert image_block._value[:100] in image_url["url"]
113+
114+
80115

81116

82117
def test_image_block_in_chat(m_session: MelleaSession, pil_image: Image.Image, gh_run: int):
@@ -87,15 +122,45 @@ def test_image_block_in_chat(m_session: MelleaSession, pil_image: Image.Image, g
87122
if not gh_run == 1:
88123
assert "yes" in ct.content.lower() or "no" in ct.content.lower()
89124

90-
# make sure you get the last prompt
91-
lp = m_session.last_prompt()
92-
assert isinstance(lp, list)
93-
assert "images" in lp[0]
125+
# make sure you get the last action
126+
_, log = m_session.ctx.last_output_and_logs()
127+
last_action = log.action
128+
assert isinstance(last_action, Message)
129+
assert len(last_action.images) > 0
94130

95131
# first image in image list should be the same as the image block
96-
image0_str = lp[0]["images"][0]
132+
image0_str = last_action.images[0]
97133
assert image0_str == ImageBlock.from_pil_image(pil_image)._value
98134

135+
# get prompt message
136+
lp = log.prompt
137+
assert isinstance(lp, list)
138+
assert len(lp) == 1
139+
140+
# prompt message is a dict
141+
prompt_msg = lp[0]
142+
assert isinstance(prompt_msg, dict)
143+
144+
# ### OPENAI SPECIFIC TEST ####
145+
146+
# get content
147+
content_list = prompt_msg.get("content", None)
148+
assert isinstance(content_list, list)
149+
assert len(content_list) == 2
150+
151+
# get the image content
152+
content_img = content_list[1]
153+
assert isinstance(content_img, dict)
154+
assert content_img.get("type") == "image_url"
155+
156+
# image url
157+
image_url = content_img.get("image_url")
158+
assert image_url is not None
159+
assert "url" in image_url
160+
161+
# check that the image is in the url content
162+
assert ImageBlock.from_pil_image(pil_image)._value[:100] in image_url["url"]
163+
99164

100165
if __name__ == "__main__":
101166
pytest.main([__file__])

0 commit comments

Comments
 (0)