Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add args to Jinja filters #902

Merged
merged 7 commits into from
May 17, 2024
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 docs/reference/prompting.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ Several projects (e.g.[Toolformer](https://arxiv.org/abs/2302.04761), [ViperGPT]
Can you do something?

COMMANDS
1. my_tool: Tool description, args: arg1:str, arg2:int
1. my_tool: Tool description., args: arg1: str, arg2: int

def my_tool(arg1: str, arg2: int):
"""Tool description.
Expand Down
13 changes: 13 additions & 0 deletions outlines/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ def render(template: str, **values: Optional[Dict[str, Any]]) -> str:
env.filters["source"] = get_fn_source
env.filters["signature"] = get_fn_signature
env.filters["schema"] = get_schema
env.filters["args"] = get_fn_args

jinja_template = env.from_string(cleaned_template)

Expand All @@ -226,6 +227,18 @@ def get_fn_name(fn: Callable):
return name


def get_fn_args(fn: Callable):
"""Returns the arguments of a function with annotations and default values if provided."""
if not callable(fn):
raise TypeError("The `args` filter only applies to callables.")

arg_str_list = []
signature = inspect.signature(fn)
arg_str_list = [str(param) for param in signature.parameters.values()]
arg_str = ", ".join(arg_str_list)
return arg_str


def get_fn_description(fn: Callable):
"""Returns the first line of a callable's docstring."""
if not callable(fn):
Expand Down
62 changes: 61 additions & 1 deletion tests/test_prompts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List
from typing import Dict, List

import pytest
from pydantic import BaseModel, Field
Expand Down Expand Up @@ -252,3 +252,63 @@ def source_ppt(model):

prompt = source_ppt(response)
assert prompt == '{\n "one": "a description",\n "two": ""\n}'


def test_prompt_args():
def no_args():
pass

def with_args(x, y, z):
pass

def with_annotations(x: bool, y: str, z: Dict[int, List[str]]):
pass

def with_defaults(x=True, y="Hi", z={4: ["I", "love", "outlines"]}):
pass

def with_annotations_and_defaults(
x: bool = True,
y: str = "Hi",
z: Dict[int, List[str]] = {4: ["I", "love", "outlines"]},
):
pass

def with_all(
x1,
y1,
z1,
x2: bool,
y2: str,
z2: Dict[int, List[str]],
x3=True,
y3="Hi",
z3={4: ["I", "love", "outlines"]},
x4: bool = True,
y4: str = "Hi",
z4: Dict[int, List[str]] = {4: ["I", "love", "outlines"]},
):
pass

@outlines.prompt
def args_prompt(fn):
"""args: {{ fn | args }}"""

assert args_prompt(no_args) == "args: "
assert args_prompt(with_args) == "args: x, y, z"
assert (
args_prompt(with_annotations)
== "args: x: bool, y: str, z: Dict[int, List[str]]"
)
assert (
args_prompt(with_defaults)
== "args: x=True, y='Hi', z={4: ['I', 'love', 'outlines']}"
)
assert (
args_prompt(with_annotations_and_defaults)
== "args: x: bool = True, y: str = 'Hi', z: Dict[int, List[str]] = {4: ['I', 'love', 'outlines']}"
)
assert (
args_prompt(with_all)
== "args: x1, y1, z1, x2: bool, y2: str, z2: Dict[int, List[str]], x3=True, y3='Hi', z3={4: ['I', 'love', 'outlines']}, x4: bool = True, y4: str = 'Hi', z4: Dict[int, List[str]] = {4: ['I', 'love', 'outlines']}"
)
Loading