Skip to content
Merged
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: 1 addition & 1 deletion .autoblocks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ autogenerate:

prompts:
- id: used-by-ci-dont-delete
major_version: 6
major_version: 7

- id: used-by-ci-dont-delete-with-tools
major_version: 1
Expand Down
7 changes: 6 additions & 1 deletion autoblocks/_impl/prompts/autogenerate.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
from autoblocks._impl.util import AutoblocksEnvVar
from autoblocks._impl.util import encode_uri_component

try:
from pydantic import AliasChoices
except ImportError: # pragma: no cover - older pydantic
AliasChoices = None # type: ignore


class Template(FrozenModel):
id: str
Expand Down Expand Up @@ -99,8 +104,8 @@ def generate_params_class_code(prompt: PromptCodegen) -> str:
if type_hint is None:
continue
snake_case_key = to_snake_case(key)
auto += f'{indent()}{snake_case_key}: {type_hint} = pydantic.Field(..., alias="{key}")\n'

auto += f'{indent()}{snake_case_key}: {type_hint} = pydantic.Field(..., alias="{key}")\n'
return auto


Expand Down
7 changes: 6 additions & 1 deletion autoblocks/_impl/prompts/v2/discovery/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
from autoblocks._impl.prompts.utils import infer_type
from autoblocks._impl.prompts.utils import to_snake_case
from autoblocks._impl.prompts.utils import to_title_case

try:
from pydantic import AliasChoices
except ImportError: # pragma: no cover - older pydantic
AliasChoices = None # type: ignore

