func
¶
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. Commonly used for wrapper functions to pass the closure variables.
Source code in src/appl/func.py
as_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,
executor_type: ExecutorType = ExecutorType.GENERAL_THREAD_POOL,
**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,
*,
messages: Optional[
Union[Conversation, List[BaseMessage], List[Dict]]
] = 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, Dict]] = [],
tool_format: str = "auto",
stream: Optional[bool] = None,
response_format: Optional[
Union[dict, str, Type[M]]
] = None,
response_model: Optional[Type[M]] = None,
max_relay_rounds: int = 0,
mock_response: Optional[
Union[CompletionResponse, str]
] = None,
messages_process_func: Optional[
Callable[[Conversation], Conversation]
] = 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.
-
messages
(Union[Conversation, List[BaseMessage]]
, default:None
) –the messages as the prompt for the LLM. Defaults to retrieve from the context.
-
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 | Dict | Sequence[BaseTool | Dict | 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, str, 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. use
instructor_patch_mode
to specify the mode for patching the raw completion. Recommended to useresponse_format
instead. Defaults to None. -
max_relay_rounds
(int
, default:0
) –the maximum number of relay rounds to continue the unfinished text generation. Defaults to 0.
-
mock_response
(Union[CompletionResponse, str]
, default:None
) –mock response for testing. Defaults to None.
-
messages_process_func
(Callable[[Conversation], Conversation]
, default:None
) –a function to process the messages before sending to the LLM. 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
405 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 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 |
|
get_var
¶
get_var(name: str, _ctx: PromptContext) -> Any
grow
¶
grow(
content: Any, *, _ctx: Optional[PromptContext] = None
) -> None
Append the content to the prompt in the current context.
Source code in src/appl/func.py
ppl
¶
ppl(
ctx: Union[str, F] = "new",
compositor: Optional[Compositor] = None,
*,
default_return: Optional[Literal["prompt"]] = None,
docstring_as: Optional[str] = None,
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.
-
compositor
(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.
-
docstring_as
(str
, default:None
) –Include the triple-quoted docstring as a message in the prompt. Options include "user" and "system". Defaults to None.
-
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
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 |
|
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_context(func: Callable) -> None
Reset the context for APPL functions with the 'resume' context method.