method_name stringlengths 1 78 | method_body stringlengths 3 9.66k | full_code stringlengths 31 10.7k | docstring stringlengths 4 4.74k ⌀ |
|---|---|---|---|
_euclidean_distance | """Compute the Euclidean distance between two vectors.
Args:
a (np.ndarray): The first vector.
b (np.ndarray): The second vector.
Returns:
np.floating: The Euclidean distance.
"""
return np.linalg.norm(a - b) | @staticmethod
def _euclidean_distance(a: np.ndarray, b: np.ndarray) ->np.floating:
"""Compute the Euclidean distance between two vectors.
Args:
a (np.ndarray): The first vector.
b (np.ndarray): The second vector.
Returns:
np.floating: The Euclidean distance.
... | Compute the Euclidean distance between two vectors.
Args:
a (np.ndarray): The first vector.
b (np.ndarray): The second vector.
Returns:
np.floating: The Euclidean distance. |
visit_comparison | """Translate a Comparison.""" | @abstractmethod
def visit_comparison(self, comparison: Comparison) ->Any:
"""Translate a Comparison.""" | Translate a Comparison. |
test_exception_handling_non_tool_exception | _tool = _FakeExceptionTool(exception=ValueError())
with pytest.raises(ValueError):
_tool.run({}) | def test_exception_handling_non_tool_exception() ->None:
_tool = _FakeExceptionTool(exception=ValueError())
with pytest.raises(ValueError):
_tool.run({}) | null |
__add__ | """Combine two prompt templates.
Args:
other: Another prompt template.
Returns:
Combined prompt template.
"""
prompt = ChatPromptTemplate(messages=[self])
return prompt + other | def __add__(self, other: Any) ->ChatPromptTemplate:
"""Combine two prompt templates.
Args:
other: Another prompt template.
Returns:
Combined prompt template.
"""
prompt = ChatPromptTemplate(messages=[self])
return prompt + other | Combine two prompt templates.
Args:
other: Another prompt template.
Returns:
Combined prompt template. |
test_public_api | """Hard-code public API to help determine if we have broken it."""
assert sorted(__all__) == ['Blob', 'BlobLoader', 'FileSystemBlobLoader',
'YoutubeAudioLoader'] | def test_public_api() ->None:
"""Hard-code public API to help determine if we have broken it."""
assert sorted(__all__) == ['Blob', 'BlobLoader', 'FileSystemBlobLoader',
'YoutubeAudioLoader'] | Hard-code public API to help determine if we have broken it. |
_llm_type | """Returns the type of LLM."""
return 'human-input-chat-model' | @property
def _llm_type(self) ->str:
"""Returns the type of LLM."""
return 'human-input-chat-model' | Returns the type of LLM. |
lazy_load | """A lazy loader for document content."""
for row in self.rdd_df.toLocalIterator():
metadata = {self.column_names[i]: row[i] for i in range(len(row))}
text = metadata[self.page_content_column]
metadata.pop(self.page_content_column)
yield Document(page_content=text, metadata=metadata) | def lazy_load(self) ->Iterator[Document]:
"""A lazy loader for document content."""
for row in self.rdd_df.toLocalIterator():
metadata = {self.column_names[i]: row[i] for i in range(len(row))}
text = metadata[self.page_content_column]
metadata.pop(self.page_content_column)
yield ... | A lazy loader for document content. |
input_iter | for token in STREAMED_TOKENS:
yield token | def input_iter(_: Any) ->Iterator[str]:
for token in STREAMED_TOKENS:
yield token | null |
__getattr__ | if name == 'AnalyticDB':
return _import_analyticdb()
elif name == 'AlibabaCloudOpenSearch':
return _import_alibaba_cloud_open_search()
elif name == 'AlibabaCloudOpenSearchSettings':
return _import_alibaba_cloud_open_search_settings()
elif name == 'AzureCosmosDBVectorSearch':
return _import_azure_cosmos_... | def __getattr__(name: str) ->Any:
if name == 'AnalyticDB':
return _import_analyticdb()
elif name == 'AlibabaCloudOpenSearch':
return _import_alibaba_cloud_open_search()
elif name == 'AlibabaCloudOpenSearchSettings':
return _import_alibaba_cloud_open_search_settings()
elif name ==... | null |
__getattr__ | """Get attr name."""
if name == 'create_python_agent':
HERE = Path(__file__).parents[3]
here = as_import_path(Path(__file__).parent, relative_to=HERE)
old_path = 'langchain.' + here + '.' + name
new_path = 'langchain_experimental.' + here + '.' + name
raise ImportError(
f"""This agent has be... | def __getattr__(name: str) ->Any:
"""Get attr name."""
if name == 'create_python_agent':
HERE = Path(__file__).parents[3]
here = as_import_path(Path(__file__).parent, relative_to=HERE)
old_path = 'langchain.' + here + '.' + name
new_path = 'langchain_experimental.' + here + '.' +... | Get attr name. |
test_simple_question | """Test simple question that should not need python."""
question = 'What is 1 plus 1?'
output = fake_llm_symbolic_math_chain.run(question)
assert output == 'Answer: 2' | def test_simple_question(fake_llm_symbolic_math_chain: LLMSymbolicMathChain
) ->None:
"""Test simple question that should not need python."""
question = 'What is 1 plus 1?'
output = fake_llm_symbolic_math_chain.run(question)
assert output == 'Answer: 2' | Test simple question that should not need python. |
from_texts | """Create an DocArrayInMemorySearch store and insert data.
Args:
texts (List[str]): Text data.
embedding (Embeddings): Embedding function.
metadatas (Optional[List[Dict[Any, Any]]]): Metadata for each text
if it exists. Defaults to None.
metric (s... | @classmethod
def from_texts(cls, texts: List[str], embedding: Embeddings, metadatas:
Optional[List[Dict[Any, Any]]]=None, **kwargs: Any
) ->DocArrayInMemorySearch:
"""Create an DocArrayInMemorySearch store and insert data.
Args:
texts (List[str]): Text data.
embedding (Embed... | Create an DocArrayInMemorySearch store and insert data.
Args:
texts (List[str]): Text data.
embedding (Embeddings): Embedding function.
metadatas (Optional[List[Dict[Any, Any]]]): Metadata for each text
if it exists. Defaults to None.
metric (str): metric for exact nearest-neighbor search.
... |
messages | """Retrieve all session messages from DB"""
message_blobs = [doc['body_blob'] for doc in sorted(self.collection.
paginated_find(filter={'session_id': self.session_id}, projection={
'timestamp': 1, 'body_blob': 1}), key=lambda _doc: _doc['timestamp'])]
items = [json.loads(message_blob) for message_blob in messag... | @property
def messages(self) ->List[BaseMessage]:
"""Retrieve all session messages from DB"""
message_blobs = [doc['body_blob'] for doc in sorted(self.collection.
paginated_find(filter={'session_id': self.session_id}, projection={
'timestamp': 1, 'body_blob': 1}), key=lambda _doc: _doc['timestam... | Retrieve all session messages from DB |
test_confirm_full_coverage | self.assertEqual(list(AgentType), list(AGENT_TO_CLASS.keys())) | def test_confirm_full_coverage(self) ->None:
self.assertEqual(list(AgentType), list(AGENT_TO_CLASS.keys())) | null |
similarity_search_by_vector | """Perform a similarity search against the query string."""
res = self.similarity_search_with_score_by_vector(embedding=embedding, k=k,
param=param, expr=expr, timeout=timeout, **kwargs)
return [doc for doc, _ in res] | def similarity_search_by_vector(self, embedding: List[float], k: int=4,
param: Optional[dict]=None, expr: Optional[str]=None, timeout: Optional
[int]=None, **kwargs: Any) ->List[Document]:
"""Perform a similarity search against the query string."""
res = self.similarity_search_with_score_by_vector(embed... | Perform a similarity search against the query string. |
test_milvus_with_score | """Test end to end construction and search with scores and IDs."""
texts = ['foo', 'bar', 'baz']
metadatas = [{'page': i} for i in range(len(texts))]
docsearch = _milvus_from_texts(metadatas=metadatas)
output = docsearch.similarity_search_with_score('foo', k=3)
docs = [o[0] for o in output]
scores = [o[1] for o in outp... | def test_milvus_with_score() ->None:
"""Test end to end construction and search with scores and IDs."""
texts = ['foo', 'bar', 'baz']
metadatas = [{'page': i} for i in range(len(texts))]
docsearch = _milvus_from_texts(metadatas=metadatas)
output = docsearch.similarity_search_with_score('foo', k=3)
... | Test end to end construction and search with scores and IDs. |
test_all_imports | assert set(__all__) == set(EXPECTED_ALL) | def test_all_imports() ->None:
assert set(__all__) == set(EXPECTED_ALL) | null |
__ne__ | """Create a RedisTag inequality filter expression.
Args:
other (Union[List[str], Set[str], Tuple[str], str]):
The tag(s) to filter on.
Example:
>>> from langchain_community.vectorstores.redis import RedisTag
>>> filter = RedisTag("brand") != "nike"
... | @check_operator_misuse
def __ne__(self, other: Union[List[str], Set[str], Tuple[str], str]
) ->'RedisFilterExpression':
"""Create a RedisTag inequality filter expression.
Args:
other (Union[List[str], Set[str], Tuple[str], str]):
The tag(s) to filter on.
Example:
... | Create a RedisTag inequality filter expression.
Args:
other (Union[List[str], Set[str], Tuple[str], str]):
The tag(s) to filter on.
Example:
>>> from langchain_community.vectorstores.redis import RedisTag
>>> filter = RedisTag("brand") != "nike" |
_create_message_dicts | params = self._client_params
if stop is not None:
if 'stop' in params:
raise ValueError('`stop` found in both the input and default params.')
params['stop'] = stop
message_dicts = [convert_message_to_dict(m) for m in messages]
return message_dicts, params | def _create_message_dicts(self, messages: List[BaseMessage], stop: Optional
[List[str]]) ->Tuple[List[Dict[str, Any]], Dict[str, Any]]:
params = self._client_params
if stop is not None:
if 'stop' in params:
raise ValueError(
'`stop` found in both the input and default par... | null |
_get_elements | from unstructured.partition.xlsx import partition_xlsx
return partition_xlsx(filename=self.file_path, **self.unstructured_kwargs) | def _get_elements(self) ->List:
from unstructured.partition.xlsx import partition_xlsx
return partition_xlsx(filename=self.file_path, **self.unstructured_kwargs) | null |
_parse_json_multilevel | for section, subsections in extracted_data.items():
indentation = ' ' * level
if isinstance(subsections, str):
subsections = subsections.replace('\n', ',')
formatted_list.append(f'{indentation}{section} : {subsections}')
elif isinstance(subsections, list):
formatted_list.append(f'{i... | def _parse_json_multilevel(self, extracted_data: dict, formatted_list: list,
level: int=0) ->None:
for section, subsections in extracted_data.items():
indentation = ' ' * level
if isinstance(subsections, str):
subsections = subsections.replace('\n', ',')
formatted_list.a... | null |
get_llm_cache | """Get the value of the `llm_cache` global setting."""
try:
import langchain
with warnings.catch_warnings():
warnings.filterwarnings('ignore', message=
'Importing llm_cache from langchain root module is no longer supported'
)
old_llm_cache = langchain.llm_cache
except Imp... | def get_llm_cache() ->'BaseCache':
"""Get the value of the `llm_cache` global setting."""
try:
import langchain
with warnings.catch_warnings():
warnings.filterwarnings('ignore', message=
'Importing llm_cache from langchain root module is no longer supported'
... | Get the value of the `llm_cache` global setting. |
is_lc_serializable | return True | @classmethod
def is_lc_serializable(cls) ->bool:
return True | null |
return_stopped_response | """Return response when agent has been stopped due to max iterations."""
if early_stopping_method == 'force':
return AgentFinish({'output':
'Agent stopped due to iteration limit or time limit.'}, '')
else:
raise ValueError(
f'Got unsupported early_stopping_method `{early_stopping_method}`') | def return_stopped_response(self, early_stopping_method: str,
intermediate_steps: List[Tuple[AgentAction, str]], **kwargs: Any
) ->AgentFinish:
"""Return response when agent has been stopped due to max iterations."""
if early_stopping_method == 'force':
return AgentFinish({'output':
... | Return response when agent has been stopped due to max iterations. |
_type | return 'pydantic' | @property
def _type(self) ->str:
return 'pydantic' | null |
from_llm_and_ai_plugin | """Instantiate the toolkit from an OpenAPI Spec URL"""
spec = OpenAPISpec.from_url(ai_plugin.api.url)
return cls.from_llm_and_spec(llm=llm, spec=spec, requests=requests, verbose
=verbose, **kwargs) | @classmethod
def from_llm_and_ai_plugin(cls, llm: BaseLanguageModel, ai_plugin: AIPlugin,
requests: Optional[Requests]=None, verbose: bool=False, **kwargs: Any
) ->NLAToolkit:
"""Instantiate the toolkit from an OpenAPI Spec URL"""
spec = OpenAPISpec.from_url(ai_plugin.api.url)
return cls.from_llm_an... | Instantiate the toolkit from an OpenAPI Spec URL |
test_all_imports | assert sorted(EXPECTED_ALL) == sorted(__all__) | def test_all_imports() ->None:
assert sorted(EXPECTED_ALL) == sorted(__all__) | null |
test_load_success | docs = retriever.get_relevant_documents('HUNTER X HUNTER')
assert len(docs) > 1
assert len(docs) <= 3
assert_docs(docs, all_meta=False) | def test_load_success(retriever: WikipediaRetriever) ->None:
docs = retriever.get_relevant_documents('HUNTER X HUNTER')
assert len(docs) > 1
assert len(docs) <= 3
assert_docs(docs, all_meta=False) | null |
test_tag_filter_varied | if operation == '==':
tf = Tag('tag_field') == tags
elif operation == '!=':
tf = Tag('tag_field') != tags
else:
raise ValueError(f'Unsupported operation: {operation}')
assert str(tf) == expected | @pytest.mark.parametrize('operation,tags,expected', [('==', 'simpletag',
'@tag_field:{simpletag}'), ('==', 'tag with space',
'@tag_field:{tag\\ with\\ space}'), ('==', 'special$char',
'@tag_field:{special\\$char}'), ('!=', 'negated',
'(-@tag_field:{negated})'), ('==', ['tag1', 'tag2'],
'@tag_field:{... | null |
from_documents | """Create alibaba cloud opensearch vector store instance.
Args:
documents: Documents to be inserted into the vector storage,
should not be empty.
embedding: Embedding function, Embedding function.
config: Alibaba OpenSearch instance configuration.
id... | @classmethod
def from_documents(cls, documents: List[Document], embedding: Embeddings,
config: Optional[AlibabaCloudOpenSearchSettings]=None, **kwargs: Any
) ->'AlibabaCloudOpenSearch':
"""Create alibaba cloud opensearch vector store instance.
Args:
documents: Documents to be inserted i... | Create alibaba cloud opensearch vector store instance.
Args:
documents: Documents to be inserted into the vector storage,
should not be empty.
embedding: Embedding function, Embedding function.
config: Alibaba OpenSearch instance configuration.
ids: Specify the ID for the inserted document. If lef... |
_delete_doc | """
Delete a document from the Vectara corpus.
Args:
url (str): URL of the page to delete.
doc_id (str): ID of the document to delete.
Returns:
bool: True if deletion was successful, False otherwise.
"""
body = {'customer_id': self._vectara_customer_... | def _delete_doc(self, doc_id: str) ->bool:
"""
Delete a document from the Vectara corpus.
Args:
url (str): URL of the page to delete.
doc_id (str): ID of the document to delete.
Returns:
bool: True if deletion was successful, False otherwise.
"""... | Delete a document from the Vectara corpus.
Args:
url (str): URL of the page to delete.
doc_id (str): ID of the document to delete.
Returns:
bool: True if deletion was successful, False otherwise. |
warning_emitting_wrapper | """Wrapper for the original wrapped callable that emits a warning.
Args:
*args: The positional arguments to the function.
**kwargs: The keyword arguments to the function.
Returns:
The return value of the function being wrapped.
"""
em... | def warning_emitting_wrapper(*args: Any, **kwargs: Any) ->Any:
"""Wrapper for the original wrapped callable that emits a warning.
Args:
*args: The positional arguments to the function.
**kwargs: The keyword arguments to the function.
Returns:
... | Wrapper for the original wrapped callable that emits a warning.
Args:
*args: The positional arguments to the function.
**kwargs: The keyword arguments to the function.
Returns:
The return value of the function being wrapped. |
test_vald_delete | texts = ['foo', 'bar', 'baz']
metadatas = [{'page': i} for i in range(len(texts))]
docsearch = _vald_from_texts(metadatas=metadatas)
time.sleep(WAIT_TIME)
output = docsearch.similarity_search('foo', k=10)
assert len(output) == 3
docsearch.delete(['foo'])
time.sleep(WAIT_TIME)
output = docsearch.similarity_search('foo',... | def test_vald_delete() ->None:
texts = ['foo', 'bar', 'baz']
metadatas = [{'page': i} for i in range(len(texts))]
docsearch = _vald_from_texts(metadatas=metadatas)
time.sleep(WAIT_TIME)
output = docsearch.similarity_search('foo', k=10)
assert len(output) == 3
docsearch.delete(['foo'])
ti... | null |
_load | """
load vearch engine for standalone vearch
"""
self.vearch.load() | def _load(self) ->None:
"""
load vearch engine for standalone vearch
"""
self.vearch.load() | load vearch engine for standalone vearch |
test_generativeai_stream | llm = GooglePalm(temperature=0, model_name='gemini-pro')
outputs = list(llm.stream('Please say foo:'))
assert isinstance(outputs[0], str) | def test_generativeai_stream() ->None:
llm = GooglePalm(temperature=0, model_name='gemini-pro')
outputs = list(llm.stream('Please say foo:'))
assert isinstance(outputs[0], str) | null |
_import_meilisearch | from langchain_community.vectorstores.meilisearch import Meilisearch
return Meilisearch | def _import_meilisearch() ->Any:
from langchain_community.vectorstores.meilisearch import Meilisearch
return Meilisearch | null |
__init__ | from langchain.hub import pull
pulled = pull(owner_repo_commit, api_url=api_url, api_key=api_key)
super_kwargs = {'kwargs': {}, 'config': {}, **kwargs, 'bound': pulled,
'owner_repo_commit': owner_repo_commit}
super().__init__(**super_kwargs) | def __init__(self, owner_repo_commit: str, *, api_url: Optional[str]=None,
api_key: Optional[str]=None, **kwargs: Any) ->None:
from langchain.hub import pull
pulled = pull(owner_repo_commit, api_url=api_url, api_key=api_key)
super_kwargs = {'kwargs': {}, 'config': {}, **kwargs, 'bound': pulled,
... | null |
_generate | """Top Level call""" | @abstractmethod
def _generate(self, messages: List[BaseMessage], stop: Optional[List[str]]=
None, run_manager: Optional[CallbackManagerForLLMRun]=None, **kwargs: Any
) ->ChatResult:
"""Top Level call""" | Top Level call |
test_trajectory_output_parser_parse | trajectory_output_parser = TrajectoryOutputParser()
text = """Judgment: Given the good reasoning in the final answer
but otherwise poor performance, we give the model a score of 2.
Score: 2"""
got = trajectory_output_parser.parse_folder(text)
want = TrajectoryEval(score=0.25, reasoning=
"""Judgment: Given the good... | def test_trajectory_output_parser_parse() ->None:
trajectory_output_parser = TrajectoryOutputParser()
text = """Judgment: Given the good reasoning in the final answer
but otherwise poor performance, we give the model a score of 2.
Score: 2"""
got = trajectory_output_parser.parse_folder(text)
want = Tra... | null |
add_texts | """
Add texts along with their corresponding embeddings and optional
metadata to the BagelDB cluster.
Args:
texts (Iterable[str]): Texts to be added.
embeddings (Optional[List[float]]): List of embeddingvectors
metadatas (Optional[List[dict]]): Optional list ... | def add_texts(self, texts: Iterable[str], metadatas: Optional[List[dict]]=
None, ids: Optional[List[str]]=None, embeddings: Optional[List[List[
float]]]=None, **kwargs: Any) ->List[str]:
"""
Add texts along with their corresponding embeddings and optional
metadata to the BagelDB cluster.
... | Add texts along with their corresponding embeddings and optional
metadata to the BagelDB cluster.
Args:
texts (Iterable[str]): Texts to be added.
embeddings (Optional[List[float]]): List of embeddingvectors
metadatas (Optional[List[dict]]): Optional list of metadatas.
ids (Optional[List[str]]): List of... |
_convert_messages_to_ollama_messages | ollama_messages = []
for message in messages:
role = ''
if isinstance(message, HumanMessage):
role = 'user'
elif isinstance(message, AIMessage):
role = 'assistant'
elif isinstance(message, SystemMessage):
role = 'system'
else:
raise ValueError('Received unsupported me... | def _convert_messages_to_ollama_messages(self, messages: List[BaseMessage]
) ->List[Dict[str, Union[str, List[str]]]]:
ollama_messages = []
for message in messages:
role = ''
if isinstance(message, HumanMessage):
role = 'user'
elif isinstance(message, AIMessage):
... | null |
_run | """Use the tool."""
query_params = {'file_url': query, 'language': self.language,
'attributes_as_list': False}
return self._call_eden_ai(query_params) | def _run(self, query: str, run_manager: Optional[CallbackManagerForToolRun]
=None) ->str:
"""Use the tool."""
query_params = {'file_url': query, 'language': self.language,
'attributes_as_list': False}
return self._call_eden_ai(query_params) | Use the tool. |
similarity_search_with_score_by_vector | """Return docs most similar to query vector, with its score."""
with Session(self._engine) as _session:
real_distance_func = (self._table.embedding.squared_euclidean_distance if
distance_func == 'sqrt_euclid' else self._table.embedding.
negative_dot_product_distance if distance_func == 'neg_dot_prod... | def similarity_search_with_score_by_vector(self, query_vector: List[float],
k: int=4, distance_func: Literal['sqrt_euclid', 'neg_dot_prod',
'ned_cos']='sqrt_euclid', **kwargs: Any) ->List[Tuple[Document, float]]:
"""Return docs most similar to query vector, with its score."""
with Session(self._engine) ... | Return docs most similar to query vector, with its score. |
test_all_imports | assert set(__all__) == set(EXPECTED_ALL) | def test_all_imports() ->None:
assert set(__all__) == set(EXPECTED_ALL) | null |
validate_environment | """Validate that api key and python package exists in environment."""
wolfram_alpha_appid = get_from_dict_or_env(values, 'wolfram_alpha_appid',
'WOLFRAM_ALPHA_APPID')
values['wolfram_alpha_appid'] = wolfram_alpha_appid
try:
import wolframalpha
except ImportError:
raise ImportError(
'wolframalpha is ... | @root_validator()
def validate_environment(cls, values: Dict) ->Dict:
"""Validate that api key and python package exists in environment."""
wolfram_alpha_appid = get_from_dict_or_env(values,
'wolfram_alpha_appid', 'WOLFRAM_ALPHA_APPID')
values['wolfram_alpha_appid'] = wolfram_alpha_appid
try:
... | Validate that api key and python package exists in environment. |
_import_dashvector | from langchain_community.vectorstores.dashvector import DashVector
return DashVector | def _import_dashvector() ->Any:
from langchain_community.vectorstores.dashvector import DashVector
return DashVector | null |
test_special_tokens | assert len(_get_token_ids_default_method('a:b_c d')) == 6 | def test_special_tokens(self) ->None:
assert len(_get_token_ids_default_method('a:b_c d')) == 6 | null |
test_cleanup_with_different_batchsize | """Check that we can clean up with different batch size."""
docs = [Document(page_content='This is a test document.', metadata={
'source': str(d)}) for d in range(1000)]
assert index(docs, record_manager, vector_store, cleanup='full') == {
'num_added': 1000, 'num_deleted': 0, 'num_skipped': 0, 'num_updated': 0}... | def test_cleanup_with_different_batchsize(record_manager: SQLRecordManager,
vector_store: VectorStore) ->None:
"""Check that we can clean up with different batch size."""
docs = [Document(page_content='This is a test document.', metadata={
'source': str(d)}) for d in range(1000)]
assert index(do... | Check that we can clean up with different batch size. |
embed_query | """Return simple embeddings."""
return [float(1.0)] * (ADA_TOKEN_COUNT - 1) + [float(0.0)] | def embed_query(self, text: str) ->List[float]:
"""Return simple embeddings."""
return [float(1.0)] * (ADA_TOKEN_COUNT - 1) + [float(0.0)] | Return simple embeddings. |
from_texts | """Return Vearch VectorStore"""
vearch_db = cls(embedding_function=embedding, embedding=embedding,
path_or_url=path_or_url, db_name=db_name, table_name=table_name, flag=flag)
vearch_db.add_texts(texts=texts, metadatas=metadatas)
return vearch_db | @classmethod
def from_texts(cls: Type[Vearch], texts: List[str], embedding: Embeddings,
metadatas: Optional[List[dict]]=None, path_or_url: Optional[str]=None,
table_name: str=_DEFAULT_TABLE_NAME, db_name: str=
_DEFAULT_CLUSTER_DB_NAME, flag: int=_DEFAULT_VERSION, **kwargs: Any
) ->Vearch:
"""Return ... | Return Vearch VectorStore |
test_get_action_and_input_sql_query | """Test getting the action and action input from the text
when the LLM output is a well formed SQL query
"""
llm_output = """
I should query for the largest single shift payment for every unique user.
Action: query_sql_db
Action Input: SELECT "UserName", MAX(totalpayment) FROM user_shifts GROUP ... | def test_get_action_and_input_sql_query() ->None:
"""Test getting the action and action input from the text
when the LLM output is a well formed SQL query
"""
llm_output = """
I should query for the largest single shift payment for every unique user.
Action: query_sql_db
Action Input: SE... | Test getting the action and action input from the text
when the LLM output is a well formed SQL query |
test_huggingface_type_check | """Test that type checks are done properly on input."""
with pytest.raises(ValueError):
CharacterTextSplitter.from_huggingface_tokenizer('foo') | def test_huggingface_type_check() ->None:
"""Test that type checks are done properly on input."""
with pytest.raises(ValueError):
CharacterTextSplitter.from_huggingface_tokenizer('foo') | Test that type checks are done properly on input. |
_get_relevant_documents | """Get documents relevant for a query.
Args:
query: string to find relevant documents for
Returns:
Sequence of relevant documents
"""
docs = self.base_retriever.get_relevant_documents(query, callbacks=
run_manager.get_child(), **kwargs)
if docs:
compressed_docs ... | def _get_relevant_documents(self, query: str, *, run_manager:
CallbackManagerForRetrieverRun, **kwargs: Any) ->List[Document]:
"""Get documents relevant for a query.
Args:
query: string to find relevant documents for
Returns:
Sequence of relevant documents
"""
... | Get documents relevant for a query.
Args:
query: string to find relevant documents for
Returns:
Sequence of relevant documents |
test_character_text_splitter_discard_separator_regex | """Test splitting by characters discarding the separator
that is a regex special character."""
text = 'foo.bar.baz.123'
splitter = CharacterTextSplitter(separator=separator, chunk_size=1,
chunk_overlap=0, keep_separator=False, is_separator_regex=
is_separator_regex)
output = splitter.split_text(text)
expect... | @pytest.mark.parametrize('separator, is_separator_regex', [(re.escape('.'),
True), ('.', False)])
def test_character_text_splitter_discard_separator_regex(separator: str,
is_separator_regex: bool) ->None:
"""Test splitting by characters discarding the separator
that is a regex special character."""
... | Test splitting by characters discarding the separator
that is a regex special character. |
__init__ | """Initialize the class."""
if not endpoint_api_key or not endpoint_url:
raise ValueError(
"""A key/token and REST endpoint should
be provided to invoke the endpoint"""
)
self.endpoint_url = endpoint_url
self.endpoint_api_key = endpoint_api_key
self.deployment_name = deployment_name | def __init__(self, endpoint_url: str, endpoint_api_key: str,
deployment_name: str='') ->None:
"""Initialize the class."""
if not endpoint_api_key or not endpoint_url:
raise ValueError(
"""A key/token and REST endpoint should
be provided to invoke the endpoint"""
... | Initialize the class. |
get_format_instructions | return self.parser.get_format_instructions() | def get_format_instructions(self) ->str:
return self.parser.get_format_instructions() | null |
_default_params | """Get the default parameters for calling Cohere API."""
return {'temperature': self.temperature} | @property
def _default_params(self) ->Dict[str, Any]:
"""Get the default parameters for calling Cohere API."""
return {'temperature': self.temperature} | Get the default parameters for calling Cohere API. |
test_chroma_search_filter | """Test end to end construction and search with metadata filtering."""
texts = ['far', 'bar', 'baz']
metadatas = [{'first_letter': '{}'.format(text[0])} for text in texts]
docsearch = Chroma.from_texts(collection_name='test_collection', texts=
texts, embedding=FakeEmbeddings(), metadatas=metadatas)
output = docsear... | def test_chroma_search_filter() ->None:
"""Test end to end construction and search with metadata filtering."""
texts = ['far', 'bar', 'baz']
metadatas = [{'first_letter': '{}'.format(text[0])} for text in texts]
docsearch = Chroma.from_texts(collection_name='test_collection', texts=
texts, embed... | Test end to end construction and search with metadata filtering. |
_run | """Use the tool."""
try:
text_analysis_result = self._text_analysis(query)
return self._format_text_analysis_result(text_analysis_result)
except Exception as e:
raise RuntimeError(
f'Error while running AzureCogsTextAnalyticsHealthTool: {e}') | def _run(self, query: str, run_manager: Optional[CallbackManagerForToolRun]
=None) ->str:
"""Use the tool."""
try:
text_analysis_result = self._text_analysis(query)
return self._format_text_analysis_result(text_analysis_result)
except Exception as e:
raise RuntimeError(
... | Use the tool. |
from_texts | raise NotImplementedError | @classmethod
def from_texts(cls: Type[VST], texts: List[str], embedding: Embeddings,
metadatas: Optional[List[dict]]=None, **kwargs: Any) ->VST:
raise NotImplementedError | null |
test_add_texts_with_given_embedding | texts = ['foo', 'bar', 'baz']
embedding = FakeEmbeddings()
docsearch = Weaviate.from_texts(texts, embedding=embedding, weaviate_url=
weaviate_url)
docsearch.add_texts(['foo'])
output = docsearch.similarity_search_by_vector(embedding.embed_query('foo'),
k=2)
assert output == [Document(page_content='foo'), Docume... | def test_add_texts_with_given_embedding(self, weaviate_url: str) ->None:
texts = ['foo', 'bar', 'baz']
embedding = FakeEmbeddings()
docsearch = Weaviate.from_texts(texts, embedding=embedding,
weaviate_url=weaviate_url)
docsearch.add_texts(['foo'])
output = docsearch.similarity_search_by_vect... | null |
stream | """
Default implementation of stream, which calls invoke.
Subclasses should override this method if they support streaming output.
"""
yield self.invoke(input, config, **kwargs) | def stream(self, input: Input, config: Optional[RunnableConfig]=None, **
kwargs: Optional[Any]) ->Iterator[Output]:
"""
Default implementation of stream, which calls invoke.
Subclasses should override this method if they support streaming output.
"""
yield self.invoke(input, config, ... | Default implementation of stream, which calls invoke.
Subclasses should override this method if they support streaming output. |
_import_gradient_ai | from langchain_community.llms.gradient_ai import GradientLLM
return GradientLLM | def _import_gradient_ai() ->Any:
from langchain_community.llms.gradient_ai import GradientLLM
return GradientLLM | null |
input_keys | return [self.input_key] | @property
def input_keys(self) ->List[str]:
return [self.input_key] | null |
search | """Return the fake document."""
document = Document(page_content=_PAGE_CONTENT)
return document | def search(self, search: str) ->Union[str, Document]:
"""Return the fake document."""
document = Document(page_content=_PAGE_CONTENT)
return document | Return the fake document. |
__init__ | super().__init__(*args, **kwargs)
if model is None:
from sentence_transformers import SentenceTransformer
model = SentenceTransformer('all-mpnet-base-v2')
self.model = model
self.auto_embed = auto_embed | def __init__(self, auto_embed: bool, model: Optional[Any]=None, *args: Any,
**kwargs: Any):
super().__init__(*args, **kwargs)
if model is None:
from sentence_transformers import SentenceTransformer
model = SentenceTransformer('all-mpnet-base-v2')
self.model = model
self.auto_embed = ... | null |
test_named_tool_decorator | """Test functionality when arguments are provided as input to decorator."""
@tool('search')
def search_api(query: str) ->str:
"""Search the API for the query."""
assert isinstance(query, str)
return f'API result - {query}'
assert isinstance(search_api, BaseTool)
assert search_api.name == 'search'
assert not... | def test_named_tool_decorator() ->None:
"""Test functionality when arguments are provided as input to decorator."""
@tool('search')
def search_api(query: str) ->str:
"""Search the API for the query."""
assert isinstance(query, str)
return f'API result - {query}'
assert isinstanc... | Test functionality when arguments are provided as input to decorator. |
_import_bulk | """Import bulk if available, otherwise raise error."""
try:
from opensearchpy.helpers import bulk
except ImportError:
raise ImportError(IMPORT_OPENSEARCH_PY_ERROR)
return bulk | def _import_bulk() ->Any:
"""Import bulk if available, otherwise raise error."""
try:
from opensearchpy.helpers import bulk
except ImportError:
raise ImportError(IMPORT_OPENSEARCH_PY_ERROR)
return bulk | Import bulk if available, otherwise raise error. |
history2 | history2 = AstraDBChatMessageHistory(session_id='session-test-2',
collection_name='langchain_cmh_test', token=os.environ[
'ASTRA_DB_APPLICATION_TOKEN'], api_endpoint=os.environ[
'ASTRA_DB_API_ENDPOINT'], namespace=os.environ.get('ASTRA_DB_KEYSPACE'))
yield history2
history2.astra_db.delete_collection('langc... | @pytest.fixture(scope='function')
def history2() ->Iterable[AstraDBChatMessageHistory]:
history2 = AstraDBChatMessageHistory(session_id='session-test-2',
collection_name='langchain_cmh_test', token=os.environ[
'ASTRA_DB_APPLICATION_TOKEN'], api_endpoint=os.environ[
'ASTRA_DB_API_ENDPOINT'], ... | null |
_on_chain_end | """Process the Chain Run.""" | def _on_chain_end(self, run: Run) ->None:
"""Process the Chain Run.""" | Process the Chain Run. |
test_similarity_search_with_metadata_and_filter | """Test end to end construction and search with metadata."""
texts = ['foo', 'bar', 'baz']
metadatas = [{'page': i} for i in range(len(texts))]
docsearch = Weaviate.from_texts(texts, embedding_openai, metadatas=
metadatas, weaviate_url=weaviate_url)
output = docsearch.similarity_search('foo', k=2, where_filter={'pa... | @pytest.mark.vcr(ignore_localhost=True)
def test_similarity_search_with_metadata_and_filter(self, weaviate_url: str,
embedding_openai: OpenAIEmbeddings) ->None:
"""Test end to end construction and search with metadata."""
texts = ['foo', 'bar', 'baz']
metadatas = [{'page': i} for i in range(len(texts))]... | Test end to end construction and search with metadata. |
lc_secrets | return {'gpt_router_api_key': 'GPT_ROUTER_API_KEY'} | @property
def lc_secrets(self) ->Dict[str, str]:
return {'gpt_router_api_key': 'GPT_ROUTER_API_KEY'} | null |
OutputType | return List[self.bound.OutputType] | @property
def OutputType(self) ->Type[List[Output]]:
return List[self.bound.OutputType] | null |
test_qdrant_similarity_search | """Test end to end construction and search."""
texts = ['foo', 'bar', 'baz']
docsearch = Qdrant.from_texts(texts, ConsistentFakeEmbeddings(), location=
':memory:', content_payload_key=content_payload_key,
metadata_payload_key=metadata_payload_key, batch_size=batch_size,
vector_name=vector_name)
output = doc... | @pytest.mark.parametrize('batch_size', [1, 64])
@pytest.mark.parametrize('content_payload_key', [Qdrant.CONTENT_KEY, 'foo'])
@pytest.mark.parametrize('metadata_payload_key', [Qdrant.METADATA_KEY, 'bar'])
@pytest.mark.parametrize('vector_name', [None, 'my-vector'])
def test_qdrant_similarity_search(batch_size: int, cont... | Test end to end construction and search. |
test_baichuan_key_masked_when_passed_via_constructor | """Test initialization with an API key provided via the initializer"""
chat = ChatBaichuan(baichuan_api_key='test-api-key', baichuan_secret_key=
'test-secret-key')
print(chat.baichuan_api_key, end='')
captured = capsys.readouterr()
assert captured.out == '**********'
print(chat.baichuan_secret_key, end='')
captured... | def test_baichuan_key_masked_when_passed_via_constructor(capsys: CaptureFixture
) ->None:
"""Test initialization with an API key provided via the initializer"""
chat = ChatBaichuan(baichuan_api_key='test-api-key',
baichuan_secret_key='test-secret-key')
print(chat.baichuan_api_key, end='')
ca... | Test initialization with an API key provided via the initializer |
test_pdfminer_parser | """Test PDFMiner parser."""
_assert_with_parser(PDFMinerParser(), splits_by_page=False) | @pytest.mark.requires('pdfminer')
def test_pdfminer_parser() ->None:
"""Test PDFMiner parser."""
_assert_with_parser(PDFMinerParser(), splits_by_page=False) | Test PDFMiner parser. |
load | return list(self.lazy_load()) | def load(self) ->List[Document]:
return list(self.lazy_load()) | null |
from_texts | try:
from sklearn.feature_extraction.text import TfidfVectorizer
except ImportError:
raise ImportError(
'Could not import scikit-learn, please install with `pip install scikit-learn`.'
)
tfidf_params = tfidf_params or {}
vectorizer = TfidfVectorizer(**tfidf_params)
tfidf_array = vectorizer.fit_t... | @classmethod
def from_texts(cls, texts: Iterable[str], metadatas: Optional[Iterable[dict
]]=None, tfidf_params: Optional[Dict[str, Any]]=None, **kwargs: Any
) ->TFIDFRetriever:
try:
from sklearn.feature_extraction.text import TfidfVectorizer
except ImportError:
raise ImportError(
... | null |
file_to_base64 | """Convert a file to base64."""
with open(path, 'rb') as f:
return base64.b64encode(f.read()).decode() | def file_to_base64(path: str) ->str:
"""Convert a file to base64."""
with open(path, 'rb') as f:
return base64.b64encode(f.read()).decode() | Convert a file to base64. |
test_valid_single_message | expected_output = f'Human: {self.human_msg.content}'
self.assertEqual(get_buffer_string([self.human_msg]), expected_output) | def test_valid_single_message(self) ->None:
expected_output = f'Human: {self.human_msg.content}'
self.assertEqual(get_buffer_string([self.human_msg]), expected_output) | null |
_import_searx_search | from langchain_community.utilities.searx_search import SearxSearchWrapper
return SearxSearchWrapper | def _import_searx_search() ->Any:
from langchain_community.utilities.searx_search import SearxSearchWrapper
return SearxSearchWrapper | null |
_llm_type | return 'llama-2-chat' | @property
def _llm_type(self) ->str:
return 'llama-2-chat' | null |
_parse_input | """Parse the json string into a dict."""
return json.loads(text) | def _parse_input(text: str) ->Dict[str, Any]:
"""Parse the json string into a dict."""
return json.loads(text) | Parse the json string into a dict. |
validate_environment | values['qianfan_ak'] = convert_to_secret_str(get_from_dict_or_env(values,
'qianfan_ak', 'QIANFAN_AK', default=''))
values['qianfan_sk'] = convert_to_secret_str(get_from_dict_or_env(values,
'qianfan_sk', 'QIANFAN_SK', default=''))
params = {**values.get('init_kwargs', {}), 'model': values['model'],
'stream':... | @root_validator()
def validate_environment(cls, values: Dict) ->Dict:
values['qianfan_ak'] = convert_to_secret_str(get_from_dict_or_env(
values, 'qianfan_ak', 'QIANFAN_AK', default=''))
values['qianfan_sk'] = convert_to_secret_str(get_from_dict_or_env(
values, 'qianfan_sk', 'QIANFAN_SK', default... | null |
_call | """Call the Llama model and return the output.
Args:
prompt: The prompt to use for generation.
stop: A list of strings to stop generation when encountered.
Returns:
The generated text.
Example:
.. code-block:: python
from langch... | def _call(self, prompt: str, stop: Optional[List[str]]=None, run_manager:
Optional[CallbackManagerForLLMRun]=None, **kwargs: Any) ->str:
"""Call the Llama model and return the output.
Args:
prompt: The prompt to use for generation.
stop: A list of strings to stop generation when... | Call the Llama model and return the output.
Args:
prompt: The prompt to use for generation.
stop: A list of strings to stop generation when encountered.
Returns:
The generated text.
Example:
.. code-block:: python
from langchain_community.llms import LlamaCpp
llm = LlamaCpp(model_pat... |
get_sync | """Get the equivalent sync RunManager.
Returns:
RunManager: The sync RunManager.
""" | @abstractmethod
def get_sync(self) ->RunManager:
"""Get the equivalent sync RunManager.
Returns:
RunManager: The sync RunManager.
""" | Get the equivalent sync RunManager.
Returns:
RunManager: The sync RunManager. |
test_openai_call | """Test valid call to openai."""
llm = OpenAI()
output = llm('Say something nice:')
assert isinstance(output, str) | @pytest.mark.scheduled
def test_openai_call() ->None:
"""Test valid call to openai."""
llm = OpenAI()
output = llm('Say something nice:')
assert isinstance(output, str) | Test valid call to openai. |
from_texts | """Return VectorStore initialized from texts and embeddings."""
if not index_name:
raise ValueError('`index_name` is required')
if not client:
client = TigrisClient()
store = cls(client, embedding, index_name)
store.add_texts(texts=texts, metadatas=metadatas, ids=ids)
return store | @classmethod
def from_texts(cls, texts: List[str], embedding: Embeddings, metadatas:
Optional[List[dict]]=None, ids: Optional[List[str]]=None, client:
Optional[TigrisClient]=None, index_name: Optional[str]=None, **kwargs: Any
) ->Tigris:
"""Return VectorStore initialized from texts and embeddings."""
... | Return VectorStore initialized from texts and embeddings. |
process_image | try:
import pytesseract
from PIL import Image
except ImportError:
raise ImportError(
'`pytesseract` or `Pillow` package not found, please run `pip install pytesseract Pillow`'
)
response = self.confluence.request(path=link, absolute=True)
text = ''
if response.status_code != 200 or response.... | def process_image(self, link: str, ocr_languages: Optional[str]=None) ->str:
try:
import pytesseract
from PIL import Image
except ImportError:
raise ImportError(
'`pytesseract` or `Pillow` package not found, please run `pip install pytesseract Pillow`'
)
respo... | null |
_llm_type | """Return type of llm."""
return 'bananadev' | @property
def _llm_type(self) ->str:
"""Return type of llm."""
return 'bananadev' | Return type of llm. |
_read_json | """Read JSON data from a zip subfile."""
with zip_file.open(file_path, 'r') as f:
data = json.load(f)
return data | def _read_json(self, zip_file: zipfile.ZipFile, file_path: str) ->List[dict]:
"""Read JSON data from a zip subfile."""
with zip_file.open(file_path, 'r') as f:
data = json.load(f)
return data | Read JSON data from a zip subfile. |
_validate_func | if isinstance(func, Operator) and self.allowed_operators is not None:
if func not in self.allowed_operators:
raise ValueError(
f'Received disallowed operator {func}. Allowed comparators are {self.allowed_operators}'
)
if isinstance(func, Comparator) and self.allowed_comparators is no... | def _validate_func(self, func: Union[Operator, Comparator]) ->None:
if isinstance(func, Operator) and self.allowed_operators is not None:
if func not in self.allowed_operators:
raise ValueError(
f'Received disallowed operator {func}. Allowed comparators are {self.allowed_operator... | null |
get_lc_namespace | """Get the namespace of the langchain object."""
return ['langchain', 'llms', 'replicate'] | @classmethod
def get_lc_namespace(cls) ->List[str]:
"""Get the namespace of the langchain object."""
return ['langchain', 'llms', 'replicate'] | Get the namespace of the langchain object. |
resolve_prompt_inputs | return {'question': self.question, **{f'idea_{i + 1}': idea for i, idea in
enumerate(self.ideas)}, 'critique': self.critique} | def resolve_prompt_inputs(self) ->Dict[str, Any]:
return {'question': self.question, **{f'idea_{i + 1}': idea for i, idea in
enumerate(self.ideas)}, 'critique': self.critique} | null |
__getitem__ | return self._children[item] | def __getitem__(self, item: Union[int, slice]) ->Union[Iterator[T], Tuple[
Iterator[T], ...]]:
return self._children[item] | null |
test_openai_streaming_best_of_error | """Test validation for streaming fails if best_of is not 1."""
with pytest.raises(ValueError):
_get_llm(best_of=2, streaming=True) | def test_openai_streaming_best_of_error() ->None:
"""Test validation for streaming fails if best_of is not 1."""
with pytest.raises(ValueError):
_get_llm(best_of=2, streaming=True) | Test validation for streaming fails if best_of is not 1. |
upsert_messages | """Update the cosmosdb item."""
if not self._container:
raise ValueError('Container not initialized')
self._container.upsert_item(body={'id': self.session_id, 'user_id': self.
user_id, 'messages': messages_to_dict(self.messages)}) | def upsert_messages(self) ->None:
"""Update the cosmosdb item."""
if not self._container:
raise ValueError('Container not initialized')
self._container.upsert_item(body={'id': self.session_id, 'user_id':
self.user_id, 'messages': messages_to_dict(self.messages)}) | Update the cosmosdb item. |
test_from_existing_index_with_namespaces | """Test that namespaces are properly handled."""
texts_1 = ['foo', 'bar', 'baz']
metadatas = [{'page': i} for i in range(len(texts_1))]
Pinecone.from_texts(texts_1, embedding_openai, index_name=index_name,
metadatas=metadatas, namespace=f'{index_name}-1')
texts_2 = ['foo2', 'bar2', 'baz2']
metadatas = [{'page': i} ... | def test_from_existing_index_with_namespaces(self, embedding_openai:
OpenAIEmbeddings) ->None:
"""Test that namespaces are properly handled."""
texts_1 = ['foo', 'bar', 'baz']
metadatas = [{'page': i} for i in range(len(texts_1))]
Pinecone.from_texts(texts_1, embedding_openai, index_name=index_name,... | Test that namespaces are properly handled. |
get_relevant_documents | assert isinstance(self, FakeRetrieverV1)
return [Document(page_content=query, metadata=where_filter or {})] | def get_relevant_documents(self, query: str, where_filter: Optional[Dict[
str, object]]=None) ->List[Document]:
assert isinstance(self, FakeRetrieverV1)
return [Document(page_content=query, metadata=where_filter or {})] | null |
save_history | input.pop('response')
graph.query(
"""MERGE (u:User {id: $user_id})
WITH u
OPTIONAL MATCH (u)-[:HAS_SESSION]->(s:Session{id: $session_id}),
(s)-[l:LAST_MESSAGE]->(last_message)
FOREACH (_ IN CASE WHEN last_message IS NULL THEN [1] ELSE [] END |
CREATE (u)-[:HAS_SESSION]->(s1:Session ... | def save_history(input):
input.pop('response')
graph.query(
"""MERGE (u:User {id: $user_id})
WITH u
OPTIONAL MATCH (u)-[:HAS_SESSION]->(s:Session{id: $session_id}),
(s)-[l:LAST_MESSAGE]->(last_message)
FOREACH (_ IN CASE WHEN last_message IS NULL THEN [1] ELSE [] END |
CR... | null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.