Controlled Generations ensure that the model always follows your supplied JSON schema. Portkey supports Vertex AI’s Controlled Generations feature out of the box with our SDKs & APIs.
Controlled Generations allows you to constrain model responses to predefined sets of values. This is particularly useful for classification tasks, multiple choice responses, and structured data extraction.
This feature is available for Gemini 1.5 Pro & Gemini 1.5 Flash models.
Portkey SDKs for Python and JavaScript also make it easy to define object schemas using Pydantic and Zod respectively. Below, you can see how to extract information from unstructured text that conforms to a schema defined in code.
Copy
Ask AI
from portkey_ai import Portkeyfrom pydantic import BaseModelclass Step(BaseModel): explanation: str output: strclass MathReasoning(BaseModel): steps: list[Step] final_answer: strportkey = Portkey( apiKey= "PORTKEY_API_KEY", virtual_key="VERTEX_VIRTUAL_KEY")completion = portkey.beta.chat.completions.parse( model="gemini-1.5-pro-002", messages=[ {"role": "system", "content": "You are a helpful math tutor. Guide the user through the solution step by step."}, {"role": "user", "content": "how can I solve 8x + 7 = -23"} ], response_format=MathReasoning,)print(completion.choices[0].message)print(completion.choices[0].message.parsed)
You can also use enums to constrain the model’s output to a predefined set of values. This is particularly useful for classification tasks and multiple choice responses.
Copy
Ask AI
from portkey_ai import Portkeyfrom enum import Enumfrom pydantic import BaseModelclass InstrumentClass(Enum): PERCUSSION = "Percussion" STRING = "String" WOODWIND = "Woodwind" BRASS = "Brass" KEYBOARD = "Keyboard"# Initialize Portkey with API detailsportkey = Portkey( api_key="PORTKEY_API_KEY", virtual_key="VERTEX_VIRTUAL_KEY")# Simple enum classificationcompletion = portkey.chat.completions.create( model="gemini-1.5-pro-002", messages=[ {"role": "system", "content": "Classify the musical instrument."}, {"role": "user", "content": "What type of instrument is a piano?"} ], response_format={ "type": "json_schema", "json_schema": { "type": "string", "enum": [e.value for e in InstrumentClass], "title": "instrument_classification" } })print(completion.choices[0].message.content)
This method is more portable across different languages and doesn’t require additional libraries, but lacks the integrated type checking of the Pydantic/Zod approach. Choose the method that best fits your project’s needs.