-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Image Support - thanks, @montasaurus
* add image support * bump python and llm versions * reset user directory on each test * add recordings to readme * Ensure compatible OpenAI library --------- Co-authored-by: Simon Willison <swillison@gmail.com>
- Loading branch information
1 parent
b01ab71
commit 8fbcfec
Showing
10 changed files
with
1,780 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
608 changes: 608 additions & 0 deletions
608
tests/cassettes/test_llm_openrouter/test_image_prompt.yaml
Large diffs are not rendered by default.
Oops, something went wrong.
483 changes: 483 additions & 0 deletions
483
tests/cassettes/test_llm_openrouter/test_llm_models.yaml
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,71 @@ | ||
import llm | ||
import pytest | ||
from click.testing import CliRunner | ||
from inline_snapshot import snapshot | ||
from llm.cli import cli | ||
import json | ||
import pytest | ||
|
||
TINY_PNG = ( | ||
b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\xa6\x00\x00\x01\x1a" | ||
b"\x02\x03\x00\x00\x00\xe6\x99\xc4^\x00\x00\x00\tPLTE\xff\xff\xff" | ||
b"\x00\xff\x00\xfe\x01\x00\x12t\x01J\x00\x00\x00GIDATx\xda\xed\xd81\x11" | ||
b"\x000\x08\xc0\xc0.]\xea\xaf&Q\x89\x04V\xe0>\xf3+\xc8\x91Z\xf4\xa2\x08EQ\x14E" | ||
b"Q\x14EQ\x14EQ\xd4B\x91$I3\xbb\xbf\x08EQ\x14EQ\x14EQ\x14E\xd1\xa5" | ||
b"\xd4\x17\x91\xc6\x95\x05\x15\x0f\x9f\xc5\t\x9f\xa4\x00\x00\x00\x00IEND\xaeB`" | ||
b"\x82" | ||
) | ||
|
||
|
||
@pytest.mark.vcr | ||
def test_prompt(): | ||
model = llm.get_model("openrouter/openai/gpt-4o") | ||
response = model.prompt("Two names for a pet pelican, be brief") | ||
assert str(response) == snapshot("Gully or Skipper") | ||
response_dict = dict(response.response_json) | ||
response_dict.pop("id") # differs between requests | ||
assert response_dict == snapshot( | ||
{ | ||
"content": "Gully or Skipper", | ||
"role": "assistant", | ||
"finish_reason": "stop", | ||
"usage": {"completion_tokens": 5, "prompt_tokens": 17, "total_tokens": 22}, | ||
"object": "chat.completion.chunk", | ||
"model": "openai/gpt-4o", | ||
"created": 1731200404, | ||
} | ||
) | ||
|
||
|
||
@pytest.mark.parametrize("set_key", (False, True)) | ||
def test_llm_models(set_key, user_path): | ||
@pytest.mark.vcr | ||
def test_llm_models(): | ||
runner = CliRunner() | ||
if set_key: | ||
(user_path / "keys.json").write_text(json.dumps({"openrouter": "x"}), "utf-8") | ||
result = runner.invoke(cli, ["models", "list"]) | ||
assert result.exit_code == 0, result.output | ||
fragments = ( | ||
"OpenRouter: openrouter/openai/gpt-3.5-turbo", | ||
"OpenRouter: openrouter/anthropic/claude-2", | ||
) | ||
for fragment in fragments: | ||
if set_key: | ||
assert fragment in result.output | ||
else: | ||
assert fragment not in result.output | ||
assert fragment in result.output | ||
|
||
|
||
@pytest.mark.vcr | ||
def test_image_prompt(): | ||
model = llm.get_model("openrouter/anthropic/claude-3.5-sonnet") | ||
response = model.prompt( | ||
"Describe image in three words", | ||
attachments=[llm.Attachment(content=TINY_PNG)], | ||
) | ||
assert str(response) == snapshot("Red and green") | ||
response_dict = response.response_json | ||
response_dict.pop("id") # differs between requests | ||
assert response_dict == snapshot( | ||
{ | ||
"content": "Red and green", | ||
"role": "assistant", | ||
"finish_reason": "end_turn", | ||
"usage": {"completion_tokens": 7, "prompt_tokens": 82, "total_tokens": 89}, | ||
"object": "chat.completion.chunk", | ||
"model": "anthropic/claude-3.5-sonnet", | ||
"created": 1731200406, | ||
} | ||
) |