Git Product home page Git Product logo

Comments (10)

simonw avatar simonw commented on May 27, 2024

The trickiest thing about these templates is their design.

The simplest form of template is some text with a replacement variable where the user's additional input is pasted in - something like this:

Summarize this: {input}

But there are all sorts of other things to consider:

  • How should system prompts be handled? I'd much rather write the above template as a system prompt of Summarize this with the input being sent as the full regular prompt.
  • How to support more than one placeholder? Would be neat if you could say llm -t persona -p name Tom -p age 22
  • If a template has both a prompt and a system prompt, how are they both stored?

I'm leaning towards YAML for this, because it has neat support for multi-line text blocks.

from llm.

simonw avatar simonw commented on May 27, 2024

Templates can go in ~/Library/Application Support/io.datasette.llm/templates - each one can be a file.

Will I force them to be YAML files, or can you have one that is pure text if it's a really simple one?

from llm.

simonw avatar simonw commented on May 27, 2024

I think YAML will do. A YAML file can contain just a string, and it will be treated right unless it happens to contain a colon character or similar:

>>> import yaml
>>> yaml.safe_load("this is just a string")
'this is just a string'
>>> yaml.safe_load("this is just a string: hooray")
{'this is just a string': 'hooray'}
>>> yaml.safe_load("\"this is just a string: hooray\"")
'this is just a string: hooray'

I can explain enough of this in the documentation to avoid people getting caught out.

from llm.

simonw avatar simonw commented on May 27, 2024

What should I use for the actual variable substitution part of this? I'd like to keep that to the Python standard library if possible.

I'm tempted to just use this:

>>> s = "Summarize: {input}"
>>> s.format(blah='foo')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'input'
>>> s.format(input='foo')
'Summarize: foo'

Another option is Template strings: https://docs.python.org/3/library/string.html#template-strings

Template strings support $-based substitutions, using the following rules:

  • $$ is an escape; it is replaced with a single $.
  • $identifier names a substitution placeholder matching a mapping key of "identifier". By default, "identifier" is restricted to any case-insensitive ASCII alphanumeric string (including underscores) that starts with an underscore or ASCII letter. The first non-identifier character after the $ character terminates this placeholder specification.
  • ${identifier} is equivalent to $identifier. It is required when valid identifier characters follow the placeholder but are not part of the placeholder, such as "${noun}ification".
>>> from string import Template
>>> t = Template("Summarize: $input")
>>> t.substitute(input="Hello")
'Summarize: Hello'
>>> t.substitute(input2="Hello")
Traceback (most recent call last):
...
KeyError: 'input'

from llm.

simonw avatar simonw commented on May 27, 2024

OK, I think I'm going to go with $variable substitution using string.Template - and the templates themselves will be YAML files which can contain either a string or a dictionary.

If it's a dictionary it can have prompt: and system: keys for prompt + system templates.

If you just have a system then the prompt will default to $input.

Any other $name variables will be treated as required parameters, passed using -p name value.

from llm.

simonw avatar simonw commented on May 27, 2024

So a set of commands:

llm templates edit name-of-template # Edit the specified template, using $EDITOR
llm templates list # list available templates
llm templates show name-of-template # show a template
llm templates path # path to the templates directory

from llm.

simonw avatar simonw commented on May 27, 2024

Templates can also have a model: ... to set the default model for that template.

from llm.

simonw avatar simonw commented on May 27, 2024

I can use this: https://click.palletsprojects.com/en/8.1.x/utils/#launching-editors

>>> import click
>>> click.edit(filename='/tmp/hello.txt')

from llm.

simonw avatar simonw commented on May 27, 2024

Idea from:

Slight twist: what if you want to use another template at the same time?

Might be an argument for supporting combined templates - pass -t multiple times and the result is a combination of those templates - the most recent of each of the prompt, system prompt, logit biases etc.

from llm.

simonw avatar simonw commented on May 27, 2024

It would be amazing if you could install new templates by installing Python packages - using a plugin hook. Could be a neat way to distribute more follows templates, especially ones that include functions.

from llm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.