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

Fix Issues #966

Merged
merged 7 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 7 additions & 3 deletions instructor/client_vertexai.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,22 @@
) -> gm.Content:
if isinstance(message["content"], str):
return gm.Content(
role=message["role"], parts=[gm.Part.from_text(message["content"])]
role=message["role"], # type:ignore
parts=[gm.Part.from_text(message["content"])],
)
elif isinstance(message["content"], list):
parts = []
parts: list[gm.Part] = []
for item in message["content"]:
if isinstance(item, str):
parts.append(gm.Part.from_text(item))
elif isinstance(item, gm.Part):
parts.append(item)
else:
raise ValueError(f"Unsupported content type in list: {type(item)}")
return gm.Content(role=message["role"], parts=parts)
return gm.Content(
role=message["role"], # type:ignore
parts=parts,
)
else:
raise ValueError("Unsupported message content type")

Expand Down Expand Up @@ -82,7 +86,7 @@

def vertexai_process_response(_kwargs: dict[str, Any], model: BaseModel):
messages: list[dict[str, str]] = _kwargs.pop("messages")
contents = _vertexai_message_list_parser(messages)

Check failure on line 89 in instructor/client_vertexai.py

View workflow job for this annotation

GitHub Actions / Pyright (ubuntu-latest, 3.11)

Argument of type "list[dict[str, str]]" cannot be assigned to parameter "messages" of type "list[dict[str, str | Part | list[str | Part]]]" in function "_vertexai_message_list_parser"   "list[dict[str, str]]" is incompatible with "list[dict[str, str | Part | list[str | Part]]]"     Type parameter "_T@list" is invariant, but "dict[str, str]" is not the same as "dict[str, str | Part | list[str | Part]]"     Consider switching from "list" to "Sequence" which is covariant (reportArgumentType)

Check failure on line 89 in instructor/client_vertexai.py

View workflow job for this annotation

GitHub Actions / Pyright (macos-latest, 3.9)

Argument of type "list[dict[str, str]]" cannot be assigned to parameter "messages" of type "list[dict[str, str | Part | list[str | Part]]]" in function "_vertexai_message_list_parser"   "list[dict[str, str]]" is incompatible with "list[dict[str, str | Part | list[str | Part]]]"     Type parameter "_T@list" is invariant, but "dict[str, str]" is not the same as "dict[str, str | Part | list[str | Part]]"     Consider switching from "list" to "Sequence" which is covariant (reportArgumentType)

Check failure on line 89 in instructor/client_vertexai.py

View workflow job for this annotation

GitHub Actions / Pyright (macos-latest, 3.11)

Argument of type "list[dict[str, str]]" cannot be assigned to parameter "messages" of type "list[dict[str, str | Part | list[str | Part]]]" in function "_vertexai_message_list_parser"   "list[dict[str, str]]" is incompatible with "list[dict[str, str | Part | list[str | Part]]]"     Type parameter "_T@list" is invariant, but "dict[str, str]" is not the same as "dict[str, str | Part | list[str | Part]]"     Consider switching from "list" to "Sequence" which is covariant (reportArgumentType)

tool = _create_vertexai_tool(model=model)

Expand All @@ -96,7 +100,7 @@

def vertexai_process_json_response(_kwargs: dict[str, Any], model: BaseModel):
messages: list[dict[str, str]] = _kwargs.pop("messages")
contents = _vertexai_message_list_parser(messages)

Check failure on line 103 in instructor/client_vertexai.py

View workflow job for this annotation

GitHub Actions / Pyright (ubuntu-latest, 3.11)

Argument of type "list[dict[str, str]]" cannot be assigned to parameter "messages" of type "list[dict[str, str | Part | list[str | Part]]]" in function "_vertexai_message_list_parser"   "list[dict[str, str]]" is incompatible with "list[dict[str, str | Part | list[str | Part]]]"     Type parameter "_T@list" is invariant, but "dict[str, str]" is not the same as "dict[str, str | Part | list[str | Part]]"     Consider switching from "list" to "Sequence" which is covariant (reportArgumentType)

Check failure on line 103 in instructor/client_vertexai.py

View workflow job for this annotation

GitHub Actions / Pyright (macos-latest, 3.9)

Argument of type "list[dict[str, str]]" cannot be assigned to parameter "messages" of type "list[dict[str, str | Part | list[str | Part]]]" in function "_vertexai_message_list_parser"   "list[dict[str, str]]" is incompatible with "list[dict[str, str | Part | list[str | Part]]]"     Type parameter "_T@list" is invariant, but "dict[str, str]" is not the same as "dict[str, str | Part | list[str | Part]]"     Consider switching from "list" to "Sequence" which is covariant (reportArgumentType)

Check failure on line 103 in instructor/client_vertexai.py

View workflow job for this annotation

GitHub Actions / Pyright (macos-latest, 3.11)

Argument of type "list[dict[str, str]]" cannot be assigned to parameter "messages" of type "list[dict[str, str | Part | list[str | Part]]]" in function "_vertexai_message_list_parser"   "list[dict[str, str]]" is incompatible with "list[dict[str, str | Part | list[str | Part]]]"     Type parameter "_T@list" is invariant, but "dict[str, str]" is not the same as "dict[str, str | Part | list[str | Part]]"     Consider switching from "list" to "Sequence" which is covariant (reportArgumentType)

config: dict[str, Any] | None = _kwargs.pop("generation_config", None)

Expand Down
14 changes: 4 additions & 10 deletions tests/llm/test_vertexai/test_message_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@
import vertexai.generative_models as gm
from instructor.client_vertexai import vertexai_message_parser

from .util import models, modes


@pytest.mark.parametrize("model, mode", [(models[0], modes[0])])
def test_vertexai_message_parser_string_content(model, mode):
def test_vertexai_message_parser_string_content():
message = {"role": "user", "content": "Hello, world!"}
result = vertexai_message_parser(message)

Expand All @@ -17,8 +14,7 @@ def test_vertexai_message_parser_string_content(model, mode):
assert result.parts[0].text == "Hello, world!"


@pytest.mark.parametrize("model, mode", [(models[0], modes[0])])
def test_vertexai_message_parser_list_content(model, mode):
def test_vertexai_message_parser_list_content():
message = {
"role": "user",
"content": [
Expand All @@ -40,16 +36,14 @@ def test_vertexai_message_parser_list_content(model, mode):
assert result.parts[2].text == " How are you?"


@pytest.mark.parametrize("model, mode", [(models[0], modes[0])])
def test_vertexai_message_parser_invalid_content(model, mode):
def test_vertexai_message_parser_invalid_content():
message = {"role": "user", "content": 123} # Invalid content type

with pytest.raises(ValueError, match="Unsupported message content type"):
vertexai_message_parser(message)


@pytest.mark.parametrize("model, mode", [(models[0], modes[0])])
def test_vertexai_message_parser_invalid_list_item(model, mode):
def test_vertexai_message_parser_invalid_list_item():
message = {"role": "user", "content": ["Hello", 123, gm.Part.from_text("world!")]}

with pytest.raises(ValueError, match="Unsupported content type in list"):
Expand Down
Loading