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 initial prompt stuff #1

Merged
merged 2 commits into from Oct 17, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions Makefile
@@ -0,0 +1,14 @@
.PHONY: format lint tests

format:
black .
isort .

lint:
black . --check
isort . --check
flake8 .
mypy .

tests:
pytest tests
4 changes: 0 additions & 4 deletions format.sh

This file was deleted.

21 changes: 21 additions & 0 deletions langchain/formatting.py
@@ -0,0 +1,21 @@
from string import Formatter


class StrictFormatter(Formatter):
"""A subclass of formatter that checks for extra keys."""

def check_unused_args(self, used_args, args, kwargs):
extra = set(kwargs).difference(used_args)
if extra:
raise KeyError(extra)

def vformat(self, format_string, args, kwargs):
if len(args) > 0:
raise ValueError(
"No arguments should be provided, "
"everything should be passed as keyword arguments."
)
return super().vformat(format_string, args, kwargs)


formatter = StrictFormatter()
27 changes: 27 additions & 0 deletions langchain/prompt.py
@@ -0,0 +1,27 @@
"""Base schema types."""
from typing import Dict, List

from pydantic import BaseModel, Extra, root_validator

from langchain.formatting import formatter


class Prompt(BaseModel):
"""Schema to represent a prompt for an LLM."""

input_variables: List[str]
template: str

class Config:
extra = Extra.forbid

@root_validator()
def template_is_valid(cls, values: Dict) -> Dict:
input_variables = values["input_variables"]
template = values["template"]
dummy_inputs = {input_variable: "foo" for input_variable in input_variables}
try:
formatter.format(template, **dummy_inputs)
except KeyError:
raise ValueError("Invalid prompt schema.")
return values
5 changes: 5 additions & 0 deletions tests/unit_tests/data/prompts/prompt_extra_args.json
@@ -0,0 +1,5 @@
{
"input_variables": ["foo"],
"template": "This is a {foo} test.",
"bad_var": 1
}
3 changes: 3 additions & 0 deletions tests/unit_tests/data/prompts/prompt_missing_args.json
@@ -0,0 +1,3 @@
{
"input_variables": ["foo"]
}
4 changes: 4 additions & 0 deletions tests/unit_tests/data/prompts/simple_prompt.json
@@ -0,0 +1,4 @@
{
"input_variables": ["foo"],
"template": "This is a {foo} test."
}
22 changes: 22 additions & 0 deletions tests/unit_tests/test_formatting.py
@@ -0,0 +1,22 @@
import pytest

from langchain.formatting import formatter


def test_valid_formatting():
template = "This is a {foo} test."
output = formatter.format(template, foo="good")
expected_output = "This is a good test."
assert output == expected_output


def test_does_not_allow_args():
template = "This is a {} test."
with pytest.raises(ValueError):
formatter.format(template, "good")


def test_does_not_allow_extra_kwargs():
template = "This is a {foo} test."
with pytest.raises(KeyError):
formatter.format(template, foo="good", bar="oops")
32 changes: 32 additions & 0 deletions tests/unit_tests/test_schema.py
@@ -0,0 +1,32 @@
import pytest

from langchain.prompt import Prompt


def test_prompt_valid():
template = "This is a {foo} test."
input_variables = ["foo"]
prompt = Prompt(input_variables=input_variables, template=template)
assert prompt.template == template
assert prompt.input_variables == input_variables


def test_prompt_missing_input_variables():
template = "This is a {foo} test."
input_variables = []
with pytest.raises(ValueError):
Prompt(input_variables=input_variables, template=template)


def test_prompt_extra_input_variables():
template = "This is a {foo} test."
input_variables = ["foo", "bar"]
with pytest.raises(ValueError):
Prompt(input_variables=input_variables, template=template)


def test_prompt_wrong_input_variables():
template = "This is a {foo} test."
input_variables = ["bar"]
with pytest.raises(ValueError):
Prompt(input_variables=input_variables, template=template)