Replies: 1 comment
-
Maybe this will solve it @LinXin04 ? For more complex schema, I'd advise you to use Pydantic. Change the model_kwargs to your machine such as cuda, or just remove it, in terms of the model choice! from typing import Literal
import outlines
import torch
from pydantic import BaseModel, Field
class Armor(str):
leather = "leather"
chainmail = "chainmail"
plate = "plate"
class Weapon(str):
sword = "sword"
axe = "axe"
mace = "mace"
spear = "spear"
bow = "bow"
crossbow = "crossbow"
class Character(BaseModel):
name: str = Field(title="Name", max_length=10)
age: int = Field(title="Age")
armor: Literal[Armor.leather, Armor.chainmail, Armor.plate] = Field(title="Armor")
weapon: Literal[
Weapon.sword, Weapon.axe, Weapon.mace, Weapon.spear, Weapon.bow, Weapon.crossbow
] = Field(title="Weapon")
strength: int = Field(title="Strength")
model = outlines.models.transformers(
"HuggingFaceTB/SmolLM2-1.7B-Instruct",
model_kwargs={"device_map": "mps", "torch_dtype": torch.float16},
)
generator = outlines.generate.json(model, Character)
character = generator("Give me a character description")
print(character) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Can the type be "list"? if I want the type of name is list, how to set?
import outlines
schema = '''{
"title": "Character",
"type": "object",
"properties": {
"name": {
"title": "Name",
"maxLength": 10,
"type": "string"
},
"age": {
"title": "Age",
"type": "integer"
},
"armor": {"$ref": "#/definitions/Armor"},
"weapon": {"$ref": "#/definitions/Weapon"},
"strength": {
"title": "Strength",
"type": "integer"
}
},
"required": ["name", "age", "armor", "weapon", "strength"],
"definitions": {
"Armor": {
"title": "Armor",
"description": "An enumeration.",
"enum": ["leather", "chainmail", "plate"],
"type": "string"
},
"Weapon": {
"title": "Weapon",
"description": "An enumeration.",
"enum": ["sword", "axe", "mace", "spear", "bow", "crossbow"],
"type": "string"
}
}
}'''
model = outlines.models.transformers("microsoft/Phi-3-mini-4k-instruct")
generator = outlines.generate.json(model, schema)
character = generator("Give me a character description")
Beta Was this translation helpful? Give feedback.
All reactions