Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add agent response format for structured output #72

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/llama_stack_client/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ def __init__(
if default_headers is None:
default_headers = {}
default_headers["X-LlamaStack-ProviderData"] = json.dumps(provider_data)

super().__init__(
version=__version__,
base_url=base_url,
Expand Down Expand Up @@ -316,6 +317,7 @@ def __init__(
if default_headers is None:
default_headers = {}
default_headers["X-LlamaStack-ProviderData"] = json.dumps(provider_data)

super().__init__(
version=__version__,
base_url=base_url,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"Message",
"Logprobs",
"ResponseFormat",
"ResponseFormatJsonSchemaFormat",
"ResponseFormatGrammarFormat",
"ResponseFormatUnionMember0",
"ResponseFormatUnionMember1",
"Tool",
"InferenceChatCompletionParamsNonStreaming",
"InferenceChatCompletionParamsStreaming",
Expand Down Expand Up @@ -64,19 +64,19 @@ class Logprobs(TypedDict, total=False):
top_k: int


class ResponseFormatJsonSchemaFormat(TypedDict, total=False):
class ResponseFormatUnionMember0(TypedDict, total=False):
json_schema: Required[Dict[str, Union[bool, float, str, Iterable[object], object, None]]]

type: Required[Literal["json_schema"]]


class ResponseFormatGrammarFormat(TypedDict, total=False):
class ResponseFormatUnionMember1(TypedDict, total=False):
bnf: Required[Dict[str, Union[bool, float, str, Iterable[object], object, None]]]

type: Required[Literal["grammar"]]


ResponseFormat: TypeAlias = Union[ResponseFormatJsonSchemaFormat, ResponseFormatGrammarFormat]
ResponseFormat: TypeAlias = Union[ResponseFormatUnionMember0, ResponseFormatUnionMember1]


class Tool(TypedDict, total=False):
Expand Down
10 changes: 5 additions & 5 deletions src/llama_stack_client/types/inference_completion_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
"InferenceCompletionParamsBase",
"Logprobs",
"ResponseFormat",
"ResponseFormatJsonSchemaFormat",
"ResponseFormatGrammarFormat",
"ResponseFormatUnionMember0",
"ResponseFormatUnionMember1",
"InferenceCompletionParamsNonStreaming",
"InferenceCompletionParamsStreaming",
]
Expand All @@ -38,19 +38,19 @@ class Logprobs(TypedDict, total=False):
top_k: int


class ResponseFormatJsonSchemaFormat(TypedDict, total=False):
class ResponseFormatUnionMember0(TypedDict, total=False):
json_schema: Required[Dict[str, Union[bool, float, str, Iterable[object], object, None]]]

type: Required[Literal["json_schema"]]


class ResponseFormatGrammarFormat(TypedDict, total=False):
class ResponseFormatUnionMember1(TypedDict, total=False):
bnf: Required[Dict[str, Union[bool, float, str, Iterable[object], object, None]]]

type: Required[Literal["grammar"]]


ResponseFormat: TypeAlias = Union[ResponseFormatJsonSchemaFormat, ResponseFormatGrammarFormat]
ResponseFormat: TypeAlias = Union[ResponseFormatUnionMember0, ResponseFormatUnionMember1]


class InferenceCompletionParamsNonStreaming(InferenceCompletionParamsBase, total=False):
Expand Down
21 changes: 19 additions & 2 deletions src/llama_stack_client/types/shared/agent_config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

from typing import List, Union, Optional
from typing import Dict, List, Union, Optional
from typing_extensions import Literal, TypeAlias

from ..._models import BaseModel
Expand All @@ -12,7 +12,22 @@
from .wolfram_alpha_tool_definition import WolframAlphaToolDefinition
from .code_interpreter_tool_definition import CodeInterpreterToolDefinition

__all__ = ["AgentConfig", "Tool"]
__all__ = ["AgentConfig", "ResponseFormat", "ResponseFormatUnionMember0", "ResponseFormatUnionMember1", "Tool"]


class ResponseFormatUnionMember0(BaseModel):
json_schema: Dict[str, Union[bool, float, str, List[object], object, None]]

type: Literal["json_schema"]


class ResponseFormatUnionMember1(BaseModel):
bnf: Dict[str, Union[bool, float, str, List[object], object, None]]

type: Literal["grammar"]


ResponseFormat: TypeAlias = Union[ResponseFormatUnionMember0, ResponseFormatUnionMember1]

Tool: TypeAlias = Union[
SearchToolDefinition,
Expand All @@ -37,6 +52,8 @@ class AgentConfig(BaseModel):

output_shields: Optional[List[str]] = None

response_format: Optional[ResponseFormat] = None

sampling_params: Optional[SamplingParams] = None

tool_choice: Optional[Literal["auto", "required"]] = None
Expand Down
21 changes: 19 additions & 2 deletions src/llama_stack_client/types/shared_params/agent_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import annotations

from typing import List, Union, Iterable
from typing import Dict, List, Union, Iterable
from typing_extensions import Literal, Required, TypeAlias, TypedDict

from .sampling_params import SamplingParams
Expand All @@ -13,7 +13,22 @@
from .wolfram_alpha_tool_definition import WolframAlphaToolDefinition
from .code_interpreter_tool_definition import CodeInterpreterToolDefinition

__all__ = ["AgentConfig", "Tool"]
__all__ = ["AgentConfig", "ResponseFormat", "ResponseFormatUnionMember0", "ResponseFormatUnionMember1", "Tool"]


class ResponseFormatUnionMember0(TypedDict, total=False):
json_schema: Required[Dict[str, Union[bool, float, str, Iterable[object], object, None]]]

type: Required[Literal["json_schema"]]


class ResponseFormatUnionMember1(TypedDict, total=False):
bnf: Required[Dict[str, Union[bool, float, str, Iterable[object], object, None]]]

type: Required[Literal["grammar"]]


ResponseFormat: TypeAlias = Union[ResponseFormatUnionMember0, ResponseFormatUnionMember1]

Tool: TypeAlias = Union[
SearchToolDefinition,
Expand All @@ -38,6 +53,8 @@ class AgentConfig(TypedDict, total=False):

output_shields: List[str]

response_format: ResponseFormat

sampling_params: SamplingParams

tool_choice: Literal["auto", "required"]
Expand Down
8 changes: 8 additions & 0 deletions tests/api_resources/test_agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def test_method_create_with_all_params(self, client: LlamaStackClient) -> None:
"model": "model",
"input_shields": ["string"],
"output_shields": ["string"],
"response_format": {
"json_schema": {"foo": True},
"type": "json_schema",
},
"sampling_params": {
"strategy": "greedy",
"max_tokens": 0,
Expand Down Expand Up @@ -169,6 +173,10 @@ async def test_method_create_with_all_params(self, async_client: AsyncLlamaStack
"model": "model",
"input_shields": ["string"],
"output_shields": ["string"],
"response_format": {
"json_schema": {"foo": True},
"type": "json_schema",
},
"sampling_params": {
"strategy": "greedy",
"max_tokens": 0,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1631,7 +1631,7 @@ def test_get_platform(self) -> None:
import threading

from llama_stack_client._utils import asyncify
from llama_stack_client._base_client import get_platform
from llama_stack_client._base_client import get_platform

async def test_main() -> None:
result = await asyncify(get_platform)()
Expand Down
Loading