func
¶
ASSISTANT_ROLE
module-attribute
¶
ASSISTANT_ROLE = MessageRole(ASSISTANT)
The assistant role with name not specified.
MaybeOneOrMany
module-attribute
¶
A type that can be either a single item, a sequence of items, or None.
OneOrMany
module-attribute
¶
A type that can be either a single item or a sequence of items.
SYSTEM_ROLE
module-attribute
¶
SYSTEM_ROLE = MessageRole(SYSTEM)
The system role with name not specified.
StrOrImg
module-attribute
¶
A type that can be either a string or an image.
String
module-attribute
¶
String = Union[StringFuture, str]
String is a type alias for StringFuture or str.
AIMessage
¶
AIMessage(
content: Any = None,
*,
role: Optional[MessageRole] = None,
tool_calls: Optional[List[ToolCall]] = None,
**kwargs: Any
)
Bases: BaseMessage
An assistant message in the conversation.
Source code in src/appl/core/message.py
get_content
¶
get_content(as_str: bool = False) -> Any
Get the content of the message.
Materialize the content if it is a FutureValue.
Source code in src/appl/core/message.py
get_dict
¶
get_dict(
default_role: Optional[MessageRole] = None,
) -> Dict[str, Any]
Return a dict representation of the message.
Source code in src/appl/core/message.py
merge
¶
Merge the message with another message.
Source code in src/appl/core/message.py
should_merge
¶
should_merge(other: 'BaseMessage') -> bool
Whether the message should be merged with the other message.
Source code in src/appl/core/message.py
str_with_default_role
¶
str_with_default_role(
default_role: Optional[MessageRole] = None,
) -> str
Return the string representation of the message with default role.
validate_role
¶
validate_role(target_role: MessageRole) -> None
Validate the role of the message, fill the role if not provided.
Source code in src/appl/core/message.py
BaseMessage
¶
Bases: BaseModel
, ABC
The base class for messages.
Provides a more flexible way to create a message.
Source code in src/appl/core/message.py
get_content
¶
get_content(as_str: bool = False) -> Any
Get the content of the message.
Materialize the content if it is a FutureValue.
Source code in src/appl/core/message.py
get_dict
¶
get_dict(
default_role: Optional[MessageRole] = None,
) -> Dict[str, Any]
Return a dict representation of the message.
Source code in src/appl/core/message.py
merge
¶
Merge the message with another message.
Source code in src/appl/core/message.py
should_merge
¶
should_merge(other: 'BaseMessage') -> bool
Whether the message should be merged with the other message.
Source code in src/appl/core/message.py
str_with_default_role
¶
str_with_default_role(
default_role: Optional[MessageRole] = None,
) -> str
Return the string representation of the message with default role.
validate_role
¶
validate_role(target_role: MessageRole) -> None
Validate the role of the message, fill the role if not provided.
Source code in src/appl/core/message.py
BaseServer
¶
Bases: ABC
The base class for all servers.
Servers are responsible for communicating with the underlying model.
close
abstractmethod
¶
create
¶
create(
args: GenArgs, gen_id: str, **kwargs: Any
) -> CompletionResponse
Create a CompletionResponse from the model with given arguments.
Parameters:
-
args
(GenArgs
) –The arguments for generating the response
-
gen_id
(str
) –The ID of the generation
-
**kwargs
(Any
, default:{}
) –Additional keyword arguments
Returns: The response from the model.
Source code in src/appl/core/server.py
BracketedDefinition
¶
BracketedDefinition(
name: Optional[String] = None,
desc: Optional[String] = None,
*,
sep: String = ": ",
details: Any = None,
fstr: Optional[str] = None,
var_name: Optional[str] = None
)
Bases: Definition
A Definition that is formatted with square brackets.
Parameters:
-
name
(Optional[String]
, default:None
) –The name of the definition.
-
desc
(Optional[String]
, default:None
) –A description of the definition.
-
sep
(String
, default:': '
) –The separator between the name and description.
-
details
(Any
, default:None
) –Additional details about the definition.
-
fstr
(Optional[str]
, default:None
) –The format string for the definition.
-
var_name
(Optional[str]
, default:None
) –The name of the variable that the definition is stored in.
Source code in src/appl/core/promptable/definition.py
CallFuture
¶
CallFuture(
func: Callable,
*args: Any,
use_process: bool = False,
lazy_eval: bool = False,
**kwargs: Any
)
Bases: FutureValue
Represent a function call that may not be ready yet.
Parameters:
-
func
(Callable
) –The function to call.
-
*args
(Any
, default:()
) –The arguments of the function.
-
use_process
(bool
, default:False
) –Whether to use a process pool executor.
-
lazy_eval
(bool
, default:False
) –Whether to delay the start of the call until needed.
-
**kwargs
(Any
, default:{}
) –The keyword arguments of the function.
Source code in src/appl/core/types/futures.py
CmpStringFuture
¶
CmpStringFuture(
a: StringFuture,
b: StringFuture,
op: Callable[[str, str], bool],
)
Bases: FutureValue
Represent a comparison between a StringFuture and another value.
Source code in src/appl/core/types/futures.py
CompletionResponse
¶
Bases: BaseModel
A class wrapping the response from the LLM model.
For a streaming response, it tracks the chunks of the response and builds the complete response when the streaming is finished.
chunks
class-attribute
instance-attribute
¶
chunks: List[Union[ModelResponse, ChatCompletionChunk]] = (
Field(
[],
description="The chunks of the response when streaming",
)
)
The chunks of the response when streaming.
complete_response
property
¶
The complete response from the model. This will block until the response is finished.
cost
class-attribute
instance-attribute
¶
cost: Optional[float] = Field(
None, description="The cost of the completion"
)
The cost of the completion.
is_finished
class-attribute
instance-attribute
¶
is_finished: bool = Field(
False,
description="Whether the response stream is finished",
)
Whether the response stream is finished.
is_stream
class-attribute
instance-attribute
¶
is_stream: bool = Field(
False, description="Whether the response is a stream"
)
Whether the response is a stream.
message
class-attribute
instance-attribute
¶
message: Optional[str] = Field(
None,
description="The top-choice message from the completion",
)
The top-choice message from the completion.
post_finish_callbacks
class-attribute
instance-attribute
¶
The post finish callbacks.
raw_response
class-attribute
instance-attribute
¶
The raw response from the model.
response_model
class-attribute
instance-attribute
¶
response_model: Any = Field(
None,
description="The BaseModel's subclass specifying the response format.",
)
The BaseModel's subclass specifying the response format.
response_obj
class-attribute
instance-attribute
¶
response_obj: Any = Field(
None,
description="The response object of response model, could be a stream",
)
The response object of response model, could be a stream.
results
property
¶
tool_calls
class-attribute
instance-attribute
¶
tool_calls: List[ToolCall] = Field(
[], description="The tool calls"
)
The tool calls.
usage
class-attribute
instance-attribute
¶
The usage of the completion.
format_stream
¶
Format the stream response as a text generator.
Source code in src/appl/core/response.py
register_post_finish_callback
¶
Register a post finish callback.
The callback will be called after the response is finished.
Source code in src/appl/core/response.py
set_response_obj
¶
streaming
¶
streaming(
display: bool = True, title: str = "APPL Streaming"
) -> CompletionResponse
Stream the response object and finish the response.
Source code in src/appl/core/response.py
ContentList
¶
Bases: BaseModel
Represent a list of contents containing text and images.
append
¶
append(content: StrOrImg) -> None
Append a content to the list.
If the last content is a string, it will be concatenated with the new content.
Source code in src/appl/core/types/content.py
extend
¶
get_contents
¶
get_contents() -> List[Dict[str, Any]]
Return the contents as a list of dictionaries.
Source code in src/appl/core/types/content.py
Definition
¶
Definition(
name: Optional[String] = None,
desc: Optional[String] = None,
*,
sep: String = ": ",
details: Any = None,
fstr: Optional[str] = None,
var_name: Optional[str] = None
)
Bases: Promptable
, Formattable
Represent a definition of a concept.
Attributes:
-
fstr
(str
) –The format string for the definition.
-
name
(Optional[String]
) –The name of the definition.
-
desc
(String
) –A description of the definition.
-
_forks
(List[Definition]
) –A list of all instances of this class.
Parameters:
-
name
(Optional[String]
, default:None
) –The name of the definition.
-
desc
(Optional[String]
, default:None
) –A description of the definition.
-
sep
(String
, default:': '
) –The separator between the name and description.
-
details
(Any
, default:None
) –Additional details about the definition.
-
fstr
(Optional[str]
, default:None
) –The format string for the definition.
-
var_name
(Optional[str]
, default:None
) –The name of the variable that the definition is stored in.
Source code in src/appl/core/promptable/definition.py
FutureValue
¶
Bases: ABC
Represents a value that may not be ready yet.
Image
¶
Bases: BaseModel
Represent an image in the message.
See the guide for more information about the detail level.
Source code in src/appl/core/types/content.py
from_file
classmethod
¶
Construct an image prompt from an image file.
from_image
classmethod
¶
Construct an image prompt from a PIL ImageFile.
Source code in src/appl/core/types/content.py
Indexing
¶
Indexing(
method: Optional[str] = None,
ind: int = 0,
prefix: str = "",
suffix: Optional[str] = None,
)
The indexing method for the printer.
Source code in src/appl/core/printer.py
get_index
¶
Get the index string for the current or given index.
Source code in src/appl/core/printer.py
MessageRole
¶
PromptPrinter
¶
PromptPrinter(
states: Optional[List[PrinterState]] = None,
is_newline: bool = True,
)
A class to print prompt records as conversation.
The printer maintains a stack of printer states about the current role, separator, indexing, and indentation.
Source code in src/appl/core/printer.py
pop
¶
push
¶
push(data: PrinterPush) -> None
Promptable
¶
Bases: ABC
Interface for objects that can be converted to a prompt string.
ResponseType
¶
The type of generation response.
UNFINISHED
class-attribute
instance-attribute
¶
The response is not finished.
StringFuture
¶
StringFuture(content: Any = '', set_value: bool = False)
Bases: FutureValue
, BaseModel
StringFuture is a string that may not be ready yet.
Source code in src/appl/core/types/futures.py
from_list
classmethod
¶
from_list(content: List[Any]) -> StringFuture
join
¶
join(iterable: Iterable[StringFuture]) -> StringFuture
Concatenate any number of strings.
The StringFuture whose method is called is inserted in between each given StringFuture. The result is returned as a new StringFuture.
Source code in src/appl/core/types/futures.py
materialized
¶
materialized() -> StringFuture
SystemMessage
¶
SystemMessage(
content: Any = None,
*,
role: Optional[MessageRole] = None,
**kwargs: Any
)
Bases: BaseMessage
A system message in the conversation.
Source code in src/appl/core/message.py
get_content
¶
get_content(as_str: bool = False) -> Any
Get the content of the message.
Materialize the content if it is a FutureValue.
Source code in src/appl/core/message.py
get_dict
¶
get_dict(
default_role: Optional[MessageRole] = None,
) -> Dict[str, Any]
Return a dict representation of the message.
Source code in src/appl/core/message.py
merge
¶
Merge the message with another message.
Source code in src/appl/core/message.py
should_merge
¶
should_merge(other: 'BaseMessage') -> bool
Whether the message should be merged with the other message.
Source code in src/appl/core/message.py
str_with_default_role
¶
str_with_default_role(
default_role: Optional[MessageRole] = None,
) -> str
Return the string representation of the message with default role.
validate_role
¶
validate_role(target_role: MessageRole) -> None
Validate the role of the message, fill the role if not provided.
Source code in src/appl/core/message.py
ToolCall
¶
Bases: BaseModel
The class representing a tool call.
args
class-attribute
instance-attribute
¶
args: str = Field(
...,
description="The arguments to call the function with.",
)
The arguments to call the function with.
id
class-attribute
instance-attribute
¶
id: str = Field(..., description="The ID of the tool call.")
The ID of the tool call.
name
class-attribute
instance-attribute
¶
name: str = Field(
..., description="The name of the function to call."
)
The name of the function to call.
from_dict
classmethod
¶
from_dict(call: Dict) -> ToolCall
Create a ToolCall from a dictionary in the OpenAI format.
Source code in src/appl/core/tool.py
from_openai_tool_call
classmethod
¶
from_openai_tool_call(
call: ChatCompletionMessageToolCall,
) -> ToolCall
Create a ToolCall from an OpenAI tool call.
get_dict
¶
Get the OpenAI format dictionary representation of the tool call.
ToolMessage
¶
ToolMessage(
content: Any = None,
*,
role: Optional[MessageRole] = None,
tool_call_id: str = "",
**kwargs: Any
)
Bases: BaseMessage
A tool message in the conversation.
Source code in src/appl/core/message.py
get_content
¶
get_content(as_str: bool = False) -> Any
Get the content of the message.
Materialize the content if it is a FutureValue.
Source code in src/appl/core/message.py
get_dict
¶
get_dict(*args: Any, **kwargs: Any) -> Dict[str, Any]
Return a dict representation of the message.
merge
¶
Merge the message with another message.
Source code in src/appl/core/message.py
should_merge
¶
should_merge(other: 'BaseMessage') -> bool
Whether the message should be merged with the other message.
Source code in src/appl/core/message.py
str_with_default_role
¶
str_with_default_role(
default_role: Optional[MessageRole] = None,
) -> str
Return the string representation of the message with default role.
validate_role
¶
validate_role(target_role: MessageRole) -> None
Validate the role of the message, fill the role if not provided.
Source code in src/appl/core/message.py
UserMessage
¶
UserMessage(
content: Any = None,
*,
role: Optional[MessageRole] = None,
**kwargs: Any
)
Bases: BaseMessage
A user message in the conversation.
Source code in src/appl/core/message.py
get_content
¶
get_content(as_str: bool = False) -> Any
Get the content of the message.
Materialize the content if it is a FutureValue.
Source code in src/appl/core/message.py
get_dict
¶
get_dict(
default_role: Optional[MessageRole] = None,
) -> Dict[str, Any]
Return a dict representation of the message.
Source code in src/appl/core/message.py
merge
¶
Merge the message with another message.
Source code in src/appl/core/message.py
should_merge
¶
should_merge(other: 'BaseMessage') -> bool
Whether the message should be merged with the other message.
Source code in src/appl/core/message.py
str_with_default_role
¶
str_with_default_role(
default_role: Optional[MessageRole] = None,
) -> str
Return the string representation of the message with default role.
validate_role
¶
validate_role(target_role: MessageRole) -> None
Validate the role of the message, fill the role if not provided.
Source code in src/appl/core/message.py
as_func
¶
as_func(
func: Callable[P, T],
_globals: Optional[Dict] = None,
_locals: Optional[Dict] = None,
) -> Callable[P, T]
Fill the globals and locals for a ppl function.
When locals not provided, it will use the locals from the caller.
Source code in src/appl/func.py
as_tool
¶
as_tool(func: Callable, **kwargs: Any) -> Tool
Wrap a given function with additional predefined arguments into a Tool.
This function allows converting a standard function into a 'Tool' by specifying the function and any additional arguments that should be pre-defined for it. These additional arguments are passed as keyword arguments and will be bound to the function within the Tool object, so that these arguments are not required when using this tool.
Parameters:
-
func
(Callable
) –The function to be converted into a Tool.
-
**kwargs
(Any
, default:{}
) –Keyword arguments that will be predefined for the function in the Tool object.
Returns:
-
Tool
(Tool
) –An object encapsulating the given function and its predefined arguments, ready to be utilized as a Tool.
Examples:
Given a function move_disk
that requires an environment and two
pegs to move a disk from one peg to another in the Tower of Hanoi
puzzle, one can create a tool with a predefined environment by:
def move_disk(env: HanoiEnv, from_peg: int, to_peg: int) -> str:
pass
env = HanoiEnv()
tools = [as_tool(move_disk, env=env)]
In this example, move_disk
is encapsulated into a Tool with env
predefined, so only from_peg
and to_peg
are required.
Source code in src/appl/func.py
as_tool_choice
¶
Build a tool choice argument for the OpenAI API from an object.
Source code in src/appl/func.py
auto_prime_gen
¶
Decorate a generator to automatically prime the generator.
build_tools
¶
Build a list of tools from the given tools or functions.
Source code in src/appl/func.py
call
¶
call(
func: Callable,
*args: Any,
use_process: bool = False,
**kwargs: Any
) -> CallFuture
Create a CallFuture object from a function and its arguments.
The CallFuture object will call the function in a separate thread or process, therefore the function need to be thread-safe or process-safe.
Source code in src/appl/func.py
convo
¶
convo(_ctx: Optional[PromptContext] = None) -> Conversation
Return the full conversation in the context.
Similar to globals() in Python in some sense.
Source code in src/appl/func.py
empty_line
¶
empty_line(num_lines: int = 1) -> PromptRecords
Create empty lines regardless of other compositor.
Source code in src/appl/func.py
gen
¶
gen(
server: Optional[str] = None,
*,
max_tokens: Optional[int] = None,
stop: MaybeOneOrMany[str] = None,
temperature: Optional[float] = None,
top_p: Optional[float] = None,
n: Optional[int] = None,
tools: OneOrMany[Union[BaseTool, Callable]] = [],
tool_format: str = "auto",
stream: Optional[bool] = None,
response_format: Optional[Union[dict, Type[M]]] = None,
response_model: Optional[Type[M]] = None,
mock_response: Optional[
Union[CompletionResponse, str]
] = None,
_ctx: Optional[PromptContext] = None,
**kwargs: Any
) -> Generation[M]
Send a generation request to the LLM backend.
Parameters:
-
server
(str
, default:None
) –name of the backend server. Defaults to the default server set in the configs.
-
max_tokens
(int
, default:None
) –maximum number of tokens to generate. Defaults to None.
-
stop
(str | Sequence[str]
, default:None
) –stop sequence(s). Defaults to None.
-
temperature
(float
, default:None
) –temperature for sampling. Defaults to None.
-
top_p
(float
, default:None
) –nucleus sampling parameter. Defaults to None.
-
n
(int
, default:None
) –number of choices to generate. Defaults to 1.
-
tools
(BaseTool | Callable | Sequence[BaseTool | Callable]
, default:[]
) –tools can be used. Defaults to None.
-
tool_format
(str
, default:'auto'
) –the format for the tools. Defaults to "auto".
-
stream
(bool
, default:None
) –whether to stream the results. Defaults to False.
-
response_format
(Union[dict, Type[M]]
, default:None
) –OpenAI's argument specifies the response format. Defaults to None.
-
response_model
(Type[M]
, default:None
) –instructor's argument specifies the response format as a Pydantic model. Recommended to use
response_format
instead. Defaults to None. -
mock_response
(Union[CompletionResponse, str]
, default:None
) –mock response for testing. Defaults to None.
-
_ctx
(PromptContext
, default:None
) –prompt context, will be automatically filled.
-
kwargs
(Any
, default:{}
) –extra arguments for the generation.
Returns:
-
Generation
(Generation[M]
) –a future object representing the generation result
Source code in src/appl/func.py
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 |
|
get_var
¶
get_var(name: str, _ctx: PromptContext) -> Any
grow
¶
grow(
content: Any, *, _ctx: Optional[PromptContext] = None
) -> None
Append the content to the prompt.
Source code in src/appl/func.py
need_ctx
¶
partial
¶
Create a new function with partial application of the given arguments and keywords.
Source code in src/appl/func.py
ppl
¶
ppl(
ctx: Union[str, F] = "new",
comp: Optional[Compositor] = None,
*,
default_return: Optional[Literal[prompt]] = None,
include_docstring: bool = False,
auto_prime: bool = False,
num_extra_wrappers: int = 0,
new_ctx_func: Callable = PromptContext
) -> Union[Callable[[F], F], F]
Decorate a function to mark it as an APPL function.
The function contains a prompt context, which could be same as or copied from its caller function, or created from scratch, or resumed from the last run.
Parameters:
-
ctx
(str
, default:'new'
) –the method to deal with the child context, available methods includes:
- (default) "new" or "new_ctx": create a brand new context.
- "copy" or "copy_ctx": copy from the parent's context, the change will not affect the parent's context.
- "same" or "same_ctx": use the same context as the parent's, the change will affect the parent's context.
- "resume" or "resume_ctx": resume its own context from the last run. For the first run, it will use the parent's context.
-
comp
(Compositor
, default:None
) –the default compositor to be used. Defaults to None.
-
default_return
(str
, default:None
) –The default return value, "prompt" means return the prompt within the function. Defaults to None.
-
include_docstring
(bool
, default:False
) –set to True to include the triple-quoted docstring in the prompt. Defaults to False.
-
auto_prime
(bool
, default:False
) –set to True to automatically prime the generator. Defaults to False.
-
num_extra_wrappers
(int
, default:0
) –the number of extra wrappers to go back to the caller frame.
-
new_ctx_func
(Callable
, default:PromptContext
) –the function to create a new context. Defaults to PromptContext.
Source code in src/appl/func.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
|
records
¶
records(
_ctx: Optional[PromptContext] = None,
) -> PromptRecords
Return the prompt defined in the current function.
Similar to locals() in Python in some sense.
Source code in src/appl/func.py
reset_context
¶
Reset the context for APPL functions with the 'resume' context method.
Source code in src/appl/func.py
str_future
¶
str_future(obj: Any) -> StringFuture
wraps
¶
Replace the functools.wraps to take care of the type hint.