Skip to content

Commit

Permalink
Refactored example_generator (langchain-ai#8099)
Browse files Browse the repository at this point in the history
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
leo-gan authored Jul 24, 2023
1 parent 1cc7d4c commit 3eb4112
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 31 deletions.
20 changes: 11 additions & 9 deletions libs/langchain/langchain/chains/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
ChatVectorDBChain,
ConversationalRetrievalChain,
)
from langchain.chains.example_generator import generate_example
from langchain.chains.flare.base import FlareChain
from langchain.chains.graph_qa.base import GraphQAChain
from langchain.chains.graph_qa.cypher import GraphCypherQAChain
Expand Down Expand Up @@ -84,9 +85,9 @@
"GraphCypherQAChain",
"GraphQAChain",
"GraphSparqlQAChain",
"HugeGraphQAChain",
"HypotheticalDocumentEmbedder",
"KuzuQAChain",
"HugeGraphQAChain",
"LLMBashChain",
"LLMChain",
"LLMCheckerChain",
Expand All @@ -95,6 +96,8 @@
"LLMRouterChain",
"LLMSummarizationCheckerChain",
"MapReduceChain",
"MapReduceDocumentsChain",
"MapRerankDocumentsChain",
"MultiPromptChain",
"MultiRetrievalQAChain",
"MultiRouteChain",
Expand All @@ -105,27 +108,26 @@
"PALChain",
"QAGenerationChain",
"QAWithSourcesChain",
"ReduceDocumentsChain",
"RefineDocumentsChain",
"RetrievalQA",
"RetrievalQAWithSourcesChain",
"RouterChain",
"SQLDatabaseChain",
"SQLDatabaseSequentialChain",
"SequentialChain",
"SimpleSequentialChain",
"StuffDocumentsChain",
"TransformChain",
"VectorDBQA",
"VectorDBQAWithSourcesChain",
"create_citation_fuzzy_match_chain",
"create_extraction_chain",
"create_extraction_chain_pydantic",
"create_qa_with_sources_chain",
"create_qa_with_structure_chain",
"create_tagging_chain",
"create_tagging_chain_pydantic",
"generate_example",
"load_chain",
"create_citation_fuzzy_match_chain",
"create_qa_with_structure_chain",
"create_qa_with_sources_chain",
"StuffDocumentsChain",
"MapRerankDocumentsChain",
"MapReduceDocumentsChain",
"RefineDocumentsChain",
"ReduceDocumentsChain",
]
22 changes: 22 additions & 0 deletions libs/langchain/langchain/chains/example_generator.py
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()
25 changes: 3 additions & 22 deletions libs/langchain/langchain/example_generator.py
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"]

0 comments on commit 3eb4112

Please sign in to comment.