method_name stringlengths 1 78 | method_body stringlengths 3 9.66k | full_code stringlengths 31 10.7k | docstring stringlengths 4 4.74k ⌀ |
|---|---|---|---|
get | return self.store.get(key, default) | def get(self, key: str, default: Optional[str]=None) ->Optional[str]:
return self.store.get(key, default) | null |
_run | return 'foo' | def _run(self, some_arg: str, run_manager: Optional[
CallbackManagerForToolRun]=None, **kwargs: Any) ->str:
return 'foo' | null |
_initialize_table | """Validates or creates the BigQuery table."""
from google.cloud import bigquery
table_ref = bigquery.TableReference.from_string(self._full_table_id)
table = self.bq_client.create_table(table_ref, exists_ok=True)
changed_schema = False
schema = table.schema.copy()
columns = {c.name: c for c in schema}
if self.doc_id_fi... | def _initialize_table(self) ->Any:
"""Validates or creates the BigQuery table."""
from google.cloud import bigquery
table_ref = bigquery.TableReference.from_string(self._full_table_id)
table = self.bq_client.create_table(table_ref, exists_ok=True)
changed_schema = False
schema = table.schema.cop... | Validates or creates the BigQuery table. |
on_agent_finish | """Run when agent ends running."""
self.metrics['step'] += 1
self.metrics['agent_ends'] += 1
self.metrics['ends'] += 1
agent_ends = self.metrics['agent_ends']
resp: Dict[str, Any] = {}
resp.update({'action': 'on_agent_finish', 'output': finish.return_values[
'output'], 'log': finish.log})
resp.update(self.metrics)
... | def on_agent_finish(self, finish: AgentFinish, **kwargs: Any) ->None:
"""Run when agent ends running."""
self.metrics['step'] += 1
self.metrics['agent_ends'] += 1
self.metrics['ends'] += 1
agent_ends = self.metrics['agent_ends']
resp: Dict[str, Any] = {}
resp.update({'action': 'on_agent_fini... | Run when agent ends running. |
test_valid_formatting | """Test formatting works as expected."""
template = 'This is a {foo} test.'
output = formatter.format(template, foo='good')
expected_output = 'This is a good test.'
assert output == expected_output | def test_valid_formatting() ->None:
"""Test formatting works as expected."""
template = 'This is a {foo} test.'
output = formatter.format(template, foo='good')
expected_output = 'This is a good test.'
assert output == expected_output | Test formatting works as expected. |
stream | if type(self)._stream == BaseLLM._stream:
yield self.invoke(input, config=config, stop=stop, **kwargs)
else:
prompt = self._convert_input(input).to_string()
config = ensure_config(config)
params = self.dict()
params['stop'] = stop
params = {**params, **kwargs}
options = {'stop': stop}
ca... | def stream(self, input: LanguageModelInput, config: Optional[RunnableConfig
]=None, *, stop: Optional[List[str]]=None, **kwargs: Any) ->Iterator[str]:
if type(self)._stream == BaseLLM._stream:
yield self.invoke(input, config=config, stop=stop, **kwargs)
else:
prompt = self._convert_input(inp... | null |
get_tool_label | """Return the label for an LLMThought that has an associated
tool.
Parameters
----------
tool
The tool's ToolRecord
is_complete
True if the thought is complete; False if the thought
is still receiving input.
Returns
-------
... | def get_tool_label(self, tool: ToolRecord, is_complete: bool) ->str:
"""Return the label for an LLMThought that has an associated
tool.
Parameters
----------
tool
The tool's ToolRecord
is_complete
True if the thought is complete; False if the thought... | Return the label for an LLMThought that has an associated
tool.
Parameters
----------
tool
The tool's ToolRecord
is_complete
True if the thought is complete; False if the thought
is still receiving input.
Returns
-------
The markdown label for the thought's container. |
_get_llm_cache | index_name = self._index_name(llm_string)
if index_name in self._cache_dict:
return self._cache_dict[index_name]
try:
self._cache_dict[index_name] = RedisVectorstore.from_existing_index(
embedding=self.embedding, index_name=index_name, redis_url=self.
redis_url, schema=cast(Dict, self.DEFAULT_SC... | def _get_llm_cache(self, llm_string: str) ->RedisVectorstore:
index_name = self._index_name(llm_string)
if index_name in self._cache_dict:
return self._cache_dict[index_name]
try:
self._cache_dict[index_name] = RedisVectorstore.from_existing_index(
embedding=self.embedding, index... | null |
_stream | params = self._format_params(messages=messages, stop=stop, **kwargs)
with self._client.beta.messages.stream(**params) as stream:
for text in stream.text_stream:
yield ChatGenerationChunk(message=AIMessageChunk(content=text)) | def _stream(self, messages: List[BaseMessage], stop: Optional[List[str]]=
None, run_manager: Optional[CallbackManagerForLLMRun]=None, **kwargs: Any
) ->Iterator[ChatGenerationChunk]:
params = self._format_params(messages=messages, stop=stop, **kwargs)
with self._client.beta.messages.stream(**params) as ... | null |
validate_environment | """Validate that api key and python package exists in environment."""
steamship_api_key = get_from_dict_or_env(values, 'steamship_api_key',
'STEAMSHIP_API_KEY')
try:
from steamship import Steamship
except ImportError:
raise ImportError(
'steamship is not installed. Please install it with `pip instal... | @root_validator(pre=True)
def validate_environment(cls, values: Dict) ->Dict:
"""Validate that api key and python package exists in environment."""
steamship_api_key = get_from_dict_or_env(values, 'steamship_api_key',
'STEAMSHIP_API_KEY')
try:
from steamship import Steamship
except Impor... | Validate that api key and python package exists in environment. |
_get_tags | """Get combined tags for a run."""
tags = set(run.tags or [])
tags.update(self.tags or [])
return list(tags) | def _get_tags(self, run: Run) ->List[str]:
"""Get combined tags for a run."""
tags = set(run.tags or [])
tags.update(self.tags or [])
return list(tags) | Get combined tags for a run. |
ignore_chain | """Whether to ignore chain callbacks."""
return self.ignore_chain_ | @property
def ignore_chain(self) ->bool:
"""Whether to ignore chain callbacks."""
return self.ignore_chain_ | Whether to ignore chain callbacks. |
prompt_length | """Return the prompt length given the documents passed in.
This can be used by a caller to determine whether passing in a list
of documents would exceed a certain prompt length. This useful when
trying to ensure that the size of a prompt remains below a certain
context limit.
A... | def prompt_length(self, docs: List[Document], **kwargs: Any) ->Optional[int]:
"""Return the prompt length given the documents passed in.
This can be used by a caller to determine whether passing in a list
of documents would exceed a certain prompt length. This useful when
trying to ensure t... | Return the prompt length given the documents passed in.
This can be used by a caller to determine whether passing in a list
of documents would exceed a certain prompt length. This useful when
trying to ensure that the size of a prompt remains below a certain
context limit.
Args:
docs: List[Document], a list of do... |
test_load_returns_no_result | """Test that returns no docs"""
docs = api_client.load_docs('1605.08386WWW')
assert len(docs) == 0 | def test_load_returns_no_result(api_client: PubMedAPIWrapper) ->None:
"""Test that returns no docs"""
docs = api_client.load_docs('1605.08386WWW')
assert len(docs) == 0 | Test that returns no docs |
_default_parsing_function | return str(content.get_text()) | def _default_parsing_function(content: Any) ->str:
return str(content.get_text()) | null |
test_initialization | loader = GoogleSpeechToTextLoader(project_id='test_project_id', file_path=
'./testfile.mp3')
assert loader.project_id == 'test_project_id'
assert loader.file_path == './testfile.mp3'
assert loader.location == 'us-central1'
assert loader.recognizer_id == '_' | @pytest.mark.requires('google.api_core')
def test_initialization() ->None:
loader = GoogleSpeechToTextLoader(project_id='test_project_id',
file_path='./testfile.mp3')
assert loader.project_id == 'test_project_id'
assert loader.file_path == './testfile.mp3'
assert loader.location == 'us-central1'... | null |
create_kv_docstore | """Create a store for langchain Document objects from a bytes store.
This store does run time type checking to ensure that the values are
Document objects.
Args:
store: A bytes store to use as the underlying store.
key_encoder: A function to encode keys; if None uses identity function.
... | def create_kv_docstore(store: ByteStore, *, key_encoder: Optional[Callable[
[str], str]]=None) ->BaseStore[str, Document]:
"""Create a store for langchain Document objects from a bytes store.
This store does run time type checking to ensure that the values are
Document objects.
Args:
store... | Create a store for langchain Document objects from a bytes store.
This store does run time type checking to ensure that the values are
Document objects.
Args:
store: A bytes store to use as the underlying store.
key_encoder: A function to encode keys; if None uses identity function.
Returns:
A key-value ... |
_import_ddg_search_tool_DuckDuckGoSearchResults | from langchain_community.tools.ddg_search.tool import DuckDuckGoSearchResults
return DuckDuckGoSearchResults | def _import_ddg_search_tool_DuckDuckGoSearchResults() ->Any:
from langchain_community.tools.ddg_search.tool import DuckDuckGoSearchResults
return DuckDuckGoSearchResults | null |
_llm_type | """Return type of llm."""
return 'deepinfra' | @property
def _llm_type(self) ->str:
"""Return type of llm."""
return 'deepinfra' | Return type of llm. |
teardown_class | collection = prepare_collection()
collection.delete_many({}) | @classmethod
def teardown_class(cls) ->None:
collection = prepare_collection()
collection.delete_many({}) | null |
test_parse_disallowed_comparator | parser = get_parser(allowed_comparators=[Comparator.EQ])
with pytest.raises(ValueError):
parser.parse_folder('gt("a", 2)') | def test_parse_disallowed_comparator() ->None:
parser = get_parser(allowed_comparators=[Comparator.EQ])
with pytest.raises(ValueError):
parser.parse_folder('gt("a", 2)') | null |
validate_translator | """Validate translator."""
if 'structured_query_translator' not in values:
values['structured_query_translator'] = _get_builtin_translator(values[
'vectorstore'])
return values | @root_validator(pre=True)
def validate_translator(cls, values: Dict) ->Dict:
"""Validate translator."""
if 'structured_query_translator' not in values:
values['structured_query_translator'] = _get_builtin_translator(values
['vectorstore'])
return values | Validate translator. |
_get_link_ratio | links = section.find_all('a')
total_text = ''.join(str(s) for s in section.stripped_strings)
if len(total_text) == 0:
return 0
link_text = ''.join(str(string.string.strip()) for link in links for string in
link.strings if string)
return len(link_text) / len(total_text) | def _get_link_ratio(section: Tag) ->float:
links = section.find_all('a')
total_text = ''.join(str(s) for s in section.stripped_strings)
if len(total_text) == 0:
return 0
link_text = ''.join(str(string.string.strip()) for link in links for
string in link.strings if string)
return len(... | null |
retrieve_existing_index | """
Check if the vector index exists in the Neo4j database
and returns its embedding dimension.
This method queries the Neo4j database for existing indexes
and attempts to retrieve the dimension of the vector index
with the specified name. If the index exists, its dimension is r... | def retrieve_existing_index(self) ->Optional[int]:
"""
Check if the vector index exists in the Neo4j database
and returns its embedding dimension.
This method queries the Neo4j database for existing indexes
and attempts to retrieve the dimension of the vector index
with the ... | Check if the vector index exists in the Neo4j database
and returns its embedding dimension.
This method queries the Neo4j database for existing indexes
and attempts to retrieve the dimension of the vector index
with the specified name. If the index exists, its dimension is returned.
If the index doesn't exist, `None` ... |
__init__ | self.file_path = file_path if isinstance(file_path, str) else str(file_path)
self.encoding = encoding
self.namespaces = namespaces
self.skip_redirects = skip_redirects
self.stop_on_error = stop_on_error | def __init__(self, file_path: Union[str, Path], encoding: Optional[str]=
'utf8', namespaces: Optional[Sequence[int]]=None, skip_redirects:
Optional[bool]=False, stop_on_error: Optional[bool]=True):
self.file_path = file_path if isinstance(file_path, str) else str(file_path
)
self.encoding = enco... | null |
embed_documents | """Compute doc embeddings using Cloudflare Workers AI.
Args:
texts: The list of texts to embed.
Returns:
List of embeddings, one for each text.
"""
if self.strip_new_lines:
texts = [text.replace('\n', ' ') for text in texts]
batches = [texts[i:i + self.batch_size] f... | def embed_documents(self, texts: List[str]) ->List[List[float]]:
"""Compute doc embeddings using Cloudflare Workers AI.
Args:
texts: The list of texts to embed.
Returns:
List of embeddings, one for each text.
"""
if self.strip_new_lines:
texts = [text.re... | Compute doc embeddings using Cloudflare Workers AI.
Args:
texts: The list of texts to embed.
Returns:
List of embeddings, one for each text. |
_import_elastic_knn_search | from langchain_community.vectorstores.elastic_vector_search import ElasticKnnSearch
return ElasticKnnSearch | def _import_elastic_knn_search() ->Any:
from langchain_community.vectorstores.elastic_vector_search import ElasticKnnSearch
return ElasticKnnSearch | null |
_import_cohere | from langchain_community.llms.cohere import Cohere
return Cohere | def _import_cohere() ->Any:
from langchain_community.llms.cohere import Cohere
return Cohere | null |
pending | return any(task.pending() for task in self.tasks) | def pending(self) ->bool:
return any(task.pending() for task in self.tasks) | null |
_prepare | ... | @abstractmethod
def _prepare(self, config: Optional[RunnableConfig]=None) ->Tuple[Runnable[
Input, Output], RunnableConfig]:
... | null |
load | """Load and return all documents."""
return list(self.lazy_load()) | def load(self) ->List[Document]:
"""Load and return all documents."""
return list(self.lazy_load()) | Load and return all documents. |
weaviate_url | """Return the weaviate url."""
from weaviate import Client
url = 'http://localhost:8080'
yield url
client = Client(url)
client.schema.delete_all() | @pytest.fixture(scope='class', autouse=True)
def weaviate_url(self) ->Union[str, Generator[str, None, None]]:
"""Return the weaviate url."""
from weaviate import Client
url = 'http://localhost:8080'
yield url
client = Client(url)
client.schema.delete_all() | Return the weaviate url. |
delete_triple | """Delete a triple from the graph."""
if self._graph.has_edge(knowledge_triple.subject, knowledge_triple.object_):
self._graph.remove_edge(knowledge_triple.subject, knowledge_triple.object_) | def delete_triple(self, knowledge_triple: KnowledgeTriple) ->None:
"""Delete a triple from the graph."""
if self._graph.has_edge(knowledge_triple.subject, knowledge_triple.object_
):
self._graph.remove_edge(knowledge_triple.subject, knowledge_triple.
object_) | Delete a triple from the graph. |
get_pseudoanonymizer_mapping | try:
from faker import Faker
except ImportError as e:
raise ImportError(
'Could not import faker, please install it with `pip install Faker`.'
) from e
fake = Faker()
fake.seed_instance(seed)
return {'PERSON': lambda _: fake.name(), 'EMAIL_ADDRESS': lambda _: fake.
email(), 'PHONE_NUMBER': l... | def get_pseudoanonymizer_mapping(seed: Optional[int]=None) ->Dict[str, Callable
]:
try:
from faker import Faker
except ImportError as e:
raise ImportError(
'Could not import faker, please install it with `pip install Faker`.'
) from e
fake = Faker()
fake.seed_... | null |
add_content_field | if self.text is None:
self.text = []
for field in self.text:
if field.name == self.content_key:
return
self.text.append(TextFieldSchema(name=self.content_key)) | def add_content_field(self) ->None:
if self.text is None:
self.text = []
for field in self.text:
if field.name == self.content_key:
return
self.text.append(TextFieldSchema(name=self.content_key)) | null |
test_bad_action_input_line | """Test handling when no action input found."""
llm_output = """Thought: I need to search for NBA
Action: Search
Thought: NBA"""
with pytest.raises(OutputParserException) as e_info:
get_action_and_input(llm_output)
assert e_info.value.observation is not None | def test_bad_action_input_line() ->None:
"""Test handling when no action input found."""
llm_output = (
'Thought: I need to search for NBA\nAction: Search\nThought: NBA')
with pytest.raises(OutputParserException) as e_info:
get_action_and_input(llm_output)
assert e_info.value.observation... | Test handling when no action input found. |
test_list_output_parser | parser = QuestionListOutputParser()
result = parser.parse_folder(text)
assert result.lines == expected | @pytest.mark.parametrize('text,expected', (('1. Line one.\n', [
'1. Line one.\n']), ('1. Line one.', ['1. Line one.']), (
"""1. Line one.
2. Line two.
""", ['1. Line one.\n', '2. Line two.\n']),
("""1. Line one.
2. Line two.""", ['1. Line one.\n', '2. Line two.']),
("""1. Line one.
2. Line two.
3. Line ... | null |
test_cohere_call | """Test valid call to cohere."""
llm = Cohere(max_tokens=10)
output = llm('Say foo:')
assert isinstance(output, str) | def test_cohere_call() ->None:
"""Test valid call to cohere."""
llm = Cohere(max_tokens=10)
output = llm('Say foo:')
assert isinstance(output, str) | Test valid call to cohere. |
similarity_search_by_vector | """Run similarity search on query embedding
Args:
embedding (List[float]): Query embedding
k (int): Number of results to return. Defaults to 4.
Returns:
List of Documents most similar to the query
"""
async def _similarity_search_by_vector() ->List[Document]... | def similarity_search_by_vector(self, embedding: List[float], k: int=4, **
kwargs: Any) ->List[Document]:
"""Run similarity search on query embedding
Args:
embedding (List[float]): Query embedding
k (int): Number of results to return. Defaults to 4.
Returns:
... | Run similarity search on query embedding
Args:
embedding (List[float]): Query embedding
k (int): Number of results to return. Defaults to 4.
Returns:
List of Documents most similar to the query |
test_prompt_from_examples_valid | """Test prompt can be successfully constructed from examples."""
template = """Test Prompt:
Question: who are you?
Answer: foo
Question: what are you?
Answer: bar
Question: {question}
Answer:"""
input_variables = ['question']
example_separator = '\n\n'
prefix = 'Test Prompt:'
suffix = """Question: {question}
Answer:... | def test_prompt_from_examples_valid() ->None:
"""Test prompt can be successfully constructed from examples."""
template = """Test Prompt:
Question: who are you?
Answer: foo
Question: what are you?
Answer: bar
Question: {question}
Answer:"""
input_variables = ['question']
example_separator = '\n\n'
... | Test prompt can be successfully constructed from examples. |
__init__ | """Initialize with API token and the URLs to scrape"""
self.api_token = api_token
"""Browserless API token."""
self.urls = urls
"""List of URLs to scrape."""
self.text_content = text_content | def __init__(self, api_token: str, urls: Union[str, List[str]],
text_content: bool=True):
"""Initialize with API token and the URLs to scrape"""
self.api_token = api_token
"""Browserless API token."""
self.urls = urls
"""List of URLs to scrape."""
self.text_content = text_content | Initialize with API token and the URLs to scrape |
_identifying_params | return self._default_params | @property
def _identifying_params(self) ->Dict[str, Any]:
return self._default_params | null |
lazy_parse | """Lazily parse the blob."""
with blob.as_bytes_io() as file_obj:
poller = self.client.begin_analyze_document(self.api_model, file_obj,
content_type='application/octet-stream', output_content_format=
'markdown' if self.mode == 'markdown' else 'text')
result = poller.result()
if self.mode in ... | def lazy_parse(self, blob: Blob) ->Iterator[Document]:
"""Lazily parse the blob."""
with blob.as_bytes_io() as file_obj:
poller = self.client.begin_analyze_document(self.api_model,
file_obj, content_type='application/octet-stream',
output_content_format='markdown' if self.mode ==... | Lazily parse the blob. |
_embed_with_retry | return embeddings.client.generate_embeddings(*args, **kwargs) | @retry_decorator
def _embed_with_retry(*args: Any, **kwargs: Any) ->Any:
return embeddings.client.generate_embeddings(*args, **kwargs) | null |
load | """Load text from the url(s) in web_path."""
try:
asyncio.get_running_loop()
with ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(asyncio.run, self.fetch_all(self.web_paths))
results = future.result()
except RuntimeError:
results = asyncio.run(self.fetch_all(self.web_... | def load(self) ->List[Document]:
"""Load text from the url(s) in web_path."""
try:
asyncio.get_running_loop()
with ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(asyncio.run, self.fetch_all(self.
web_paths))
results = future.result... | Load text from the url(s) in web_path. |
_import_faiss | from langchain_community.vectorstores.faiss import FAISS
return FAISS | def _import_faiss() ->Any:
from langchain_community.vectorstores.faiss import FAISS
return FAISS | null |
test_parse_chat_history_correct | from vertexai.language_models import ChatMessage
text_context = (
'My name is Ned. You are my personal assistant. My favorite movies are Lord of the Rings and Hobbit.'
)
context = SystemMessage(content=text_context)
text_question = (
'Hello, could you recommend a good movie for me to watch this evening, ple... | def test_parse_chat_history_correct() ->None:
from vertexai.language_models import ChatMessage
text_context = (
'My name is Ned. You are my personal assistant. My favorite movies are Lord of the Rings and Hobbit.'
)
context = SystemMessage(content=text_context)
text_question = (
... | null |
output_keys | """Expect output key.
:meta private:
"""
return [self.output_key] | @property
def output_keys(self) ->List[str]:
"""Expect output key.
:meta private:
"""
return [self.output_key] | Expect output key.
:meta private: |
input_iter | for token in STREAMED_TOKENS:
yield AIMessageChunk(content='', additional_kwargs={'function_call': {
'arguments': token}}) | def input_iter(_: Any) ->Iterator[AIMessageChunk]:
for token in STREAMED_TOKENS:
yield AIMessageChunk(content='', additional_kwargs={'function_call':
{'arguments': token}}) | null |
test_hippo_add_extra | """Test end to end construction and MRR search."""
texts = ['foo', 'bar', 'baz']
metadatas = [{'page': i} for i in range(len(texts))]
docsearch = _hippo_from_texts(metadatas=metadatas)
docsearch.add_texts(texts, metadatas)
output = docsearch.similarity_search('foo', k=1)
print(output)
assert len(output) == 1 | def test_hippo_add_extra() ->None:
"""Test end to end construction and MRR search."""
texts = ['foo', 'bar', 'baz']
metadatas = [{'page': i} for i in range(len(texts))]
docsearch = _hippo_from_texts(metadatas=metadatas)
docsearch.add_texts(texts, metadatas)
output = docsearch.similarity_search('... | Test end to end construction and MRR search. |
load_agent | """Unified method for loading an agent from LangChainHub or local fs.
Args:
path: Path to the agent file.
**kwargs: Additional keyword arguments passed to the agent executor.
Returns:
An agent executor.
"""
valid_suffixes = {'json', 'yaml'}
if (hub_result := try_load_from_hub(path,... | def load_agent(path: Union[str, Path], **kwargs: Any) ->Union[
BaseSingleActionAgent, BaseMultiActionAgent]:
"""Unified method for loading an agent from LangChainHub or local fs.
Args:
path: Path to the agent file.
**kwargs: Additional keyword arguments passed to the agent executor.
Re... | Unified method for loading an agent from LangChainHub or local fs.
Args:
path: Path to the agent file.
**kwargs: Additional keyword arguments passed to the agent executor.
Returns:
An agent executor. |
_import_vectorstore_tool_VectorStoreQAWithSourcesTool | from langchain_community.tools.vectorstore.tool import VectorStoreQAWithSourcesTool
return VectorStoreQAWithSourcesTool | def _import_vectorstore_tool_VectorStoreQAWithSourcesTool() ->Any:
from langchain_community.tools.vectorstore.tool import VectorStoreQAWithSourcesTool
return VectorStoreQAWithSourcesTool | null |
load_memory_variables | """Return key-value pairs given the text input to the chain.""" | @abstractmethod
def load_memory_variables(self, inputs: Dict[str, Any]) ->Dict[str, Any]:
"""Return key-value pairs given the text input to the chain.""" | Return key-value pairs given the text input to the chain. |
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.
ids: Optional list of ids to associate with the texts.
refresh_indice... | def add_texts(self, texts: Iterable[str], metadatas: Optional[List[Dict[Any,
Any]]]=None, ids: Optional[List[str]]=None, refresh_indices: bool=True,
create_index_if_not_exists: bool=True, bulk_kwargs: Optional[Dict]=None,
**kwargs: Any) ->List[str]:
"""Run more texts through the embeddings and add to th... | 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.
ids: Optional list of ids to associate with the texts.
refresh_indices: Whether to refresh the Elasticsearch ind... |
test_similarity_search_with_score_by_vector | """Test vector similarity with score by vector."""
texts = ['foo', 'bar', 'baz']
docsearch = TileDB.from_texts(texts=texts, embedding=
ConsistentFakeEmbeddings(), index_uri=f'{str(tmp_path)}/flat',
index_type='FLAT')
query_vec = FakeEmbeddings().embed_query(text='foo')
output = docsearch.similarity_search_with_... | @pytest.mark.requires('tiledb-vector-search')
def test_similarity_search_with_score_by_vector(tmp_path: Path) ->None:
"""Test vector similarity with score by vector."""
texts = ['foo', 'bar', 'baz']
docsearch = TileDB.from_texts(texts=texts, embedding=
ConsistentFakeEmbeddings(), index_uri=f'{str(tm... | Test vector similarity with score by vector. |
get_lc_namespace | """Get the namespace of the langchain object."""
return ['langchain', 'schema', 'messages'] | @classmethod
def get_lc_namespace(cls) ->List[str]:
"""Get the namespace of the langchain object."""
return ['langchain', 'schema', 'messages'] | Get the namespace of the langchain object. |
test_api_key_is_string | chat = ChatGoogleGenerativeAI(model='gemini-nano', google_api_key=
'secret-api-key')
assert isinstance(chat.google_api_key, SecretStr) | def test_api_key_is_string() ->None:
chat = ChatGoogleGenerativeAI(model='gemini-nano', google_api_key=
'secret-api-key')
assert isinstance(chat.google_api_key, SecretStr) | null |
test_sklearn_with_metadatas | """Test end to end construction and search."""
texts = ['foo', 'bar', 'baz']
metadatas = [{'page': str(i)} for i in range(len(texts))]
docsearch = SKLearnVectorStore.from_texts(texts, FakeEmbeddings(),
metadatas=metadatas)
output = docsearch.similarity_search('foo', k=1)
assert output[0].metadata['page'] == '0' | @pytest.mark.requires('numpy', 'sklearn')
def test_sklearn_with_metadatas() ->None:
"""Test end to end construction and search."""
texts = ['foo', 'bar', 'baz']
metadatas = [{'page': str(i)} for i in range(len(texts))]
docsearch = SKLearnVectorStore.from_texts(texts, FakeEmbeddings(),
metadatas=... | Test end to end construction and search. |
test_gradient_wrong_setup3 | with pytest.raises(Exception):
GradientEmbeddings(gradient_api_url='-', gradient_access_token=
_GRADIENT_SECRET, gradient_workspace_id=_GRADIENT_WORKSPACE_ID,
model=_MODEL_ID) | def test_gradient_wrong_setup3() ->None:
with pytest.raises(Exception):
GradientEmbeddings(gradient_api_url='-', gradient_access_token=
_GRADIENT_SECRET, gradient_workspace_id=_GRADIENT_WORKSPACE_ID,
model=_MODEL_ID) | null |
get_task | """
Retrieve a specific task.
"""
params, error = load_query(query, fault_tolerant=True)
if params is None:
return {'Error': error}
url = f"{DEFAULT_URL}/task/{params['task_id']}"
params = {'custom_task_ids': 'true', 'team_id': self.team_id,
'include_subtasks': 'true'}
response = requests.get(ur... | def get_task(self, query: str, fault_tolerant: bool=True) ->Dict:
"""
Retrieve a specific task.
"""
params, error = load_query(query, fault_tolerant=True)
if params is None:
return {'Error': error}
url = f"{DEFAULT_URL}/task/{params['task_id']}"
params = {'custom_task_ids': '... | Retrieve a specific task. |
_chat | base_url = f'{self.ernie_api_base}/rpc/2.0/ai_custom/v1/wenxinworkshop/chat'
model_paths = {'ERNIE-Bot-turbo': 'eb-instant', 'ERNIE-Bot': 'completions',
'ERNIE-Bot-8K': 'ernie_bot_8k', 'ERNIE-Bot-4': 'completions_pro',
'ERNIE-Bot-turbo-AI': 'ai_apaas', 'BLOOMZ-7B': 'bloomz_7b1',
'Llama-2-7b-chat': 'llama_2_... | def _chat(self, payload: object) ->dict:
base_url = (
f'{self.ernie_api_base}/rpc/2.0/ai_custom/v1/wenxinworkshop/chat')
model_paths = {'ERNIE-Bot-turbo': 'eb-instant', 'ERNIE-Bot':
'completions', 'ERNIE-Bot-8K': 'ernie_bot_8k', 'ERNIE-Bot-4':
'completions_pro', 'ERNIE-Bot-turbo-AI': 'ai... | null |
get_num_tokens | """Get the number of tokens present in the text.
Useful for checking if an input will fit in a model's context window.
Args:
text: The string input to tokenize.
Returns:
The integer number of tokens in the text.
"""
if self.is_gemini:
raise ValueError('Coun... | def get_num_tokens(self, text: str) ->int:
"""Get the number of tokens present in the text.
Useful for checking if an input will fit in a model's context window.
Args:
text: The string input to tokenize.
Returns:
The integer number of tokens in the text.
""... | Get the number of tokens present in the text.
Useful for checking if an input will fit in a model's context window.
Args:
text: The string input to tokenize.
Returns:
The integer number of tokens in the text. |
find_and_replace | rtn = source
finds = sorted(replacements.keys())
for find in finds:
replace = replacements[find]
rtn = rtn.replace(find, replace)
return rtn | def find_and_replace(source: str, replacements: Dict[str, str]) ->str:
rtn = source
finds = sorted(replacements.keys())
for find in finds:
replace = replacements[find]
rtn = rtn.replace(find, replace)
return rtn | null |
_llm_type | """Return type of llm."""
return 'Anyscale LLM' | @property
def _llm_type(self) ->str:
"""Return type of llm."""
return 'Anyscale LLM' | Return type of llm. |
logout | """
Logout to cleanup resources
Args: no args
Returns: None
"""
self._jag.logout(self._token) | def logout(self) ->None:
"""
Logout to cleanup resources
Args: no args
Returns: None
"""
self._jag.logout(self._token) | Logout to cleanup resources
Args: no args
Returns: None |
_get_docs | question = inputs[self.question_key]
docs = self.vectorstore.similarity_search(question, k=self.k, **self.
search_kwargs)
return self._reduce_tokens_below_limit(docs) | def _get_docs(self, inputs: Dict[str, Any], *, run_manager:
CallbackManagerForChainRun) ->List[Document]:
question = inputs[self.question_key]
docs = self.vectorstore.similarity_search(question, k=self.k, **self.
search_kwargs)
return self._reduce_tokens_below_limit(docs) | null |
test_sanitize_input | query = """
```
p = 5
```
"""
expected = 'p = 5'
actual = sanitize_input(query)
assert expected == actual
query = """
```python
p = 5
```
"""
expected = 'p = 5'
actual = sanitize_input(query)
assert expected == actual
query = '\n p = 5\n '
expected = 'p = 5'
actual = san... | def test_sanitize_input() ->None:
query = """
```
p = 5
```
"""
expected = 'p = 5'
actual = sanitize_input(query)
assert expected == actual
query = """
```python
p = 5
```
"""
expected = 'p = 5'
actual = sanitize_input(query)
assert expected == ... | null |
get_req_generation | """Method for an end-to-end post query with NVE post-processing."""
response = self.get_req(model_name, payload, invoke_url)
output, _ = self.postprocess(response, stop=stop)
return output | def get_req_generation(self, model_name: Optional[str]=None, payload: dict=
{}, invoke_url: Optional[str]=None, stop: Optional[Sequence[str]]=None
) ->dict:
"""Method for an end-to-end post query with NVE post-processing."""
response = self.get_req(model_name, payload, invoke_url)
output, _ = self.p... | Method for an end-to-end post query with NVE post-processing. |
format_property_key | words = s.split()
if not words:
return s
first_word = words[0].lower()
capitalized_words = [word.capitalize() for word in words[1:]]
return ''.join([first_word] + capitalized_words) | def format_property_key(s: str) ->str:
words = s.split()
if not words:
return s
first_word = words[0].lower()
capitalized_words = [word.capitalize() for word in words[1:]]
return ''.join([first_word] + capitalized_words) | null |
_get_tags | req_tag = urllib.request.Request(self._get_tag_url.format(id=note_id))
with urllib.request.urlopen(req_tag) as response:
json_data = json.loads(response.read().decode())
return [tag['title'] for tag in json_data['items']] | def _get_tags(self, note_id: str) ->List[str]:
req_tag = urllib.request.Request(self._get_tag_url.format(id=note_id))
with urllib.request.urlopen(req_tag) as response:
json_data = json.loads(response.read().decode())
return [tag['title'] for tag in json_data['items']] | null |
test_hologres_with_metadatas_with_scores | """Test end to end construction and search."""
texts = ['foo', 'bar', 'baz']
metadatas = [{'page': str(i)} for i in range(len(texts))]
docsearch = Hologres.from_texts(texts=texts, table_name='test_table',
embedding=FakeEmbeddingsWithAdaDimension(), metadatas=metadatas,
connection_string=CONNECTION_STRING, pre_d... | def test_hologres_with_metadatas_with_scores() ->None:
"""Test end to end construction and search."""
texts = ['foo', 'bar', 'baz']
metadatas = [{'page': str(i)} for i in range(len(texts))]
docsearch = Hologres.from_texts(texts=texts, table_name='test_table',
embedding=FakeEmbeddingsWithAdaDimen... | Test end to end construction and search. |
lazy_load | """Lazy loads the chat data from the folder.
Yields:
ChatSession: A chat session containing the loaded messages.
"""
inbox_path = self.directory_path / 'inbox'
for _dir in inbox_path.iterdir():
if _dir.is_dir():
for _file in _dir.iterdir():
if _file.suffix.lower() =... | def lazy_load(self) ->Iterator[ChatSession]:
"""Lazy loads the chat data from the folder.
Yields:
ChatSession: A chat session containing the loaded messages.
"""
inbox_path = self.directory_path / 'inbox'
for _dir in inbox_path.iterdir():
if _dir.is_dir():
f... | Lazy loads the chat data from the folder.
Yields:
ChatSession: A chat session containing the loaded messages. |
_create_index | """Create a index on the collection"""
from transwarp_hippo_api.hippo_client import HippoTable
from transwarp_hippo_api.hippo_type import IndexType, MetricType
if isinstance(self.col, HippoTable) and self._get_index() is None:
if self._get_index() is None:
if self.index_params is None:
self.inde... | def _create_index(self) ->None:
"""Create a index on the collection"""
from transwarp_hippo_api.hippo_client import HippoTable
from transwarp_hippo_api.hippo_type import IndexType, MetricType
if isinstance(self.col, HippoTable) and self._get_index() is None:
if self._get_index() is None:
... | Create a index on the collection |
predict | """Pass a single string input to the model and return a string prediction.
Use this method when passing in raw text. If you want to pass in specific
types of chat messages, use predict_messages.
Args:
text: String input to pass to the model.
stop: Stop words to use... | @abstractmethod
def predict(self, text: str, *, stop: Optional[Sequence[str]]=None, **
kwargs: Any) ->str:
"""Pass a single string input to the model and return a string prediction.
Use this method when passing in raw text. If you want to pass in specific
types of chat messages, use predic... | Pass a single string input to the model and return a string prediction.
Use this method when passing in raw text. If you want to pass in specific
types of chat messages, use predict_messages.
Args:
text: String input to pass to the model.
stop: Stop words to use when generating. Model output is cut off a... |
test_visit_structured_query_no_filter | query = 'What is the capital of France?'
structured_query = StructuredQuery(query=query, filter=None)
expected: Tuple[str, Dict] = (query, {})
actual = translator.visit_structured_query(structured_query)
assert expected == actual | def test_visit_structured_query_no_filter(translator: RedisTranslator) ->None:
query = 'What is the capital of France?'
structured_query = StructuredQuery(query=query, filter=None)
expected: Tuple[str, Dict] = (query, {})
actual = translator.visit_structured_query(structured_query)
assert expected =... | null |
evaluate_strings | """Evaluate Chain or LLM output, based on optional input and label.
Args:
prediction (str): The LLM or chain prediction to evaluate.
reference (Optional[str], optional): The reference label to evaluate against.
input (Optional[str], optional): The input to consider during ev... | def evaluate_strings(self, *, prediction: str, reference: Optional[str]=
None, input: Optional[str]=None, **kwargs: Any) ->dict:
"""Evaluate Chain or LLM output, based on optional input and label.
Args:
prediction (str): The LLM or chain prediction to evaluate.
reference (Option... | Evaluate Chain or LLM output, based on optional input and label.
Args:
prediction (str): The LLM or chain prediction to evaluate.
reference (Optional[str], optional): The reference label to evaluate against.
input (Optional[str], optional): The input to consider during evaluation.
**kwargs: Additional ... |
test_init | mock_client.return_value = MagicMock()
FalkorDBGraph(database=self.graph, host=self.host, port=self.port) | @patch('redis.Redis')
def test_init(self, mock_client: Any) ->None:
mock_client.return_value = MagicMock()
FalkorDBGraph(database=self.graph, host=self.host, port=self.port) | null |
mock_trello_client | """Fixture that creates a mock for trello.TrelloClient."""
with patch('trello.TrelloClient') as mock_trello_client:
list_objs = list_to_objects(TRELLO_LISTS)
cards_qa_objs = card_list_to_objects(TRELLO_CARDS_QA)
boards = [MockBoard('5555eaafea917522902a2a2c', 'Research', [],
list_objs), MockBoard('5... | @pytest.fixture
def mock_trello_client() ->Any:
"""Fixture that creates a mock for trello.TrelloClient."""
with patch('trello.TrelloClient') as mock_trello_client:
list_objs = list_to_objects(TRELLO_LISTS)
cards_qa_objs = card_list_to_objects(TRELLO_CARDS_QA)
boards = [MockBoard('5555eaa... | Fixture that creates a mock for trello.TrelloClient. |
__init__ | self._embedding = embedding
self.target = host + ':' + str(port)
self.grpc_options = grpc_options
self.grpc_use_secure = grpc_use_secure
self.grpc_credentials = grpc_credentials | def __init__(self, embedding: Embeddings, host: str='localhost', port: int=
8080, grpc_options: Tuple=(('grpc.keepalive_time_ms', 1000 * 10), (
'grpc.keepalive_timeout_ms', 1000 * 10)), grpc_use_secure: bool=False,
grpc_credentials: Optional[Any]=None):
self._embedding = embedding
self.target = host... | null |
_import_milvus | from langchain_community.vectorstores.milvus import Milvus
return Milvus | def _import_milvus() ->Any:
from langchain_community.vectorstores.milvus import Milvus
return Milvus | null |
validate_prompt | prompt: BasePromptTemplate = values['prompt']
if 'agent_scratchpad' not in prompt.input_variables:
raise ValueError(
f'`agent_scratchpad` should be one of the variables in the prompt, got {prompt.input_variables}'
)
return values | @root_validator
def validate_prompt(cls, values: dict) ->dict:
prompt: BasePromptTemplate = values['prompt']
if 'agent_scratchpad' not in prompt.input_variables:
raise ValueError(
f'`agent_scratchpad` should be one of the variables in the prompt, got {prompt.input_variables}'
)
... | null |
clear | """Remove the container and its contents entirely. A cleared container can't
be reused.
"""
self._container = self._parent_cursor.empty()
self._child_records.clear() | def clear(self) ->None:
"""Remove the container and its contents entirely. A cleared container can't
be reused.
"""
self._container = self._parent_cursor.empty()
self._child_records.clear() | Remove the container and its contents entirely. A cleared container can't
be reused. |
stop_stream | """Close the streaming connection."""
if signal:
self._send_stop_signals(model_name, request_id)
self.client.stop_stream() | def stop_stream(self, model_name: str, request_id: str, signal: bool=True
) ->None:
"""Close the streaming connection."""
if signal:
self._send_stop_signals(model_name, request_id)
self.client.stop_stream() | Close the streaming connection. |
_create_collection | fields = [{'name': 'vec', 'type': 'float[]', 'num_dim': num_dim}, {'name':
f'{self._text_key}', 'type': 'string'}, {'name': '.*', 'type': 'auto'}]
self._typesense_client.collections.create({'name': self.
_typesense_collection_name, 'fields': fields}) | def _create_collection(self, num_dim: int) ->None:
fields = [{'name': 'vec', 'type': 'float[]', 'num_dim': num_dim}, {
'name': f'{self._text_key}', 'type': 'string'}, {'name': '.*',
'type': 'auto'}]
self._typesense_client.collections.create({'name': self.
_typesense_collection_name, 'fie... | null |
embed_query | return self.embed_documents([text])[0] | def embed_query(self, text: str) ->List[float]:
return self.embed_documents([text])[0] | null |
test_api_key_masked_when_passed_via_constructor | """Test initialization with an API key provided via the initializer"""
llm = Minimax(minimax_api_key='secret-api-key', minimax_group_id='group_id')
print(llm.minimax_api_key, end='')
captured = capsys.readouterr()
assert captured.out == '**********' | def test_api_key_masked_when_passed_via_constructor(capsys: CaptureFixture
) ->None:
"""Test initialization with an API key provided via the initializer"""
llm = Minimax(minimax_api_key='secret-api-key', minimax_group_id='group_id'
)
print(llm.minimax_api_key, end='')
captured = capsys.reado... | Test initialization with an API key provided via the initializer |
test_md_header_text_splitter_1 | """Test markdown splitter by header: Case 1."""
markdown_document = """# Foo
## Bar
Hi this is Jim
Hi this is Joe
## Baz
Hi this is Molly"""
headers_to_split_on = [('#', 'Header 1'), ('##', 'Header 2')]
markdown_splitter = MarkdownHeaderTextSplitter(headers_to_split_on=
headers_to_split_on)
output = mark... | def test_md_header_text_splitter_1() ->None:
"""Test markdown splitter by header: Case 1."""
markdown_document = """# Foo
## Bar
Hi this is Jim
Hi this is Joe
## Baz
Hi this is Molly"""
headers_to_split_on = [('#', 'Header 1'), ('##', 'Header 2')]
markdown_splitter = MarkdownHeaderTextSplitte... | Test markdown splitter by header: Case 1. |
_parse_chat_history | """Parse a sequence of messages into history."""
chat_history = []
for message in history:
content = cast(str, message.content)
if isinstance(message, HumanMessage):
chat_history.append(_parse_message('USER', content))
if isinstance(message, AIMessage):
chat_history.append(_parse_message('BO... | def _parse_chat_history(history: List[BaseMessage]) ->List:
"""Parse a sequence of messages into history."""
chat_history = []
for message in history:
content = cast(str, message.content)
if isinstance(message, HumanMessage):
chat_history.append(_parse_message('USER', content))
... | Parse a sequence of messages into history. |
from_documents | """Return VectorStore initialized from documents and embeddings."""
texts = [d.page_content for d in documents]
metadatas = [d.metadata for d in documents]
return cls.from_texts(texts, embedding, metadatas=metadatas, **kwargs) | @classmethod
def from_documents(cls: Type[VST], documents: List[Document], embedding:
Embeddings, **kwargs: Any) ->VST:
"""Return VectorStore initialized from documents and embeddings."""
texts = [d.page_content for d in documents]
metadatas = [d.metadata for d in documents]
return cls.from_texts(te... | Return VectorStore initialized from documents and embeddings. |
delete | """Delete by vector ID.
Args:
ids (List[str]): List of ids to delete.
kwargs (Any): Other optional parameters (unused)
Returns:
Optional[bool]: True if deletion is successful,
False otherwise, None if not implemented.
"""
from momento.responses.v... | def delete(self, ids: Optional[List[str]]=None, **kwargs: Any) ->Optional[bool
]:
"""Delete by vector ID.
Args:
ids (List[str]): List of ids to delete.
kwargs (Any): Other optional parameters (unused)
Returns:
Optional[bool]: True if deletion is successful,
... | Delete by vector ID.
Args:
ids (List[str]): List of ids to delete.
kwargs (Any): Other optional parameters (unused)
Returns:
Optional[bool]: True if deletion is successful,
False otherwise, None if not implemented. |
_get_index | """Return the vector index information if it exists"""
from transwarp_hippo_api.hippo_client import HippoTable
if isinstance(self.col, HippoTable):
table_info = self.hc.get_table_info(self.table_name, self.database_name
).get(self.table_name, {})
embedding_indexes = table_info.get('embedding_indexes', N... | def _get_index(self) ->Optional[Dict[str, Any]]:
"""Return the vector index information if it exists"""
from transwarp_hippo_api.hippo_client import HippoTable
if isinstance(self.col, HippoTable):
table_info = self.hc.get_table_info(self.table_name, self.database_name
).get(self.table_na... | Return the vector index information if it exists |
construct_url_and_params | params = self.get_params(query)
params['source'] = 'python'
if self.serpapi_api_key:
params['serp_api_key'] = self.serpapi_api_key
params['output'] = 'json'
url = 'https://serpapi.com/search'
return url, params | def construct_url_and_params() ->Tuple[str, Dict[str, str]]:
params = self.get_params(query)
params['source'] = 'python'
if self.serpapi_api_key:
params['serp_api_key'] = self.serpapi_api_key
params['output'] = 'json'
url = 'https://serpapi.com/search'
return url, params | null |
_import_arxiv | from langchain_community.utilities.arxiv import ArxivAPIWrapper
return ArxivAPIWrapper | def _import_arxiv() ->Any:
from langchain_community.utilities.arxiv import ArxivAPIWrapper
return ArxivAPIWrapper | null |
full_form | return type_mapping[x] if x in type_mapping else x | def full_form(x: str) ->str:
return type_mapping[x] if x in type_mapping else x | null |
llm_prefix | """Prefix to append the llm call with."""
return 'Thought:' | @property
def llm_prefix(self) ->str:
"""Prefix to append the llm call with."""
return 'Thought:' | Prefix to append the llm call with. |
_identifying_params | """Get the identifying parameters."""
return {} | @property
def _identifying_params(self) ->Dict[str, Any]:
"""Get the identifying parameters."""
return {} | Get the identifying parameters. |
get_input_schema | return create_model('ChainInput', **{k: (Any, None) for k in self.input_keys}) | def get_input_schema(self, config: Optional[RunnableConfig]=None) ->Type[
BaseModel]:
return create_model('ChainInput', **{k: (Any, None) for k in self.
input_keys}) | null |
to_string | """Return prompt value as string.""" | @abstractmethod
def to_string(self) ->str:
"""Return prompt value as string.""" | Return prompt value as string. |
requests | return Requests(headers=self.headers, aiosession=self.aiosession, auth=self
.auth) | @property
def requests(self) ->Requests:
return Requests(headers=self.headers, aiosession=self.aiosession, auth=
self.auth) | null |
test_search_similarity | r = zep_vectorstore.search(query='Test Document', search_type='similarity', k=2
)
assert len(r) == 2
assert r[0].page_content == 'Test Document'
assert r[0].metadata == {'key': 'value'} | @pytest.mark.requires('zep_python')
def test_search_similarity(zep_vectorstore: ZepVectorStore) ->None:
r = zep_vectorstore.search(query='Test Document', search_type=
'similarity', k=2)
assert len(r) == 2
assert r[0].page_content == 'Test Document'
assert r[0].metadata == {'key': 'value'} | null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.