-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
95 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,23 @@ | ||
# SPDX-FileCopyrightText: 2023-present Massimiliano Pippi <[email protected]> | ||
# | ||
# SPDX-License-Identifier: MIT | ||
import html | ||
import os | ||
import json | ||
|
||
import requests | ||
from jinja2 import TemplateSyntaxError, nodes | ||
from jinja2.ext import Extension | ||
|
||
|
||
SUPPORTED_TYPES = ("system", "user") | ||
|
||
|
||
class ChatMessage(Extension): | ||
""" | ||
`chat` can be used to render prompt text as structured ChatMessage objects. | ||
Example: | ||
``` | ||
{% chat role="system" %} | ||
You are a helpful assistant. | ||
{% endchat %} | ||
``` | ||
""" | ||
|
||
|
@@ -65,5 +65,4 @@ def _store_chat_messages(self, role, caller): | |
""" | ||
Helper callback. | ||
""" | ||
print({"role": role, "content": caller()}) | ||
return caller() | ||
return json.dumps({"role": role, "content": caller()}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,11 @@ | ||
# SPDX-FileCopyrightText: 2023-present Massimiliano Pippi <[email protected]> | ||
# | ||
# SPDX-License-Identifier: MIT | ||
import uuid | ||
from typing import Any | ||
|
||
from pydantic import BaseModel, ValidationError | ||
|
||
from .cache import DefaultCache, RenderCache | ||
from .config import config | ||
from .env import env | ||
|
@@ -12,6 +15,11 @@ | |
DEFAULT_VERSION = "0" | ||
|
||
|
||
class ChatMessage(BaseModel): | ||
role: str | ||
content: str | ||
|
||
|
||
class BasePrompt: | ||
def __init__( | ||
self, | ||
|
@@ -37,7 +45,7 @@ def __init__( | |
be used. | ||
""" | ||
self._metadata = metadata or {} | ||
self._name = name | ||
self._name = name or str(uuid.uuid4()) | ||
self._raw: str = text | ||
self._render_cache = render_cache or DefaultCache() | ||
self._template = env.from_string(text) | ||
|
@@ -55,7 +63,7 @@ def metadata(self) -> dict[str, Any]: | |
return self._metadata | ||
|
||
@property | ||
def name(self) -> str | None: | ||
def name(self) -> str: | ||
return self._name | ||
|
||
@property | ||
|
@@ -105,6 +113,28 @@ def text(self, data: dict[str, Any] | None = None) -> str: | |
self._render_cache.set(data, rendered) | ||
return rendered | ||
|
||
def chat_messages(self, data: dict[str, Any] | None = None) -> list[ChatMessage]: | ||
""" | ||
Render the prompt using variables present in `data` | ||
Parameters: | ||
data: A dictionary containing the context variables. | ||
""" | ||
data = self._get_context(data) | ||
rendered = self._render_cache.get(data) | ||
if not rendered: | ||
rendered = self._template.render(data) | ||
self._render_cache.set(data, rendered) | ||
|
||
messages: list[ChatMessage] = [] | ||
for line in rendered.strip().split("\n"): | ||
try: | ||
messages.append(ChatMessage.model_validate_json(line)) | ||
except ValidationError: | ||
# Ignore lines that are not a message | ||
pass | ||
return messages | ||
|
||
|
||
class AsyncPrompt(BasePrompt): | ||
""" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import pytest | ||
from jinja2 import TemplateSyntaxError | ||
|
||
from banks import Prompt | ||
|
||
|
||
def test_wrong_tag(): | ||
with pytest.raises(TemplateSyntaxError): | ||
Prompt("{% chat %}{% endchat %}") | ||
|
||
|
||
def test_wrong_tag_params(): | ||
with pytest.raises(TemplateSyntaxError): | ||
Prompt('{% chat foo="bar" %}{% endchat %}') | ||
|
||
|
||
def test_wrong_role_type(): | ||
with pytest.raises(TemplateSyntaxError): | ||
Prompt('{% chat role="does not exist" %}{% endchat %}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters