Skip to content
Open
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
48 changes: 48 additions & 0 deletions langfun/core/structured/querying.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class _MyLfQuery(LFQuery):
_DEFAULT_PROTOCOL_VERSIONS: ClassVar[dict[str, str]] = {
'python': '2.0',
'json': '1.0',
'markdown': '1.0',
}

def __init_subclass__(cls) -> Any:
Expand Down Expand Up @@ -273,6 +274,53 @@ class Answer:
)


class _LfQueryMarkdownV1(LfQuery):
"""Query a structured value using Markdown as the protocol."""

preamble = """
Please respond to the last {{ input_title }} with {{ output_title }} only according to {{ schema_title }}.

{{ input_title }}:
1 + 1 =

{{ schema_title }}:
Answer

## final_answer
...

{{ output_title }}:
## final_answer
2
"""
version = '1.0'
protocol = 'markdown'
input_title = 'REQUEST'
schema_title = 'OUTPUT MARKDOWN SCHEMA'
output_title = 'OUTPUT MARKDOWN'
mapping_template = lf.Template("""
{%- if example.context -%}
{{ context_title}}:
{{ example.context | indent(2, True)}}

{% endif -%}

{{ input_title }}:
{{ example.input_repr(protocol, compact=False) | indent(2, True) }}

{% if example.schema -%}
{{ schema_title }}:
{{ example.schema_repr(protocol) | indent(2, True) }}

{% endif -%}

{{ output_title }}:
{%- if example.has_output %}
{{ example.output_repr(protocol, compact=False) | indent(2, True) }}
{% endif -%}
""")


def query(
prompt: Union[str, lf.Template, lf.Message, Any],
schema: schema_lib.SchemaType | None = None,
Expand Down
3 changes: 3 additions & 0 deletions langfun/core/structured/schema/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,6 @@
from langfun.core.structured.schema.python import class_definition
from langfun.core.structured.schema.python import class_definitions
from langfun.core.structured.schema.python import include_method_in_prompt

# Markdown protocol.
from langfun.core.structured.schema.markdown import MarkdownPromptingProtocol
Loading