method_name
stringlengths
1
78
method_body
stringlengths
3
9.66k
full_code
stringlengths
31
10.7k
docstring
stringlengths
4
4.74k
is_lc_serializable
"""Is this class serializable?""" return False
@classmethod def is_lc_serializable(cls) ->bool: """Is this class serializable?""" return False
Is this class serializable?
test_visit_comparison_date
comp = Comparison(comparator=Comparator.LT, attribute='foo', value={'type': 'date', 'date': '2023-09-13'}) expected = {'operator': 'LessThan', 'path': ['foo'], 'valueDate': '2023-09-13T00:00:00Z'} actual = DEFAULT_TRANSLATOR.visit_comparison(comp) assert expected == actual
def test_visit_comparison_date() ->None: comp = Comparison(comparator=Comparator.LT, attribute='foo', value={ 'type': 'date', 'date': '2023-09-13'}) expected = {'operator': 'LessThan', 'path': ['foo'], 'valueDate': '2023-09-13T00:00:00Z'} actual = DEFAULT_TRANSLATOR.visit_comparison(comp) ...
null
_call
"""Call out to Cohere's generate endpoint. Args: prompt: The prompt to pass into the model. stop: Optional list of stop words to use when generating. Returns: The string generated by the model. Example: .. code-block:: python re...
def _call(self, prompt: str, stop: Optional[List[str]]=None, run_manager: Optional[CallbackManagerForLLMRun]=None, **kwargs: Any) ->str: """Call out to Cohere's generate endpoint. Args: prompt: The prompt to pass into the model. stop: Optional list of stop words to use when gene...
Call out to Cohere's generate endpoint. Args: prompt: The prompt to pass into the model. stop: Optional list of stop words to use when generating. Returns: The string generated by the model. Example: .. code-block:: python response = cohere("Tell me a joke.")
__query_collection
"""Query the collection.""" with Session(self._bind) as session: collection = self.get_collection(session) if not collection: raise ValueError('Collection not found') filter_by = self.EmbeddingStore.collection_id == collection.uuid if filter is not None: filter_clauses = [] for k...
def __query_collection(self, embedding: List[float], k: int=4, filter: Optional[Dict[str, str]]=None) ->List[Any]: """Query the collection.""" with Session(self._bind) as session: collection = self.get_collection(session) if not collection: raise ValueError('Collection not found'...
Query the collection.
_call
"""Call out to GPT4All's generate method. Args: prompt: The prompt to pass into the model. stop: A list of strings to stop generation when encountered. Returns: The string generated by the model. Example: .. code-block:: python ...
def _call(self, prompt: str, stop: Optional[List[str]]=None, run_manager: Optional[CallbackManagerForLLMRun]=None, **kwargs: Any) ->str: """Call out to GPT4All's generate method. Args: prompt: The prompt to pass into the model. stop: A list of strings to stop generation when enc...
Call out to GPT4All's generate method. Args: prompt: The prompt to pass into the model. stop: A list of strings to stop generation when encountered. Returns: The string generated by the model. Example: .. code-block:: python prompt = "Once upon a time, " response = model(prompt, n_pr...
from_llm_and_tools
"""Construct an agent from an LLM and tools.""" prompt = cls.create_prompt(extra_prompt_messages=extra_prompt_messages, system_message=system_message) return cls(llm=llm, prompt=prompt, tools=tools, callback_manager= callback_manager, **kwargs)
@classmethod def from_llm_and_tools(cls, llm: BaseLanguageModel, tools: Sequence[ BaseTool], callback_manager: Optional[BaseCallbackManager]=None, extra_prompt_messages: Optional[List[BaseMessagePromptTemplate]]=None, system_message: Optional[SystemMessage]=SystemMessage(content= 'You are a helpful AI a...
Construct an agent from an LLM and tools.
embed_query
"""Compute query embeddings using a HuggingFace transformer model. Args: text: The text to embed. Returns: Embeddings for the text. """ text = text.replace('\n', ' ') embedding = self.client.encode(self.query_instruction + text, **self. encode_kwargs) return embeddi...
def embed_query(self, text: str) ->List[float]: """Compute query embeddings using a HuggingFace transformer model. Args: text: The text to embed. Returns: Embeddings for the text. """ text = text.replace('\n', ' ') embedding = self.client.encode(self.query_i...
Compute query embeddings using a HuggingFace transformer model. Args: text: The text to embed. Returns: Embeddings for the text.
_parse_chat_history
messages: List[genai.types.MessageDict] = [] raw_system_message: Optional[SystemMessage] = None for i, message in enumerate(input_messages): if i == 0 and isinstance(message, SystemMessage ) and not convert_system_message_to_human: raise ValueError( """SystemMessages are not yet supporte...
def _parse_chat_history(input_messages: Sequence[BaseMessage], convert_system_message_to_human: bool=False) ->List[genai.types.ContentDict ]: messages: List[genai.types.MessageDict] = [] raw_system_message: Optional[SystemMessage] = None for i, message in enumerate(input_messages): if i == 0...
null
test_maximal_marginal_relevance_lambda_zero
query_embedding = np.random.random(size=5) embedding_list = [query_embedding, query_embedding, np.zeros(5)] expected = [0, 2] actual = maximal_marginal_relevance(query_embedding, embedding_list, lambda_mult=0, k=2) assert expected == actual
def test_maximal_marginal_relevance_lambda_zero() ->None: query_embedding = np.random.random(size=5) embedding_list = [query_embedding, query_embedding, np.zeros(5)] expected = [0, 2] actual = maximal_marginal_relevance(query_embedding, embedding_list, lambda_mult=0, k=2) assert expected == ...
null
_on_llm_start
"""Process the LLM Run upon start.""" self._process_start_trace(run)
def _on_llm_start(self, run: 'Run') ->None: """Process the LLM Run upon start.""" self._process_start_trace(run)
Process the LLM Run upon start.
generate_schema
""" Generates the schema of the ArangoDB Database and returns it User can specify a **sample_ratio** (0 to 1) to determine the ratio of documents/edges used (in relation to the Collection size) to render each Collection Schema. """ if not 0 <= sample_ratio <= 1: raise ValueEr...
def generate_schema(self, sample_ratio: float=0) ->Dict[str, List[Dict[str, Any]]]: """ Generates the schema of the ArangoDB Database and returns it User can specify a **sample_ratio** (0 to 1) to determine the ratio of documents/edges used (in relation to the Collection size) to...
Generates the schema of the ArangoDB Database and returns it User can specify a **sample_ratio** (0 to 1) to determine the ratio of documents/edges used (in relation to the Collection size) to render each Collection Schema.
input_variables
""" Input variables for this prompt template. Returns: List of input variable names. """ return self.prompt.input_variables
@property def input_variables(self) ->List[str]: """ Input variables for this prompt template. Returns: List of input variable names. """ return self.prompt.input_variables
Input variables for this prompt template. Returns: List of input variable names.
on_llm_error
"""Run when LLM errors.""" self.step += 1 self.errors += 1
def on_llm_error(self, error: BaseException, **kwargs: Any) ->None: """Run when LLM errors.""" self.step += 1 self.errors += 1
Run when LLM errors.
_load_map_reduce_chain
map_chain = LLMChain(llm=llm, prompt=question_prompt, verbose=verbose) _reduce_llm = reduce_llm or llm reduce_chain = LLMChain(llm=_reduce_llm, prompt=combine_prompt, verbose=verbose ) combine_documents_chain = StuffDocumentsChain(llm_chain=reduce_chain, document_variable_name=combine_document_variable_name, do...
def _load_map_reduce_chain(llm: BaseLanguageModel, question_prompt: BasePromptTemplate=map_reduce_prompt.QUESTION_PROMPT, combine_prompt: BasePromptTemplate=map_reduce_prompt.COMBINE_PROMPT, document_prompt: BasePromptTemplate=map_reduce_prompt.EXAMPLE_PROMPT, combine_document_variable_name: str='summar...
null
rag_chain
""" The RAG chain :param retriever: A function that retrieves the necessary context for the model. :return: A chain of functions representing the multi-modal RAG process. """ model = ChatOpenAI(temperature=0, model='gpt-4-1106-preview', max_tokens=1024) prompt = ChatPromptTemplate.from_messages([('syst...
def rag_chain(retriever): """ The RAG chain :param retriever: A function that retrieves the necessary context for the model. :return: A chain of functions representing the multi-modal RAG process. """ model = ChatOpenAI(temperature=0, model='gpt-4-1106-preview', max_tokens=1024) pro...
The RAG chain :param retriever: A function that retrieves the necessary context for the model. :return: A chain of functions representing the multi-modal RAG process.
similarity_search
"""Return docs most similar to query.""" return []
def similarity_search(self, query: str, k: int=4, **kwargs: Any) ->List[ Document]: """Return docs most similar to query.""" return []
Return docs most similar to query.
is_lc_serializable
return True
@classmethod def is_lc_serializable(self) ->bool: return True
null
load_prompt_from_config
"""Load prompt from Config Dict.""" if '_type' not in config: logger.warning('No `_type` key found, defaulting to `prompt`.') config_type = config.pop('_type', 'prompt') if config_type not in type_to_loader_dict: raise ValueError(f'Loading {config_type} prompt not supported') prompt_loader = type_to_loader_dict...
def load_prompt_from_config(config: dict) ->BasePromptTemplate: """Load prompt from Config Dict.""" if '_type' not in config: logger.warning('No `_type` key found, defaulting to `prompt`.') config_type = config.pop('_type', 'prompt') if config_type not in type_to_loader_dict: raise Value...
Load prompt from Config Dict.
__add__
if isinstance(other, AIMessageChunk): if self.example != other.example: raise ValueError( 'Cannot concatenate AIMessageChunks with different example values.' ) return self.__class__(example=self.example, content=merge_content(self. content, other.content), additional_kwar...
def __add__(self, other: Any) ->BaseMessageChunk: if isinstance(other, AIMessageChunk): if self.example != other.example: raise ValueError( 'Cannot concatenate AIMessageChunks with different example values.' ) return self.__class__(example=self.example, co...
null
on_llm_new_token
self.on_llm_new_token_common()
def on_llm_new_token(self, *args: Any, **kwargs: Any) ->Any: self.on_llm_new_token_common()
null
lazy_import_playwright_browsers
""" Lazy import playwright browsers. Returns: Tuple[Type[AsyncBrowser], Type[SyncBrowser]]: AsyncBrowser and SyncBrowser classes. """ try: from playwright.async_api import Browser as AsyncBrowser from playwright.sync_api import Browser as SyncBrowser except ImportError: rais...
def lazy_import_playwright_browsers() ->Tuple[Type[AsyncBrowser], Type[ SyncBrowser]]: """ Lazy import playwright browsers. Returns: Tuple[Type[AsyncBrowser], Type[SyncBrowser]]: AsyncBrowser and SyncBrowser classes. """ try: from playwright.async_api import Browser ...
Lazy import playwright browsers. Returns: Tuple[Type[AsyncBrowser], Type[SyncBrowser]]: AsyncBrowser and SyncBrowser classes.
_generation_from_qwen_resp
return dict(text=resp['output']['text'], generation_info=dict(finish_reason =resp['output']['finish_reason'], request_id=resp['request_id'], token_usage=dict(resp['usage'])))
@staticmethod def _generation_from_qwen_resp(resp: Any) ->Dict[str, Any]: return dict(text=resp['output']['text'], generation_info=dict( finish_reason=resp['output']['finish_reason'], request_id=resp[ 'request_id'], token_usage=dict(resp['usage'])))
null
test_deprecated_method_pydantic
"""Test deprecated method.""" with warnings.catch_warnings(record=True) as warning_list: warnings.simplefilter('always') obj = MyModel() assert obj.deprecated_method() == 'This is a deprecated method.' assert len(warning_list) == 1 warning = warning_list[0].message assert str(warning ) =...
def test_deprecated_method_pydantic() ->None: """Test deprecated method.""" with warnings.catch_warnings(record=True) as warning_list: warnings.simplefilter('always') obj = MyModel() assert obj.deprecated_method() == 'This is a deprecated method.' assert len(warning_list) == 1 ...
Test deprecated method.
_get_relevant_documents
return [Document(page_content=query)]
def _get_relevant_documents(self, query: str) ->List[Document]: return [Document(page_content=query)]
null
__init__
super().__init__() self.runnable = evaluator_runnable
def __init__(self, evaluator_runnable: Runnable) ->None: super().__init__() self.runnable = evaluator_runnable
null
_critique
"""Critique each of the ideas from ideation stage & select best one.""" llm = self.critique_llm if self.critique_llm else self.llm prompt = self.critique_prompt().format_prompt(**self.history. critique_prompt_inputs()) callbacks = run_manager.handlers if run_manager else None if llm: critique = self._get_text_f...
def _critique(self, stop: Optional[List[str]]=None, run_manager: Optional[ CallbackManagerForChainRun]=None) ->str: """Critique each of the ideas from ideation stage & select best one.""" llm = self.critique_llm if self.critique_llm else self.llm prompt = self.critique_prompt().format_prompt(**self.hist...
Critique each of the ideas from ideation stage & select best one.
test_load_returns_list_of_documents
loader = GeoDataFrameLoader(sample_gdf) docs = loader.load() assert isinstance(docs, list) assert all(isinstance(doc, Document) for doc in docs) assert len(docs) == 2
@pytest.mark.requires('geopandas') def test_load_returns_list_of_documents(sample_gdf: GeoDataFrame) ->None: loader = GeoDataFrameLoader(sample_gdf) docs = loader.load() assert isinstance(docs, list) assert all(isinstance(doc, Document) for doc in docs) assert len(docs) == 2
null
__init__
"""Initialize with necessary components.""" warnings.warn( 'ElasticVectorSearch will be removed in a future release. SeeElasticsearch integration docs on how to upgrade.' ) try: import elasticsearch except ImportError: raise ImportError( 'Could not import elasticsearch python package. Please ins...
def __init__(self, elasticsearch_url: str, index_name: str, embedding: Embeddings, *, ssl_verify: Optional[Dict[str, Any]]=None): """Initialize with necessary components.""" warnings.warn( 'ElasticVectorSearch will be removed in a future release. SeeElasticsearch integration docs on how to upgrade.'...
Initialize with necessary components.
test_importable_all
for path in glob.glob('../core/langchain_core/*'): relative_path = Path(path).parts[-1] if relative_path.endswith('.typed'): continue module_name = relative_path.split('.')[0] module = importlib.import_module('langchain_core.' + module_name) all_ = getattr(module, '__all__', []) for cls_...
def test_importable_all() ->None: for path in glob.glob('../core/langchain_core/*'): relative_path = Path(path).parts[-1] if relative_path.endswith('.typed'): continue module_name = relative_path.split('.')[0] module = importlib.import_module('langchain_core.' + module_na...
null
__init__
"""Initialize with BOS config, bucket and prefix. :param conf(BosConfig): BOS config. :param bucket(str): BOS bucket. :param prefix(str): prefix. """ self.conf = conf self.bucket = bucket self.prefix = prefix
def __init__(self, conf: Any, bucket: str, prefix: str=''): """Initialize with BOS config, bucket and prefix. :param conf(BosConfig): BOS config. :param bucket(str): BOS bucket. :param prefix(str): prefix. """ self.conf = conf self.bucket = bucket self.prefix = prefix
Initialize with BOS config, bucket and prefix. :param conf(BosConfig): BOS config. :param bucket(str): BOS bucket. :param prefix(str): prefix.
validate_prompt
"""Validate that prompt matches format.""" prompt = values['llm_chain'].prompt if 'agent_scratchpad' not in prompt.input_variables: logger.warning( '`agent_scratchpad` should be a variable in prompt.input_variables. Did not find it, so adding it at the end.' ) prompt.input_variables.append('agen...
@root_validator() def validate_prompt(cls, values: Dict) ->Dict: """Validate that prompt matches format.""" prompt = values['llm_chain'].prompt if 'agent_scratchpad' not in prompt.input_variables: logger.warning( '`agent_scratchpad` should be a variable in prompt.input_variables. Did not...
Validate that prompt matches format.
test__convert_message_to_dict_function
message = FunctionMessage(name='foo', content='bar') with pytest.raises(TypeError) as e: _convert_message_to_dict(message) assert 'Got unknown type' in str(e)
def test__convert_message_to_dict_function() ->None: message = FunctionMessage(name='foo', content='bar') with pytest.raises(TypeError) as e: _convert_message_to_dict(message) assert 'Got unknown type' in str(e)
null
__init__
"""Initialize with supabase client.""" try: import supabase except ImportError: raise ImportError( 'Could not import supabase python package. Please install it with `pip install supabase`.' ) self._client = client self._embedding: Embeddings = embedding self.table_name = table_name or 'documents...
def __init__(self, client: supabase.client.Client, embedding: Embeddings, table_name: str, chunk_size: int=500, query_name: Union[str, None]=None ) ->None: """Initialize with supabase client.""" try: import supabase except ImportError: raise ImportError( 'Could not import...
Initialize with supabase client.
test_csv_loader_load_empty_file
file_path = self._get_csv_file_path('test_empty.csv') expected_docs: list = [] loader = CSVLoader(file_path=file_path) result = loader.load() assert result == expected_docs
def test_csv_loader_load_empty_file(self) ->None: file_path = self._get_csv_file_path('test_empty.csv') expected_docs: list = [] loader = CSVLoader(file_path=file_path) result = loader.load() assert result == expected_docs
null
process_attachment
try: from PIL import Image except ImportError: raise ImportError( '`Pillow` package not found, please run `pip install Pillow`') attachments = self.confluence.get_attachments_from_content(page_id)['results'] texts = [] for attachment in attachments: media_type = attachment['metadata']['mediaType'] ...
def process_attachment(self, page_id: str, ocr_languages: Optional[str]=None ) ->List[str]: try: from PIL import Image except ImportError: raise ImportError( '`Pillow` package not found, please run `pip install Pillow`') attachments = self.confluence.get_attachments_from_cont...
null
test_specifying_parser_via_class_attribute
class TextLoader(GenericLoader): """Parser created for testing purposes.""" @staticmethod def get_parser(**kwargs: Any) ->BaseBlobParser: return TextParser() loader = TextLoader.from_filesystem(toy_dir, suffixes=['.txt']) docs = loader.load() assert len(docs) == 3 assert docs[0].page_content == 'Th...
def test_specifying_parser_via_class_attribute(toy_dir: str) ->None: class TextLoader(GenericLoader): """Parser created for testing purposes.""" @staticmethod def get_parser(**kwargs: Any) ->BaseBlobParser: return TextParser() loader = TextLoader.from_filesystem(toy_dir, s...
null
_anonymize
"""Anonymize text. Each PII entity is replaced with a fake value. Each time fake values will be different, as they are generated randomly. PresidioAnonymizer has no built-in memory - so it will not remember the effects of anonymizing previous texts. >>> anonymizer = PresidioAnon...
def _anonymize(self, text: str, language: Optional[str]=None, allow_list: Optional[List[str]]=None) ->str: """Anonymize text. Each PII entity is replaced with a fake value. Each time fake values will be different, as they are generated randomly. PresidioAnonymizer has no built-in memory...
Anonymize text. Each PII entity is replaced with a fake value. Each time fake values will be different, as they are generated randomly. PresidioAnonymizer has no built-in memory - so it will not remember the effects of anonymizing previous texts. >>> anonymizer = PresidioAnonymizer() >>> anonymizer.anonymize("My name ...
import_jsonformer
"""Lazily import jsonformer.""" try: import jsonformer except ImportError: raise ImportError( 'Could not import jsonformer python package. Please install it with `pip install jsonformer`.' ) return jsonformer
def import_jsonformer() ->jsonformer: """Lazily import jsonformer.""" try: import jsonformer except ImportError: raise ImportError( 'Could not import jsonformer python package. Please install it with `pip install jsonformer`.' ) return jsonformer
Lazily import jsonformer.
test_tracing_sequential
from langchain.agents import AgentType, initialize_agent, load_tools os.environ['LANGCHAIN_TRACING'] = 'true' for q in questions[:3]: llm = OpenAI(temperature=0) tools = load_tools(['llm-math', 'serpapi'], llm=llm) agent = initialize_agent(tools, llm, agent=AgentType. ZERO_SHOT_REACT_DESCRIPTION, ve...
def test_tracing_sequential() ->None: from langchain.agents import AgentType, initialize_agent, load_tools os.environ['LANGCHAIN_TRACING'] = 'true' for q in questions[:3]: llm = OpenAI(temperature=0) tools = load_tools(['llm-math', 'serpapi'], llm=llm) agent = initialize_agent(tools,...
null
test_cohere_embedding_documents
"""Test cohere embeddings.""" documents = ['foo bar'] embedding = CohereEmbeddings() output = embedding.embed_documents(documents) assert len(output) == 1 assert len(output[0]) == 2048
def test_cohere_embedding_documents() ->None: """Test cohere embeddings.""" documents = ['foo bar'] embedding = CohereEmbeddings() output = embedding.embed_documents(documents) assert len(output) == 1 assert len(output[0]) == 2048
Test cohere embeddings.
__str__
return str(self.value)
def __str__(self) ->str: return str(self.value)
null
random_name
"""Generate a random name.""" adjective = random.choice(adjectives) noun = random.choice(nouns) number = random.randint(1, 100) return f'{adjective}-{noun}-{number}'
def random_name() ->str: """Generate a random name.""" adjective = random.choice(adjectives) noun = random.choice(nouns) number = random.randint(1, 100) return f'{adjective}-{noun}-{number}'
Generate a random name.
_call
outputs = {} for var in self.output_variables: variables = [inputs[k] for k in self.input_variables] outputs[var] = f"{' '.join(variables)}foo" return outputs
def _call(self, inputs: Dict[str, str], run_manager: Optional[ CallbackManagerForChainRun]=None) ->Dict[str, str]: outputs = {} for var in self.output_variables: variables = [inputs[k] for k in self.input_variables] outputs[var] = f"{' '.join(variables)}foo" return outputs
null
consolidate_updates
self.vector_index = self.vector_index.consolidate_updates(**kwargs)
def consolidate_updates(self, **kwargs: Any) ->None: self.vector_index = self.vector_index.consolidate_updates(**kwargs)
null
test_watsonxllm_call
watsonxllm = WatsonxLLM(model_id='google/flan-ul2', url= 'https://us-south.ml.cloud.ibm.com', apikey='***', project_id='***') response = watsonxllm('What color sunflower is?') assert isinstance(response, str)
def test_watsonxllm_call() ->None: watsonxllm = WatsonxLLM(model_id='google/flan-ul2', url= 'https://us-south.ml.cloud.ibm.com', apikey='***', project_id='***') response = watsonxllm('What color sunflower is?') assert isinstance(response, str)
null
run
"""Run the tool.""" parsed_input = self._parse_input(tool_input) if not self.verbose and verbose is not None: verbose_ = verbose else: verbose_ = self.verbose callback_manager = CallbackManager.configure(callbacks, self.callbacks, verbose_, tags, self.tags, metadata, self.metadata) new_arg_supported = signa...
def run(self, tool_input: Union[str, Dict], verbose: Optional[bool]=None, start_color: Optional[str]='green', color: Optional[str]='green', callbacks: Callbacks=None, *, tags: Optional[List[str]]=None, metadata: Optional[Dict[str, Any]]=None, run_name: Optional[str]=None, **kwargs: Any ) ->Any: """R...
Run the tool.
test_json_equality_evaluator_requires_input
assert json_equality_evaluator.requires_input is False
def test_json_equality_evaluator_requires_input(json_equality_evaluator: JsonEqualityEvaluator) ->None: assert json_equality_evaluator.requires_input is False
null
embeddings
return self.embedding_function
@property def embeddings(self) ->Embeddings: return self.embedding_function
null
test_all_imports
assert set(__all__) == set(EXPECTED_ALL)
def test_all_imports() ->None: assert set(__all__) == set(EXPECTED_ALL)
null
__init__
""" Initializes the AssemblyAI AudioTranscriptLoader. Args: file_path: An URL or a local file path. transcript_format: Transcript format to use. See class ``TranscriptFormat`` for more info. config: Transcription options and features. If ``None`` is g...
def __init__(self, file_path: str, *, transcript_format: TranscriptFormat= TranscriptFormat.TEXT, config: Optional[assemblyai.TranscriptionConfig] =None, api_key: Optional[str]=None): """ Initializes the AssemblyAI AudioTranscriptLoader. Args: file_path: An URL or a local file p...
Initializes the AssemblyAI AudioTranscriptLoader. Args: file_path: An URL or a local file path. transcript_format: Transcript format to use. See class ``TranscriptFormat`` for more info. config: Transcription options and features. If ``None`` is given, the Transcriber's default configuratio...
test_ToSelectFrom_not_a_list_throws
llm, PROMPT = setup() chain = pick_best_chain.PickBest.from_llm(llm=llm, prompt=PROMPT, feature_embedder=pick_best_chain.PickBestFeatureEmbedder(auto_embed= False, model=MockEncoder())) actions = {'actions': ['0', '1', '2']} with pytest.raises(ValueError): chain.run(User=rl_chain.BasedOn('Context'), action=...
@pytest.mark.requires('vowpal_wabbit_next', 'sentence_transformers') def test_ToSelectFrom_not_a_list_throws() ->None: llm, PROMPT = setup() chain = pick_best_chain.PickBest.from_llm(llm=llm, prompt=PROMPT, feature_embedder=pick_best_chain.PickBestFeatureEmbedder(auto_embed =False, model=MockEnc...
null
_prepare_query
params_dict: Dict[str, Union[str, bytes, float]] = {'vector': _array_to_buffer(query_embedding, self._schema.vector_dtype)} return_fields = [self._schema.content_key] if with_distance: return_fields.append('distance') if with_metadata: return_fields.extend(self._schema.metadata_keys) if distance_threshold: ...
def _prepare_query(self, query_embedding: List[float], k: int=4, filter: Optional[RedisFilterExpression]=None, distance_threshold: Optional[ float]=None, with_metadata: bool=True, with_distance: bool=False) ->Tuple[ 'Query', Dict[str, Any]]: params_dict: Dict[str, Union[str, bytes, float]] = {'vector': ...
null
get_schema
""" Returns the schema of the graph database. """ return self.schema
@property def get_schema(self) ->str: """ Returns the schema of the graph database. """ return self.schema
Returns the schema of the graph database.
_import_azure_cognitive_services_AzureCogsFormRecognizerTool
from langchain_community.tools.azure_cognitive_services import AzureCogsFormRecognizerTool return AzureCogsFormRecognizerTool
def _import_azure_cognitive_services_AzureCogsFormRecognizerTool() ->Any: from langchain_community.tools.azure_cognitive_services import AzureCogsFormRecognizerTool return AzureCogsFormRecognizerTool
null
embed_image
try: from PIL import Image as _PILImage except ImportError: raise ImportError('Please install the PIL library: pip install pillow') pil_images = [_PILImage.open(uri) for uri in uris] image_features = [] for pil_image in pil_images: preprocessed_image = self.preprocess(pil_image).unsqueeze(0) embeddings_...
def embed_image(self, uris: List[str]) ->List[List[float]]: try: from PIL import Image as _PILImage except ImportError: raise ImportError('Please install the PIL library: pip install pillow') pil_images = [_PILImage.open(uri) for uri in uris] image_features = [] for pil_image in pil_...
null
test_update_with_delayed_score
llm, PROMPT = setup() chain = pick_best_chain.PickBest.from_llm(llm=llm, prompt=PROMPT, selection_scorer=None, feature_embedder=pick_best_chain. PickBestFeatureEmbedder(auto_embed=False, model=MockEncoder())) actions = ['0', '1', '2'] response = chain.run(User=rl_chain.BasedOn('Context'), action=rl_chain. T...
@pytest.mark.requires('vowpal_wabbit_next', 'sentence_transformers') def test_update_with_delayed_score() ->None: llm, PROMPT = setup() chain = pick_best_chain.PickBest.from_llm(llm=llm, prompt=PROMPT, selection_scorer=None, feature_embedder=pick_best_chain. PickBestFeatureEmbedder(auto_embed=Fa...
null
_llm_type
"""Return type of llm.""" return 'gooseai'
@property def _llm_type(self) ->str: """Return type of llm.""" return 'gooseai'
Return type of llm.
from_documents
"""Create an Epsilla vectorstore from a list of documents. Args: texts (List[str]): List of text data to be inserted. embeddings (Embeddings): Embedding function. client (pyepsilla.vectordb.Client): Epsilla client to connect to. metadatas (Optional[List[dict]]): ...
@classmethod def from_documents(cls: Type[Epsilla], documents: List[Document], embedding: Embeddings, client: Any=None, db_path: Optional[str]= _LANGCHAIN_DEFAULT_DB_PATH, db_name: Optional[str]= _LANGCHAIN_DEFAULT_DB_NAME, collection_name: Optional[str]= _LANGCHAIN_DEFAULT_TABLE_NAME, drop_old: Optiona...
Create an Epsilla vectorstore from a list of documents. Args: texts (List[str]): List of text data to be inserted. embeddings (Embeddings): Embedding function. client (pyepsilla.vectordb.Client): Epsilla client to connect to. metadatas (Optional[List[dict]]): Metadata for each text. Default...
_import_docarray_inmemory
from langchain_community.vectorstores.docarray import DocArrayInMemorySearch return DocArrayInMemorySearch
def _import_docarray_inmemory() ->Any: from langchain_community.vectorstores.docarray import DocArrayInMemorySearch return DocArrayInMemorySearch
null
combine_docs
"""Combine by mapping first chain over all, then stuffing into final chain. Args: docs: List of documents to combine callbacks: Callbacks to be passed through **kwargs: additional parameters to be passed to LLM calls (like other input variables besides the do...
def combine_docs(self, docs: List[Document], callbacks: Callbacks=None, ** kwargs: Any) ->Tuple[str, dict]: """Combine by mapping first chain over all, then stuffing into final chain. Args: docs: List of documents to combine callbacks: Callbacks to be passed through ...
Combine by mapping first chain over all, then stuffing into final chain. Args: docs: List of documents to combine callbacks: Callbacks to be passed through **kwargs: additional parameters to be passed to LLM calls (like other input variables besides the documents) Returns: The first element re...
test_parse_input
input_text = '{"url": "https://example.com", "data": {"key": "value"}}' expected_output = {'url': 'https://example.com', 'data': {'key': 'value'}} assert _parse_input(input_text) == expected_output
def test_parse_input() ->None: input_text = '{"url": "https://example.com", "data": {"key": "value"}}' expected_output = {'url': 'https://example.com', 'data': {'key': 'value'}} assert _parse_input(input_text) == expected_output
null
add_texts
"""Insert more texts through the embeddings and add to the VectorStore. Args: texts: Iterable of strings to add to the VectorStore. ids: Optional list of ids to associate with the texts. batch_size: Batch size of insertion metadata: Optional column data to be ins...
def add_texts(self, texts: Iterable[str], metadatas: Optional[List[dict]]= None, batch_size: int=32, ids: Optional[Iterable[str]]=None, **kwargs: Any ) ->List[str]: """Insert more texts through the embeddings and add to the VectorStore. Args: texts: Iterable of strings to add to the Vec...
Insert more texts through the embeddings and add to the VectorStore. Args: texts: Iterable of strings to add to the VectorStore. ids: Optional list of ids to associate with the texts. batch_size: Batch size of insertion metadata: Optional column data to be inserted Returns: List of ids from adding...
__add__
"""Combine two prompt templates. Args: other: Another prompt template. Returns: Combined prompt template. """ if isinstance(other, ChatPromptTemplate): return ChatPromptTemplate(messages=self.messages + other.messages) elif isinstance(other, (BaseMessagePromptTempla...
def __add__(self, other: Any) ->ChatPromptTemplate: """Combine two prompt templates. Args: other: Another prompt template. Returns: Combined prompt template. """ if isinstance(other, ChatPromptTemplate): return ChatPromptTemplate(messages=self.messages +...
Combine two prompt templates. Args: other: Another prompt template. Returns: Combined prompt template.
_iterate_files
"""Iterate over files in a directory or zip file. Args: path (str): Path to the directory or zip file. Yields: str: Path to each file. """ if os.path.isfile(path) and path.endswith(('.html', '.json')): yield path elif os.path.isdir(path): for root, _, files in o...
def _iterate_files(self, path: str) ->Iterator[str]: """Iterate over files in a directory or zip file. Args: path (str): Path to the directory or zip file. Yields: str: Path to each file. """ if os.path.isfile(path) and path.endswith(('.html', '.json')): ...
Iterate over files in a directory or zip file. Args: path (str): Path to the directory or zip file. Yields: str: Path to each file.
test_invalid_initialization
with pytest.raises(ValueError): GitHubIssuesLoader(invalid='parameter') with pytest.raises(ValueError): GitHubIssuesLoader(state='invalid_state') with pytest.raises(ValueError): GitHubIssuesLoader(labels='not_a_list') with pytest.raises(ValueError): GitHubIssuesLoader(since='not_a_date')
def test_invalid_initialization() ->None: with pytest.raises(ValueError): GitHubIssuesLoader(invalid='parameter') with pytest.raises(ValueError): GitHubIssuesLoader(state='invalid_state') with pytest.raises(ValueError): GitHubIssuesLoader(labels='not_a_list') with pytest.raises(V...
null
setup
collection = prepare_collection() collection.delete_many({})
@pytest.fixture(autouse=True) def setup(self) ->None: collection = prepare_collection() collection.delete_many({})
null
_import_anthropic
from langchain_community.llms.anthropic import Anthropic return Anthropic
def _import_anthropic() ->Any: from langchain_community.llms.anthropic import Anthropic return Anthropic
null
setup_class
import rockset import rockset.models assert os.environ.get('ROCKSET_API_KEY') is not None assert os.environ.get('ROCKSET_REGION') is not None api_key = os.environ.get('ROCKSET_API_KEY') region = os.environ.get('ROCKSET_REGION') if region == 'use1a1': host = rockset.Regions.use1a1 elif region == 'usw2a1': host =...
@classmethod def setup_class(cls) ->None: import rockset import rockset.models assert os.environ.get('ROCKSET_API_KEY') is not None assert os.environ.get('ROCKSET_REGION') is not None api_key = os.environ.get('ROCKSET_API_KEY') region = os.environ.get('ROCKSET_REGION') if region == 'use1a1':...
null
test_cosine
"""Test cosine distance.""" docsearch = Redis.from_texts(texts, FakeEmbeddings(), redis_url= TEST_REDIS_URL, vector_schema=cosine_schema) output = docsearch.similarity_search_with_score('far', k=2) _, score = output[1] assert score == COSINE_SCORE assert drop(docsearch.index_name)
def test_cosine(texts: List[str]) ->None: """Test cosine distance.""" docsearch = Redis.from_texts(texts, FakeEmbeddings(), redis_url= TEST_REDIS_URL, vector_schema=cosine_schema) output = docsearch.similarity_search_with_score('far', k=2) _, score = output[1] assert score == COSINE_SCORE ...
Test cosine distance.
__init__
"""Initialize with necessary components.""" try: import pyepsilla except ImportError as e: raise ImportError( 'Could not import pyepsilla python package. Please install pyepsilla package with `pip install pyepsilla`.' ) from e if not isinstance(client, pyepsilla.vectordb.Client): raise TypeE...
def __init__(self, client: Any, embeddings: Embeddings, db_path: Optional[ str]=_LANGCHAIN_DEFAULT_DB_PATH, db_name: Optional[str]= _LANGCHAIN_DEFAULT_DB_NAME): """Initialize with necessary components.""" try: import pyepsilla except ImportError as e: raise ImportError( '...
Initialize with necessary components.
_import_nasa_tool
from langchain_community.tools.nasa.tool import NasaAction return NasaAction
def _import_nasa_tool() ->Any: from langchain_community.tools.nasa.tool import NasaAction return NasaAction
null
_import_spark_sql_tool_QuerySparkSQLTool
from langchain_community.tools.spark_sql.tool import QuerySparkSQLTool return QuerySparkSQLTool
def _import_spark_sql_tool_QuerySparkSQLTool() ->Any: from langchain_community.tools.spark_sql.tool import QuerySparkSQLTool return QuerySparkSQLTool
null
get_format_instructions
return f"Select one of the following options: {', '.join(self._valid_values)}"
def get_format_instructions(self) ->str: return ( f"Select one of the following options: {', '.join(self._valid_values)}" )
null
requires_reference
return True
@property def requires_reference(self) ->bool: return True
null
embed_documents
"""Call out to Aleph Alpha's Document endpoint. Args: texts: The list of texts to embed. Returns: List of embeddings, one for each text. """ document_embeddings = [] for text in texts: document_embeddings.append(self._embed(text)) return document_embeddings
def embed_documents(self, texts: List[str]) ->List[List[float]]: """Call out to Aleph Alpha's Document endpoint. Args: texts: The list of texts to embed. Returns: List of embeddings, one for each text. """ document_embeddings = [] for text in texts: ...
Call out to Aleph Alpha's Document endpoint. Args: texts: The list of texts to embed. Returns: List of embeddings, one for each text.
chain
...
@overload def chain(func: Callable[[Input], Output]) ->Runnable[Input, Output]: ...
null
_Lambda
self.write('(') self.write('lambda ') self.dispatch(t.args) self.write(': ') self.dispatch(t.body) self.write(')')
def _Lambda(self, t): self.write('(') self.write('lambda ') self.dispatch(t.args) self.write(': ') self.dispatch(t.body) self.write(')')
null
get_lc_namespace
"""Get the namespace of the langchain object.""" return ['langchain', 'schema', 'messages']
@classmethod def get_lc_namespace(cls) ->List[str]: """Get the namespace of the langchain object.""" return ['langchain', 'schema', 'messages']
Get the namespace of the langchain object.
_skip_input_warning
"""Warning to show when input is ignored.""" return f'Ignoring input in {self.__class__.__name__}, as it is not expected.'
@property def _skip_input_warning(self) ->str: """Warning to show when input is ignored.""" return ( f'Ignoring input in {self.__class__.__name__}, as it is not expected.')
Warning to show when input is ignored.
get_req_stream
invoke_url = self._get_invoke_url(model, invoke_url) if payload.get('stream', True) is False: payload = {**payload, 'stream': True} last_inputs = {'url': invoke_url, 'headers': self.headers['stream'], 'json': payload, 'stream': True} response = self.get_session_fn().post(**last_inputs) self._try_raise(response)...
def get_req_stream(self, model: Optional[str]=None, payload: dict={}, invoke_url: Optional[str]=None, stop: Optional[Sequence[str]]=None ) ->Iterator: invoke_url = self._get_invoke_url(model, invoke_url) if payload.get('stream', True) is False: payload = {**payload, 'stream': True} last_inpu...
null
_get_relevant_documents
try: from tavily import Client except ImportError: raise ImportError( 'Tavily python package not found. Please install it with `pip install tavily-python`.' ) tavily = Client(api_key=self.api_key or os.environ['TAVILY_API_KEY']) max_results = self.k if not self.include_generated_answer else self...
def _get_relevant_documents(self, query: str, *, run_manager: CallbackManagerForRetrieverRun) ->List[Document]: try: from tavily import Client except ImportError: raise ImportError( 'Tavily python package not found. Please install it with `pip install tavily-python`.' ...
null
ApproxRetrievalStrategy
"""Used to perform approximate nearest neighbor search using the HNSW algorithm. At build index time, this strategy will create a dense vector field in the index and store the embedding vectors in the index. At query time, the text will either be embedded using the prov...
@staticmethod def ApproxRetrievalStrategy(query_model_id: Optional[str]=None, hybrid: Optional[bool]=False, rrf: Optional[Union[dict, bool]]=True ) ->'ApproxRetrievalStrategy': """Used to perform approximate nearest neighbor search using the HNSW algorithm. At build index time, this strateg...
Used to perform approximate nearest neighbor search using the HNSW algorithm. At build index time, this strategy will create a dense vector field in the index and store the embedding vectors in the index. At query time, the text will either be embedded using the provided embedding function or the query_model_id will ...
lookup
"""Look up the cache data. First, retrieve the corresponding cache object using the `llm_string` parameter, and then retrieve the data from the cache based on the `prompt`. """ from gptcache.adapter.api import get _gptcache = self._get_gptcache(llm_string) res = get(prompt, cache_obj=_gptcache) ...
def lookup(self, prompt: str, llm_string: str) ->Optional[RETURN_VAL_TYPE]: """Look up the cache data. First, retrieve the corresponding cache object using the `llm_string` parameter, and then retrieve the data from the cache based on the `prompt`. """ from gptcache.adapter.api import ge...
Look up the cache data. First, retrieve the corresponding cache object using the `llm_string` parameter, and then retrieve the data from the cache based on the `prompt`.
test_normal_output_parsing
_test_convo_output(""" Action: my_action Action Input: my action input """, 'my_action', 'my action input')
def test_normal_output_parsing() ->None: _test_convo_output('\nAction: my_action\nAction Input: my action input\n', 'my_action', 'my action input')
null
from_documents
"""Create a Cassandra vectorstore from a document list. No support for specifying text IDs Returns: a Cassandra vectorstore. """ texts = [doc.page_content for doc in documents] metadatas = [doc.metadata for doc in documents] session: Session = kwargs['session'] keyspace: str = kwar...
@classmethod def from_documents(cls: Type[CVST], documents: List[Document], embedding: Embeddings, batch_size: int=16, **kwargs: Any) ->CVST: """Create a Cassandra vectorstore from a document list. No support for specifying text IDs Returns: a Cassandra vectorstore. """ ...
Create a Cassandra vectorstore from a document list. No support for specifying text IDs Returns: a Cassandra vectorstore.
test_exact_matching_with_ignore_case
prediction = 'Mindy is the CTO' reference = 'mindy is the cto' result = exact_match_string_evaluator_ignore_case.evaluate_strings(prediction =prediction, reference=reference) assert result['score'] == 1.0 reference = 'mindy is the CEO' result = exact_match_string_evaluator_ignore_case.evaluate_strings(prediction ...
def test_exact_matching_with_ignore_case( exact_match_string_evaluator_ignore_case: ExactMatchStringEvaluator ) ->None: prediction = 'Mindy is the CTO' reference = 'mindy is the cto' result = exact_match_string_evaluator_ignore_case.evaluate_strings( prediction=prediction, reference=referenc...
null
_llm_type
"""Return type of llm.""" return 'writer'
@property def _llm_type(self) ->str: """Return type of llm.""" return 'writer'
Return type of llm.
get_lc_namespace
"""Get the namespace of the langchain object.""" return ['langchain', 'schema', 'runnable']
@classmethod def get_lc_namespace(cls) ->List[str]: """Get the namespace of the langchain object.""" return ['langchain', 'schema', 'runnable']
Get the namespace of the langchain object.
on_chain_start
"""Run when chain starts running.""" self.metrics['step'] += 1 self.metrics['chain_starts'] += 1 self.metrics['starts'] += 1 chain_starts = self.metrics['chain_starts'] resp: Dict[str, Any] = {} resp.update({'action': 'on_chain_start'}) resp.update(flatten_dict(serialized)) resp.update(self.metrics) chain_input = ','.j...
def on_chain_start(self, serialized: Dict[str, Any], inputs: Dict[str, Any], **kwargs: Any) ->None: """Run when chain starts running.""" self.metrics['step'] += 1 self.metrics['chain_starts'] += 1 self.metrics['starts'] += 1 chain_starts = self.metrics['chain_starts'] resp: Dict[str, Any] = ...
Run when chain starts running.
test_failure_no_ticker
"""Test that the tool fails.""" tool = YahooFinanceNewsTool() query = '' result = tool.run(query) assert f'Company ticker {query} not found.' in result
def test_failure_no_ticker() ->None: """Test that the tool fails.""" tool = YahooFinanceNewsTool() query = '' result = tool.run(query) assert f'Company ticker {query} not found.' in result
Test that the tool fails.
_create_session_analysis_df
"""Create a dataframe with all the information from the session.""" pd = import_pandas() on_llm_start_records_df = pd.DataFrame(self.records['on_llm_start_records']) on_llm_end_records_df = pd.DataFrame(self.records['on_llm_end_records']) llm_input_columns = ['step', 'prompt'] if 'name' in on_llm_start_records_df.colum...
def _create_session_analysis_df(self) ->Any: """Create a dataframe with all the information from the session.""" pd = import_pandas() on_llm_start_records_df = pd.DataFrame(self.records['on_llm_start_records'] ) on_llm_end_records_df = pd.DataFrame(self.records['on_llm_end_records']) llm_inp...
Create a dataframe with all the information from the session.
test_timescalevector_relevance_score
"""Test to make sure the relevance score is scaled to 0-1.""" texts = ['foo', 'bar', 'baz'] metadatas = [{'page': str(i)} for i in range(len(texts))] docsearch = TimescaleVector.from_texts(texts=texts, collection_name= 'test_collection', embedding=FakeEmbeddingsWithAdaDimension(), metadatas=metadatas, service_u...
def test_timescalevector_relevance_score() ->None: """Test to make sure the relevance score is scaled to 0-1.""" texts = ['foo', 'bar', 'baz'] metadatas = [{'page': str(i)} for i in range(len(texts))] docsearch = TimescaleVector.from_texts(texts=texts, collection_name= 'test_collection', embeddi...
Test to make sure the relevance score is scaled to 0-1.
_get_encoding_model
tiktoken_ = _import_tiktoken() if self.tiktoken_model_name is not None: model = self.tiktoken_model_name else: model = self.model_name try: encoding = tiktoken_.encoding_for_model('gpt-3.5-turbo-0301') except KeyError: logger.warning('Warning: model not found. Using cl100k_base encoding.') model = '...
def _get_encoding_model(self) ->tuple[str, tiktoken.Encoding]: tiktoken_ = _import_tiktoken() if self.tiktoken_model_name is not None: model = self.tiktoken_model_name else: model = self.model_name try: encoding = tiktoken_.encoding_for_model('gpt-3.5-turbo-0301') except KeyE...
null
_bulk_ingest_embeddings
"""Bulk Ingest Embeddings into given index.""" if not mapping: mapping = dict() bulk = _import_bulk() not_found_error = _import_not_found_error() requests = [] return_ids = [] mapping = mapping try: client.indices.get(index=index_name) except not_found_error: client.indices.create(index=index_name, body=map...
def _bulk_ingest_embeddings(client: Any, index_name: str, embeddings: List[ List[float]], texts: Iterable[str], metadatas: Optional[List[dict]]= None, ids: Optional[List[str]]=None, vector_field: str='vector_field', text_field: str='text', mapping: Optional[Dict]=None, max_chunk_bytes: Optional[int]=1 *...
Bulk Ingest Embeddings into given index.
test_loading_flawed_xml
loader = MWDumpLoader((PARENT_DIR / 'mwtest_current_pages.xml').absolute()) with pytest.raises(TypeError): loader.load()
@pytest.mark.requires('mwparserfromhell', 'mwxml') def test_loading_flawed_xml() ->None: loader = MWDumpLoader((PARENT_DIR / 'mwtest_current_pages.xml').absolute()) with pytest.raises(TypeError): loader.load()
null
test_readonly_memory
read_only_memory = ReadOnlySharedMemory(memory=memory) memory.save_context({'input': 'bar'}, {'output': 'foo'}) assert read_only_memory.load_memory_variables({} ) == memory.load_memory_variables({})
@pytest.mark.parametrize('memory', [ConversationBufferMemory(memory_key= 'baz'), ConversationSummaryMemory(llm=FakeLLM(), memory_key='baz'), ConversationBufferWindowMemory(memory_key='baz')]) def test_readonly_memory(memory: BaseMemory) ->None: read_only_memory = ReadOnlySharedMemory(memory=memory) memo...
null
load_memory_variables
return self.memories
def load_memory_variables(self, inputs: Dict[str, Any]) ->Dict[str, str]: return self.memories
null
_stream_callback
"""Add streamed result to queue.""" if error: result_queue.put(error) else: response_raw: dict = result.get_response(as_json=True) if 'outputs' in response_raw: response = self._process_result(response_raw) if response in stop_words: result_queue.put(None) else: ...
def _stream_callback(self, result_queue: queue.Queue[Union[Optional[Dict[ str, str]], str]], result: grpcclient.InferResult, error: str, stop_words: List[str]) ->None: """Add streamed result to queue.""" if error: result_queue.put(error) else: response_raw: dict = result.get_response...
Add streamed result to queue.
_default_params
"""Get the default parameters for calling Cohere API.""" return {'max_new_tokens': self.max_new_tokens, 'temperature': self. temperature, 'top_k': self.top_k, 'top_p': self.top_p, 'stop_sequences': [], 'do_sample': self.do_sample, 'use_cache': self.use_cache}
@property def _default_params(self) ->Dict[str, Any]: """Get the default parameters for calling Cohere API.""" return {'max_new_tokens': self.max_new_tokens, 'temperature': self. temperature, 'top_k': self.top_k, 'top_p': self.top_p, 'stop_sequences': [], 'do_sample': self.do_sample, 'use_cache'...
Get the default parameters for calling Cohere API.
_import_edenai
from langchain_community.llms.edenai import EdenAI return EdenAI
def _import_edenai() ->Any: from langchain_community.llms.edenai import EdenAI return EdenAI
null