mirror of
https://github.com/hwchase17/langchain
synced 2024-11-18 09:25:54 +00:00
8e0d5813c2
Import from core instead. Ran: ```bash git grep -l 'from langchain.schema\.output_parser' | xargs -L 1 sed -i '' "s/from\ langchain\.schema\.output_parser/from\ langchain_core.output_parsers/g" git grep -l 'from langchain.schema\.messages' | xargs -L 1 sed -i '' "s/from\ langchain\.schema\.messages/from\ langchain_core.messages/g" git grep -l 'from langchain.schema\.document' | xargs -L 1 sed -i '' "s/from\ langchain\.schema\.document/from\ langchain_core.documents/g" git grep -l 'from langchain.schema\.runnable' | xargs -L 1 sed -i '' "s/from\ langchain\.schema\.runnable/from\ langchain_core.runnables/g" git grep -l 'from langchain.schema\.vectorstore' | xargs -L 1 sed -i '' "s/from\ langchain\.schema\.vectorstore/from\ langchain_core.vectorstores/g" git grep -l 'from langchain.schema\.language_model' | xargs -L 1 sed -i '' "s/from\ langchain\.schema\.language_model/from\ langchain_core.language_models/g" git grep -l 'from langchain.schema\.embeddings' | xargs -L 1 sed -i '' "s/from\ langchain\.schema\.embeddings/from\ langchain_core.embeddings/g" git grep -l 'from langchain.schema\.storage' | xargs -L 1 sed -i '' "s/from\ langchain\.schema\.storage/from\ langchain_core.stores/g" git checkout master libs/langchain/tests/unit_tests/schema/ make format cd libs/experimental make format cd ../langchain make format ```
50 lines
1.5 KiB
Python
50 lines
1.5 KiB
Python
from typing import Any, Dict, List, Optional
|
|
|
|
from langchain.chains.base import Chain
|
|
from langchain.chains.llm import LLMChain
|
|
from langchain.prompts import PromptTemplate
|
|
from langchain_core.language_models import BaseLanguageModel
|
|
|
|
from langchain_experimental.synthetic_data.prompts import SENTENCE_PROMPT
|
|
|
|
|
|
def create_data_generation_chain(
|
|
llm: BaseLanguageModel,
|
|
prompt: Optional[PromptTemplate] = None,
|
|
) -> Chain:
|
|
"""Creates a chain that generates synthetic sentences with
|
|
provided fields.
|
|
|
|
Args:
|
|
llm: The language model to use.
|
|
prompt: Prompt to feed the language model with.
|
|
If not provided, the default one will be used.
|
|
"""
|
|
prompt = prompt or SENTENCE_PROMPT
|
|
return LLMChain(
|
|
llm=llm,
|
|
prompt=prompt,
|
|
)
|
|
|
|
|
|
class DatasetGenerator:
|
|
"""Generates synthetic dataset with a given language model."""
|
|
|
|
def __init__(
|
|
self,
|
|
llm: BaseLanguageModel,
|
|
sentence_preferences: Optional[Dict[str, Any]] = None,
|
|
):
|
|
self.generator = create_data_generation_chain(llm)
|
|
self.sentence_preferences = sentence_preferences or {}
|
|
|
|
def __call__(self, fields_collection: List[List[Any]]) -> List[Dict[str, Any]]:
|
|
results: List[Dict[str, Any]] = []
|
|
for fields in fields_collection:
|
|
results.append(
|
|
self.generator(
|
|
{"fields": fields, "preferences": self.sentence_preferences}
|
|
)
|
|
)
|
|
return results
|