method_name stringlengths 1 78 | method_body stringlengths 3 9.66k | full_code stringlengths 31 10.7k | docstring stringlengths 4 4.74k ⌀ |
|---|---|---|---|
on_agent_finish_common | self.agent_ends += 1
self.ends += 1 | def on_agent_finish_common(self) ->None:
self.agent_ends += 1
self.ends += 1 | null |
_import_requests_tool_RequestsPatchTool | from langchain_community.tools.requests.tool import RequestsPatchTool
return RequestsPatchTool | def _import_requests_tool_RequestsPatchTool() ->Any:
from langchain_community.tools.requests.tool import RequestsPatchTool
return RequestsPatchTool | null |
test_clone_different_repo | """
Test that trying to clone a different repository into a directory already
containing a clone raises a ValueError.
"""
clone_url = init_repo(tmpdir, 'remote_repo')
repo_path = tmpdir.mkdir('local_repo').strpath
loader = GitLoader(repo_path=repo_path, clone_url=clone_url)
documents = loader.load()
assert ... | @pytest.mark.requires('git')
def test_clone_different_repo(tmpdir: py.path.local) ->None:
"""
Test that trying to clone a different repository into a directory already
containing a clone raises a ValueError.
"""
clone_url = init_repo(tmpdir, 'remote_repo')
repo_path = tmpdir.mkdir('local_repo').... | Test that trying to clone a different repository into a directory already
containing a clone raises a ValueError. |
test_init_fail_no_text_column | index = mock_index(index_details)
with pytest.raises(ValueError) as ex:
DatabricksVectorSearch(index, embedding=DEFAULT_EMBEDDING_MODEL)
assert '`text_column` is required for this index.' in str(ex.value) | @pytest.mark.requires('databricks', 'databricks.vector_search')
@pytest.mark.parametrize('index_details', [
DELTA_SYNC_INDEX_SELF_MANAGED_EMBEDDINGS, DIRECT_ACCESS_INDEX])
def test_init_fail_no_text_column(index_details: dict) ->None:
index = mock_index(index_details)
with pytest.raises(ValueError) as ex:
... | null |
save_context | """Save the context of this model run to memory."""
mem = outputs.get(self.add_memory_key)
now = outputs.get(self.now_key)
if mem:
self.add_memory(mem, now=now) | def save_context(self, inputs: Dict[str, Any], outputs: Dict[str, Any]) ->None:
"""Save the context of this model run to memory."""
mem = outputs.get(self.add_memory_key)
now = outputs.get(self.now_key)
if mem:
self.add_memory(mem, now=now) | Save the context of this model run to memory. |
test_bedrock_batch | """Test batch tokens from BedrockChat."""
result = chat.batch(["I'm Pickle Rick", "I'm not Pickle Rick"])
for token in result:
assert isinstance(token.content, str) | @pytest.mark.scheduled
def test_bedrock_batch(chat: BedrockChat) ->None:
"""Test batch tokens from BedrockChat."""
result = chat.batch(["I'm Pickle Rick", "I'm not Pickle Rick"])
for token in result:
assert isinstance(token.content, str) | Test batch tokens from BedrockChat. |
on_retriever_start_common | self.starts += 1
self.retriever_starts += 1 | def on_retriever_start_common(self) ->None:
self.starts += 1
self.retriever_starts += 1 | null |
config_specs | with _enums_for_spec_lock:
if (which_enum := _enums_for_spec.get(self.which)):
pass
else:
which_enum = StrEnum(self.which.name or self.which.id, ((v, v) for
v in list(self.alternatives.keys()) + [self.default_key]))
_enums_for_spec[self.which] = cast(Type[StrEnum], which_enum... | @property
def config_specs(self) ->List[ConfigurableFieldSpec]:
with _enums_for_spec_lock:
if (which_enum := _enums_for_spec.get(self.which)):
pass
else:
which_enum = StrEnum(self.which.name or self.which.id, ((v, v) for
v in list(self.alternatives.keys()) + [... | null |
test_extra_kwargs | chat = ChatBaichuan(temperature=0.88, top_p=0.7)
assert chat.temperature == 0.88
assert chat.top_p == 0.7 | def test_extra_kwargs() ->None:
chat = ChatBaichuan(temperature=0.88, top_p=0.7)
assert chat.temperature == 0.88
assert chat.top_p == 0.7 | null |
_create_stream | if self.stop is not None and stop is not None:
raise ValueError('`stop` found in both the input and default params.')
elif self.stop is not None:
stop = self.stop
elif stop is None:
stop = []
params = self._default_params
if 'model' in kwargs:
params['model'] = kwargs['model']
if 'options' in kwargs:
... | def _create_stream(self, api_url: str, payload: Any, stop: Optional[List[
str]]=None, **kwargs: Any) ->Iterator[str]:
if self.stop is not None and stop is not None:
raise ValueError('`stop` found in both the input and default params.')
elif self.stop is not None:
stop = self.stop
elif st... | null |
test_memory_with_message_store | """Test the memory with a message store."""
message_history = UpstashRedisChatMessageHistory(url=URL, token=TOKEN, ttl=
10, session_id='my-test-session')
memory = ConversationBufferMemory(memory_key='baz', chat_memory=
message_history, return_messages=True)
memory.chat_memory.add_ai_message('This is me, the AI'... | @pytest.mark.requires('upstash_redis')
def test_memory_with_message_store() ->None:
"""Test the memory with a message store."""
message_history = UpstashRedisChatMessageHistory(url=URL, token=TOKEN,
ttl=10, session_id='my-test-session')
memory = ConversationBufferMemory(memory_key='baz', chat_memory... | Test the memory with a message store. |
test_all_imports | assert set(__all__) == set(EXPECTED_ALL) | def test_all_imports() ->None:
assert set(__all__) == set(EXPECTED_ALL) | null |
drop | """
Helper function: Drop data
"""
get_named_result(self.connection,
f'DROP TABLE IF EXISTS {self.config.database}.{self.config.table}') | def drop(self) ->None:
"""
Helper function: Drop data
"""
get_named_result(self.connection,
f'DROP TABLE IF EXISTS {self.config.database}.{self.config.table}') | Helper function: Drop data |
test_bilibili_loader | """Test Bilibili Loader."""
loader = BiliBiliLoader(['https://www.bilibili.com/video/BV1xt411o7Xu/',
'https://www.bilibili.com/video/av330407025/'])
docs = loader.load()
assert len(docs) == 2
assert len(docs[0].page_content) > 0
assert docs[1].metadata['owner']['mid'] == 398095160
assert docs[1].page_content == ''
... | def test_bilibili_loader() ->None:
"""Test Bilibili Loader."""
loader = BiliBiliLoader(['https://www.bilibili.com/video/BV1xt411o7Xu/',
'https://www.bilibili.com/video/av330407025/'])
docs = loader.load()
assert len(docs) == 2
assert len(docs[0].page_content) > 0
assert docs[1].metadata[... | Test Bilibili Loader. |
_call | try:
from predibase import PredibaseClient
pc = PredibaseClient(token=self.predibase_api_key.get_secret_value())
except ImportError as e:
raise ImportError(
'Could not import Predibase Python package. Please install it with `pip install predibase`.'
) from e
except ValueError as e:
raise... | def _call(self, prompt: str, stop: Optional[List[str]]=None, run_manager:
Optional[CallbackManagerForLLMRun]=None, **kwargs: Any) ->str:
try:
from predibase import PredibaseClient
pc = PredibaseClient(token=self.predibase_api_key.get_secret_value())
except ImportError as e:
raise Imp... | null |
truncate_run_iterative | """Utility to truncate a list of runs dictionaries to only keep the specified
keys in each run.
:param runs: The list of runs to truncate.
:param keep_keys: The keys to keep in each run.
:return: The truncated list of runs.
"""
def truncate_single(run: Dict[str, Any]) ->Dict[... | def truncate_run_iterative(self, runs: List[Dict[str, Any]], keep_keys:
Tuple[str, ...]=()) ->List[Dict[str, Any]]:
"""Utility to truncate a list of runs dictionaries to only keep the specified
keys in each run.
:param runs: The list of runs to truncate.
:param keep_keys: The keys to... | Utility to truncate a list of runs dictionaries to only keep the specified
keys in each run.
:param runs: The list of runs to truncate.
:param keep_keys: The keys to keep in each run.
:return: The truncated list of runs. |
add_vectors | return self._add_vectors(self._client, self.table_name, vectors, documents,
ids, self.chunk_size) | def add_vectors(self, vectors: List[List[float]], documents: List[Document],
ids: List[str]) ->List[str]:
return self._add_vectors(self._client, self.table_name, vectors,
documents, ids, self.chunk_size) | null |
_raw_similarity_search_with_score | """Return raw opensearch documents (dict) including vectors,
scores most similar to query.
By default, supports Approximate Search.
Also supports Script Scoring and Painless Scripting.
Args:
query: Text to look up documents similar to.
k: Number of Documents to ... | def _raw_similarity_search_with_score(self, query: str, k: int=4, **kwargs: Any
) ->List[dict]:
"""Return raw opensearch documents (dict) including vectors,
scores most similar to query.
By default, supports Approximate Search.
Also supports Script Scoring and Painless Scripting.
... | Return raw opensearch documents (dict) including vectors,
scores most similar to query.
By default, supports Approximate Search.
Also supports Script Scoring and Painless Scripting.
Args:
query: Text to look up documents similar to.
k: Number of Documents to return. Defaults to 4.
Returns:
List of dict w... |
_import_elastic_vector_search | from langchain_community.vectorstores.elastic_vector_search import ElasticVectorSearch
return ElasticVectorSearch | def _import_elastic_vector_search() ->Any:
from langchain_community.vectorstores.elastic_vector_search import ElasticVectorSearch
return ElasticVectorSearch | null |
_import_oci_md_tgi | from langchain_community.llms.oci_data_science_model_deployment_endpoint import OCIModelDeploymentTGI
return OCIModelDeploymentTGI | def _import_oci_md_tgi() ->Any:
from langchain_community.llms.oci_data_science_model_deployment_endpoint import OCIModelDeploymentTGI
return OCIModelDeploymentTGI | null |
test_vald_search | """Test end to end construction and search."""
docsearch = _vald_from_texts()
time.sleep(WAIT_TIME)
output = docsearch.similarity_search('foo', k=3)
assert output == [Document(page_content='foo'), Document(page_content='bar'
), Document(page_content='baz')] | def test_vald_search() ->None:
"""Test end to end construction and search."""
docsearch = _vald_from_texts()
time.sleep(WAIT_TIME)
output = docsearch.similarity_search('foo', k=3)
assert output == [Document(page_content='foo'), Document(page_content=
'bar'), Document(page_content='baz')] | Test end to end construction and search. |
observation_prefix | """Prefix to append the observation with."""
return 'Observation: ' | @property
def observation_prefix(self) ->str:
"""Prefix to append the observation with."""
return 'Observation: ' | Prefix to append the observation with. |
test_load_jsonlines | file_path = '/workspaces/langchain/test.json'
expected_docs = [Document(page_content='value1', metadata={'source':
file_path, 'seq_num': 1}), Document(page_content='value2', metadata={
'source': file_path, 'seq_num': 2})]
mocker.patch('pathlib.Path.open', return_value=io.StringIO(
"""
{"text": "... | def test_load_jsonlines(mocker: MockerFixture) ->None:
file_path = '/workspaces/langchain/test.json'
expected_docs = [Document(page_content='value1', metadata={'source':
file_path, 'seq_num': 1}), Document(page_content='value2', metadata
={'source': file_path, 'seq_num': 2})]
mocker.patch('p... | null |
_Await | self.write('(')
self.write('await')
if t.value:
self.write(' ')
self.dispatch(t.value)
self.write(')') | def _Await(self, t):
self.write('(')
self.write('await')
if t.value:
self.write(' ')
self.dispatch(t.value)
self.write(')') | null |
pause_to_reflect | """Reflect on recent observations and generate 'insights'."""
if self.verbose:
logger.info('Character is reflecting')
new_insights = []
topics = self._get_topics_of_reflection()
for topic in topics:
insights = self._get_insights_on_topic(topic, now=now)
for insight in insights:
self.add_memory(insig... | def pause_to_reflect(self, now: Optional[datetime]=None) ->List[str]:
"""Reflect on recent observations and generate 'insights'."""
if self.verbose:
logger.info('Character is reflecting')
new_insights = []
topics = self._get_topics_of_reflection()
for topic in topics:
insights = self... | Reflect on recent observations and generate 'insights'. |
completion_with_retry | """Use tenacity to retry the completion call."""
retry_decorator = _create_retry_decorator(llm, run_manager=run_manager)
@retry_decorator
def _completion_with_retry(**kwargs: Any) ->Any:
ordered_generation_requests = get_ordered_generation_requests(
models_priority_list, **kwargs)
return llm.client.gene... | def completion_with_retry(llm: GPTRouter, models_priority_list: List[
GPTRouterModel], run_manager: Optional[CallbackManagerForLLMRun]=None,
**kwargs: Any) ->Union[GenerationResponse, Generator[
ChunkedGenerationResponse]]:
"""Use tenacity to retry the completion call."""
retry_decorator = _create_r... | Use tenacity to retry the completion call. |
add_message | """Store a message in the cache.
Args:
message (BaseMessage): The message object to store.
Raises:
SdkException: Momento service or network error.
Exception: Unexpected response.
"""
from momento.responses import CacheListPushBack
item = json.dumps(message_t... | def add_message(self, message: BaseMessage) ->None:
"""Store a message in the cache.
Args:
message (BaseMessage): The message object to store.
Raises:
SdkException: Momento service or network error.
Exception: Unexpected response.
"""
from momento.re... | Store a message in the cache.
Args:
message (BaseMessage): The message object to store.
Raises:
SdkException: Momento service or network error.
Exception: Unexpected response. |
_run | schedule = self.account.schedule()
calendar = schedule.get_default_calendar()
event = calendar.new_event()
event.body = body
event.subject = subject
event.start = dt.strptime(start_datetime, UTC_FORMAT)
event.end = dt.strptime(end_datetime, UTC_FORMAT)
for attendee in attendees:
event.attendees.add(attendee)
event.... | def _run(self, body: str, attendees: List[str], subject: str,
start_datetime: str, end_datetime: str, run_manager: Optional[
CallbackManagerForToolRun]=None) ->str:
schedule = self.account.schedule()
calendar = schedule.get_default_calendar()
event = calendar.new_event()
event.body = body
ev... | null |
test_loadnotebook_eachnotehasexpectedmetadata | documents = EverNoteLoader(self.example_notebook_path(
'sample_notebook.enex'), False).load()
metadata_note1 = documents[0].metadata
assert 'title' in metadata_note1.keys()
assert 'created' in metadata_note1.keys()
assert 'updated' in metadata_note1.keys()
assert 'note-attributes.author' in metadata_note1.keys()
as... | def test_loadnotebook_eachnotehasexpectedmetadata(self) ->None:
documents = EverNoteLoader(self.example_notebook_path(
'sample_notebook.enex'), False).load()
metadata_note1 = documents[0].metadata
assert 'title' in metadata_note1.keys()
assert 'created' in metadata_note1.keys()
assert 'updat... | null |
load | p = Path(self.path)
docs = []
items = p.rglob(self.glob) if self.recursive else p.glob(self.glob)
for i in items:
if i.is_file():
if self._is_visible(i.relative_to(p)) or self.load_hidden:
try:
loader = PyPDFLoader(str(i), extract_images=self.extract_images
)
... | def load(self) ->List[Document]:
p = Path(self.path)
docs = []
items = p.rglob(self.glob) if self.recursive else p.glob(self.glob)
for i in items:
if i.is_file():
if self._is_visible(i.relative_to(p)) or self.load_hidden:
try:
loader = PyPDFLoader(... | null |
test_as_import_path | """Test that the path is converted to a LangChain import path."""
assert path.PACKAGE_DIR == ROOT / 'langchain_core'
assert path.as_import_path(HERE, relative_to=ROOT) == 'tests.unit_tests._api'
assert path.as_import_path(__file__, relative_to=ROOT
) == 'tests.unit_tests._api.test_path'
assert path.as_import_path(_... | def test_as_import_path() ->None:
"""Test that the path is converted to a LangChain import path."""
assert path.PACKAGE_DIR == ROOT / 'langchain_core'
assert path.as_import_path(HERE, relative_to=ROOT
) == 'tests.unit_tests._api'
assert path.as_import_path(__file__, relative_to=ROOT
) ==... | Test that the path is converted to a LangChain import path. |
chat | return ChatFireworks(model_kwargs={'temperature': 0, 'max_tokens': 512}) | @pytest.fixture
def chat() ->ChatFireworks:
return ChatFireworks(model_kwargs={'temperature': 0, 'max_tokens': 512}) | null |
from_texts | """
Create a BM25Retriever from a list of texts.
Args:
texts: A list of texts to vectorize.
metadatas: A list of metadata dicts to associate with each text.
bm25_params: Parameters to pass to the BM25 vectorizer.
preprocess_func: A function to preprocess e... | @classmethod
def from_texts(cls, texts: Iterable[str], metadatas: Optional[Iterable[dict
]]=None, bm25_params: Optional[Dict[str, Any]]=None, preprocess_func:
Callable[[str], List[str]]=default_preprocessing_func, **kwargs: Any
) ->BM25Retriever:
"""
Create a BM25Retriever from a list of texts.
... | Create a BM25Retriever from a list of texts.
Args:
texts: A list of texts to vectorize.
metadatas: A list of metadata dicts to associate with each text.
bm25_params: Parameters to pass to the BM25 vectorizer.
preprocess_func: A function to preprocess each text before vectorization.
**kwargs: Any oth... |
merge_content | """Merge two message contents.
Args:
first_content: The first content.
second_content: The second content.
Returns:
The merged content.
"""
if isinstance(first_content, str):
if isinstance(second_content, str):
return first_content + second_content
else:
ret... | def merge_content(first_content: Union[str, List[Union[str, Dict]]],
second_content: Union[str, List[Union[str, Dict]]]) ->Union[str, List[
Union[str, Dict]]]:
"""Merge two message contents.
Args:
first_content: The first content.
second_content: The second content.
Returns:
... | Merge two message contents.
Args:
first_content: The first content.
second_content: The second content.
Returns:
The merged content. |
_get_schema_type | if schema is None:
return None
schema_type: SCHEMA_TYPE = APIProperty._cast_schema_list_type(schema)
if schema_type == 'array':
schema_type = APIProperty._get_schema_type_for_array(schema)
elif schema_type == 'object':
raise NotImplementedError('Objects not yet supported')
elif schema_type in PRIMITIVE_TYPE... | @staticmethod
def _get_schema_type(parameter: Parameter, schema: Optional[Schema]
) ->SCHEMA_TYPE:
if schema is None:
return None
schema_type: SCHEMA_TYPE = APIProperty._cast_schema_list_type(schema)
if schema_type == 'array':
schema_type = APIProperty._get_schema_type_for_array(schema)
... | null |
from_model_id | """Construct the pipeline object from model_id and task."""
try:
from transformers import AutoModelForCausalLM, AutoModelForSeq2SeqLM, AutoTokenizer
from transformers import pipeline as hf_pipeline
except ImportError:
raise ValueError(
'Could not import transformers python package. Please install it... | @classmethod
def from_model_id(cls, model_id: str, task: str, device: Optional[int]=-1,
device_map: Optional[str]=None, model_kwargs: Optional[dict]=None,
pipeline_kwargs: Optional[dict]=None, batch_size: int=
DEFAULT_BATCH_SIZE, **kwargs: Any) ->HuggingFacePipeline:
"""Construct the pipeline object fro... | Construct the pipeline object from model_id and task. |
_handle_missing_document | replacement_result = self.collection.find_one_and_replace(filter={'_id':
missing_document['_id']}, replacement=missing_document)
return replacement_result['data']['document']['_id'] | def _handle_missing_document(missing_document: DocDict) ->str:
replacement_result = self.collection.find_one_and_replace(filter={'_id':
missing_document['_id']}, replacement=missing_document)
return replacement_result['data']['document']['_id'] | null |
create_anonymizer_mapping | """Creates or updates the mapping used to anonymize and/or deanonymize text.
This method exploits the results returned by the
analysis and anonymization processes.
If is_reversed is True, it constructs a mapping from each original
entity to its anonymized value.
If is_reversed is False, it constr... | def create_anonymizer_mapping(original_text: str, analyzer_results: List[
'RecognizerResult'], anonymizer_results: 'EngineResult', is_reversed:
bool=False) ->MappingDataType:
"""Creates or updates the mapping used to anonymize and/or deanonymize text.
This method exploits the results returned by the
... | Creates or updates the mapping used to anonymize and/or deanonymize text.
This method exploits the results returned by the
analysis and anonymization processes.
If is_reversed is True, it constructs a mapping from each original
entity to its anonymized value.
If is_reversed is False, it constructs a mapping from eac... |
__init__ | """Initialize the NLTK splitter."""
super().__init__(**kwargs)
try:
from nltk.tokenize import sent_tokenize
self._tokenizer = sent_tokenize
except ImportError:
raise ImportError(
'NLTK is not installed, please install it with `pip install nltk`.')
self._separator = separator
self._language = languag... | def __init__(self, separator: str='\n\n', language: str='english', **kwargs:
Any) ->None:
"""Initialize the NLTK splitter."""
super().__init__(**kwargs)
try:
from nltk.tokenize import sent_tokenize
self._tokenizer = sent_tokenize
except ImportError:
raise ImportError(
... | Initialize the NLTK splitter. |
requires_reference | """Whether the evaluation requires a reference text."""
return False | @property
def requires_reference(self) ->bool:
"""Whether the evaluation requires a reference text."""
return False | Whether the evaluation requires a reference text. |
from_llm | """Load QA Generate Chain from LLM."""
return cls(llm=llm, prompt=PROMPT, **kwargs) | @classmethod
def from_llm(cls, llm: BaseLanguageModel, **kwargs: Any) ->QAGenerateChain:
"""Load QA Generate Chain from LLM."""
return cls(llm=llm, prompt=PROMPT, **kwargs) | Load QA Generate Chain from LLM. |
_default_params | """Get the default parameters for calling Tongyi Qwen API."""
normal_params = {'model': self.model_name, 'top_p': self.top_p, 'api_key':
self.dashscope_api_key}
return {**normal_params, **self.model_kwargs} | @property
def _default_params(self) ->Dict[str, Any]:
"""Get the default parameters for calling Tongyi Qwen API."""
normal_params = {'model': self.model_name, 'top_p': self.top_p,
'api_key': self.dashscope_api_key}
return {**normal_params, **self.model_kwargs} | Get the default parameters for calling Tongyi Qwen API. |
_run | """Use the tool."""
res = self.api_wrapper.results(query, self.max_results, source=self.backend)
res_strs = [', '.join([f'{k}: {v}' for k, v in d.items()]) for d in res]
return ', '.join([f'[{rs}]' for rs in res_strs]) | def _run(self, query: str, run_manager: Optional[CallbackManagerForToolRun]
=None) ->str:
"""Use the tool."""
res = self.api_wrapper.results(query, self.max_results, source=self.backend
)
res_strs = [', '.join([f'{k}: {v}' for k, v in d.items()]) for d in res]
return ', '.join([f'[{rs}]' for... | Use the tool. |
_import_myscale_settings | from langchain_community.vectorstores.myscale import MyScaleSettings
return MyScaleSettings | def _import_myscale_settings() ->Any:
from langchain_community.vectorstores.myscale import MyScaleSettings
return MyScaleSettings | null |
_create_chat_result | generations = []
for m in response['data']['messages']:
message = _convert_dict_to_message(m)
gen = ChatGeneration(message=message)
generations.append(gen)
token_usage = response['usage']
llm_output = {'token_usage': token_usage, 'model': self.model}
return ChatResult(generations=generations, llm_output=llm... | def _create_chat_result(self, response: Mapping[str, Any]) ->ChatResult:
generations = []
for m in response['data']['messages']:
message = _convert_dict_to_message(m)
gen = ChatGeneration(message=message)
generations.append(gen)
token_usage = response['usage']
llm_output = {'toke... | null |
extract_cypher | """Extract Cypher code from a text.
Args:
text: Text to extract Cypher code from.
Returns:
Cypher code extracted from the text.
"""
pattern = '```(.*?)```'
matches = re.findall(pattern, text, re.DOTALL)
return matches[0] if matches else text | def extract_cypher(text: str) ->str:
"""Extract Cypher code from a text.
Args:
text: Text to extract Cypher code from.
Returns:
Cypher code extracted from the text.
"""
pattern = '```(.*?)```'
matches = re.findall(pattern, text, re.DOTALL)
return matches[0] if matches else ... | Extract Cypher code from a text.
Args:
text: Text to extract Cypher code from.
Returns:
Cypher code extracted from the text. |
_get_span | import regex
minor = quote
major = context
errs_ = 0
s = regex.search(f'({minor}){{e<={errs_}}}', major)
while s is None and errs_ <= errs:
errs_ += 1
s = regex.search(f'({minor}){{e<={errs_}}}', major)
if s is not None:
yield from s.spans() | def _get_span(self, quote: str, context: str, errs: int=100) ->Iterator[str]:
import regex
minor = quote
major = context
errs_ = 0
s = regex.search(f'({minor}){{e<={errs_}}}', major)
while s is None and errs_ <= errs:
errs_ += 1
s = regex.search(f'({minor}){{e<={errs_}}}', major)... | null |
_FUNCTION_COMPOSER | """
Composer for functions.
Args:
op_name: Name of the function.
Returns:
Callable that takes a list of arguments and returns a string.
"""
def f(*args: Any) ->str:
args_: map[str] = map(str, args)
return f"{op_name}({','.join(args_)})"
return f | def _FUNCTION_COMPOSER(op_name: str) ->Callable:
"""
Composer for functions.
Args:
op_name: Name of the function.
Returns:
Callable that takes a list of arguments and returns a string.
"""
def f(*args: Any) ->str:
args_: map[str] = map(str, args)
return f"{op_n... | Composer for functions.
Args:
op_name: Name of the function.
Returns:
Callable that takes a list of arguments and returns a string. |
_get_embedding_dimension | if self._embedding_dimension is None:
self._embedding_dimension = len(self.embedding.embed_query(
'This is a sample sentence.'))
return self._embedding_dimension | def _get_embedding_dimension(self) ->int:
if self._embedding_dimension is None:
self._embedding_dimension = len(self.embedding.embed_query(
'This is a sample sentence.'))
return self._embedding_dimension | null |
_select_eval_results | if isinstance(results, EvaluationResult):
results_ = [results]
elif isinstance(results, dict) and 'results' in results:
results_ = cast(List[EvaluationResult], results['results'])
else:
raise TypeError(
f'Invalid evaluation result type {type(results)}. Expected EvaluationResult or EvaluationResults.... | def _select_eval_results(self, results: Union[EvaluationResult,
EvaluationResults]) ->List[EvaluationResult]:
if isinstance(results, EvaluationResult):
results_ = [results]
elif isinstance(results, dict) and 'results' in results:
results_ = cast(List[EvaluationResult], results['results'])
... | null |
test_parse_with_language | llm_output = """I can use the `foo` tool to achieve the goal.
Action:
```json
{
"action": "foo",
"action_input": "bar"
}
```
"""
action, action_input = get_action_and_input(llm_output)
assert action == 'foo'
assert action_input == 'bar' | def test_parse_with_language() ->None:
llm_output = """I can use the `foo` tool to achieve the goal.
Action:
```json
{
"action": "foo",
"action_input": "bar"
}
```
"""
action, action_input = get_action_and_input(llm_output)
assert action == 'foo'
assert action_input ... | null |
test_loc_call | search = DataForSeoAPIWrapper(params={'location_name': 'Spain',
'language_code': 'es'})
output = search.results('iphone')
assert '/es/' in output[0]['url'] | def test_loc_call() ->None:
search = DataForSeoAPIWrapper(params={'location_name': 'Spain',
'language_code': 'es'})
output = search.results('iphone')
assert '/es/' in output[0]['url'] | 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. |
assert_docs | for doc in docs:
assert doc.page_content
assert doc.metadata
main_meta = {'Published', 'Title', 'Authors', 'Summary'}
assert set(doc.metadata).issuperset(main_meta)
if all_meta:
assert len(set(doc.metadata)) > len(main_meta)
else:
assert len(set(doc.metadata)) == len(main_meta) | def assert_docs(docs: List[Document], all_meta: bool=False) ->None:
for doc in docs:
assert doc.page_content
assert doc.metadata
main_meta = {'Published', 'Title', 'Authors', 'Summary'}
assert set(doc.metadata).issuperset(main_meta)
if all_meta:
assert len(set(doc... | null |
test_math_question_2 | """Test simple question."""
question = """Michael had 58 golf balls. On tuesday, he lost 23 golf balls.
On wednesday, he lost 2 more. How many golf balls did he have
at the end of wednesday?"""
prompt = MATH_PROMPT.format(question=question)
queries = {prompt: _MATH_SOLUTION_2}
fake_llm... | def test_math_question_2() ->None:
"""Test simple question."""
question = """Michael had 58 golf balls. On tuesday, he lost 23 golf balls.
On wednesday, he lost 2 more. How many golf balls did he have
at the end of wednesday?"""
prompt = MATH_PROMPT.format(question=question... | Test simple question. |
_setter | values[done] = value
done.set()
return value | def _setter(done: threading.Event, values: Values, value: T) ->T:
values[done] = value
done.set()
return value | null |
get_label | cost = None
if event.selected:
chosen_action = event.selected.index
cost = (-1.0 * event.selected.score if event.selected.score is not None
else None)
prob = event.selected.probability
return chosen_action, cost, prob
else:
return None, None, None | def get_label(self, event: PickBestEvent) ->tuple:
cost = None
if event.selected:
chosen_action = event.selected.index
cost = (-1.0 * event.selected.score if event.selected.score is not
None else None)
prob = event.selected.probability
return chosen_action, cost, prob... | null |
test_run_with_callback | """Test run method works when callback manager is passed."""
handler = FakeCallbackHandler()
chain = FakeChain(callbacks=[handler])
output = chain.run('bar')
assert output == 'baz'
assert handler.starts == 1
assert handler.ends == 1
assert handler.errors == 0 | def test_run_with_callback() ->None:
"""Test run method works when callback manager is passed."""
handler = FakeCallbackHandler()
chain = FakeChain(callbacks=[handler])
output = chain.run('bar')
assert output == 'baz'
assert handler.starts == 1
assert handler.ends == 1
assert handler.err... | Test run method works when callback manager is passed. |
max_marginal_relevance_search | """Return docs selected using the maximal marginal relevance.
Maximal marginal relevance optimizes for similarity to query AND diversity
among selected documents.
Args:
query (str): Text to look up documents similar to.
k (int = 4): Number of Documents to return.
... | def max_marginal_relevance_search(self, query: str, k: int=4, fetch_k: int=
20, lambda_mult: float=0.5, filter: Optional[Dict[str, str]]=None, **
kwargs: Any) ->List[Document]:
"""Return docs selected using the maximal marginal relevance.
Maximal marginal relevance optimizes for similarity to query ... | Return docs selected using the maximal marginal relevance.
Maximal marginal relevance optimizes for similarity to query AND diversity
among selected documents.
Args:
query (str): Text to look up documents similar to.
k (int = 4): Number of Documents to return.
fetch_k (int = 20): Number of Documents to fetc... |
load_memory_variables | """
Returns chat history and all generated entities with summaries if available,
and updates or clears the recent entity cache.
New entity name can be found when calling this method, before the entity
summaries are generated, so the entity cache values may be empty if no entity
... | def load_memory_variables(self, inputs: Dict[str, Any]) ->Dict[str, Any]:
"""
Returns chat history and all generated entities with summaries if available,
and updates or clears the recent entity cache.
New entity name can be found when calling this method, before the entity
summarie... | Returns chat history and all generated entities with summaries if available,
and updates or clears the recent entity cache.
New entity name can be found when calling this method, before the entity
summaries are generated, so the entity cache values may be empty if no entity
descriptions are generated yet. |
__add__ | ... | def __add__(self, __x: _T_contra) ->_T_co:
... | null |
load | return list(self.lazy_load()) | def load(self) ->List[Document]:
return list(self.lazy_load()) | null |
__str__ | result = ''
for task in self.tasks:
result += f'{task}\n'
result += f'status: {task.status}\n'
if task.failed():
result += f'message: {task.message}\n'
if task.completed():
result += f'result: {task.result}\n'
return result | def __str__(self) ->str:
result = ''
for task in self.tasks:
result += f'{task}\n'
result += f'status: {task.status}\n'
if task.failed():
result += f'message: {task.message}\n'
if task.completed():
result += f'result: {task.result}\n'
return result | null |
_load_credentials | """Load credentials."""
try:
from google.auth.transport.requests import Request
from google.oauth2 import service_account
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from youtube_transcript_api import YouTubeTranscriptApi
except ImportErro... | def _load_credentials(self) ->Any:
"""Load credentials."""
try:
from google.auth.transport.requests import Request
from google.oauth2 import service_account
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from youtu... | Load credentials. |
_default_params | """Get the default parameters for calling the Aleph Alpha API."""
return {'maximum_tokens': self.maximum_tokens, 'temperature': self.
temperature, 'top_k': self.top_k, 'top_p': self.top_p,
'presence_penalty': self.presence_penalty, 'frequency_penalty': self.
frequency_penalty, 'n': self.n, 'repetition_penal... | @property
def _default_params(self) ->Dict[str, Any]:
"""Get the default parameters for calling the Aleph Alpha API."""
return {'maximum_tokens': self.maximum_tokens, 'temperature': self.
temperature, 'top_k': self.top_k, 'top_p': self.top_p,
'presence_penalty': self.presence_penalty, 'frequency... | Get the default parameters for calling the Aleph Alpha API. |
__getitem__ | ... | @overload
def __getitem__(self, item: slice) ->Tuple[AsyncIterator[T], ...]:
... | null |
__init__ | """
Args:
file_path: The path to the file to load.
mode: The mode to use when loading the file. Can be one of "single",
"multi", or "all". Default is "single".
**unstructured_kwargs: Any kwargs to pass to the unstructured.
"""
validate_unstructured_ve... | def __init__(self, file_path: str, mode: str='single', **
unstructured_kwargs: Any):
"""
Args:
file_path: The path to the file to load.
mode: The mode to use when loading the file. Can be one of "single",
"multi", or "all". Default is "single".
**unst... | Args:
file_path: The path to the file to load.
mode: The mode to use when loading the file. Can be one of "single",
"multi", or "all". Default is "single".
**unstructured_kwargs: Any kwargs to pass to the unstructured. |
install_python_packages | """Install python packages in the sandbox."""
self.session.install_python_packages(package_names) | def install_python_packages(self, package_names: Union[str, List[str]]) ->None:
"""Install python packages in the sandbox."""
self.session.install_python_packages(package_names) | Install python packages in the sandbox. |
_llm_type | """Return type of llm."""
return 'titan_takeoff' | @property
def _llm_type(self) ->str:
"""Return type of llm."""
return 'titan_takeoff' | Return type of llm. |
format_response_payload | """Formats the response body according to the output
schema of the model. Returns the data type that is
received from the response.
""" | @abstractmethod
def format_response_payload(self, output: bytes) ->str:
"""Formats the response body according to the output
schema of the model. Returns the data type that is
received from the response.
""" | Formats the response body according to the output
schema of the model. Returns the data type that is
received from the response. |
on_tool_error | """Run when tool errors."""
self.metrics['step'] += 1
self.metrics['errors'] += 1 | def on_tool_error(self, error: BaseException, **kwargs: Any) ->None:
"""Run when tool errors."""
self.metrics['step'] += 1
self.metrics['errors'] += 1 | Run when tool errors. |
_stop | return ['Observation:'] | @property
def _stop(self) ->List[str]:
return ['Observation:'] | null |
_search | """
Return docs similar to query.
Args:
query (str, optional): Text to look up similar docs.
embedding (Union[List[float], np.ndarray], optional): Query's embedding.
embedding_function (Callable, optional): Function to convert `query`
into embedding.
... | def _search(self, query: Optional[str]=None, embedding: Optional[Union[List
[float], np.ndarray]]=None, embedding_function: Optional[Callable]=None,
k: int=4, distance_metric: Optional[str]=None,
use_maximal_marginal_relevance: bool=False, fetch_k: Optional[int]=20,
filter: Optional[Union[Dict, Callable... | Return docs similar to query.
Args:
query (str, optional): Text to look up similar docs.
embedding (Union[List[float], np.ndarray], optional): Query's embedding.
embedding_function (Callable, optional): Function to convert `query`
into embedding.
k (int): Number of Documents to return.
dist... |
get_initial_label | """Return the markdown label for a new LLMThought that doesn't have
an associated tool yet.
"""
return f'{THINKING_EMOJI} **Thinking...**' | def get_initial_label(self) ->str:
"""Return the markdown label for a new LLMThought that doesn't have
an associated tool yet.
"""
return f'{THINKING_EMOJI} **Thinking...**' | Return the markdown label for a new LLMThought that doesn't have
an associated tool yet. |
embed_documents | """Call out to Aleph Alpha's asymmetric Document endpoint.
Args:
texts: The list of texts to embed.
Returns:
List of embeddings, one for each text.
"""
try:
from aleph_alpha_client import Prompt, SemanticEmbeddingRequest, SemanticRepresentation
except ImportError:
... | def embed_documents(self, texts: List[str]) ->List[List[float]]:
"""Call out to Aleph Alpha's asymmetric Document endpoint.
Args:
texts: The list of texts to embed.
Returns:
List of embeddings, one for each text.
"""
try:
from aleph_alpha_client import P... | Call out to Aleph Alpha's asymmetric Document endpoint.
Args:
texts: The list of texts to embed.
Returns:
List of embeddings, one for each text. |
test_all_imports | """Simple test to make sure all things can be imported."""
for cls in llms.__all__:
assert issubclass(getattr(llms, cls), BaseLLM)
assert set(llms.__all__) == set(EXPECT_ALL) | def test_all_imports() ->None:
"""Simple test to make sure all things can be imported."""
for cls in llms.__all__:
assert issubclass(getattr(llms, cls), BaseLLM)
assert set(llms.__all__) == set(EXPECT_ALL) | Simple test to make sure all things can be imported. |
test_regex_dict_result | """Test regex dict result."""
regex_dict_parser = RegexDictParser(output_key_to_format=
DEF_OUTPUT_KEY_TO_FORMAT, no_update_value='N/A')
result_dict = regex_dict_parser.parse_folder(DEF_README)
print('parse_result:', result_dict)
assert DEF_EXPECTED_RESULT == result_dict | def test_regex_dict_result() ->None:
"""Test regex dict result."""
regex_dict_parser = RegexDictParser(output_key_to_format=
DEF_OUTPUT_KEY_TO_FORMAT, no_update_value='N/A')
result_dict = regex_dict_parser.parse_folder(DEF_README)
print('parse_result:', result_dict)
assert DEF_EXPECTED_RESUL... | Test regex dict result. |
_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 |
test_model | documents = ['hi', 'qianfan']
embedding = QianfanEmbeddingsEndpoint(model='Embedding-V1')
output = embedding.embed_documents(documents)
assert len(output) == 2 | def test_model() ->None:
documents = ['hi', 'qianfan']
embedding = QianfanEmbeddingsEndpoint(model='Embedding-V1')
output = embedding.embed_documents(documents)
assert len(output) == 2 | null |
_import_databricks | from langchain_community.llms.databricks import Databricks
return Databricks | def _import_databricks() ->Any:
from langchain_community.llms.databricks import Databricks
return Databricks | null |
_llm_type | """Return type of llm."""
return 'volc-engine-maas-llm' | @property
def _llm_type(self) ->str:
"""Return type of llm."""
return 'volc-engine-maas-llm' | Return type of llm. |
test_call | """Test that call gives the correct answer."""
chain = SerpAPIWrapper()
output = chain.run("What was Obama's first name?")
assert output == 'Barack Hussein Obama II' | def test_call() ->None:
"""Test that call gives the correct answer."""
chain = SerpAPIWrapper()
output = chain.run("What was Obama's first name?")
assert output == 'Barack Hussein Obama II' | Test that call gives the correct answer. |
_import_together | from langchain_community.llms.together import Together
return Together | def _import_together() ->Any:
from langchain_community.llms.together import Together
return Together | null |
run | """Run query through OpenAI and parse result."""
if is_openai_v1():
response = self.client.generate(prompt=query, n=self.n, size=self.size,
model=self.model_name, quality=self.quality)
image_urls = self.separator.join([item.url for item in response.data])
else:
response = self.client.create(prompt=q... | def run(self, query: str) ->str:
"""Run query through OpenAI and parse result."""
if is_openai_v1():
response = self.client.generate(prompt=query, n=self.n, size=self.
size, model=self.model_name, quality=self.quality)
image_urls = self.separator.join([item.url for item in response.d... | Run query through OpenAI and parse result. |
dict | """Return dictionary representation of agent."""
_dict = super().dict()
try:
_type = self._agent_type
except NotImplementedError:
_type = None
if isinstance(_type, AgentType):
_dict['_type'] = str(_type.value)
elif _type is not None:
_dict['_type'] = _type
return _dict | def dict(self, **kwargs: Any) ->Dict:
"""Return dictionary representation of agent."""
_dict = super().dict()
try:
_type = self._agent_type
except NotImplementedError:
_type = None
if isinstance(_type, AgentType):
_dict['_type'] = str(_type.value)
elif _type is not None:
... | Return dictionary representation of agent. |
_Slice | if t.lower:
self.dispatch(t.lower)
self.write(':')
if t.upper:
self.dispatch(t.upper)
if t.step:
self.write(':')
self.dispatch(t.step) | def _Slice(self, t):
if t.lower:
self.dispatch(t.lower)
self.write(':')
if t.upper:
self.dispatch(t.upper)
if t.step:
self.write(':')
self.dispatch(t.step) | null |
__init__ | super().__init__()
self.ignore_case = ignore_case
self.ignore_punctuation = ignore_punctuation
self.ignore_numbers = ignore_numbers | def __init__(self, *, ignore_case: bool=False, ignore_punctuation: bool=
False, ignore_numbers: bool=False, **kwargs: Any):
super().__init__()
self.ignore_case = ignore_case
self.ignore_punctuation = ignore_punctuation
self.ignore_numbers = ignore_numbers | null |
test_embaas_embed_query | """Test embaas embeddings with multiple texts."""
text = 'foo'
embeddings = EmbaasEmbeddings()
output = embeddings.embed_query(text)
assert len(output) == 1024 | def test_embaas_embed_query() ->None:
"""Test embaas embeddings with multiple texts."""
text = 'foo'
embeddings = EmbaasEmbeddings()
output = embeddings.embed_query(text)
assert len(output) == 1024 | Test embaas embeddings with multiple texts. |
pretty_print_dict | return title + '\n' + json.dumps(d, indent=4) | def pretty_print_dict(title: str, d: dict) ->str:
return title + '\n' + json.dumps(d, indent=4) | null |
test_llm_chain_extractor_empty | texts = ['I love chocolate chip cookies—my mother makes great cookies.',
"Don't you just love Caesar salad?", "Let's go to Olive Garden!"]
doc = Document(page_content=' '.join(texts))
compressor = LLMChainExtractor.from_llm(ChatOpenAI())
actual = compressor.compress_documents([doc], 'Tell me about the Roman Empire'... | def test_llm_chain_extractor_empty() ->None:
texts = ['I love chocolate chip cookies—my mother makes great cookies.',
"Don't you just love Caesar salad?", "Let's go to Olive Garden!"]
doc = Document(page_content=' '.join(texts))
compressor = LLMChainExtractor.from_llm(ChatOpenAI())
actual = comp... | null |
get_lc_namespace | """Get the namespace of the langchain object."""
return ['langchain', 'llms', 'fireworks'] | @classmethod
def get_lc_namespace(cls) ->List[str]:
"""Get the namespace of the langchain object."""
return ['langchain', 'llms', 'fireworks'] | Get the namespace of the langchain object. |
_combine_llm_outputs | if llm_outputs[0] is None:
return {}
return llm_outputs[0] | def _combine_llm_outputs(self, llm_outputs: List[Optional[dict]]) ->dict:
if llm_outputs[0] is None:
return {}
return llm_outputs[0] | null |
from_zapier_nla_wrapper | """Create a toolkit from a ZapierNLAWrapper."""
actions = zapier_nla_wrapper.list()
tools = [ZapierNLARunAction(action_id=action['id'], zapier_description=
action['description'], params_schema=action['params'], api_wrapper=
zapier_nla_wrapper) for action in actions]
return cls(tools=tools) | @classmethod
def from_zapier_nla_wrapper(cls, zapier_nla_wrapper: ZapierNLAWrapper
) ->'ZapierToolkit':
"""Create a toolkit from a ZapierNLAWrapper."""
actions = zapier_nla_wrapper.list()
tools = [ZapierNLARunAction(action_id=action['id'], zapier_description=
action['description'], params_schema... | Create a toolkit from a ZapierNLAWrapper. |
test_load_max_content_chars | """Test that cuts off document contents at max_content_chars."""
loader = BibtexLoader(file_path=str(BIBTEX_EXAMPLE_FILE), max_content_chars=10)
doc = loader.load()[0]
assert len(doc.page_content) == 10 | @pytest.mark.requires('fitz', 'bibtexparser')
def test_load_max_content_chars() ->None:
"""Test that cuts off document contents at max_content_chars."""
loader = BibtexLoader(file_path=str(BIBTEX_EXAMPLE_FILE),
max_content_chars=10)
doc = loader.load()[0]
assert len(doc.page_content) == 10 | Test that cuts off document contents at max_content_chars. |
copy_repo | """
Copies a repo, ignoring git folders.
Raises FileNotFound error if it can't find source
"""
def ignore_func(_, files):
return [f for f in files if f == '.git']
shutil.copytree(source, destination, ignore=ignore_func) | def copy_repo(source: Path, destination: Path) ->None:
"""
Copies a repo, ignoring git folders.
Raises FileNotFound error if it can't find source
"""
def ignore_func(_, files):
return [f for f in files if f == '.git']
shutil.copytree(source, destination, ignore=ignore_func) | Copies a repo, ignoring git folders.
Raises FileNotFound error if it can't find source |
_import_outline | from langchain_community.utilities.outline import OutlineAPIWrapper
return OutlineAPIWrapper | def _import_outline() ->Any:
from langchain_community.utilities.outline import OutlineAPIWrapper
return OutlineAPIWrapper | null |
test_clear | zep_chat.clear()
zep_chat.zep_client.memory.delete_memory.assert_called_once_with('test_session'
) | @pytest.mark.requires('zep_python')
def test_clear(mocker: MockerFixture, zep_chat: ZepChatMessageHistory) ->None:
zep_chat.clear()
zep_chat.zep_client.memory.delete_memory.assert_called_once_with(
'test_session') | null |
_call_before_predict | ... | @abstractmethod
def _call_before_predict(self, inputs: Dict[str, Any]) ->TEvent:
... | null |
test_pickbest_textembedder_w_full_label_w_emb | feature_embedder = pick_best_chain.PickBestFeatureEmbedder(auto_embed=False,
model=MockEncoder())
str1 = '0'
str2 = '1'
str3 = '2'
encoded_str1 = rl_chain.stringify_embedding(list(encoded_keyword + str1))
encoded_str2 = rl_chain.stringify_embedding(list(encoded_keyword + str2))
encoded_str3 = rl_chain.stringify_emb... | @pytest.mark.requires('vowpal_wabbit_next')
def test_pickbest_textembedder_w_full_label_w_emb() ->None:
feature_embedder = pick_best_chain.PickBestFeatureEmbedder(auto_embed=
False, model=MockEncoder())
str1 = '0'
str2 = '1'
str3 = '2'
encoded_str1 = rl_chain.stringify_embedding(list(encoded... | null |
assert_docs | for doc in docs:
assert doc.page_content
assert doc.metadata
assert set(doc.metadata) == {'Published', 'Title', 'Authors', 'Summary'} | def assert_docs(docs: List[Document]) ->None:
for doc in docs:
assert doc.page_content
assert doc.metadata
assert set(doc.metadata) == {'Published', 'Title', 'Authors', 'Summary'
} | null |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.