forked from langchain-ai/langchain
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored
example_generator
(langchain-ai#8099)
Refactored `example_generator.py`. The same as langchain-ai#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` @hwchase17
- Loading branch information
Showing
3 changed files
with
36 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"] |