mirror of https://github.com/hwchase17/langchain
Refactored `example_generator` (#8099)
Refactored `example_generator.py`. The same as #7961 `example_generator.py` is in the root code folder. This creates the `langchain.example_generator: Example Generator ` group on the API Reference navigation ToC, on the same level as `Chains` and `Agents` which is not correct. Refactoring: - moved `example_generator.py` content into `chains/example_generator.py` (not in `utils` because the `example_generator` has dependencies on other LangChain classes. It also doesn't work for moving into `utilities/`) - added the backwards compatibility ref in the original `example_generator.py` @hwchase17pull/7726/head
parent
1cc7d4c9eb
commit
3eb4112a1f
@ -0,0 +1,22 @@
|
||||
from typing import List
|
||||
|
||||
from langchain.chains.llm import LLMChain
|
||||
from langchain.prompts.few_shot import FewShotPromptTemplate
|
||||
from langchain.prompts.prompt import PromptTemplate
|
||||
from langchain.schema.language_model import BaseLanguageModel
|
||||
|
||||
TEST_GEN_TEMPLATE_SUFFIX = "Add another example."
|
||||
|
||||
|
||||
def generate_example(
|
||||
examples: List[dict], llm: BaseLanguageModel, prompt_template: PromptTemplate
|
||||
) -> str:
|
||||
"""Return another example given a list of examples for a prompt."""
|
||||
prompt = FewShotPromptTemplate(
|
||||
examples=examples,
|
||||
suffix=TEST_GEN_TEMPLATE_SUFFIX,
|
||||
input_variables=[],
|
||||
example_prompt=prompt_template,
|
||||
)
|
||||
chain = LLMChain(llm=llm, prompt=prompt)
|
||||
return chain.predict()
|
@ -1,23 +1,4 @@
|
||||
"""Utility functions for working with prompts."""
|
||||
from typing import List
|
||||
"""Keep here for backwards compatibility."""
|
||||
from langchain.chains.example_generator import generate_example
|
||||
|
||||
from langchain.chains.llm import LLMChain
|
||||
from langchain.prompts.few_shot import FewShotPromptTemplate
|
||||
from langchain.prompts.prompt import PromptTemplate
|
||||
from langchain.schema.language_model import BaseLanguageModel
|
||||
|
||||
TEST_GEN_TEMPLATE_SUFFIX = "Add another example."
|
||||
|
||||
|
||||
def generate_example(
|
||||
examples: List[dict], llm: BaseLanguageModel, prompt_template: PromptTemplate
|
||||
) -> str:
|
||||
"""Return another example given a list of examples for a prompt."""
|
||||
prompt = FewShotPromptTemplate(
|
||||
examples=examples,
|
||||
suffix=TEST_GEN_TEMPLATE_SUFFIX,
|
||||
input_variables=[],
|
||||
example_prompt=prompt_template,
|
||||
)
|
||||
chain = LLMChain(llm=llm, prompt=prompt)
|
||||
return chain.predict()
|
||||
__all__ = ["generate_example"]
|
||||
|
Loading…
Reference in New Issue