mirror of
https://github.com/hwchase17/langchain.git
synced 2025-08-08 12:31:49 +00:00
add from string method (#820)
This commit is contained in:
parent
ba26a879e0
commit
3f952eb597
@ -1,4 +1,5 @@
|
|||||||
"""Chain that just formats a prompt and calls an LLM."""
|
"""Chain that just formats a prompt and calls an LLM."""
|
||||||
|
from string import Formatter
|
||||||
from typing import Any, Dict, List, Sequence, Union
|
from typing import Any, Dict, List, Sequence, Union
|
||||||
|
|
||||||
from pydantic import BaseModel, Extra
|
from pydantic import BaseModel, Extra
|
||||||
@ -7,6 +8,7 @@ from langchain.chains.base import Chain
|
|||||||
from langchain.input import get_colored_text
|
from langchain.input import get_colored_text
|
||||||
from langchain.llms.base import BaseLLM
|
from langchain.llms.base import BaseLLM
|
||||||
from langchain.prompts.base import BasePromptTemplate
|
from langchain.prompts.base import BasePromptTemplate
|
||||||
|
from langchain.prompts.prompt import PromptTemplate
|
||||||
from langchain.schema import LLMResult
|
from langchain.schema import LLMResult
|
||||||
|
|
||||||
|
|
||||||
@ -126,3 +128,14 @@ class LLMChain(Chain, BaseModel):
|
|||||||
@property
|
@property
|
||||||
def _chain_type(self) -> str:
|
def _chain_type(self) -> str:
|
||||||
return "llm_chain"
|
return "llm_chain"
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_string(cls, llm: BaseLLM, template: str) -> Chain:
|
||||||
|
"""Create LLMChain from LLM and template."""
|
||||||
|
input_variables = {
|
||||||
|
v for _, v, _, _ in Formatter().parse(template) if v is not None
|
||||||
|
}
|
||||||
|
prompt_template = PromptTemplate(
|
||||||
|
input_variables=list(input_variables), template=template
|
||||||
|
)
|
||||||
|
return cls(llm=llm, prompt=prompt_template)
|
||||||
|
Loading…
Reference in New Issue
Block a user