llm/tests/test_async.py

62 lines
1.8 KiB
Python
Raw Normal View History

llm.get_async_model(), llm.AsyncModel base class and OpenAI async models (#613) - https://github.com/simonw/llm/issues/507#issuecomment-2458639308 * register_model is now async aware Refs https://github.com/simonw/llm/issues/507#issuecomment-2458658134 * Refactor Chat and AsyncChat to use _Shared base class Refs https://github.com/simonw/llm/issues/507#issuecomment-2458692338 * fixed function name * Fix for infinite loop * Applied Black * Ran cog * Applied Black * Add Response.from_row() classmethod back again It does not matter that this is a blocking call, since it is a classmethod * Made mypy happy with llm/models.py * mypy fixes for openai_models.py I am unhappy with this, had to duplicate some code. * First test for AsyncModel * Still have not quite got this working * Fix for not loading plugins during tests, refs #626 * audio/wav not audio/wave, refs #603 * Black and mypy and ruff all happy * Refactor to avoid generics * Removed obsolete response() method * Support text = await async_mock_model.prompt("hello") * Initial docs for llm.get_async_model() and await model.prompt() Refs #507 * Initial async model plugin creation docs * duration_ms ANY to pass test * llm models --async option Refs https://github.com/simonw/llm/pull/613#issuecomment-2474724406 * Removed obsolete TypeVars * Expanded register_models() docs for async * await model.prompt() now returns AsyncResponse Refs https://github.com/simonw/llm/pull/613#issuecomment-2475157822 --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2024-11-14 01:51:00 +00:00
import llm
import pytest
@pytest.mark.asyncio
async def test_async_model(async_mock_model):
gathered = []
async_mock_model.enqueue(["hello world"])
async for chunk in async_mock_model.prompt("hello"):
gathered.append(chunk)
assert gathered == ["hello world"]
# Not as an iterator
async_mock_model.enqueue(["hello world"])
response = await async_mock_model.prompt("hello")
text = await response.text()
assert text == "hello world"
assert isinstance(response, llm.AsyncResponse)
usage = await response.usage()
assert usage.input == 1
assert usage.output == 1
assert usage.details is None
@pytest.mark.asyncio
async def test_async_model_conversation(async_mock_model):
async_mock_model.enqueue(["joke 1"])
conversation = async_mock_model.conversation()
response = await conversation.prompt("joke")
text = await response.text()
assert text == "joke 1"
async_mock_model.enqueue(["joke 2"])
response2 = await conversation.prompt("again")
text2 = await response2.text()
assert text2 == "joke 2"
@pytest.mark.asyncio
async def test_async_on_done(async_mock_model):
async_mock_model.enqueue(["hello world"])
response = await async_mock_model.prompt(prompt="hello")
caught = []
def done(response):
caught.append(response)
assert len(caught) == 0
await response.on_done(done)
await response.text()
assert response._done
assert len(caught) == 1
@pytest.mark.asyncio
async def test_async_conversation(async_mock_model):
async_mock_model.enqueue(["one"])
conversation = async_mock_model.conversation()
response1 = await conversation.prompt("hi").text()
async_mock_model.enqueue(["two"])
response2 = await conversation.prompt("hi").text()
assert response1 == "one"
assert response2 == "two"