You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I searched existing ideas and did not find a similar one
I added a very descriptive title
I've clearly described the feature request and motivation for it
Feature request
Static definition of templates that provide:
IDE support
1.1. Multiple-rename
1.2. Auto-complete, etc.
1.3. Possibility in theory to refer to variables by names and not by strings.
That are returned as auto-complete from a template-typed variable of the MagicTemplate type.
Example:
Current definition of templates is dynamic, and the variable_name has to be handled by the user. A pedantic user would centralize that definition and have to maintain all bookkeeping by themselves, though that's supposed to be the responsibility of the library.
Most basic usage:
from langchain_core.prompts import PromptTemplate
# Define the prompt template with a single variable
prompt_template = PromptTemplate.from_template("Your input is: {VARIABLE_NAME}")
# Invoke the template with a specific value for VARIABLE_NAME
result = prompt_template.invoke({"VARIABLE_NAME": "example_value"})
# Output the result
print(result)
With pedantic external bookkeeping of VARIABLE_NAME, answering requirement (1.1) above, but not (1.2) or more advanced use-cases and definitely not (2):
from langchain_core.prompts import PromptTemplate
# Centralized variable name definition
VARIABLE_NAME = "VARIABLE_NAME"
# Define the prompt template using the centralized variable name
prompt_template = PromptTemplate.from_template(f"Your input is: {{{VARIABLE_NAME}}}")
# Invoke the template with a specific value for VARIABLE_NAME
result = prompt_template.invoke({VARIABLE_NAME: "example_value"})
# Output the result
print(result)
I believe the responsibility for template-variable-name bookkeeping should be of the library, and thus should allow static definition of templates (they are templates, after all). They should still be allowed to be dynamic, in case they themselves are LLM-generated, but a static way should be supported.
Motivation
The responsibility for handling the naming of template variables should be of the library once a name was given to it. Not of the user. *
Support for IDE assisted coding
Multiple replace
auto complete
clean code - single point of reference for naming of template variables
Lang-chain is a library used more and more by non-programmer majors. They don't even know the damage they cause by referring to variables as strings and how hard it will be to maintain when their projects grow. Every string would have to be replaced in more than one location. It is CleanCode chapter 0. We can still save them while it's young. I would go so far as to make a breaking change of this.
Proposal (If applicable)
class MagicTemplate(PromptTemplate):
def __init__(self, VARIABLE_NAME):
self.VARIABLE_NAME = VARIABLE_NAME
self.template = f"Your input is {{{VARIABLE_NAME}}}."
and then VARIABLE_NAME can be accessed statically with auto-complete and auto-rename in the IDE later
from langchain_core.prompts import PromptTemplate
class MagicTemplate(PromptTemplate):
def __init__(self, VARIABLE_NAME):
self.VARIABLE_NAME = VARIABLE_NAME
self.template = f"Your input is {{{VARIABLE_NAME}}}."
# Define the prompt template with a single variable
prompt_template = MagicTemplate(VARIABLE_NAME="VARIABLE_NAME")
# Invoke the template with a specific value for VARIABLE_NAME
result = prompt_template.invoke(VARIABLE_NAME="VARIABLE_VALUE")
# Output the result
print(result)
Clean, easy, natively supported by IDE, access to VARIABLE_NAME by reference!
How to do this and keep dynamic support like now? Simply add a from_template constructor that can parse the template
from langchain_core.prompts import PromptTemplate
class MagicTemplate(PromptTemplate):
def __init__(self, template: str, **variable_names):
self._variable_names = variable_names
# Dynamically define instance variable names for IDE support
for var_name in variable_names:
setattr(self, var_name, var_name)
self.template = template
@staticmethod # c'tor from dynamic variable names as dict
def from_variable_names(**variable_names):
return MagicTemplate("", **variable_names)
@staticmethod # c'tor from template
def from_template(template: str):
import re
variable_names = re.findall(r"{(\w+)}", template)
return MagicTemplate(template, **{name: name for name in variable_names})
def invoke(self, **kwargs):
return self.template.format(**kwargs)
Example Usage
template = MagicTemplate(VARIABLE_NAME="example")
result = template.invoke(VARIABLE_NAME="test value")
print(result)
# Static usage
class MyMagicTemplate(MagicTemplate):
VARIABLE_NAME = "VARIABLE_NAME"
template = f"Your input is {{{VARIABLE_NAME}}}."
my_magic_template = MyMagicTemplate(template=MyMagicTemplate.template)
my_magic_template.invoke(VARIABLE_NAME="Static Test Value")
# Dynamic usage
dynamic_template = MagicTemplate.from_template("Your input is: {VARIABLE_NAME}.")
dynamic_template.invoke(VARIABLE_NAME="Dynamic Test Value")
Please notice none of the code above is tested.
I hope the idea is clear though:
The responsibility for handling the naming of template variables should be of the library once a name was given to it. Not of the user. *
I hope this makes enough sense as a proposal, it's my first on GH
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Checked
Feature request
Static definition of templates that provide:
IDE support
1.1. Multiple-rename
1.2. Auto-complete, etc.
1.3. Possibility in theory to refer to variables by names and not by strings.
That are returned as auto-complete from a template-typed variable of the MagicTemplate type.
Example:
Current definition of templates is dynamic, and the variable_name has to be handled by the user. A pedantic user would centralize that definition and have to maintain all bookkeeping by themselves, though that's supposed to be the responsibility of the library.
Most basic usage:
With pedantic external bookkeeping of VARIABLE_NAME, answering requirement (1.1) above, but not (1.2) or more advanced use-cases and definitely not (2):
I believe the responsibility for template-variable-name bookkeeping should be of the library, and thus should allow static definition of templates (they are templates, after all). They should still be allowed to be dynamic, in case they themselves are LLM-generated, but a static way should be supported.
Motivation
The responsibility for handling the naming of template variables should be of the library once a name was given to it. Not of the user. *
Support for IDE assisted coding
Multiple replace
auto complete
clean code - single point of reference for naming of template variables
Lang-chain is a library used more and more by non-programmer majors. They don't even know the damage they cause by referring to variables as strings and how hard it will be to maintain when their projects grow. Every string would have to be replaced in more than one location. It is CleanCode chapter 0. We can still save them while it's young. I would go so far as to make a breaking change of this.
Proposal (If applicable)
and then VARIABLE_NAME can be accessed statically with auto-complete and auto-rename in the IDE later
Clean, easy, natively supported by IDE, access to VARIABLE_NAME by reference!
How to do this and keep dynamic support like now? Simply add a from_template constructor that can parse the template
Example Usage
Please notice none of the code above is tested.
I hope the idea is clear though:
I hope this makes enough sense as a proposal, it's my first on GH
Beta Was this translation helpful? Give feedback.
All reactions