Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
15 changes: 9 additions & 6 deletions outlines/models/openai.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Integration with OpenAI's API."""

import asyncio
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -298,9 +299,10 @@ def generate_batch(
output_type = None,
**inference_kwargs,
):
raise NotImplementedError(
"The `openai` library does not support batch inference."
)
return [
self.generate(single_input, output_type, **inference_kwargs)
for single_input in model_input
]

def generate_stream(
self,
Expand Down Expand Up @@ -448,9 +450,10 @@ async def generate_batch(
output_type = None,
**inference_kwargs,
):
raise NotImplementedError(
"The `openai` library does not support batch inference."
)
return list(await asyncio.gather(*(
self.generate(single_input, output_type, **inference_kwargs)
for single_input in model_input
)))

async def generate_stream( # type: ignore
self,
Expand Down
66 changes: 56 additions & 10 deletions tests/models/test_openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,34 @@ def test_openai_streaming(model):
assert isinstance(next(result), str)


def test_openai_batch(model):
with pytest.raises(NotImplementedError, match="does not support"):
model.batch(
["Respond with one word.", "Respond with one word."],
)
def test_openai_batch():
from tests.test_utils.mock_openai_client import MockOpenAIClient

mock_client = MockOpenAIClient()
mock_client.add_mock_responses([
(
{
"messages": [{"role": "user", "content": "Say hello"}],
"model": MODEL_NAME,
},
"hello",
),
(
{
"messages": [{"role": "user", "content": "Say world"}],
"model": MODEL_NAME,
},
"world",
),
])

model = OpenAI(mock_client, MODEL_NAME)
results = model.batch(["Say hello", "Say world"])

assert isinstance(results, list)
assert len(results) == 2
assert results[0] == "hello"
assert results[1] == "world"


def test_openai_async_init_from_client(api_key):
Expand Down Expand Up @@ -352,8 +375,31 @@ async def test_openai_async_streaming(async_model):


@pytest.mark.asyncio
async def test_openai_async_batch(async_model):
with pytest.raises(NotImplementedError, match="does not support"):
await async_model.batch(
["Respond with one word.", "Respond with one word."],
)
async def test_openai_async_batch():
from tests.test_utils.mock_openai_client import MockAsyncOpenAIClient

mock_client = MockAsyncOpenAIClient()
mock_client.add_mock_responses([
(
{
"messages": [{"role": "user", "content": "Say hello"}],
"model": MODEL_NAME,
},
"hello",
),
(
{
"messages": [{"role": "user", "content": "Say world"}],
"model": MODEL_NAME,
},
"world",
),
])

model = AsyncOpenAI(mock_client, MODEL_NAME)
results = await model.batch(["Say hello", "Say world"])

assert isinstance(results, list)
assert len(results) == 2
assert results[0] == "hello"
assert results[1] == "world"