role_changer
¶
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
AIRole
¶
AIRole(name: Optional[str] = None, **kwargs: Any)
Bases: RoleChanger
Change the role of the prompts to assistant.
Parameters:
-
name
(Optional[str]
, default:None
) –The name of the assistant role. Defaults to None.
-
**kwargs
(Any
, default:{}
) –The keyword arguments to pass to the Role
Source code in src/appl/role_changer.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
BaseTool
¶
Bases: BaseModel
, ABC
The base class for a Tool.
Source code in src/appl/core/tool.py
examples
class-attribute
instance-attribute
¶
examples: List[str] = Field(
[], description="The examples of the Tool"
)
The examples of the Tool.
info
class-attribute
instance-attribute
¶
Additional information of the Tool.
long_desc
class-attribute
instance-attribute
¶
long_desc: str = Field(
"", description="The long description of the Tool"
)
The long description of the Tool.
name
class-attribute
instance-attribute
¶
name: str = Field(..., description='The name of the Tool')
The name of the Tool.
params
class-attribute
instance-attribute
¶
params: type[BaseModel] = Field(
..., description="The parameters of the Tool"
)
The parameters of the Tool.
raises
class-attribute
instance-attribute
¶
raises: List[Dict[str, Optional[str]]] = Field(
[], description="The exceptions raised by the Tool"
)
The exceptions raised by the Tool.
returns
class-attribute
instance-attribute
¶
returns: type[BaseModel] = Field(
..., description="The return of the Tool"
)
The return of the Tool.
short_desc
class-attribute
instance-attribute
¶
short_desc: str = Field(
"", description="The short description of the Tool"
)
The short description of the Tool.
parse_data
classmethod
¶
parse_data(
sig: Signature,
docstring: Optional[str],
predefined: Dict[str, Any],
) -> Dict[str, Any]
Parse data from the signature and docstring of a function.
Source code in src/appl/core/tool.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
Compositor
¶
Compositor(
sep: Optional[str] = None,
indexing: Union[Indexing, Optional[str]] = None,
indent: Optional[Union[str, int]] = None,
new_indent: Optional[Union[str, int]] = None,
is_inline: Optional[bool] = None,
role: Optional[MessageRole] = None,
_ctx: Optional[PromptContext] = None,
)
Bases: PrinterModifier
The contextual compositor of the prompts.
This class represents a contextual compositor that modifies the behavior of the printer. It provides various options for customizing the output format of the prompts within this context manager.
Attributes:
-
_sep
(Optional[str]
) –The class default separator string is None, indicating inherit from the parent.
-
_indexing
(Indexing
) –The class default indexing mode is empty indexing.
-
_inc_indent
(str
) –The class default indentation string is empty string.
-
_new_indent
(Optional[str]
) –The class default new indentation string is None, indicating not overwrite the indent.
-
_is_inline
(bool
) –The class default inline flag is False.
-
_new_role
(Optional[MessageRole]
) –The class default role of the modifier is None, indicating not overwrite the role.
Parameters:
-
sep
(Optional[str]
, default:None
) –The separator string. Defaults to use the class default.
-
indexing
(Union[Indexing, Optional[str]]
, default:None
) –The indexing mode. Defaults to use the class default.
-
indent
(Optional[Union[str, int]]
, default:None
) –The indentation string. Defaults to use the class default.
-
new_indent
(Optional[Union[str, int]]
, default:None
) –The new indentation string. Defaults to use the class default.
-
is_inline
(Optional[bool]
, default:None
) –Flag indicating if the modifier is inline. Defaults to use the class default.
-
role
(Optional[MessageRole]
, default:None
) –The role of the modifier. Defaults to use the class default.
-
_ctx
(Optional[PromptContext]
, default:None
) –The prompt context filled automatically by the APPL function.
Source code in src/appl/core/modifiers.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
Conversation
¶
Bases: BaseModel
A conversation containing messages.
append
¶
Append a message to the conversation.
Source code in src/appl/core/message.py
as_list
¶
as_list(
default_role: Optional[MessageRole] = USER_ROLE,
) -> List[Dict[str, str]]
Return a list of dict representation of the conversation.
Source code in src/appl/core/message.py
collapse
¶
Collapse the messages in the conversation.
Source code in src/appl/core/message.py
extend
¶
Extend the conversation with another conversation.
make_copy
¶
materialize
¶
set_system_messages
¶
set_system_messages(messages: List[SystemMessage]) -> None
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.
GenArgs
¶
Bases: BaseModel
Common arguments for generating a response from a model.
preprocess
¶
Convert the GenArgs into a dictionary for creating the response.
Source code in src/appl/core/server.py
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
¶
PromptFunc
¶
PromptFunc(
func: Callable,
ctx_method: str = "new",
comp: Optional[Compositor] = None,
default_return: Optional[Literal["prompt"]] = None,
include_docstring: bool = False,
new_ctx_func: Callable = PromptContext,
)
A wrapper for an APPL function, can be called as a normal 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:
-
func
(Callable
) –the function being wrapped
-
ctx_method
(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 copy 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.
-
new_ctx_func
(Callable
, default:PromptContext
) –the function to create a new context. Defaults to PromptContext.
Source code in src/appl/core/function.py
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
PromptRecords
¶
A class represents a list of prompt records.
Source code in src/appl/core/printer.py
as_convo
¶
as_convo() -> Conversation
copy
¶
extend
¶
record
¶
record(record: Union[str, RecordType]) -> None
Record a string, message, image, printer push or printer pop.
Source code in src/appl/core/printer.py
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.
RoleChanger
¶
RoleChanger(
role: Optional[MessageRole] = None,
_ctx: Optional[PromptContext] = None,
)
Bases: PrinterModifier
The contextual role changer of the prompts.
Parameters:
-
role
(Optional[MessageRole]
, default:None
) –The new role of the prompts. Defaults to None.
-
_ctx
(Optional[PromptContext]
, default:None
) –The prompt context filled automatically by the APPL function.
Source code in src/appl/role_changer.py
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
SystemRole
¶
SystemRole(name: Optional[str] = None, **kwargs: Any)
Bases: RoleChanger
Change the role of the prompts to system.
Parameters:
-
name
(Optional[str]
, default:None
) –The name of the system role. Defaults to None.
-
**kwargs
(Any
, default:{}
) –The keyword arguments to pass to the RoleChanger constructor.
Source code in src/appl/role_changer.py
Tool
¶
Tool(
func: Callable,
use_short_desc: bool = False,
**kwargs: Any
)
Bases: BaseTool
The Tool class that can be called by LLMs.
Parameters:
-
func
(Callable
) –The function to create the tool from.
-
use_short_desc
(bool
, default:False
) –Whether to use the short description instead of the full description.
-
kwargs
(Any
, default:{}
) –Additional arguments for the tool.
Source code in src/appl/core/tool.py
examples
class-attribute
instance-attribute
¶
examples: List[str] = Field(
[], description="The examples of the Tool"
)
The examples of the Tool.
info
class-attribute
instance-attribute
¶
Additional information of the Tool.
long_desc
class-attribute
instance-attribute
¶
long_desc: str = Field(
"", description="The long description of the Tool"
)
The long description of the Tool.
name
class-attribute
instance-attribute
¶
name: str = Field(..., description='The name of the Tool')
The name of the Tool.
params
class-attribute
instance-attribute
¶
params: type[BaseModel] = Field(
..., description="The parameters of the Tool"
)
The parameters of the Tool.
raises
class-attribute
instance-attribute
¶
raises: List[Dict[str, Optional[str]]] = Field(
[], description="The exceptions raised by the Tool"
)
The exceptions raised by the Tool.
returns
class-attribute
instance-attribute
¶
returns: type[BaseModel] = Field(
..., description="The return of the Tool"
)
The return of the Tool.
short_desc
class-attribute
instance-attribute
¶
short_desc: str = Field(
"", description="The short description of the Tool"
)
The short description of the Tool.
parse_data
classmethod
¶
parse_data(
sig: Signature,
docstring: Optional[str],
predefined: Dict[str, Any],
) -> Dict[str, Any]
Parse data from the signature and docstring of a function.
Source code in src/appl/core/tool.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
ToolRole
¶
ToolRole(name: Optional[str] = None, **kwargs: Any)
Bases: RoleChanger
Change the role of the prompts to tool.
Parameters:
-
name
(Optional[str]
, default:None
) –The name of the tool role. Defaults to None.
-
**kwargs
(Any
, default:{}
) –The keyword arguments to pass to the RoleChanger constructor.
Source code in src/appl/role_changer.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
UserRole
¶
UserRole(name: Optional[str] = None, **kwargs: Any)
Bases: RoleChanger
Change the role of the prompts to user.
Parameters:
-
name
(Optional[str]
, default:None
) –The name of the user role. Defaults to None.
-
**kwargs
(Any
, default:{}
) –The keyword arguments to pass to the RoleChanger constructor.