-
Notifications
You must be signed in to change notification settings - Fork 391
/
Copy pathpydantic_schema.py
50 lines (38 loc) · 1.41 KB
/
pydantic_schema.py
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
from enum import Enum
import json
from typing import List, Optional
from pydantic import BaseModel, Field
from mistralrs import Runner, Which, ChatCompletionRequest
runner = Runner(
which=Which.Plain(
model_id="meta-llama/Llama-3.2-3B-Instruct",
),
)
class AirplaneType(str, Enum):
commercial = "commercial"
cargo = "cargo"
private = "private"
class Airplane(BaseModel):
id: int = Field(
..., title="Airplane ID", description="Unique identifier for the airplane"
)
model: str = Field(..., title="Model", min_length=1, max_length=100)
manufacturer: str = Field(..., title="Manufacturer", min_length=1, max_length=100)
type: AirplaneType = Field(default=AirplaneType.commercial, title="Airplane Type")
capacity: Optional[int] = Field(None, title="Passenger Capacity", ge=1, le=1000)
class Fleet(BaseModel):
fleet_name: str = Field(..., title="Fleet Name", min_length=1, max_length=50)
airplanes: List[Airplane] = Field(..., title="Fleet Airplanes", min_length=1)
fleet_schema = Fleet.model_json_schema()
res = runner.send_chat_completion_request(
ChatCompletionRequest(
model="phi",
messages=[{"role": "user", "content": "Give me a sample address."}],
max_tokens=256,
temperature=0.1,
grammar_type="json_schema",
grammar=json.dumps(fleet_schema),
)
)
print(res.choices[0].message.content)
print(res.usage)