from autoblocks._impl.prompts.v2.client import PromptsAPIClient
from autoblocks._impl.prompts.v2.discovery.managers import generate_execution_context_class_code
from autoblocks._impl.prompts.v2.discovery.managers import generate_factory_class_code
Expand Down Expand Up @@ -42,7 +48,6 @@ def generate_params_class_code(title_case_id: str, version: str, params: Dict[st
continue
snake_case_key = to_snake_case(key)
auto += f'{indent()}{snake_case_key}: {type_hint} = pydantic.Field(..., alias="{key}")\n'

return auto


Expand Down
16 changes: 8 additions & 8 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ cuid2 = ">=2.0.0"
opentelemetry-exporter-otlp-proto-http = ">=1.0.0"
opentelemetry-api = ">=1.0.0"
opentelemetry-sdk = ">=1.0.0"
pydantic = ">=2.11.7"

[tool.poetry.group.dev.dependencies]
pre-commit = "^4.0.0"
Expand Down
4 changes: 2 additions & 2 deletions tests/autoblocks/__snapshots__/test_prompts_autogenerate.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

class PromptAParams(FrozenModel):
frequency_penalty: Union[float, int] = pydantic.Field(..., alias="frequencyPenalty")
max_tokens: Union[float, int] = pydantic.Field(..., alias="maxTokens")
max_completion_tokens: Union[float, int] = pydantic.Field(..., alias="maxCompletionTokens")
model: str = pydantic.Field(..., alias="model")
presence_penalty: Union[float, int] = pydantic.Field(..., alias="presencePenalty")
temperature: Union[float, int] = pydantic.Field(..., alias="temperature")
Expand Down Expand Up @@ -89,7 +89,7 @@

class PromptBParams(FrozenModel):
frequency_penalty: Union[float, int] = pydantic.Field(..., alias="frequencyPenalty")
max_tokens: Union[float, int] = pydantic.Field(..., alias="maxTokens")
max_completion_tokens: Union[float, int] = pydantic.Field(..., alias="maxCompletionTokens")
model: str = pydantic.Field(..., alias="model")
presence_penalty: Union[float, int] = pydantic.Field(..., alias="presencePenalty")
temperature: Union[float, int] = pydantic.Field(..., alias="temperature")
Expand Down
13 changes: 9 additions & 4 deletions tests/autoblocks/discovery/test_prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
from autoblocks._impl.prompts.v2.discovery.prompts import generate_prompt_implementations
from autoblocks._impl.prompts.v2.discovery.prompts import generate_version_implementations

try:
from pydantic import AliasChoices
except ImportError: # pragma: no cover - older pydantic
AliasChoices = None # type: ignore


class TestPrompts:
def test_generate_params_class_code_empty(self):
Expand All @@ -26,7 +31,7 @@ def test_generate_params_class_code(self):
version = "1"
params = {
"temperature": 0.7,
"maxTokens": 100,
"maxCompletionTokens": 100,
"model": "gpt-4",
"isEnabled": True,
}
Expand All @@ -36,7 +41,7 @@ def test_generate_params_class_code(self):
# Check class definition and parameters
assert "class _TestPromptV1Params(FrozenModel):" in result
assert 'temperature: Union[float, int] = pydantic.Field(..., alias="temperature")' in result
assert 'max_tokens: Union[float, int] = pydantic.Field(..., alias="maxTokens")' in result
assert 'max_completion_tokens: Union[float, int] = pydantic.Field(..., alias="maxCompletionTokens")' in result
assert ' model: str = pydantic.Field(..., alias="model")' in result
assert ' is_enabled: bool = pydantic.Field(..., alias="isEnabled")' in result

Expand All @@ -47,7 +52,7 @@ def test_generate_params_class_code_nested(self):
params = {
"params": {
"temperature": 0.7,
"maxTokens": 100,
"maxCompletionTokens": 100,
}
}

Expand All @@ -56,7 +61,7 @@ def test_generate_params_class_code_nested(self):
# Check class definition and extracted parameters
assert "class _TestPromptV1Params(FrozenModel):" in result
assert 'temperature: Union[float, int] = pydantic.Field(..., alias="temperature")' in result
assert 'max_tokens: Union[float, int] = pydantic.Field(..., alias="maxTokens")' in result
assert 'max_completion_tokens: Union[float, int] = pydantic.Field(..., alias="maxCompletionTokens")' in result

@patch("autoblocks._impl.prompts.v2.discovery.prompts.generate_params_class_code")
@patch("autoblocks._impl.prompts.v2.discovery.prompts.generate_template_renderer_class_code")
Expand Down
4 changes: 2 additions & 2 deletions tests/autoblocks/test_prompts_autogenerate.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def test_write(httpx_mock, snapshot):
"params": {
"params": {
"frequencyPenalty": 0,
"maxTokens": 256,
"maxCompletionTokens": 256,
"model": "gpt-4",
"presencePenalty": 0.3,
"stopSequences": [],
Expand Down Expand Up @@ -209,7 +209,7 @@ def test_write(httpx_mock, snapshot):
"params": {
"params": {
"frequencyPenalty": 0,
"maxTokens": 256,
"maxCompletionTokens": 256,
"model": "gpt-4",
"presencePenalty": -0.3,
"stopSequences": [],
Expand Down
23 changes: 19 additions & 4 deletions tests/e2e/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@
from autoblocks.prompts.renderer import TemplateRenderer
from autoblocks.prompts.renderer import ToolRenderer

try:
from pydantic import AliasChoices
except ImportError: # pragma: no cover - older pydantic
AliasChoices = None # type: ignore


class QuestionAnswererParams(FrozenModel):
temperature: Union[float, int] = pydantic.Field(..., alias="temperature")
top_p: Union[float, int] = pydantic.Field(..., alias="topP")
frequency_penalty: Union[float, int] = pydantic.Field(..., alias="frequencyPenalty")
presence_penalty: Union[float, int] = pydantic.Field(..., alias="presencePenalty")
max_tokens: Union[float, int] = pydantic.Field(..., alias="maxTokens")
max_completion_tokens: Union[float, int] = pydantic.Field(
...,
alias="maxCompletionTokens",
)
model: str = pydantic.Field(..., alias="model")


Expand Down Expand Up @@ -93,7 +101,10 @@ class TextSummarizationParams(FrozenModel):
top_p: Union[float, int] = pydantic.Field(..., alias="topP")
frequency_penalty: Union[float, int] = pydantic.Field(..., alias="frequencyPenalty")
presence_penalty: Union[float, int] = pydantic.Field(..., alias="presencePenalty")
max_tokens: Union[float, int] = pydantic.Field(..., alias="maxTokens")
max_completion_tokens: Union[float, int] = pydantic.Field(
...,
alias="maxCompletionTokens",
)
model: str = pydantic.Field(..., alias="model")


Expand Down Expand Up @@ -178,7 +189,11 @@ class UsedByCiDontDeleteParams(FrozenModel):
top_p: Union[float, int] = pydantic.Field(..., alias="topP")
frequency_penalty: Union[float, int] = pydantic.Field(..., alias="frequencyPenalty")
presence_penalty: Union[float, int] = pydantic.Field(..., alias="presencePenalty")
max_tokens: Union[float, int] = pydantic.Field(..., alias="maxTokens")
max_completion_tokens: Union[float, int] = pydantic.Field(
...,
alias="maxCompletionTokens",
)

seed: Union[float, int] = pydantic.Field(..., alias="seed")
model: str = pydantic.Field(..., alias="model")
response_format: Dict[str, Any] = pydantic.Field(..., alias="responseFormat")
Expand Down Expand Up @@ -240,7 +255,7 @@ class UsedByCiDontDeletePromptManager(
AutoblocksPromptManager[UsedByCiDontDeleteExecutionContext],
):
__prompt_id__ = "used-by-ci-dont-delete"
__prompt_major_version__ = "6"
__prompt_major_version__ = "7"
__execution_context_class__ = UsedByCiDontDeleteExecutionContext


Expand Down
7 changes: 6 additions & 1 deletion tests/e2e/prompts_v2/apps/ci_app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ def prompt_basic_prompt_manager(
init_timeout: Optional[float] = None,
refresh_timeout: Optional[float] = None,
refresh_interval: Optional[float] = None,
) -> Union[prompts._PromptBasicV1PromptManager, prompts._PromptBasicV2PromptManager]:
) -> Union[
prompts._PromptBasicV1PromptManager,
prompts._PromptBasicV2PromptManager,
prompts._PromptBasicV3PromptManager,
prompts._PromptBasicV4PromptManager,
]:
return prompts.PromptBasicFactory.create(
major_version=major_version,
minor_version=minor_version,
Expand Down
97 changes: 93 additions & 4 deletions tests/e2e/prompts_v2/apps/ci_app/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,88 @@
from autoblocks.prompts.v2.renderer import ToolRenderer


class _PromptBasicV4Params(FrozenModel):
max_completion_tokens: Union[float, int] = pydantic.Field(
...,
alias="maxCompletionTokens",
)
model: str = pydantic.Field(..., alias="model")


class _PromptBasicV4TemplateRenderer(TemplateRenderer):
__name_mapper__ = {
"first_name": "first_name",
}

def template_c(
self,
*,
first_name: str,
) -> str:
return self._render(
"template-c",
first_name=first_name,
)


class _PromptBasicV4ToolRenderer(ToolRenderer):
__name_mapper__ = {}


class _PromptBasicV4ExecutionContext(
PromptExecutionContext[_PromptBasicV4Params, _PromptBasicV4TemplateRenderer, _PromptBasicV4ToolRenderer]
):
__params_class__ = _PromptBasicV4Params
__template_renderer_class__ = _PromptBasicV4TemplateRenderer
__tool_renderer_class__ = _PromptBasicV4ToolRenderer


class _PromptBasicV4PromptManager(AutoblocksPromptManager[_PromptBasicV4ExecutionContext]):
__app_id__ = "b5sz3k5d61w9f8325fhxuxkr"
__prompt_id__ = "prompt-basic"
__prompt_major_version__ = "4"
__execution_context_class__ = _PromptBasicV4ExecutionContext


class _PromptBasicV3Params(FrozenModel):
max_completion_tokens: Union[float, int] = pydantic.Field(
...,
alias="maxCompletionTokens",
)
model: str = pydantic.Field(..., alias="model")


class _PromptBasicV3TemplateRenderer(TemplateRenderer):
__name_mapper__ = {}

def template_c(
self,
) -> str:
return self._render(
"template-c",
)


class _PromptBasicV3ToolRenderer(ToolRenderer):
__name_mapper__ = {}


class _PromptBasicV3ExecutionContext(
PromptExecutionContext[_PromptBasicV3Params, _PromptBasicV3TemplateRenderer, _PromptBasicV3ToolRenderer]
):
__params_class__ = _PromptBasicV3Params
__template_renderer_class__ = _PromptBasicV3TemplateRenderer
__tool_renderer_class__ = _PromptBasicV3ToolRenderer


class _PromptBasicV3PromptManager(AutoblocksPromptManager[_PromptBasicV3ExecutionContext]):
__app_id__ = "b5sz3k5d61w9f8325fhxuxkr"
__prompt_id__ = "prompt-basic"
__prompt_major_version__ = "3"
__execution_context_class__ = _PromptBasicV3ExecutionContext


class _PromptBasicV2Params(FrozenModel):
max_tokens: Union[float, int] = pydantic.Field(..., alias="maxTokens")
model: str = pydantic.Field(..., alias="model")


Expand Down Expand Up @@ -103,7 +183,12 @@ def create(
init_timeout: Optional[float] = None,
refresh_timeout: Optional[float] = None,
refresh_interval: Optional[float] = None,
) -> Union[_PromptBasicV1PromptManager, _PromptBasicV2PromptManager]:
) -> Union[
_PromptBasicV1PromptManager,
_PromptBasicV2PromptManager,
_PromptBasicV3PromptManager,
_PromptBasicV4PromptManager,
]:
kwargs: Dict[str, Any] = {}
if api_key is not None:
kwargs["api_key"] = api_key
Expand All @@ -115,11 +200,15 @@ def create(
kwargs["refresh_interval"] = refresh_interval

if major_version is None:
major_version = "2" # Latest version
major_version = "4" # Latest version

if major_version == "1":
return _PromptBasicV1PromptManager(minor_version=minor_version, **kwargs)
if major_version == "2":
return _PromptBasicV2PromptManager(minor_version=minor_version, **kwargs)
if major_version == "3":
return _PromptBasicV3PromptManager(minor_version=minor_version, **kwargs)
if major_version == "4":
return _PromptBasicV4PromptManager(minor_version=minor_version, **kwargs)

raise ValueError("Unsupported major version. Available versions: 1, 2")
raise ValueError("Unsupported major version. Available versions: 1, 2, 3, 4")
Loading