types: enable passing messages with arbitrary role (#462)
test / test (3.10) (push) Has been cancelled
test / test (3.11) (push) Has been cancelled
test / test (3.12) (push) Has been cancelled
test / test (3.13) (push) Has been cancelled
test / test (3.8) (push) Has been cancelled
test / test (3.9) (push) Has been cancelled
test / lint (push) Has been cancelled

---------

Co-authored-by: Ryan Stewart <ryanstewart@Ryans-MacBook-Pro.local>
Co-authored-by: Gabe Goodhart <gabe.l.hart@gmail.com>
This commit is contained in:
rylativity
2025-03-20 16:46:40 -04:00
committed by GitHub
parent 6b235b2d60
commit 07eec6d517
2 changed files with 33 additions and 2 deletions
+1 -1
View File
@@ -256,7 +256,7 @@ class Message(SubscriptableBaseModel):
Chat message. Chat message.
""" """
role: Literal['user', 'assistant', 'system', 'tool'] role: str
"Assumed role of the message. Response messages has role 'assistant' or 'tool'." "Assumed role of the message. Response messages has role 'assistant' or 'tool'."
content: Optional[str] = None content: Optional[str] = None
+32 -1
View File
@@ -4,14 +4,16 @@ import os
import re import re
import tempfile import tempfile
from pathlib import Path from pathlib import Path
from typing import Any
import pytest import pytest
from httpx import Response as httpxResponse
from pydantic import BaseModel, ValidationError from pydantic import BaseModel, ValidationError
from pytest_httpserver import HTTPServer, URIPattern from pytest_httpserver import HTTPServer, URIPattern
from werkzeug.wrappers import Request, Response from werkzeug.wrappers import Request, Response
from ollama._client import CONNECTION_ERROR_MESSAGE, AsyncClient, Client, _copy_tools from ollama._client import CONNECTION_ERROR_MESSAGE, AsyncClient, Client, _copy_tools
from ollama._types import Image from ollama._types import Image, Message
PNG_BASE64 = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGNgYGAAAAAEAAH2FzhVAAAAAElFTkSuQmCC' PNG_BASE64 = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAIAAACQd1PeAAAADElEQVR4nGNgYGAAAAAEAAH2FzhVAAAAAElFTkSuQmCC'
PNG_BYTES = base64.b64decode(PNG_BASE64) PNG_BYTES = base64.b64decode(PNG_BASE64)
@@ -1181,3 +1183,32 @@ async def test_async_client_connection_error():
with pytest.raises(ConnectionError) as exc_info: with pytest.raises(ConnectionError) as exc_info:
await client.show('model') await client.show('model')
assert str(exc_info.value) == 'Failed to connect to Ollama. Please check that Ollama is downloaded, running and accessible. https://ollama.com/download' assert str(exc_info.value) == 'Failed to connect to Ollama. Please check that Ollama is downloaded, running and accessible. https://ollama.com/download'
def test_arbitrary_roles_accepted_in_message():
_ = Message(role='somerandomrole', content="I'm ok with you adding any role message now!")
def _mock_request(*args: Any, **kwargs: Any) -> Response:
return httpxResponse(status_code=200, content="{'response': 'Hello world!'}")
def test_arbitrary_roles_accepted_in_message_request(monkeypatch: pytest.MonkeyPatch):
monkeypatch.setattr(Client, '_request', _mock_request)
client = Client()
client.chat(model='llama3.1', messages=[{'role': 'somerandomrole', 'content': "I'm ok with you adding any role message now!"}, {'role': 'user', 'content': 'Hello world!'}])
async def _mock_request_async(*args: Any, **kwargs: Any) -> Response:
return httpxResponse(status_code=200, content="{'response': 'Hello world!'}")
@pytest.mark.asyncio
async def test_arbitrary_roles_accepted_in_message_request_async(monkeypatch: pytest.MonkeyPatch):
monkeypatch.setattr(AsyncClient, '_request', _mock_request_async)
client = AsyncClient()
await client.chat(model='llama3.1', messages=[{'role': 'somerandomrole', 'content': "I'm ok with you adding any role message now!"}, {'role': 'user', 'content': 'Hello world!'}])