Skip to content

Commit 46ea9e6

Browse files
nv-guomingzRia Jain
authored andcommitted
chore: add trtllm-serve json schema example into doc. (NVIDIA#6418)
Signed-off-by: nv-guomingz <[email protected]>
1 parent 24b60c9 commit 46ea9e6

File tree

3 files changed

+49
-11
lines changed

3 files changed

+49
-11
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
### :title OpenAI Completion Client with JSON Schema
2+
3+
from openai import OpenAI
4+
5+
client = OpenAI(
6+
base_url="http://localhost:8000/v1",
7+
api_key="tensorrt_llm",
8+
)
9+
10+
response = client.chat.completions.create(
11+
model="TinyLlama-1.1B-Chat-v1.0",
12+
messages=[{
13+
"role": "system",
14+
"content": "you are a helpful assistant"
15+
}, {
16+
"role":
17+
"user",
18+
"content":
19+
f"Give me the information of the biggest city of China in the JSON format.",
20+
}],
21+
max_tokens=100,
22+
temperature=0,
23+
response_format={
24+
"type": "json",
25+
"schema": {
26+
"type": "object",
27+
"properties": {
28+
"name": {
29+
"type": "string"
30+
},
31+
"population": {
32+
"type": "integer"
33+
},
34+
},
35+
"required": ["name", "population"],
36+
"chat_template_kwargs": {
37+
"enable_thinking": False
38+
}
39+
}
40+
},
41+
)
42+
print(response.choices[0].message.content)

tests/unittest/llmapi/apps/_test_openai_chat_json.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,6 @@ def client(server: RemoteOpenAIServer):
5757
return server.get_client()
5858

5959

60-
@pytest.fixture(scope="module")
61-
def async_client(server: RemoteOpenAIServer):
62-
return server.get_async_client()
63-
64-
6560
@pytest.fixture(scope="module")
6661
def user_profile_schema():
6762
"""Provides a sample JSON schema for a user profile."""

tests/unittest/llmapi/apps/_test_trtllm_serve_example.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ def example_root():
2929
return os.path.join(llm_root, "examples", "serve")
3030

3131

32-
@pytest.mark.parametrize("exe, script",
33-
[("python3", "openai_chat_client.py"),
34-
("python3", "openai_completion_client.py"),
35-
("bash", "curl_chat_client.sh"),
36-
("bash", "curl_completion_client.sh"),
37-
("bash", "genai_perf_client.sh")])
32+
@pytest.mark.parametrize(
33+
"exe, script", [("python3", "openai_chat_client.py"),
34+
("python3", "openai_completion_client.py"),
35+
("python3", "openai_completion_client_json_schema.py"),
36+
("bash", "curl_chat_client.sh"),
37+
("bash", "curl_completion_client.sh"),
38+
("bash", "genai_perf_client.sh")])
3839
def test_trtllm_serve_examples(exe: str, script: str,
3940
server: RemoteOpenAIServer, example_root: str):
4041
client_script = os.path.join(example_root, script)

0 commit comments

Comments
 (0)