method_name
stringlengths
1
78
method_body
stringlengths
3
9.66k
full_code
stringlengths
31
10.7k
docstring
stringlengths
4
4.74k
format_messages
"""Format messages from kwargs. Args: **kwargs: Keyword arguments to use for formatting. Returns: List of BaseMessage. """ value = kwargs.get(self.variable_name, []) if self.optional else kwargs[self .variable_name] if not isinstance(value, list): raise ValueErr...
def format_messages(self, **kwargs: Any) ->List[BaseMessage]: """Format messages from kwargs. Args: **kwargs: Keyword arguments to use for formatting. Returns: List of BaseMessage. """ value = kwargs.get(self.variable_name, []) if self.optional else kwargs[ ...
Format messages from kwargs. Args: **kwargs: Keyword arguments to use for formatting. Returns: List of BaseMessage.
_run
return f'{arg1} {arg2} {arg3}'
def _run(self, arg1: int, arg2: bool, arg3: Optional[dict]=None) ->str: return f'{arg1} {arg2} {arg3}'
null
lazy_load
"""Load Couchbase data into Document objects lazily.""" from datetime import timedelta self.cluster.wait_until_ready(timedelta(seconds=5)) result = self.cluster.query(self.query) for row in result: metadata_fields = self.metadata_fields page_content_fields = self.page_content_fields if not page_content_fiel...
def lazy_load(self) ->Iterator[Document]: """Load Couchbase data into Document objects lazily.""" from datetime import timedelta self.cluster.wait_until_ready(timedelta(seconds=5)) result = self.cluster.query(self.query) for row in result: metadata_fields = self.metadata_fields page_...
Load Couchbase data into Document objects lazily.
_run
mailbox = self.account.mailbox() if folder != '': mailbox = mailbox.get_folder(folder_name=folder) query = mailbox.q().search(query) messages = mailbox.get_messages(limit=max_results, query=query) output_messages = [] for message in messages: output_message = {} output_message['from'] = message.sender i...
def _run(self, query: str, folder: str='', max_results: int=10, truncate: bool=True, run_manager: Optional[CallbackManagerForToolRun]=None, truncate_limit: int=150) ->List[Dict[str, Any]]: mailbox = self.account.mailbox() if folder != '': mailbox = mailbox.get_folder(folder_name=folder) quer...
null
get_num_tokens
"""Return number of tokens in text.""" return len(text.split())
def get_num_tokens(self, text: str) ->int: """Return number of tokens in text.""" return len(text.split())
Return number of tokens in text.
_config
config = ensure_config(config) if config.get('run_name') is None: try: run_name = callable.__name__ except AttributeError: run_name = None if run_name is not None: return patch_config(config, run_name=run_name) return config
def _config(self, config: Optional[RunnableConfig], callable: Callable[..., Any]) ->RunnableConfig: config = ensure_config(config) if config.get('run_name') is None: try: run_name = callable.__name__ except AttributeError: run_name = None if run_name is not No...
null
format_docs
return '\n\n'.join([d.page_content for d in docs])
def format_docs(docs): return '\n\n'.join([d.page_content for d in docs])
null
embed_documents
"""Call out to HuggingFaceHub's embedding endpoint for embedding search docs. Args: texts: The list of texts to embed. Returns: List of embeddings, one for each text. """ texts = [text.replace('\n', ' ') for text in texts] _model_kwargs = self.model_kwargs or {} respons...
def embed_documents(self, texts: List[str]) ->List[List[float]]: """Call out to HuggingFaceHub's embedding endpoint for embedding search docs. Args: texts: The list of texts to embed. Returns: List of embeddings, one for each text. """ texts = [text.replace('\n'...
Call out to HuggingFaceHub's embedding endpoint for embedding search docs. Args: texts: The list of texts to embed. Returns: List of embeddings, one for each text.
node_data
if isinstance(node.data, Runnable): try: data = str(node.data) if data.startswith('<') or data[0] != data[0].upper() or len(data. splitlines()) > 1: data = node.data.__class__.__name__ elif len(data) > 42: data = data[:42] + '...' except Exception: ...
def node_data(node: Node) ->str: if isinstance(node.data, Runnable): try: data = str(node.data) if data.startswith('<') or data[0] != data[0].upper() or len(data .splitlines()) > 1: data = node.data.__class__.__name__ elif len(data) > 42: ...
null
_JoinedStr
self.write('f') string = io.StringIO() self._fstring_JoinedStr(t, string.write) self.write(repr(string.getvalue()))
def _JoinedStr(self, t): self.write('f') string = io.StringIO() self._fstring_JoinedStr(t, string.write) self.write(repr(string.getvalue()))
null
knn_search
""" Perform a k-NN search on the Elasticsearch index. Args: query (str, optional): The query text to search for. k (int, optional): The number of nearest neighbors to return. query_vector (List[float], optional): The query vector to search for. model_id (...
def knn_search(self, query: Optional[str]=None, k: Optional[int]=10, query_vector: Optional[List[float]]=None, model_id: Optional[str]=None, size: Optional[int]=10, source: Optional[bool]=True, fields: Optional[ Union[List[Mapping[str, Any]], Tuple[Mapping[str, Any], ...], None]]= None, page_content: Op...
Perform a k-NN search on the Elasticsearch index. Args: query (str, optional): The query text to search for. k (int, optional): The number of nearest neighbors to return. query_vector (List[float], optional): The query vector to search for. model_id (str, optional): The ID of the model to use for trans...
_invoke
assert isinstance(input, dict ), 'The input to RunnablePassthrough.assign() must be a dict.' return {**input, **self.mapper.invoke(input, patch_config(config, callbacks =run_manager.get_child()), **kwargs)}
def _invoke(self, input: Dict[str, Any], run_manager: CallbackManagerForChainRun, config: RunnableConfig, **kwargs: Any) ->Dict[ str, Any]: assert isinstance(input, dict ), 'The input to RunnablePassthrough.assign() must be a dict.' return {**input, **self.mapper.invoke(input, patch_config(confi...
null
test_get_nfts_invalid_contract
contract_address = '0x111D4e82EA7eCA7F62c3fdf6D39A541be95Bf111' with pytest.raises(ValueError) as error_NoNfts: BlockchainDocumentLoader(contract_address).load() assert str(error_NoNfts.value ) == 'No NFTs found for contract address ' + contract_address
@pytest.mark.skipif(not alchemyKeySet, reason='Alchemy API key not provided.') def test_get_nfts_invalid_contract() ->None: contract_address = '0x111D4e82EA7eCA7F62c3fdf6D39A541be95Bf111' with pytest.raises(ValueError) as error_NoNfts: BlockchainDocumentLoader(contract_address).load() assert str(err...
null
test_result_with_params_call
"""Test that call gives the correct answer with extra params.""" search = GoogleSearchAPIWrapper() output = search.results(query="What was Obama's first name?", num_results=5, search_params={'cr': 'us', 'safe': 'active'}) assert len(output)
def test_result_with_params_call() ->None: """Test that call gives the correct answer with extra params.""" search = GoogleSearchAPIWrapper() output = search.results(query="What was Obama's first name?", num_results=5, search_params={'cr': 'us', 'safe': 'active'}) assert len(output)
Test that call gives the correct answer with extra params.
_combine_llm_outputs
if llm_outputs[0] is None: return {} return llm_outputs[0]
def _combine_llm_outputs(self, llm_outputs: List[Optional[dict]]) ->dict: if llm_outputs[0] is None: return {} return llm_outputs[0]
null
__init__
try: import openllm except ImportError as e: raise ImportError( "Could not import openllm. Make sure to install it with 'pip install openllm.'" ) from e llm_kwargs = llm_kwargs or {} if server_url is not None: logger.debug("'server_url' is provided, returning a openllm.Client") assert mo...
def __init__(self, model_name: Optional[str]=None, *, model_id: Optional[ str]=None, server_url: Optional[str]=None, server_type: Literal['grpc', 'http']='http', embedded: bool=True, **llm_kwargs: Any): try: import openllm except ImportError as e: raise ImportError( "Could no...
null
test_init_with_pipeline
"""Test initialization with a HF pipeline.""" from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline model_id = 'gpt2' tokenizer = AutoTokenizer.from_pretrained(model_id) model = AutoModelForCausalLM.from_pretrained(model_id) pipe = pipeline('text-generation', model=model, tokenizer=tokenizer, max_n...
def test_init_with_pipeline() ->None: """Test initialization with a HF pipeline.""" from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline model_id = 'gpt2' tokenizer = AutoTokenizer.from_pretrained(model_id) model = AutoModelForCausalLM.from_pretrained(model_id) pipe = pipeline(...
Test initialization with a HF pipeline.
_import_epsilla
from langchain_community.vectorstores.epsilla import Epsilla return Epsilla
def _import_epsilla() ->Any: from langchain_community.vectorstores.epsilla import Epsilla return Epsilla
null
test_add_documents
zep_vectorstore.add_documents(mock_documents) mock_collection.add_documents.assert_called_once_with( texts_metadatas_as_zep_documents)
@pytest.mark.requires('zep_python') def test_add_documents(zep_vectorstore: ZepVectorStore, mock_collection: 'DocumentCollection', mock_documents: List[Document], texts_metadatas_as_zep_documents: List['ZepDocument']) ->None: zep_vectorstore.add_documents(mock_documents) mock_collection.add_documents.as...
null
_collect_test_results
wait_for_all_evaluators() all_eval_results = self._collect_metrics() results = self._merge_test_outputs(batch_results, all_eval_results) return TestResult(project_name=self.project.name, results=results)
def _collect_test_results(self, batch_results: List[Union[dict, str, LLMResult, ChatResult]]) ->TestResult: wait_for_all_evaluators() all_eval_results = self._collect_metrics() results = self._merge_test_outputs(batch_results, all_eval_results) return TestResult(project_name=self.project.name, resul...
null
messages
"""Retrieve all session messages from DB""" message_blobs = self.blob_history.retrieve(self.session_id) items = [json.loads(message_blob) for message_blob in message_blobs] messages = messages_from_dict(items) return messages
@property def messages(self) ->List[BaseMessage]: """Retrieve all session messages from DB""" message_blobs = self.blob_history.retrieve(self.session_id) items = [json.loads(message_blob) for message_blob in message_blobs] messages = messages_from_dict(items) return messages
Retrieve all session messages from DB
delete_by_metadata
"""Delete by vector ID or other criteria. Args: ids: List of ids to delete. **kwargs: Other keyword arguments that subclasses might use. Returns: Optional[bool]: True if deletion is successful, False otherwise, None if not implemented. """ self.s...
def delete_by_metadata(self, filter: Union[Dict[str, str], List[Dict[str, str]]], **kwargs: Any) ->Optional[bool]: """Delete by vector ID or other criteria. Args: ids: List of ids to delete. **kwargs: Other keyword arguments that subclasses might use. Returns: ...
Delete by vector ID or other criteria. Args: ids: List of ids to delete. **kwargs: Other keyword arguments that subclasses might use. Returns: Optional[bool]: True if deletion is successful, False otherwise, None if not implemented.
test_connect_file_rdf
""" Test loading online resource. """ berners_lee_card = 'http://www.w3.org/People/Berners-Lee/card' graph = RdfGraph(source_file=berners_lee_card, standard='rdf') query = """SELECT ?s ?p ?o WHERE { ?s ?p ?o }""" output = graph.query(query) assert len(output) == 86
def test_connect_file_rdf() ->None: """ Test loading online resource. """ berners_lee_card = 'http://www.w3.org/People/Berners-Lee/card' graph = RdfGraph(source_file=berners_lee_card, standard='rdf') query = 'SELECT ?s ?p ?o\nWHERE { ?s ?p ?o }' output = graph.query(query) assert len(out...
Test loading online resource.
test_graph_cypher_qa_chain_prompt_selection_2
chain = GraphCypherQAChain.from_llm(llm=FakeLLM(), graph=FakeGraphStore(), verbose=True, return_intermediate_steps=False) assert chain.qa_chain.prompt == CYPHER_QA_PROMPT assert chain.cypher_generation_chain.prompt == CYPHER_GENERATION_PROMPT
def test_graph_cypher_qa_chain_prompt_selection_2() ->None: chain = GraphCypherQAChain.from_llm(llm=FakeLLM(), graph=FakeGraphStore (), verbose=True, return_intermediate_steps=False) assert chain.qa_chain.prompt == CYPHER_QA_PROMPT assert chain.cypher_generation_chain.prompt == CYPHER_GENERATION_PRO...
null
requires_reference
"""Whether this evaluator requires a reference label.""" return False
@property def requires_reference(self) ->bool: """Whether this evaluator requires a reference label.""" return False
Whether this evaluator requires a reference label.
_get_news_api
news_api_key = kwargs['news_api_key'] chain = APIChain.from_llm_and_api_docs(llm, news_docs.NEWS_DOCS, headers={ 'X-Api-Key': news_api_key}, limit_to_domains=['https://newsapi.org/']) return Tool(name='News-API', description= 'Use this when you want to get information about the top headlines of current news sto...
def _get_news_api(llm: BaseLanguageModel, **kwargs: Any) ->BaseTool: news_api_key = kwargs['news_api_key'] chain = APIChain.from_llm_and_api_docs(llm, news_docs.NEWS_DOCS, headers={'X-Api-Key': news_api_key}, limit_to_domains=[ 'https://newsapi.org/']) return Tool(name='News-API', descriptio...
null
get_schema
"""Returns the schema of the Graph database""" pass
@property @abstractmethod def get_schema(self) ->str: """Returns the schema of the Graph database""" pass
Returns the schema of the Graph database
_load_vector_db_qa_with_sources_chain
if 'vectorstore' in kwargs: vectorstore = kwargs.pop('vectorstore') else: raise ValueError('`vectorstore` must be present.') if 'combine_documents_chain' in config: combine_documents_chain_config = config.pop('combine_documents_chain') combine_documents_chain = load_chain_from_config( combine_do...
def _load_vector_db_qa_with_sources_chain(config: dict, **kwargs: Any ) ->VectorDBQAWithSourcesChain: if 'vectorstore' in kwargs: vectorstore = kwargs.pop('vectorstore') else: raise ValueError('`vectorstore` must be present.') if 'combine_documents_chain' in config: combine_docum...
null
test_get_relevant_documents
query = 'Test query' relevant_documents = time_weighted_retriever.get_relevant_documents(query) want = [(doc, 0.5) for doc in _get_example_memories()] assert isinstance(relevant_documents, list) assert len(relevant_documents) == len(want) now = datetime.now() for doc in relevant_documents: assert now - timedelta(ho...
def test_get_relevant_documents(time_weighted_retriever: TimeWeightedVectorStoreRetriever) ->None: query = 'Test query' relevant_documents = time_weighted_retriever.get_relevant_documents(query) want = [(doc, 0.5) for doc in _get_example_memories()] assert isinstance(relevant_documents, list) as...
null
parse_from_results
try: from google.cloud.documentai_toolbox.utilities.gcs_utilities import split_gcs_uri from google.cloud.documentai_toolbox.wrappers.document import _get_shards from google.cloud.documentai_toolbox.wrappers.page import _text_from_layout except ImportError as exc: raise ImportError( 'documentai_t...
def parse_from_results(self, results: List[DocAIParsingResults]) ->Iterator[ Document]: try: from google.cloud.documentai_toolbox.utilities.gcs_utilities import split_gcs_uri from google.cloud.documentai_toolbox.wrappers.document import _get_shards from google.cloud.documentai_toolbox.wr...
null
test_get_validated_relative_path
"""Safely resolve a path.""" root = Path(__file__).parent user_path = 'data/sub/file.txt' expected = root / user_path result = get_validated_relative_path(root, user_path) assert result == expected
def test_get_validated_relative_path() ->None: """Safely resolve a path.""" root = Path(__file__).parent user_path = 'data/sub/file.txt' expected = root / user_path result = get_validated_relative_path(root, user_path) assert result == expected
Safely resolve a path.
_get_provider
return self.model_id.split('.')[0]
def _get_provider(self) ->str: return self.model_id.split('.')[0]
null
_get_serpapi
return Tool(name='Search', description= 'A search engine. Useful for when you need to answer questions about current events. Input should be a search query.' , func=SerpAPIWrapper(**kwargs).run, coroutine=SerpAPIWrapper(**kwargs) .arun)
def _get_serpapi(**kwargs: Any) ->BaseTool: return Tool(name='Search', description= 'A search engine. Useful for when you need to answer questions about current events. Input should be a search query.' , func=SerpAPIWrapper(**kwargs).run, coroutine=SerpAPIWrapper(** kwargs).arun)
null
_streaming_response_template
return {'choices': [{'text': '', 'finish_reason': None, 'logprobs': None}]}
def _streaming_response_template() ->Dict[str, Any]: return {'choices': [{'text': '', 'finish_reason': None, 'logprobs': None}]}
null
_formatted_page_summary
return f"""Page: {page_title} Summary: {wiki_page.summary}"""
@staticmethod def _formatted_page_summary(page_title: str, wiki_page: Any) ->Optional[str]: return f'Page: {page_title}\nSummary: {wiki_page.summary}'
null
_llm_type
"""Return type of LLM.""" return 'cloudflare'
@property def _llm_type(self) ->str: """Return type of LLM.""" return 'cloudflare'
Return type of LLM.
__init__
""" Initialize with a file path. Args: file_path: The path to the file to load. mode: The mode to use for partitioning. See unstructured for details. Defaults to "single". **unstructured_kwargs: Additional keyword arguments to pass to ...
def __init__(self, file_path: str, mode: str='single', ** unstructured_kwargs: Any): """ Initialize with a file path. Args: file_path: The path to the file to load. mode: The mode to use for partitioning. See unstructured for details. Defaults to "single"...
Initialize with a file path. Args: file_path: The path to the file to load. mode: The mode to use for partitioning. See unstructured for details. Defaults to "single". **unstructured_kwargs: Additional keyword arguments to pass to unstructured.
on_tool_start
self.on_tool_start_common()
def on_tool_start(self, *args: Any, **kwargs: Any) ->Any: self.on_tool_start_common()
null
add_message
"""Add a self-created message to the store""" self.messages.append(message)
def add_message(self, message: BaseMessage) ->None: """Add a self-created message to the store""" self.messages.append(message)
Add a self-created message to the store
raise_value_error
"""Raise a value error.""" raise ValueError('x is too large')
def raise_value_error(x: int) ->int: """Raise a value error.""" raise ValueError('x is too large')
Raise a value error.
_import_ainetwork_owner
from langchain_community.tools.ainetwork.owner import AINOwnerOps return AINOwnerOps
def _import_ainetwork_owner() ->Any: from langchain_community.tools.ainetwork.owner import AINOwnerOps return AINOwnerOps
null
create_citation_fuzzy_match_chain
"""Create a citation fuzzy match chain. Args: llm: Language model to use for the chain. Returns: Chain (LLMChain) that can be used to answer questions with citations. """ output_parser = PydanticOutputFunctionsParser(pydantic_schema=QuestionAnswer) schema = QuestionAnswer.schema() function...
def create_citation_fuzzy_match_chain(llm: BaseLanguageModel) ->LLMChain: """Create a citation fuzzy match chain. Args: llm: Language model to use for the chain. Returns: Chain (LLMChain) that can be used to answer questions with citations. """ output_parser = PydanticOutputFunctio...
Create a citation fuzzy match chain. Args: llm: Language model to use for the chain. Returns: Chain (LLMChain) that can be used to answer questions with citations.
_run
"""Run the tool.""" return self.requests_wrapper.delete(_clean_url(url))
def _run(self, url: str, run_manager: Optional[CallbackManagerForToolRun]=None ) ->str: """Run the tool.""" return self.requests_wrapper.delete(_clean_url(url))
Run the tool.
test_load_file_pattern
"""Test that returns no documents when json file pattern specified.""" loader = BibtexLoader(file_path=str(BIBTEX_EXAMPLE_FILE), file_pattern= '[^:]+\\.json') docs = loader.load() assert len(docs) == 0
@pytest.mark.requires('fitz', 'bibtexparser') def test_load_file_pattern() ->None: """Test that returns no documents when json file pattern specified.""" loader = BibtexLoader(file_path=str(BIBTEX_EXAMPLE_FILE), file_pattern= '[^:]+\\.json') docs = loader.load() assert len(docs) == 0
Test that returns no documents when json file pattern specified.
test_chat_openai_invalid_streaming_params
"""Test that streaming correctly invokes on_llm_new_token callback.""" with pytest.raises(ValueError): ChatOpenAI(max_tokens=10, streaming=True, temperature=0, n=5)
def test_chat_openai_invalid_streaming_params() ->None: """Test that streaming correctly invokes on_llm_new_token callback.""" with pytest.raises(ValueError): ChatOpenAI(max_tokens=10, streaming=True, temperature=0, n=5)
Test that streaming correctly invokes on_llm_new_token callback.
embed_query
"""Call out to Gradient's embedding endpoint. Args: text: The text to embed. Returns: Embeddings for the text. """ query = (f'{self.query_prompt_for_retrieval} {text}' if self. query_prompt_for_retrieval else text) return self.embed_documents([query])[0]
def embed_query(self, text: str) ->List[float]: """Call out to Gradient's embedding endpoint. Args: text: The text to embed. Returns: Embeddings for the text. """ query = (f'{self.query_prompt_for_retrieval} {text}' if self. query_prompt_for_retrieval el...
Call out to Gradient's embedding endpoint. Args: text: The text to embed. Returns: Embeddings for the text.
visit_comparison
value_type = 'valueText' value = comparison.value if isinstance(comparison.value, bool): value_type = 'valueBoolean' elif isinstance(comparison.value, float): value_type = 'valueNumber' elif isinstance(comparison.value, int): value_type = 'valueInt' elif isinstance(comparison.value, dict) and comparison.val...
def visit_comparison(self, comparison: Comparison) ->Dict: value_type = 'valueText' value = comparison.value if isinstance(comparison.value, bool): value_type = 'valueBoolean' elif isinstance(comparison.value, float): value_type = 'valueNumber' elif isinstance(comparison.value, int):...
null
visit_comparison
comparator = self._format_func(comparison.comparator) processed_value = process_value(comparison.value) attribute = comparison.attribute return '( ' + attribute + ' ' + comparator + ' ' + processed_value + ' )'
def visit_comparison(self, comparison: Comparison) ->str: comparator = self._format_func(comparison.comparator) processed_value = process_value(comparison.value) attribute = comparison.attribute return '( ' + attribute + ' ' + comparator + ' ' + processed_value + ' )'
null
build_extra
"""Build extra kwargs from additional params that were passed in.""" all_required_field_names = get_pydantic_field_names(cls) extra = values.get('model_kwargs', {}) for field_name in list(values): if field_name in extra: raise ValueError(f'Found {field_name} supplied twice.') if field_name not in all_re...
@root_validator(pre=True) def build_extra(cls, values: Dict[str, Any]) ->Dict[str, Any]: """Build extra kwargs from additional params that were passed in.""" all_required_field_names = get_pydantic_field_names(cls) extra = values.get('model_kwargs', {}) for field_name in list(values): if field_n...
Build extra kwargs from additional params that were passed in.
test_message_prompt_template_from_template_file
expected = ChatMessagePromptTemplate(prompt=PromptTemplate(template= """Question: {question} Answer:""", input_variables=['question']), role ='human') actual = ChatMessagePromptTemplate.from_template_file(Path(__file__).parent .parent / 'data' / 'prompt_file.txt', ['question'], role='human') assert expected...
def test_message_prompt_template_from_template_file() ->None: expected = ChatMessagePromptTemplate(prompt=PromptTemplate(template= """Question: {question} Answer:""", input_variables=['question']), role='human') actual = ChatMessagePromptTemplate.from_template_file(Path(__file__). parent...
null
_moderate
if results['flagged']: error_str = "Text was found that violates OpenAI's content policy." if self.error: raise ValueError(error_str) else: return error_str return text
def _moderate(self, text: str, results: dict) ->str: if results['flagged']: error_str = "Text was found that violates OpenAI's content policy." if self.error: raise ValueError(error_str) else: return error_str return text
null
load_memory_variables
"""Return history buffer.""" entities = self._get_current_entities(inputs) summary_strings = [] for entity in entities: knowledge = self.kg.get_entity_knowledge(entity) if knowledge: summary = f"On {entity}: {'. '.join(knowledge)}." summary_strings.append(summary) context: Union[str, List] if no...
def load_memory_variables(self, inputs: Dict[str, Any]) ->Dict[str, Any]: """Return history buffer.""" entities = self._get_current_entities(inputs) summary_strings = [] for entity in entities: knowledge = self.kg.get_entity_knowledge(entity) if knowledge: summary = f"On {ent...
Return history buffer.
test_build_query_sql_with_where
vector = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0] q_str = self.rockset_vectorstore._build_query_sql(vector, Rockset. DistanceFunction.COSINE_SIM, 4, 'age >= 10') vector_str = ','.join(map(str, vector)) expected = f"""SELECT * EXCEPT({embedding_key}), COSINE_SIM({embedding_key}, [{vector_str}]) as dist FR...
def test_build_query_sql_with_where(self) ->None: vector = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0] q_str = self.rockset_vectorstore._build_query_sql(vector, Rockset. DistanceFunction.COSINE_SIM, 4, 'age >= 10') vector_str = ','.join(map(str, vector)) expected = f"""SELECT * EXCEPT({e...
null
load_messages
"""Retrieve the messages from Firestore""" if not self._document: raise ValueError('Document not initialized') doc = self._document.get() if doc.exists: data = doc.to_dict() if 'messages' in data and len(data['messages']) > 0: self.messages = messages_from_dict(data['messages'])
def load_messages(self) ->None: """Retrieve the messages from Firestore""" if not self._document: raise ValueError('Document not initialized') doc = self._document.get() if doc.exists: data = doc.to_dict() if 'messages' in data and len(data['messages']) > 0: self.mess...
Retrieve the messages from Firestore
input_keys
"""Expect input key. :meta private: """ return [self.input_docs_key, self.question_key]
@property def input_keys(self) ->List[str]: """Expect input key. :meta private: """ return [self.input_docs_key, self.question_key]
Expect input key. :meta private:
embed_query
"""Get embeddings for a single text. Args: text: The text to get embeddings for. Returns: List of embeddings. """ return self.embed_documents([text])[0]
def embed_query(self, text: str) ->List[float]: """Get embeddings for a single text. Args: text: The text to get embeddings for. Returns: List of embeddings. """ return self.embed_documents([text])[0]
Get embeddings for a single text. Args: text: The text to get embeddings for. Returns: List of embeddings.
ignore_retriever
"""Whether to ignore retriever callbacks.""" return self.ignore_retriever_
@property def ignore_retriever(self) ->bool: """Whether to ignore retriever callbacks.""" return self.ignore_retriever_
Whether to ignore retriever callbacks.
_format_func
map_dict = {Operator.AND: ' and ', Operator.OR: ' or ', Comparator.EQ: '=', Comparator.NE: '!=', Comparator.GT: '>', Comparator.GTE: '>=', Comparator.LT: '<', Comparator.LTE: '<='} self._validate_func(func) return map_dict[func]
def _format_func(self, func: Union[Operator, Comparator]) ->str: map_dict = {Operator.AND: ' and ', Operator.OR: ' or ', Comparator.EQ: '=', Comparator.NE: '!=', Comparator.GT: '>', Comparator.GTE: '>=', Comparator.LT: '<', Comparator.LTE: '<='} self._validate_func(func) return map_dict[func...
null
OutputType
"""The type of output this runnable produces specified as a type annotation.""" for cls in self.__class__.__orig_bases__: type_args = get_args(cls) if type_args and len(type_args) == 2: return type_args[1] raise TypeError( f"Runnable {self.get_name()} doesn't have an inferable OutputType. Override t...
@property def OutputType(self) ->Type[Output]: """The type of output this runnable produces specified as a type annotation.""" for cls in self.__class__.__orig_bases__: type_args = get_args(cls) if type_args and len(type_args) == 2: return type_args[1] raise TypeError( f"...
The type of output this runnable produces specified as a type annotation.
embed_query
"""Embed a query using a Ollama deployed embedding model. Args: text: The text to embed. Returns: Embeddings for the text. """ instruction_pair = f'{self.query_instruction}{text}' embedding = self._embed([instruction_pair])[0] return embedding
def embed_query(self, text: str) ->List[float]: """Embed a query using a Ollama deployed embedding model. Args: text: The text to embed. Returns: Embeddings for the text. """ instruction_pair = f'{self.query_instruction}{text}' embedding = self._embed([instr...
Embed a query using a Ollama deployed embedding model. Args: text: The text to embed. Returns: Embeddings for the text.
test_complete_text_and_metadata
""" Test loading a board cards with all metadata. """ from bs4 import BeautifulSoup trello_loader = TrelloLoader.from_credentials('QA', api_key='API_KEY', token='API_TOKEN') documents = trello_loader.load() self.assertEqual(len(documents), len(TRELLO_CARDS_QA), 'Card count matches.') soup = Beautifu...
def test_complete_text_and_metadata(self) ->None: """ Test loading a board cards with all metadata. """ from bs4 import BeautifulSoup trello_loader = TrelloLoader.from_credentials('QA', api_key='API_KEY', token='API_TOKEN') documents = trello_loader.load() self.assertEqual(le...
Test loading a board cards with all metadata.
test_edenai_call
"""Test simple call to edenai's image moderation endpoint.""" image_moderation = EdenAiExplicitImageTool(providers=['amazon']) output = image_moderation('https://static.javatpoint.com/images/objects.jpg') assert image_moderation.name == 'edenai_image_explicit_content_detection' assert image_moderation.feature == 'image...
def test_edenai_call() ->None: """Test simple call to edenai's image moderation endpoint.""" image_moderation = EdenAiExplicitImageTool(providers=['amazon']) output = image_moderation( 'https://static.javatpoint.com/images/objects.jpg') assert image_moderation.name == 'edenai_image_explicit_cont...
Test simple call to edenai's image moderation endpoint.
_get_schema
"""Get the schema for a table.""" try: result = self.run( f'EVALUATE TOPN({self.sample_rows_in_table_info}, {table})') self.schemas[table] = json_to_md(result['results'][0]['tables'][0]['rows']) except Timeout: logger.warning('Timeout while getting table info for %s', table) self.schemas[table] ...
def _get_schema(self, table: str) ->None: """Get the schema for a table.""" try: result = self.run( f'EVALUATE TOPN({self.sample_rows_in_table_info}, {table})') self.schemas[table] = json_to_md(result['results'][0]['tables'][0][ 'rows']) except Timeout: logger...
Get the schema for a table.
__init__
"""initialize the SemaDB vector store.""" self.collection_name = collection_name self.vector_size = vector_size self.api_key = api_key or get_from_env('api_key', 'SEMADB_API_KEY') self._embedding = embedding self.distance_strategy = distance_strategy
def __init__(self, collection_name: str, vector_size: int, embedding: Embeddings, distance_strategy: DistanceStrategy=DistanceStrategy. EUCLIDEAN_DISTANCE, api_key: str=''): """initialize the SemaDB vector store.""" self.collection_name = collection_name self.vector_size = vector_size self.api_k...
initialize the SemaDB vector store.
test_tensorflowhub_embedding_query
"""Test tensorflowhub embeddings.""" document = 'foo bar' embedding = TensorflowHubEmbeddings() output = embedding.embed_query(document) assert len(output) == 512
def test_tensorflowhub_embedding_query() ->None: """Test tensorflowhub embeddings.""" document = 'foo bar' embedding = TensorflowHubEmbeddings() output = embedding.embed_query(document) assert len(output) == 512
Test tensorflowhub embeddings.
load_dataset
"""Load a dataset from the `LangChainDatasets on HuggingFace <https://huggingface.co/LangChainDatasets>`_. Args: uri: The uri of the dataset to load. Returns: A list of dictionaries, each representing a row in the dataset. **Prerequisites** .. code-block:: shell pip install ...
def load_dataset(uri: str) ->List[Dict]: """Load a dataset from the `LangChainDatasets on HuggingFace <https://huggingface.co/LangChainDatasets>`_. Args: uri: The uri of the dataset to load. Returns: A list of dictionaries, each representing a row in the dataset. **Prerequisites** ...
Load a dataset from the `LangChainDatasets on HuggingFace <https://huggingface.co/LangChainDatasets>`_. Args: uri: The uri of the dataset to load. Returns: A list of dictionaries, each representing a row in the dataset. **Prerequisites** .. code-block:: shell pip install datasets Examples -------- .. ...
test_solidity_code_splitter
splitter = RecursiveCharacterTextSplitter.from_language(Language.SOL, chunk_size=CHUNK_SIZE, chunk_overlap=0) code = """pragma solidity ^0.8.20; contract HelloWorld { function add(uint a, uint b) pure public returns(uint) { return a + b; } } """ chunks = splitter.split_text(code) assert chunks ...
def test_solidity_code_splitter() ->None: splitter = RecursiveCharacterTextSplitter.from_language(Language.SOL, chunk_size=CHUNK_SIZE, chunk_overlap=0) code = """pragma solidity ^0.8.20; contract HelloWorld { function add(uint a, uint b) pure public returns(uint) { return a + b; } } ...
null
validate_environment
"""Validate that api key and python package exists in environment.""" values['anyscale_api_base'] = get_from_dict_or_env(values, 'anyscale_api_base', 'ANYSCALE_API_BASE') values['anyscale_api_key'] = convert_to_secret_str(get_from_dict_or_env( values, 'anyscale_api_key', 'ANYSCALE_API_KEY')) try: import ope...
@root_validator() def validate_environment(cls, values: Dict) ->Dict: """Validate that api key and python package exists in environment.""" values['anyscale_api_base'] = get_from_dict_or_env(values, 'anyscale_api_base', 'ANYSCALE_API_BASE') values['anyscale_api_key'] = convert_to_secret_str(get_from...
Validate that api key and python package exists in environment.
_format_url
expected_path_param = re.findall('{(.*?)}', url) new_params = {} for param in expected_path_param: clean_param = param.lstrip('.;').rstrip('*') val = path_params[clean_param] if isinstance(val, list): if param[0] == '.': sep = '.' if param[-1] == '*' else ',' new_val = '.' + ...
def _format_url(url: str, path_params: dict) ->str: expected_path_param = re.findall('{(.*?)}', url) new_params = {} for param in expected_path_param: clean_param = param.lstrip('.;').rstrip('*') val = path_params[clean_param] if isinstance(val, list): if param[0] == '.':...
null
get_repl_context
"""Gets the notebook REPL context if running inside a Databricks notebook. Returns None otherwise. """ try: from dbruntime.databricks_repl_context import get_context return get_context() except ImportError: raise ImportError( 'Cannot access dbruntime, not running inside a Databricks notebook...
def get_repl_context() ->Any: """Gets the notebook REPL context if running inside a Databricks notebook. Returns None otherwise. """ try: from dbruntime.databricks_repl_context import get_context return get_context() except ImportError: raise ImportError( 'Cannot ...
Gets the notebook REPL context if running inside a Databricks notebook. Returns None otherwise.
test_exceptions_raised_while_parsing
"""Test exceptions raised correctly while using JSON parser.""" chat_generation = ChatGeneration(message=bad_message) with pytest.raises(OutputParserException): JsonOutputFunctionsParser().parse_result([chat_generation])
@pytest.mark.parametrize('bad_message', [HumanMessage(content= 'This is a test message'), AIMessage(content='This is a test message', additional_kwargs={}), AIMessage(content='This is a test message', additional_kwargs={'function_call': {'name': 'function_name', 'arguments': {}}}), AIMessage(content='Th...
Test exceptions raised correctly while using JSON parser.
test_solver_question
"""Test question about solving algebraic equations that needs sympy""" question = 'What are the solutions to this equation x**2 - x?' output = fake_llm_symbolic_math_chain.run(question) assert output == 'Answer: {0, 1}'
def test_solver_question(fake_llm_symbolic_math_chain: LLMSymbolicMathChain ) ->None: """Test question about solving algebraic equations that needs sympy""" question = 'What are the solutions to this equation x**2 - x?' output = fake_llm_symbolic_math_chain.run(question) assert output == 'Answer: {0...
Test question about solving algebraic equations that needs sympy
similarity_search
"""Run similarity search. Args: query: search query text. k: Number of Documents to return. Defaults to 4. filter: Filter on metadata properties, e.g. { "str_property": "foo", "int_pr...
def similarity_search(self, query: str, k: int=DEFAULT_TOP_K, filter: Optional[Dict[str, Any]]=None, brute_force: bool=False, fraction_lists_to_search: Optional[float]=None, **kwargs: Any) ->List[ Document]: """Run similarity search. Args: query: search query text. k: Nu...
Run similarity search. Args: query: search query text. k: Number of Documents to return. Defaults to 4. filter: Filter on metadata properties, e.g. { "str_property": "foo", "int_property": 123 } brute_force: Whether...
test_empty_steps
assert format_log_to_messages([]) == []
def test_empty_steps() ->None: assert format_log_to_messages([]) == []
null
test_get_validated_relative_path_errs_for_symlink_outside_root
"""Test that symlink pointing outside of root directory is not allowed.""" with TemporaryDirectory() as temp_dir: root = Path(temp_dir) user_path = 'symlink_outside_root' outside_path = Path('/bin/bash') symlink_path = root / user_path symlink_path.symlink_to(outside_path) match = re.escape( ...
def test_get_validated_relative_path_errs_for_symlink_outside_root() ->None: """Test that symlink pointing outside of root directory is not allowed.""" with TemporaryDirectory() as temp_dir: root = Path(temp_dir) user_path = 'symlink_outside_root' outside_path = Path('/bin/bash') ...
Test that symlink pointing outside of root directory is not allowed.
add_texts
"""Run more texts through the embeddings and add to the vectorstore. Args: texts: Iterable of strings to add to the vectorstore. metadatas: Optional list of metadatas associated with the texts. is_duplicate_texts: Optional whether to duplicate texts. Defaults to True. ...
def add_texts(self, texts: Iterable[str], metadatas: Optional[List[dict]]= None, is_duplicate_texts: Optional[bool]=None, **kwargs: Any) ->List[str]: """Run more texts through the embeddings and add to the vectorstore. Args: texts: Iterable of strings to add to the vectorstore. m...
Run more texts through the embeddings and add to the vectorstore. Args: texts: Iterable of strings to add to the vectorstore. metadatas: Optional list of metadatas associated with the texts. is_duplicate_texts: Optional whether to duplicate texts. Defaults to True. kwargs: any possible extend parameters...
_create_chat_result
if 'function_call' in response: additional_kwargs = {'function_call': dict(response.get('function_call', {}))} else: additional_kwargs = {} generations = [ChatGeneration(message=AIMessage(content=response.get( 'result'), additional_kwargs={**additional_kwargs}))] token_usage = response.get('usage', ...
def _create_chat_result(self, response: Mapping[str, Any]) ->ChatResult: if 'function_call' in response: additional_kwargs = {'function_call': dict(response.get( 'function_call', {}))} else: additional_kwargs = {} generations = [ChatGeneration(message=AIMessage(content=response.g...
null
create_qa_with_sources_chain
"""Create a question answering chain that returns an answer with sources. Args: llm: Language model to use for the chain. verbose: Whether to print the details of the chain **kwargs: Keyword arguments to pass to `create_qa_with_structure_chain`. Returns: Chain (LLMChain) that c...
def create_qa_with_sources_chain(llm: BaseLanguageModel, verbose: bool= False, **kwargs: Any) ->LLMChain: """Create a question answering chain that returns an answer with sources. Args: llm: Language model to use for the chain. verbose: Whether to print the details of the chain **kw...
Create a question answering chain that returns an answer with sources. Args: llm: Language model to use for the chain. verbose: Whether to print the details of the chain **kwargs: Keyword arguments to pass to `create_qa_with_structure_chain`. Returns: Chain (LLMChain) that can be used to answer questi...
failed
return self.status == 'failed'
def failed(self) ->bool: return self.status == 'failed'
null
test_load_success
docs = retriever.get_relevant_documents(query='chatgpt') assert len(docs) == 3 assert_docs(docs)
def test_load_success(retriever: PubMedRetriever) ->None: docs = retriever.get_relevant_documents(query='chatgpt') assert len(docs) == 3 assert_docs(docs)
null
fetch_place_details
try: place_details = self.google_map_client.place(place_id) place_details['place_id'] = place_id formatted_details = self.format_place_details(place_details) return formatted_details except Exception as e: logging.error(f'An Error occurred while fetching place details: {e}') return None
def fetch_place_details(self, place_id: str) ->Optional[str]: try: place_details = self.google_map_client.place(place_id) place_details['place_id'] = place_id formatted_details = self.format_place_details(place_details) return formatted_details except Exception as e: logg...
null
from_string
"""Create a KnowledgeTriple from a string.""" subject, predicate, object_ = triple_string.strip().split(', ') subject = subject[1:] object_ = object_[:-1] return cls(subject, predicate, object_)
@classmethod def from_string(cls, triple_string: str) ->'KnowledgeTriple': """Create a KnowledgeTriple from a string.""" subject, predicate, object_ = triple_string.strip().split(', ') subject = subject[1:] object_ = object_[:-1] return cls(subject, predicate, object_)
Create a KnowledgeTriple from a string.
test_similarity_search_with_metadata
"""Test end to end construction and search with a metadata filter. This test requires a column named "a" of type integer to be present in the Xata table.""" texts = ['foo', 'foo', 'foo'] metadatas = [{'a': i} for i in range(len(texts))] docsearch = XataVectorStore.from_texts(api_key=os.getenv('XATA_API...
def test_similarity_search_with_metadata(self, embedding_openai: OpenAIEmbeddings) ->None: """Test end to end construction and search with a metadata filter. This test requires a column named "a" of type integer to be present in the Xata table.""" texts = ['foo', 'foo', 'foo'] metadatas...
Test end to end construction and search with a metadata filter. This test requires a column named "a" of type integer to be present in the Xata table.
_import_vald
from langchain_community.vectorstores.vald import Vald return Vald
def _import_vald() ->Any: from langchain_community.vectorstores.vald import Vald return Vald
null
setup_class
collection = get_collection() assert collection.count_documents({}) == 0
@classmethod def setup_class(cls) ->None: collection = get_collection() assert collection.count_documents({}) == 0
null
test_api_key_is_string
embeddings = GoogleGenerativeAIEmbeddings(model='models/embedding-001', google_api_key='secret-api-key') assert isinstance(embeddings.google_api_key, SecretStr)
def test_api_key_is_string() ->None: embeddings = GoogleGenerativeAIEmbeddings(model='models/embedding-001', google_api_key='secret-api-key') assert isinstance(embeddings.google_api_key, SecretStr)
null
invoke
config = ensure_config(config) configurable = config.get('configurable', {}) for id_, mapper in zip(self.ids, self.keys.values()): if mapper is not None: configurable[id_](mapper.invoke(input, config)) else: configurable[id_](input) return input
def invoke(self, input: Any, config: Optional[RunnableConfig]=None) ->Any: config = ensure_config(config) configurable = config.get('configurable', {}) for id_, mapper in zip(self.ids, self.keys.values()): if mapper is not None: configurable[id_](mapper.invoke(input, config)) els...
null
update
"""Upsert records into the SQLite database.""" if group_ids is None: group_ids = [None] * len(keys) if len(keys) != len(group_ids): raise ValueError( f'Number of keys ({len(keys)}) does not match number of group_ids ({len(group_ids)})' ) update_time = self.get_time() if time_at_least and update_...
def update(self, keys: Sequence[str], *, group_ids: Optional[Sequence[ Optional[str]]]=None, time_at_least: Optional[float]=None) ->None: """Upsert records into the SQLite database.""" if group_ids is None: group_ids = [None] * len(keys) if len(keys) != len(group_ids): raise ValueError( ...
Upsert records into the SQLite database.
test_gradient_llm_sync_batch
mocker.patch('requests.post', side_effect=mocked_requests_post) llm = GradientLLM(**setup) assert llm.gradient_access_token == _GRADIENT_SECRET assert llm.gradient_api_url == _GRADIENT_BASE_URL assert llm.gradient_workspace_id == _GRADIENT_WORKSPACE_ID assert llm.model_id == _MODEL_ID inputs = ['Say foo:', 'Say baz:', ...
@pytest.mark.parametrize('setup', [dict(gradient_api_url=_GRADIENT_BASE_URL, gradient_access_token=_GRADIENT_SECRET, gradient_workspace_id= _GRADIENT_WORKSPACE_ID, model=_MODEL_ID)]) def test_gradient_llm_sync_batch(mocker: MockerFixture, setup: dict) ->None: mocker.patch('requests.post', side_effect=mocked...
null
test__convert_delta_to_message_assistant
delta = {'role': 'assistant', 'content': 'foo'} result = _convert_delta_to_message_chunk(delta, AIMessageChunk) expected_output = AIMessageChunk(content='foo') assert result == expected_output
def test__convert_delta_to_message_assistant() ->None: delta = {'role': 'assistant', 'content': 'foo'} result = _convert_delta_to_message_chunk(delta, AIMessageChunk) expected_output = AIMessageChunk(content='foo') assert result == expected_output
null
on_retriever_error
"""Run when Retriever errors.""" retrieval_run = self._get_run(run_id, run_type='retriever') retrieval_run.error = self._get_stacktrace(error) retrieval_run.end_time = datetime.now(timezone.utc) retrieval_run.events.append({'name': 'error', 'time': retrieval_run.end_time}) self._end_trace(retrieval_run) self._on_retrie...
def on_retriever_error(self, error: BaseException, *, run_id: UUID, ** kwargs: Any) ->Run: """Run when Retriever errors.""" retrieval_run = self._get_run(run_id, run_type='retriever') retrieval_run.error = self._get_stacktrace(error) retrieval_run.end_time = datetime.now(timezone.utc) retrieval_...
Run when Retriever errors.
test_handles_empty_input_list
output = format_to_openai_function_messages([]) assert output == []
def test_handles_empty_input_list() ->None: output = format_to_openai_function_messages([]) assert output == []
null
load_and_split
"""Load all documents and split them into sentences.""" raise NotImplementedError( 'Loading and splitting is not yet implemented for generic loaders. When they will be implemented they will be added via the initializer. This method should not be used going forward.' )
def load_and_split(self, text_splitter: Optional[TextSplitter]=None) ->List[ Document]: """Load all documents and split them into sentences.""" raise NotImplementedError( 'Loading and splitting is not yet implemented for generic loaders. When they will be implemented they will be added via the initi...
Load all documents and split them into sentences.
similarity_search_by_vector_with_relevance_scores
match_documents_params = self.match_args(query, filter) query_builder = self._client.rpc(self.query_name, match_documents_params) if postgrest_filter: query_builder.params = query_builder.params.set('and', f'({postgrest_filter})') query_builder.params = query_builder.params.set('limit', k) res = query_build...
def similarity_search_by_vector_with_relevance_scores(self, query: List[ float], k: int, filter: Optional[Dict[str, Any]]=None, postgrest_filter: Optional[str]=None, score_threshold: Optional[float]=None) ->List[Tuple [Document, float]]: match_documents_params = self.match_args(query, filter) query_...
null
test_load_success_init_args_more
retriever = WikipediaRetriever(lang='en', top_k_results=20, load_all_available_meta=False) docs = retriever.get_relevant_documents('HUNTER X HUNTER') assert len(docs) == 20 assert_docs(docs, all_meta=False)
def test_load_success_init_args_more() ->None: retriever = WikipediaRetriever(lang='en', top_k_results=20, load_all_available_meta=False) docs = retriever.get_relevant_documents('HUNTER X HUNTER') assert len(docs) == 20 assert_docs(docs, all_meta=False)
null
memory_variables
return list(self.memories.keys())
@property def memory_variables(self) ->List[str]: return list(self.memories.keys())
null
_llm_type
return 'litellm-chat'
@property def _llm_type(self) ->str: return 'litellm-chat'
null
_import_slack_send_message
from langchain_community.tools.slack.send_message import SlackSendMessage return SlackSendMessage
def _import_slack_send_message() ->Any: from langchain_community.tools.slack.send_message import SlackSendMessage return SlackSendMessage
null
_get_docs
"""Get docs to run questioning over.""" return inputs.pop(self.input_docs_key)
def _get_docs(self, inputs: Dict[str, Any], *, run_manager: CallbackManagerForChainRun) ->List[Document]: """Get docs to run questioning over.""" return inputs.pop(self.input_docs_key)
Get docs to run questioning over.
on_llm_error
"""Run when LLM errors. Args: error (BaseException): The error that occurred. kwargs (Any): Additional keyword arguments. - response (LLMResult): The response which was generated before the error occurred. """
def on_llm_error(self, error: BaseException, *, run_id: UUID, parent_run_id: Optional[UUID]=None, **kwargs: Any) ->Any: """Run when LLM errors. Args: error (BaseException): The error that occurred. kwargs (Any): Additional keyword arguments. - response (LLMResult)...
Run when LLM errors. Args: error (BaseException): The error that occurred. kwargs (Any): Additional keyword arguments. - response (LLMResult): The response which was generated before the error occurred.