Skip to content

Commit 3f952eb

Browse files
authored
add from string method (#820)
1 parent ba26a87 commit 3f952eb

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

langchain/chains/llm.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Chain that just formats a prompt and calls an LLM."""
2+
from string import Formatter
23
from typing import Any, Dict, List, Sequence, Union
34

45
from pydantic import BaseModel, Extra
@@ -7,6 +8,7 @@
78
from langchain.input import get_colored_text
89
from langchain.llms.base import BaseLLM
910
from langchain.prompts.base import BasePromptTemplate
11+
from langchain.prompts.prompt import PromptTemplate
1012
from langchain.schema import LLMResult
1113

1214

@@ -126,3 +128,14 @@ def apply_and_parse(
126128
@property
127129
def _chain_type(self) -> str:
128130
return "llm_chain"
131+
132+
@classmethod
133+
def from_string(cls, llm: BaseLLM, template: str) -> Chain:
134+
"""Create LLMChain from LLM and template."""
135+
input_variables = {
136+
v for _, v, _, _ in Formatter().parse(template) if v is not None
137+
}
138+
prompt_template = PromptTemplate(
139+
input_variables=list(input_variables), template=template
140+
)
141+
return cls(llm=llm, prompt=prompt_template)

0 commit comments

Comments
 (0)