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 pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "uv_build"
[project]
name = "draive"
description = "Framework designed to simplify and accelerate the development of LLM-based applications."
version = "0.79.1"
version = "0.79.2"
readme = "README.md"
maintainers = [
{ name = "Kacper Kaliński", email = "kacper.kalinski@miquido.com" },
Expand Down
4 changes: 2 additions & 2 deletions src/draive/instructions/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(
self._storage: Mapping[str, str] | None = None
self._listing: Sequence[InstructionDeclaration] | None = None

async def listing(
async def fetch_list(
self,
**extra: Any,
) -> Sequence[InstructionDeclaration]:
Expand All @@ -60,7 +60,7 @@ async def listing(

return self._listing

async def instruction(
async def fetch_instruction(
self,
name: str,
*,
Expand Down
28 changes: 12 additions & 16 deletions src/draive/instructions/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
__all__ = ("Instructions",)


async def _empty(
async def _empty_list(
**extra: Any,
) -> Sequence[InstructionDeclaration]:
return ()


async def _none(
async def _no_instruction(
name: str,
/,
*,
Expand All @@ -45,8 +45,8 @@ def of(
storage = InstructionsVolatileStorage((instruction, *instructions))

return cls(
list_fetching=storage.listing,
fetching=storage.instruction,
list_fetching=storage.fetch_list,
fetching=storage.fetch_instruction,
)

@classmethod
Expand All @@ -57,8 +57,8 @@ def file(
storage = InstructionsFileStorage(path=path)

return cls(
list_fetching=storage.listing,
fetching=storage.instruction,
list_fetching=storage.fetch_list,
fetching=storage.fetch_instruction,
)

@classmethod
Expand Down Expand Up @@ -126,26 +126,22 @@ async def fetch(
elif required and default is None:
raise InstructionMissing(f"Missing instruction: '{name}'")

elif default is None:
return None

elif isinstance(default, str):
return Instruction.of(
default,
name=name,
description=instruction.description
if isinstance(instruction, InstructionDeclaration)
else None,
meta=None,
arguments=arguments,
)

elif default is not None:
else:
return Instruction.of(
default,
arguments=arguments,
)

else:
return None

list_fetching: InstructionListFetching = _empty
fetching: InstructionFetching = _none
list_fetching: InstructionListFetching = _empty_list
fetching: InstructionFetching = _no_instruction
meta: Meta = META_EMPTY
6 changes: 3 additions & 3 deletions src/draive/instructions/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ class InstructionTemplate[**Args](ParametrizedFunction[Args, Coroutine[None, Non
def __init__(
self,
/,
name: str,
function: Callable[Args, Coroutine[None, None, str]],
*,
name: str,
description: str | None,
meta: Meta,
function: Callable[Args, Coroutine[None, None, str]],
) -> None:
super().__init__(function)

Expand Down Expand Up @@ -104,10 +104,10 @@ def wrap[**Arg](
function: Callable[Arg, Coroutine[None, None, str]],
) -> InstructionTemplate[Arg]:
return InstructionTemplate[Arg](
function=function,
name=name or function.__name__,
description=description,
meta=Meta.of(meta),
function=function,
)

if function := function:
Expand Down
62 changes: 28 additions & 34 deletions src/draive/instructions/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ def of(
*,
name: str | None = None,
description: str | None = None,
meta: Meta | MetaValues | None = None,
arguments: Mapping[str, str | float | int] | None = None,
meta: Meta | MetaValues | None = None,
) -> Self: ...

@overload
Expand All @@ -105,8 +105,8 @@ def of(
*,
name: str | None = None,
description: str | None = None,
meta: Meta | MetaValues | None = None,
arguments: Mapping[str, str | float | int] | None = None,
meta: Meta | MetaValues | None = None,
) -> Self | None: ...

@classmethod
Expand All @@ -117,37 +117,34 @@ def of(
*,
name: str | None = None,
description: str | None = None,
meta: Meta | MetaValues | None = None,
arguments: Mapping[str, str | float | int] | None = None,
meta: Meta | MetaValues | None = None,
) -> Self | None:
match instruction:
case None:
return None

case str() as content:
return cls(
name=name or uuid4().hex,
description=description,
content=content,
arguments=arguments if arguments is not None else {},
meta=Meta.of(meta),
)
if instruction is None:
return None

elif isinstance(instruction, str):
return cls(
name=name or uuid4().hex,
description=description,
content=instruction,
arguments=arguments if arguments is not None else {},
meta=Meta.of(meta),
)

case instruction:
if name is None and description is None and meta is None and arguments is None:
return instruction # nothing to update

return instruction.updated(
name=name or instruction.name,
description=description or instruction.description,
content=instruction.content,
arguments={**instruction.arguments, **arguments}
if arguments is not None
else instruction.arguments,
meta=instruction.meta.merged_with(meta)
if meta is not None
else instruction.meta,
)
else:
if name is None and description is None and meta is None and arguments is None:
return instruction # nothing to update

return instruction.updated(
name=name or instruction.name,
description=description or instruction.description,
content=instruction.content,
arguments={**instruction.arguments, **arguments}
if arguments is not None
else instruction.arguments,
meta=instruction.meta.merged_with(meta) if meta is not None else instruction.meta,
)

name: str
description: str | None = None
Expand Down Expand Up @@ -176,11 +173,8 @@ def format(
},
)

elif self.arguments:
return self.content.format_map(self.arguments)

else:
return self.content
return self.content.format_map(self.arguments)

def extended(
self,
Expand Down
9 changes: 7 additions & 2 deletions src/draive/instructions/volatile.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

@final
class InstructionsVolatileStorage:
__slots__ = (
"_listing",
"_storage",
)

def __init__(
self,
instructions: Sequence[Instruction],
Expand All @@ -23,13 +28,13 @@ def __init__(
instruction.declaration for instruction in instructions
]

async def listing(
async def fetch_list(
self,
**extra: Any,
) -> Sequence[InstructionDeclaration]:
return self._listing

async def instruction(
async def fetch_instruction(
self,
name: str,
*,
Expand Down
Loading