Skip to content

Commit 5b54226

Browse files
authored
[Test] Basic AOAI JSON test (#1253)
A very simple test of JSON functionality in AOAI. This does rely on the model having some 'baseline' knowledge and reasoning power.... should be OK for GPT-4 class models.
1 parent f566602 commit 5b54226

3 files changed

Lines changed: 40 additions & 2 deletions

File tree

tests/model_specific/common_chat_testing.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import pydantic
2+
13
from guidance import assistant, gen, models, system, user
4+
from guidance import json as gen_json
25

36

47
def smoke_chat(lm: models.Model, has_system_role: bool = True):
@@ -74,3 +77,23 @@ def longer_chat_2(lm: models.Model, has_system_role: bool = True):
7477

7578
print(str(lm))
7679
assert len(lm["number"]) > 0
80+
81+
82+
def json_output_smoke(lm: models.Model):
83+
class NameHolder(pydantic.BaseModel):
84+
my_name: str
85+
my_age: int
86+
model_config = pydantic.ConfigDict(extra="forbid")
87+
88+
with user():
89+
lm += "Hello, my name is Tweedledum and I am 10 years old. What is my twin brother's name and age?"
90+
91+
EXTRACT_KEY = "my_output"
92+
with assistant():
93+
lm += gen_json(name=EXTRACT_KEY, schema=NameHolder)
94+
95+
output_json = lm[EXTRACT_KEY]
96+
name_data = NameHolder.model_validate_json(output_json)
97+
print(name_data.model_dump_json(indent=4))
98+
assert name_data.my_name == "Tweedledee"
99+
assert name_data.my_age == 10

tests/need_credentials/test_azureai_openai.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
from urllib.parse import parse_qs, urlparse
44

5+
import json
6+
import pydantic
57
import pytest
68
import requests
79

810
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
911

1012
from guidance import assistant, gen, models, system, user, gen_audio, image
13+
from guidance import json as gen_json
1114
from guidance.models._azureai import create_azure_openai_model
1215

1316
from ..model_specific import common_chat_testing
@@ -110,6 +113,10 @@ def test_azureai_openai_chat_smoke(azureai_chat_model):
110113
common_chat_testing.smoke_chat(azureai_chat_model)
111114

112115

116+
def test_azureai_openai_chat_json(azureai_chat_model: models.Model):
117+
common_chat_testing.json_output_smoke(azureai_chat_model)
118+
119+
113120
def test_azureai_openai_audio_smoke(azureai_audio_model: models.Model):
114121
lm = azureai_audio_model
115122
with system():
@@ -189,7 +196,9 @@ def test_azureai_openai_completion_smoke():
189196
)
190197

191198
lm = models.AzureOpenAI(
192-
model=model, azure_endpoint=azureai_endpoint, azure_ad_token_provider=token_provider
199+
model=model,
200+
azure_endpoint=azureai_endpoint,
201+
azure_ad_token_provider=token_provider,
193202
)
194203
assert isinstance(lm, models.AzureOpenAI)
195204
assert isinstance(lm.engine, models._openai.OpenAIEngine)
@@ -240,7 +249,9 @@ def test_azureai_openai_chat_loop(azureai_chat_model):
240249
for i in range(2):
241250
print(f"Iteration: {i}")
242251
with system():
243-
generation = azureai_chat_model + "You will just return whatever number I give you"
252+
generation = (
253+
azureai_chat_model + "You will just return whatever number I give you"
254+
)
244255

245256
with user():
246257
generation += f"The number is: {i}"

tests/need_credentials/test_openai.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,7 @@ def test_openai_image_smoke(openai_image_model: Model):
7878
lm += gen(name="img_describe")
7979
assert "img_describe" in lm
8080
assert len(lm["img_describe"]) > 0
81+
82+
83+
def test_openai_chat_json(openai_model: Model):
84+
common_chat_testing.json_output_smoke(openai_model)

0 commit comments

Comments
 (0)