-
Notifications
You must be signed in to change notification settings - Fork 394
Expand file tree
/
Copy pathtest_generate.py
More file actions
77 lines (65 loc) · 2.58 KB
/
test_generate.py
File metadata and controls
77 lines (65 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import pytest
from dirty_equals import IsPartialDict
from pydantic import BaseModel, Field
import marvin
from marvin.utilities.testing import assert_llm_equal
class Location(BaseModel):
city: str = Field(description="The city's proper name")
state: str = Field(description="2-letter state abbreviation")
class TestBuiltins:
async def test_toy_generate(self):
result = await marvin.generate_async(int, n=3)
await assert_llm_equal(result, "a list of three integers")
async def test_generate_locations(self):
result = await marvin.generate_async(Location, n=3)
await assert_llm_equal(result, "a list of three locations")
async def test_generate_locations_with_instructions(self):
result = await marvin.generate_async(
Location,
n=3,
instructions="major cities in California",
)
await assert_llm_equal(
result,
"a list of three different locations that are major cities in California",
)
assert all(isinstance(loc, Location) for loc in result)
async def test_error_if_no_type_or_instructions(self):
with pytest.raises(ValueError, match="Instructions are required"):
await marvin.generate_async(n=3)
async def test_type_is_string_if_only_instructions_given(self):
result = await marvin.generate_async(
instructions="major cities in California", n=2
)
await assert_llm_equal(
result,
"a list of two major cities in California, both given as strings",
)
class TestGenerateSchema:
async def test_generate_list_of_integers_schema(self):
result = await marvin.generate_schema_async(
instructions="a list that contains exactly three integers",
)
assert result == {
"type": "array",
"items": {"type": "integer"},
"minItems": 3,
"maxItems": 3,
}
async def test_generate_schema_for_movie(self):
result = await marvin.generate_schema_async(
instructions="a movie with a title, director, and release_year",
)
assert result == IsPartialDict(
{
"type": "object",
"properties": IsPartialDict(
{
"title": {"type": "string"},
"director": {"type": "string"},
"release_year": {"type": "integer"},
}
),
"required": ["title", "director", "release_year"],
}
)