method_name
stringlengths
1
78
method_body
stringlengths
3
9.66k
full_code
stringlengths
31
10.7k
docstring
stringlengths
4
4.74k
get_format_instructions
if self.pydantic_object is None: return 'Return a JSON object.' else: schema = self.pydantic_object.schema() reduced_schema = schema if 'title' in reduced_schema: del reduced_schema['title'] if 'type' in reduced_schema: del reduced_schema['type'] schema_str = json.dumps(reduced_s...
def get_format_instructions(self) ->str: if self.pydantic_object is None: return 'Return a JSON object.' else: schema = self.pydantic_object.schema() reduced_schema = schema if 'title' in reduced_schema: del reduced_schema['title'] if 'type' in reduced_schema:...
null
test_all_imports
assert set(__all__) == set(EXPECTED_ALL)
def test_all_imports() ->None: assert set(__all__) == set(EXPECTED_ALL)
null
is_gemini
"""Returns whether a model is belongs to a Gemini family or not.""" return _is_gemini_model(self.model_name)
@property def is_gemini(self) ->bool: """Returns whether a model is belongs to a Gemini family or not.""" return _is_gemini_model(self.model_name)
Returns whether a model is belongs to a Gemini family or not.
_stream
"""Call the IBM watsonx.ai inference endpoint which then streams the response. Args: prompt: The prompt to pass into the model. stop: Optional list of stop words to use when generating. run_manager: Optional callback manager. Returns: The iterator which yi...
def _stream(self, prompt: str, stop: Optional[List[str]]=None, run_manager: Optional[CallbackManagerForLLMRun]=None, **kwargs: Any) ->Iterator[ GenerationChunk]: """Call the IBM watsonx.ai inference endpoint which then streams the response. Args: prompt: The prompt to pass into the model...
Call the IBM watsonx.ai inference endpoint which then streams the response. Args: prompt: The prompt to pass into the model. stop: Optional list of stop words to use when generating. run_manager: Optional callback manager. Returns: The iterator which yields generation chunks. Example: .. code-block:...
delete
"""Delete by vector IDs. Args: ids: List of ids to delete. """ self._collection.delete(ids=ids)
def delete(self, ids: Optional[List[str]]=None, **kwargs: Any) ->None: """Delete by vector IDs. Args: ids: List of ids to delete. """ self._collection.delete(ids=ids)
Delete by vector IDs. Args: ids: List of ids to delete.
on_tool_start
"""Do nothing when tool starts.""" pass
def on_tool_start(self, serialized: Dict[str, Any], input_str: str, ** kwargs: Any) ->None: """Do nothing when tool starts.""" pass
Do nothing when tool starts.
test_multiple_msg
human_msg = HumanMessage(content='human', additional_kwargs={'key': 'value'}) ai_msg = AIMessage(content='ai') sys_msg = SystemMessage(content='sys') msgs = [human_msg, ai_msg, sys_msg] assert messages_from_dict(messages_to_dict(msgs)) == msgs
def test_multiple_msg() ->None: human_msg = HumanMessage(content='human', additional_kwargs={'key': 'value'}) ai_msg = AIMessage(content='ai') sys_msg = SystemMessage(content='sys') msgs = [human_msg, ai_msg, sys_msg] assert messages_from_dict(messages_to_dict(msgs)) == msgs
null
embeddings
"""Access the query embedding object if available.""" return self._embedding
@property def embeddings(self) ->Optional[Embeddings]: """Access the query embedding object if available.""" return self._embedding
Access the query embedding object if available.
_build_query_sql
"""Builds Rockset SQL query to query similar vectors to query_vector""" q_embedding_str = ','.join(map(str, query_embedding)) distance_str = ( f'{distance_func.value}({self._embedding_key}, [{q_embedding_str}]) as dist' ) where_str = f'WHERE {where_str}\n' if where_str else '' return f"""SELECT * EXCEPT({self._...
def _build_query_sql(self, query_embedding: List[float], distance_func: DistanceFunction, k: int=4, where_str: Optional[str]=None) ->str: """Builds Rockset SQL query to query similar vectors to query_vector""" q_embedding_str = ','.join(map(str, query_embedding)) distance_str = ( f'{distance_fun...
Builds Rockset SQL query to query similar vectors to query_vector
similarity_search_with_score_by_vector
try: from timescale_vector import client except ImportError: raise ImportError( 'Could not import timescale_vector python package. Please install it with `pip install timescale-vector`.' ) results = self.sync_client.search(embedding, limit=k, filter=filter, predicates=predicates, uuid_time_f...
def similarity_search_with_score_by_vector(self, embedding: Optional[List[ float]], k: int=4, filter: Optional[Union[dict, list]]=None, predicates: Optional[Predicates]=None, **kwargs: Any) ->List[Tuple[Document, float]]: try: from timescale_vector import client except ImportError: raise...
null
test_neo4jvector_special_character
"""Test removing lucene.""" text_embeddings = FakeEmbeddingsWithOsDimension().embed_documents(texts) text_embedding_pairs = list(zip(texts, text_embeddings)) docsearch = Neo4jVector.from_embeddings(text_embeddings= text_embedding_pairs, embedding=FakeEmbeddingsWithOsDimension(), url= url, username=username, pas...
def test_neo4jvector_special_character() ->None: """Test removing lucene.""" text_embeddings = FakeEmbeddingsWithOsDimension().embed_documents(texts) text_embedding_pairs = list(zip(texts, text_embeddings)) docsearch = Neo4jVector.from_embeddings(text_embeddings= text_embedding_pairs, embedding=...
Test removing lucene.
get_pinecone_index
"""Return a Pinecone Index instance. Args: index_name: Name of the index to use. pool_threads: Number of threads to use for index upsert. Returns: Pinecone Index instance.""" try: import pinecone except ImportError: raise ValueError( 'Could not import...
@classmethod def get_pinecone_index(cls, index_name: Optional[str], pool_threads: int=4 ) ->Index: """Return a Pinecone Index instance. Args: index_name: Name of the index to use. pool_threads: Number of threads to use for index upsert. Returns: Pinecone Inde...
Return a Pinecone Index instance. Args: index_name: Name of the index to use. pool_threads: Number of threads to use for index upsert. Returns: Pinecone Index instance.
lazy_load
"""Download a selected dataset lazily. Returns: an iterator of Documents. """ return (self.sample_to_document_function(s) for s in self.dataset.take(self .load_max_docs) if self.sample_to_document_function is not None)
def lazy_load(self) ->Iterator[Document]: """Download a selected dataset lazily. Returns: an iterator of Documents. """ return (self.sample_to_document_function(s) for s in self.dataset.take( self.load_max_docs) if self.sample_to_document_function is not None)
Download a selected dataset lazily. Returns: an iterator of Documents.
_transform_prompt
"""Transform prompt.""" if self.inject_instruction_format: prompt = PROMPT_FOR_GENERATION_FORMAT.format(instruction=prompt) return prompt
def _transform_prompt(self, prompt: str) ->str: """Transform prompt.""" if self.inject_instruction_format: prompt = PROMPT_FOR_GENERATION_FORMAT.format(instruction=prompt) return prompt
Transform prompt.
test_action_w_namespace_w_some_emb
str1 = 'test1' str2 = 'test2' str3 = 'test3' encoded_str2 = base.stringify_embedding(list(encoded_keyword + str2)) encoded_str3 = base.stringify_embedding(list(encoded_keyword + str3)) expected = [{'test_namespace': str1}, {'test_namespace': encoded_str2}, { 'test_namespace': encoded_str3}] assert base.embed([{'tes...
@pytest.mark.requires('vowpal_wabbit_next') def test_action_w_namespace_w_some_emb() ->None: str1 = 'test1' str2 = 'test2' str3 = 'test3' encoded_str2 = base.stringify_embedding(list(encoded_keyword + str2)) encoded_str3 = base.stringify_embedding(list(encoded_keyword + str3)) expected = [{'test...
null
__init__
""" Create an AstraDB vector store object. See class docstring for help. """ try: from astrapy.db import AstraDB as LibAstraDB from astrapy.db import AstraDBCollection as LibAstraDBCollection except (ImportError, ModuleNotFoundError): raise ImportError( 'Could not import a recent ast...
def __init__(self, *, embedding: Embeddings, collection_name: str, token: Optional[str]=None, api_endpoint: Optional[str]=None, astra_db_client: Optional[Any]=None, namespace: Optional[str]=None, metric: Optional[str ]=None, batch_size: Optional[int]=None, bulk_insert_batch_concurrency: Optional[int]=No...
Create an AstraDB vector store object. See class docstring for help.
_import_sklearn
from langchain_community.vectorstores.sklearn import SKLearnVectorStore return SKLearnVectorStore
def _import_sklearn() ->Any: from langchain_community.vectorstores.sklearn import SKLearnVectorStore return SKLearnVectorStore
null
test_qdrant_add_texts_stores_ids
"""Test end to end Qdrant.add_texts stores provided ids.""" from qdrant_client import QdrantClient from qdrant_client.http import models as rest ids = ['fa38d572-4c31-4579-aedc-1960d79df6df', 'cdc1aa36-d6ab-4fb2-8a94-56674fd27484'] client = QdrantClient(':memory:') collection_name = uuid.uuid4().hex client.recreate...
@pytest.mark.parametrize('batch_size', [1, 64]) def test_qdrant_add_texts_stores_ids(batch_size: int) ->None: """Test end to end Qdrant.add_texts stores provided ids.""" from qdrant_client import QdrantClient from qdrant_client.http import models as rest ids = ['fa38d572-4c31-4579-aedc-1960d79df6df', ...
Test end to end Qdrant.add_texts stores provided ids.
set_llm_cache
"""Set a new LLM cache, overwriting the previous value, if any.""" import langchain with warnings.catch_warnings(): warnings.filterwarnings('ignore', message= 'Importing llm_cache from langchain root module is no longer supported' ) langchain.llm_cache = value global _llm_cache _llm_cache = valu...
def set_llm_cache(value: Optional['BaseCache']) ->None: """Set a new LLM cache, overwriting the previous value, if any.""" import langchain with warnings.catch_warnings(): warnings.filterwarnings('ignore', message= 'Importing llm_cache from langchain root module is no longer supported' ...
Set a new LLM cache, overwriting the previous value, if any.
on_agent_action
"""Do nothing when agent takes a specific action.""" pass
def on_agent_action(self, action: AgentAction, **kwargs: Any) ->Any: """Do nothing when agent takes a specific action.""" pass
Do nothing when agent takes a specific action.
run
return self._run(commands)
def run(self, commands: List[str]) ->str: return self._run(commands)
null
test_all_imports
assert set(__all__) == set(EXPECTED_ALL)
def test_all_imports() ->None: assert set(__all__) == set(EXPECTED_ALL)
null
test_imports
"""Test that you can import all top level things okay.""" from langchain_community.chat_models import ChatOpenAI from langchain_community.document_loaders import BSHTMLLoader from langchain_community.embeddings import OpenAIEmbeddings from langchain_community.llms import OpenAI from langchain_community.utilities import...
def test_imports() ->None: """Test that you can import all top level things okay.""" from langchain_community.chat_models import ChatOpenAI from langchain_community.document_loaders import BSHTMLLoader from langchain_community.embeddings import OpenAIEmbeddings from langchain_community.llms import O...
Test that you can import all top level things okay.
extract_functions_classes
def extract_func(elements: List[str], start_idx: int, end_idx: int) ->None: elements.append(self._extract_code(start_idx, end_idx)) return self._process_lines(extract_func)
def extract_functions_classes(self) ->List[str]: def extract_func(elements: List[str], start_idx: int, end_idx: int) ->None: elements.append(self._extract_code(start_idx, end_idx)) return self._process_lines(extract_func)
null
test_context_w_namespace_w_emb
str1 = 'test' encoded_str1 = base.stringify_embedding(list(encoded_keyword + str1)) expected = [{'test_namespace': encoded_str1}] assert base.embed({'test_namespace': base.Embed(str1)}, MockEncoder() ) == expected expected_embed_and_keep = [{'test_namespace': str1 + ' ' + encoded_str1}] assert base.embed({'test_nam...
@pytest.mark.requires('vowpal_wabbit_next') def test_context_w_namespace_w_emb() ->None: str1 = 'test' encoded_str1 = base.stringify_embedding(list(encoded_keyword + str1)) expected = [{'test_namespace': encoded_str1}] assert base.embed({'test_namespace': base.Embed(str1)}, MockEncoder() ) == ex...
null
test_all_imports
assert set(__all__) == set(EXPECTED_ALL)
def test_all_imports() ->None: assert set(__all__) == set(EXPECTED_ALL)
null
get_time
"""Get the current server time as a high resolution timestamp! It's important to get this from the server to ensure a monotonic clock, otherwise there may be data loss when cleaning up old documents! Returns: The current server time as a float timestamp. """
@abstractmethod def get_time(self) ->float: """Get the current server time as a high resolution timestamp! It's important to get this from the server to ensure a monotonic clock, otherwise there may be data loss when cleaning up old documents! Returns: The current server time a...
Get the current server time as a high resolution timestamp! It's important to get this from the server to ensure a monotonic clock, otherwise there may be data loss when cleaning up old documents! Returns: The current server time as a float timestamp.
get_schema
"""Returns the schema of the FalkorDB database""" return self.schema
@property def get_schema(self) ->str: """Returns the schema of the FalkorDB database""" return self.schema
Returns the schema of the FalkorDB database
_import_tigris
from langchain_community.vectorstores.tigris import Tigris return Tigris
def _import_tigris() ->Any: from langchain_community.vectorstores.tigris import Tigris return Tigris
null
__init__
"""Load a list of URLs using Selenium and unstructured.""" try: import selenium except ImportError: raise ImportError( 'selenium package not found, please install it with `pip install selenium`' ) try: import unstructured except ImportError: raise ImportError( 'unstructured packa...
def __init__(self, urls: List[str], continue_on_failure: bool=True, browser: Literal['chrome', 'firefox']='chrome', binary_location: Optional[str]= None, executable_path: Optional[str]=None, headless: bool=True, arguments: List[str]=[]): """Load a list of URLs using Selenium and unstructured.""" try...
Load a list of URLs using Selenium and unstructured.
_identifying_params
"""Get the identifying parameters.""" return {**{'endpoint_url': self.endpoint_url}, **self._default_params}
@property def _identifying_params(self) ->Mapping[str, Any]: """Get the identifying parameters.""" return {**{'endpoint_url': self.endpoint_url}, **self._default_params}
Get the identifying parameters.
similarity_search
"""Run similarity search with TimescaleVector with distance. Args: query (str): Query text to search for. k (int): Number of results to return. Defaults to 4. filter (Optional[Dict[str, str]]): Filter by metadata. Defaults to None. Returns: List of Docum...
def similarity_search(self, query: str, k: int=4, filter: Optional[Union[ dict, list]]=None, predicates: Optional[Predicates]=None, **kwargs: Any ) ->List[Document]: """Run similarity search with TimescaleVector with distance. Args: query (str): Query text to search for. k (...
Run similarity search with TimescaleVector with distance. Args: query (str): Query text to search for. k (int): Number of results to return. Defaults to 4. filter (Optional[Dict[str, str]]): Filter by metadata. Defaults to None. Returns: List of Documents most similar to the query.
_parse_prediction
if isinstance(prediction, str): return prediction if self.result_arg: try: return prediction[self.result_arg] except KeyError: if isinstance(prediction, str): error_desc = ( f'Provided non-None `result_arg` (result_arg={self.result_arg}). But got prediction of typ...
def _parse_prediction(self, prediction: Any) ->str: if isinstance(prediction, str): return prediction if self.result_arg: try: return prediction[self.result_arg] except KeyError: if isinstance(prediction, str): error_desc = ( f'...
null
_default_params
"""Get the default parameters for calling Petals API.""" normal_params = {'temperature': self.temperature, 'max_new_tokens': self. max_new_tokens, 'top_p': self.top_p, 'top_k': self.top_k, 'do_sample': self.do_sample, 'max_length': self.max_length} return {**normal_params, **self.model_kwargs}
@property def _default_params(self) ->Dict[str, Any]: """Get the default parameters for calling Petals API.""" normal_params = {'temperature': self.temperature, 'max_new_tokens': self.max_new_tokens, 'top_p': self.top_p, 'top_k': self.top_k, 'do_sample': self.do_sample, 'max_length': self.max_le...
Get the default parameters for calling Petals API.
__getitem__
...
@overload def __getitem__(self, item: int) ->AsyncIterator[T]: ...
null
test_batch_iterate
"""Test batching function.""" assert list(batch_iterate(input_size, input_iterable)) == expected_output
@pytest.mark.parametrize('input_size, input_iterable, expected_output', [(2, [1, 2, 3, 4, 5], [[1, 2], [3, 4], [5]]), (3, [10, 20, 30, 40, 50], [[10, 20, 30], [40, 50]]), (1, [100, 200, 300], [[100], [200], [300]]), (4, [ ], [])]) def test_batch_iterate(input_size: int, input_iterable: List[str], expect...
Test batching function.
_import_arxiv_tool
from langchain_community.tools.arxiv.tool import ArxivQueryRun return ArxivQueryRun
def _import_arxiv_tool() ->Any: from langchain_community.tools.arxiv.tool import ArxivQueryRun return ArxivQueryRun
null
test_psychic_loader_initialization
PsychicLoader(api_key=self.MOCK_API_KEY, connector_id=self. MOCK_CONNECTOR_ID, account_id=self.MOCK_ACCOUNT_ID) mock_psychic.assert_called_once_with(secret_key=self.MOCK_API_KEY) mock_connector_id.assert_called_once_with(self.MOCK_CONNECTOR_ID)
def test_psychic_loader_initialization(self, mock_psychic: MagicMock, mock_connector_id: MagicMock) ->None: PsychicLoader(api_key=self.MOCK_API_KEY, connector_id=self. MOCK_CONNECTOR_ID, account_id=self.MOCK_ACCOUNT_ID) mock_psychic.assert_called_once_with(secret_key=self.MOCK_API_KEY) mock_conn...
null
extract_tags
""" Extract specific tags from a given HTML content. Args: html_content: The original HTML content string. tags: A list of tags to be extracted from the HTML. Returns: A string combining the content of the extracted tags. """ from bs4 import Beautifu...
@staticmethod def extract_tags(html_content: str, tags: List[str]) ->str: """ Extract specific tags from a given HTML content. Args: html_content: The original HTML content string. tags: A list of tags to be extracted from the HTML. Returns: A string com...
Extract specific tags from a given HTML content. Args: html_content: The original HTML content string. tags: A list of tags to be extracted from the HTML. Returns: A string combining the content of the extracted tags.
__deepcopy__
return self
def __deepcopy__(self, memo: dict) ->'FakeAsyncCallbackHandler': return self
null
test_lancedb
import lancedb embeddings = FakeEmbeddings() db = lancedb.connect('/tmp/lancedb') texts = ['text 1', 'text 2', 'item 3'] vectors = embeddings.embed_documents(texts) table = db.create_table('my_table', data=[{'vector': vectors[idx], 'id': text, 'text': text} for idx, text in enumerate(texts)], mode='overwrite') stor...
def test_lancedb() ->None: import lancedb embeddings = FakeEmbeddings() db = lancedb.connect('/tmp/lancedb') texts = ['text 1', 'text 2', 'item 3'] vectors = embeddings.embed_documents(texts) table = db.create_table('my_table', data=[{'vector': vectors[idx], 'id': text, 'text': text} for...
null
_load_env
dotenv_path = os.path.join(PROJECT_DIR, 'tests', 'integration_tests', '.env') if os.path.exists(dotenv_path): from dotenv import load_dotenv load_dotenv(dotenv_path)
def _load_env() ->None: dotenv_path = os.path.join(PROJECT_DIR, 'tests', 'integration_tests', '.env') if os.path.exists(dotenv_path): from dotenv import load_dotenv load_dotenv(dotenv_path)
null
test_multiple_ToSelectFrom_throws
llm, PROMPT = setup() chain = pick_best_chain.PickBest.from_llm(llm=llm, prompt=PROMPT, feature_embedder=pick_best_chain.PickBestFeatureEmbedder(auto_embed= False, model=MockEncoder())) actions = ['0', '1', '2'] with pytest.raises(ValueError): chain.run(User=rl_chain.BasedOn('Context'), action=rl_chain. ...
@pytest.mark.requires('vowpal_wabbit_next', 'sentence_transformers') def test_multiple_ToSelectFrom_throws() ->None: llm, PROMPT = setup() chain = pick_best_chain.PickBest.from_llm(llm=llm, prompt=PROMPT, feature_embedder=pick_best_chain.PickBestFeatureEmbedder(auto_embed =False, model=MockEncod...
null
_fetch_mime_types
"""Return a dict of supported file types to corresponding mime types.""" return fetch_mime_types(self._file_types)
@property def _fetch_mime_types(self) ->Dict[str, str]: """Return a dict of supported file types to corresponding mime types.""" return fetch_mime_types(self._file_types)
Return a dict of supported file types to corresponding mime types.
__init__
"""Initializes private fields.""" super().__init__(**data) self._client = ArceeWrapper(arcee_api_key=self.arcee_api_key. get_secret_value(), arcee_api_url=self.arcee_api_url, arcee_api_version =self.arcee_api_version, model_kwargs=self.model_kwargs, model_name= self.model) self._client.validate_model_traini...
def __init__(self, **data: Any) ->None: """Initializes private fields.""" super().__init__(**data) self._client = ArceeWrapper(arcee_api_key=self.arcee_api_key. get_secret_value(), arcee_api_url=self.arcee_api_url, arcee_api_version=self.arcee_api_version, model_kwargs=self. model_kw...
Initializes private fields.
create_structured_output_runnable
"""Create a runnable that uses an OpenAI function to get a structured output. Args: output_schema: Either a dictionary or pydantic.BaseModel class. If a dictionary is passed in, it's assumed to already be a valid JsonSchema. For best results, pydantic.BaseModels should have docstrin...
def create_structured_output_runnable(output_schema: Union[Dict[str, Any], Type[BaseModel]], llm: Runnable, prompt: BasePromptTemplate, *, output_parser: Optional[Union[BaseOutputParser, BaseGenerationOutputParser]]=None, **kwargs: Any) ->Runnable: """Create a runnable that uses an OpenAI function to ge...
Create a runnable that uses an OpenAI function to get a structured output. Args: output_schema: Either a dictionary or pydantic.BaseModel class. If a dictionary is passed in, it's assumed to already be a valid JsonSchema. For best results, pydantic.BaseModels should have docstrings describing what ...
input_keys
"""Defines the input keys.""" return self.prompt.input_variables
@property def input_keys(self) ->List[str]: """Defines the input keys.""" return self.prompt.input_variables
Defines the input keys.
_parse_document_tags
"""Return a set of all tags in within the document.""" if not self.collect_metadata: return set() match = self.TAG_REGEX.findall(content) if not match: return set() return {tag for tag in match}
def _parse_document_tags(self, content: str) ->set: """Return a set of all tags in within the document.""" if not self.collect_metadata: return set() match = self.TAG_REGEX.findall(content) if not match: return set() return {tag for tag in match}
Return a set of all tags in within the document.
test_memory_with_message_store
"""Test the memory with a message store.""" message_history = FirestoreChatMessageHistory(collection_name= 'chat_history', session_id='my-test-session', user_id='my-test-user') memory = ConversationBufferMemory(memory_key='baz', chat_memory= message_history, return_messages=True) memory.chat_memory.add_ai_messa...
def test_memory_with_message_store() ->None: """Test the memory with a message store.""" message_history = FirestoreChatMessageHistory(collection_name= 'chat_history', session_id='my-test-session', user_id='my-test-user') memory = ConversationBufferMemory(memory_key='baz', chat_memory= messa...
Test the memory with a message store.
test_concurrent_language_loader_for_python_with_parser_threshold
"""Test Python ConcurrentLoader with parser enabled and below threshold.""" file_path = Path(__file__).parent.parent.parent / 'examples' loader = ConcurrentLoader.from_filesystem(file_path, glob='hello_world.py', parser=LanguageParser(language='python', parser_threshold=1000)) docs = loader.load() assert len(docs) ...
def test_concurrent_language_loader_for_python_with_parser_threshold() ->None: """Test Python ConcurrentLoader with parser enabled and below threshold.""" file_path = Path(__file__).parent.parent.parent / 'examples' loader = ConcurrentLoader.from_filesystem(file_path, glob= 'hello_world.py', parser=...
Test Python ConcurrentLoader with parser enabled and below threshold.
_score_memories_importance
"""Score the absolute importance of the given memory.""" prompt = PromptTemplate.from_template( 'On the scale of 1 to 10, where 1 is purely mundane' + ' (e.g., brushing teeth, making bed) and 10 is' + ' extremely poignant (e.g., a break up, college' + ' acceptance), rate the likely poignancy of the' + ...
def _score_memories_importance(self, memory_content: str) ->List[float]: """Score the absolute importance of the given memory.""" prompt = PromptTemplate.from_template( 'On the scale of 1 to 10, where 1 is purely mundane' + ' (e.g., brushing teeth, making bed) and 10 is' + ' extremely po...
Score the absolute importance of the given memory.
_import_scenexplain
from langchain_community.utilities.scenexplain import SceneXplainAPIWrapper return SceneXplainAPIWrapper
def _import_scenexplain() ->Any: from langchain_community.utilities.scenexplain import SceneXplainAPIWrapper return SceneXplainAPIWrapper
null
ignore_agent
"""Whether to ignore agent callbacks.""" return self.ignore_agent_
@property def ignore_agent(self) ->bool: """Whether to ignore agent callbacks.""" return self.ignore_agent_
Whether to ignore agent callbacks.
messages_to_dict
"""Convert a sequence of Messages to a list of dictionaries. Args: messages: Sequence of messages (as BaseMessages) to convert. Returns: List of messages as dicts. """ return [message_to_dict(m) for m in messages]
def messages_to_dict(messages: Sequence[BaseMessage]) ->List[dict]: """Convert a sequence of Messages to a list of dictionaries. Args: messages: Sequence of messages (as BaseMessages) to convert. Returns: List of messages as dicts. """ return [message_to_dict(m) for m in messages]
Convert a sequence of Messages to a list of dictionaries. Args: messages: Sequence of messages (as BaseMessages) to convert. Returns: List of messages as dicts.
clear
"""Clear session memory from DB""" self.blob_history.clear_session_id(self.session_id)
def clear(self) ->None: """Clear session memory from DB""" self.blob_history.clear_session_id(self.session_id)
Clear session memory from DB
_import_python_tool_PythonAstREPLTool
raise ImportError( "This tool has been moved to langchain experiment. This tool has access to a python REPL. For best practices make sure to sandbox this tool. Read https://github.com/langchain-ai/langchain/blob/master/SECURITY.md To keep using this code as is, install langchain experimental and update relevant imp...
def _import_python_tool_PythonAstREPLTool() ->Any: raise ImportError( "This tool has been moved to langchain experiment. This tool has access to a python REPL. For best practices make sure to sandbox this tool. Read https://github.com/langchain-ai/langchain/blob/master/SECURITY.md To keep using this code as...
null
lc_attributes
return {'you_can_see_me': self.you_can_see_me}
@property def lc_attributes(self) ->Dict[str, str]: return {'you_can_see_me': self.you_can_see_me}
null
test_self_ask_with_search
"""Test functionality on a prompt.""" question = "What is the hometown of the reigning men's U.S. Open champion?" chain = SelfAskWithSearchChain(llm=OpenAI(temperature=0), search_chain= SearchApiAPIWrapper(), input_key='q', output_key='a') answer = chain.run(question) final_answer = answer.split('\n')[-1] assert fi...
def test_self_ask_with_search() ->None: """Test functionality on a prompt.""" question = "What is the hometown of the reigning men's U.S. Open champion?" chain = SelfAskWithSearchChain(llm=OpenAI(temperature=0), search_chain= SearchApiAPIWrapper(), input_key='q', output_key='a') answer = chain.r...
Test functionality on a prompt.
test_warning
_ = VertexAIEmbeddings() assert len(caplog.records) == 1 record = caplog.records[0] assert record.levelname == 'WARNING' expected_message = ( 'Model_name will become a required arg for VertexAIEmbeddings starting from Feb-01-2024. Currently the default is set to textembedding-gecko@001' ) assert record.message ...
def test_warning(caplog: pytest.LogCaptureFixture) ->None: _ = VertexAIEmbeddings() assert len(caplog.records) == 1 record = caplog.records[0] assert record.levelname == 'WARNING' expected_message = ( 'Model_name will become a required arg for VertexAIEmbeddings starting from Feb-01-2024. Cu...
null
_Import
self.fill('import ') interleave(lambda : self.write(', '), self.dispatch, t.names)
def _Import(self, t): self.fill('import ') interleave(lambda : self.write(', '), self.dispatch, t.names)
null
get_table_names
"""Get names of tables available.""" return self.table_names
def get_table_names(self) ->Iterable[str]: """Get names of tables available.""" return self.table_names
Get names of tables available.
build_extra
"""Build extra kwargs from additional params that were passed in.""" all_required_field_names = get_pydantic_field_names(cls) extra = values.get('model_kwargs', {}) for field_name in list(values): if field_name in extra: raise ValueError(f'Found {field_name} supplied twice.') if field_name not in all_re...
@root_validator(pre=True) def build_extra(cls, values: Dict[str, Any]) ->Dict[str, Any]: """Build extra kwargs from additional params that were passed in.""" all_required_field_names = get_pydantic_field_names(cls) extra = values.get('model_kwargs', {}) for field_name in list(values): if field_n...
Build extra kwargs from additional params that were passed in.
test_timescalevector_from_documents
"""Test end to end construction and search.""" texts = ['foo', 'bar', 'baz'] docs = [Document(page_content=t, metadata={'a': 'b'}) for t in texts] docsearch = TimescaleVector.from_documents(documents=docs, collection_name= 'test_collection', embedding=FakeEmbeddingsWithAdaDimension(), service_url=SERVICE_URL, p...
def test_timescalevector_from_documents() ->None: """Test end to end construction and search.""" texts = ['foo', 'bar', 'baz'] docs = [Document(page_content=t, metadata={'a': 'b'}) for t in texts] docsearch = TimescaleVector.from_documents(documents=docs, collection_name='test_collection', embed...
Test end to end construction and search.
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: Text to look up documents similar to. k: Number of Documents to return. Defaults to 4. ...
def max_marginal_relevance_search(self, query: str, k: int=DEFAULT_K, fetch_k: int=20, lambda_mult: float=0.5, filter: Optional[Dict[str, str ]]=None, where_document: Optional[Dict[str, str]]=None, **kwargs: Any ) ->List[Document]: """Return docs selected using the maximal marginal relevance. Ma...
Return docs selected using the maximal marginal relevance. Maximal marginal relevance optimizes for similarity to query AND diversity among selected documents. Args: query: Text to look up documents similar to. k: Number of Documents to return. Defaults to 4. fetch_k: Number of Documents to fetch to pass t...
add_texts
"""Upload texts to NucliaDB""" ids = [] from nuclia.sdk import NucliaResource factory = NucliaResource() for i, text in enumerate(texts): extra: Dict[str, Any] = {'metadata': ''} if metadatas: extra = {'metadata': metadatas[i]} id = factory.create(texts={'text': {'body': text}}, extra=extra, url= ...
def add_texts(self, texts: Iterable[str], metadatas: Optional[List[dict]]= None, **kwargs: Any) ->List[str]: """Upload texts to NucliaDB""" ids = [] from nuclia.sdk import NucliaResource factory = NucliaResource() for i, text in enumerate(texts): extra: Dict[str, Any] = {'metadata': ''} ...
Upload texts to NucliaDB
test_bad_action_line
"""Test handling when no action found.""" llm_output = """Thought: I need to search for NBA Thought: Search Action Input: 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_line() ->None: """Test handling when no action found.""" llm_output = ( 'Thought: I need to search for NBA\nThought: Search\nAction Input: NBA' ) with pytest.raises(OutputParserException) as e_info: get_action_and_input(llm_output) assert e_info.value.observat...
Test handling when no action found.
app_creation
"""Creates a Python file which will contain your Beam app definition.""" script = textwrap.dedent( """ import beam # The environment your code will run on app = beam.App( name="{name}", cpu={cpu}, memory="{memory}", gpu="{gpu}", pyt...
def app_creation(self) ->None: """Creates a Python file which will contain your Beam app definition.""" script = textwrap.dedent( """ import beam # The environment your code will run on app = beam.App( name="{name}", cpu={cpu}, memory="{memory}...
Creates a Python file which will contain your Beam app definition.
get_details
return self.details
def get_details(self) ->Any: return self.details
null
import_comet_llm_api
"""Import comet_llm api and raise an error if it is not installed.""" try: from comet_llm import experiment_info, flush from comet_llm.chains import api as chain_api from comet_llm.chains import chain, span except ImportError: raise ImportError( 'To use the CometTracer you need to have the `come...
def import_comet_llm_api() ->SimpleNamespace: """Import comet_llm api and raise an error if it is not installed.""" try: from comet_llm import experiment_info, flush from comet_llm.chains import api as chain_api from comet_llm.chains import chain, span except ImportError: rai...
Import comet_llm api and raise an error if it is not installed.
load_ts_git_dataset
json_url = 'https://s3.amazonaws.com/assets.timescale.com/ai/ts_git_log.json' tmp_file = 'ts_git_log.json' temp_dir = tempfile.gettempdir() json_file_path = os.path.join(temp_dir, tmp_file) if not os.path.exists(json_file_path): response = requests.get(json_url) if response.status_code == 200: with open...
def load_ts_git_dataset(service_url, collection_name='timescale_commits', num_records: int=500, partition_interval=timedelta(days=7)): json_url = ( 'https://s3.amazonaws.com/assets.timescale.com/ai/ts_git_log.json') tmp_file = 'ts_git_log.json' temp_dir = tempfile.gettempdir() json_file_path...
null
update_args
for dep_id in task.dep: if dep_id == -1: continue dep_task = self.id_task_map[dep_id] for k, v in task.args.items(): if f'<resource-{dep_id}>' in v: task.args[k] = task.args[k].replace(f'<resource-{dep_id}>', dep_task.result)
def update_args(self, task: Task) ->None: for dep_id in task.dep: if dep_id == -1: continue dep_task = self.id_task_map[dep_id] for k, v in task.args.items(): if f'<resource-{dep_id}>' in v: task.args[k] = task.args[k].replace(f'<resource-{dep_id}>', ...
null
on_chain_end
"""Do nothing.""" pass
def on_chain_end(self, outputs: Dict[str, Any], **kwargs: Any) ->None: """Do nothing.""" pass
Do nothing.
_call
"""Return `foo` if longer than 10000 words, else `bar`.""" if len(prompt) > 10000: return 'foo' else: return 'bar'
def _call(self, prompt: str, stop: Optional[List[str]]=None, run_manager: Optional[CallbackManagerForLLMRun]=None, **kwargs: Any) ->str: """Return `foo` if longer than 10000 words, else `bar`.""" if len(prompt) > 10000: return 'foo' else: return 'bar'
Return `foo` if longer than 10000 words, else `bar`.
get_lc_namespace
"""Get the namespace of the langchain object.""" return ['langchain', 'llms', 'openai']
@classmethod def get_lc_namespace(cls) ->List[str]: """Get the namespace of the langchain object.""" return ['langchain', 'llms', 'openai']
Get the namespace of the langchain object.
_import_koboldai
from langchain_community.llms.koboldai import KoboldApiLLM return KoboldApiLLM
def _import_koboldai() ->Any: from langchain_community.llms.koboldai import KoboldApiLLM return KoboldApiLLM
null
_import_watsonxllm
from langchain_community.llms.watsonxllm import WatsonxLLM return WatsonxLLM
def _import_watsonxllm() ->Any: from langchain_community.llms.watsonxllm import WatsonxLLM return WatsonxLLM
null
test_catches_forbidden_keys
"""Make sure we raise exception on keys that are not allowed; e.g., absolute path""" with pytest.raises(InvalidKeyException): file_store.mset([('/etc', b'value1')]) with pytest.raises(InvalidKeyException): list(file_store.yield_keys('/etc/passwd')) with pytest.raises(InvalidKeyException): file_store.mget(['...
def test_catches_forbidden_keys(file_store: LocalFileStore) ->None: """Make sure we raise exception on keys that are not allowed; e.g., absolute path""" with pytest.raises(InvalidKeyException): file_store.mset([('/etc', b'value1')]) with pytest.raises(InvalidKeyException): list(file_store.yi...
Make sure we raise exception on keys that are not allowed; e.g., absolute path
test_chat_google_raises_with_invalid_top_k
pytest.importorskip('google.generativeai') with pytest.raises(ValueError) as e: ChatGooglePalm(google_api_key='fake', top_k=-5) assert 'must be positive' in str(e)
def test_chat_google_raises_with_invalid_top_k() ->None: pytest.importorskip('google.generativeai') with pytest.raises(ValueError) as e: ChatGooglePalm(google_api_key='fake', top_k=-5) assert 'must be positive' in str(e)
null
test_from_texts_with_scores
"""Test end to end construction and search with scores and IDs.""" texts = ['foo', 'bar', 'baz'] metadatas = [{'page': i} for i in range(len(texts))] docsearch = Pinecone.from_texts(texts, embedding_openai, index_name= index_name, metadatas=metadatas, namespace=namespace_name) output = docsearch.similarity_search_w...
@pytest.mark.vcr() def test_from_texts_with_scores(self, embedding_openai: OpenAIEmbeddings ) ->None: """Test end to end construction and search with scores and IDs.""" texts = ['foo', 'bar', 'baz'] metadatas = [{'page': i} for i in range(len(texts))] docsearch = Pinecone.from_texts(texts, embedding...
Test end to end construction and search with scores and IDs.
embed
""" Embeds the actions or context using the SentenceTransformer model (or a model that has an `encode` function) Attributes: to_embed: (Union[Union(str, _Embed(str)), Dict, List[Union(str, _Embed(str))], List[Dict]], required) The text to be embedded, either a string, a list of strings or a dictionary ...
def embed(to_embed: Union[Union[str, _Embed], Dict, List[Union[str, _Embed] ], List[Dict]], model: Any, namespace: Optional[str]=None) ->List[Dict[ str, Union[str, List[str]]]]: """ Embeds the actions or context using the SentenceTransformer model (or a model that has an `encode` function) Attribut...
Embeds the actions or context using the SentenceTransformer model (or a model that has an `encode` function) Attributes: to_embed: (Union[Union(str, _Embed(str)), Dict, List[Union(str, _Embed(str))], List[Dict]], required) The text to be embedded, either a string, a list of strings or a dictionary or a list of dic...
embed_documents
"""Compute doc embeddings using a Bedrock model. Args: texts: The list of texts to embed Returns: List of embeddings, one for each text. """ results = [] for text in texts: response = self._embedding_func(text) results.append(response) return results
def embed_documents(self, texts: List[str]) ->List[List[float]]: """Compute doc embeddings using a Bedrock model. Args: texts: The list of texts to embed Returns: List of embeddings, one for each text. """ results = [] for text in texts: response = s...
Compute doc embeddings using a Bedrock model. Args: texts: The list of texts to embed Returns: List of embeddings, one for each text.
embed_query
"""Compute query embeddings using a Bedrock model. Args: text: The text to embed. Returns: Embeddings for the text. """ return self._embedding_func(text)
def embed_query(self, text: str) ->List[float]: """Compute query embeddings using a Bedrock model. Args: text: The text to embed. Returns: Embeddings for the text. """ return self._embedding_func(text)
Compute query embeddings using a Bedrock model. Args: text: The text to embed. Returns: Embeddings for the text.
lazy_load
"""Lazy load given path as pages.""" if self.web_path: blob = Blob.from_data(open(self.file_path, 'rb').read(), path=self.web_path ) else: blob = Blob.from_path(self.file_path) yield from self.parser.parse_folder(blob)
def lazy_load(self) ->Iterator[Document]: """Lazy load given path as pages.""" if self.web_path: blob = Blob.from_data(open(self.file_path, 'rb').read(), path=self. web_path) else: blob = Blob.from_path(self.file_path) yield from self.parser.parse_folder(blob)
Lazy load given path as pages.
similarity_search_with_score_by_vector
"""Return docs most similar to embedding vector. Args: embedding (str): Embedding to look up documents similar to. k (int): Number of Documents to return. Defaults to 4. Returns: List of (Document, score), the most similar to the query vector. """ return [(do...
def similarity_search_with_score_by_vector(self, embedding: List[float], k: int=4, filter: Optional[Dict[str, str]]=None) ->List[Tuple[Document, float] ]: """Return docs most similar to embedding vector. Args: embedding (str): Embedding to look up documents similar to. k (in...
Return docs most similar to embedding vector. Args: embedding (str): Embedding to look up documents similar to. k (int): Number of Documents to return. Defaults to 4. Returns: List of (Document, score), the most similar to the query vector.
__init__
try: from timescale_vector import client except ImportError: raise ImportError( 'Could not import timescale_vector python package. Please install it with `pip install timescale-vector`.' ) self.service_url = service_url self.embedding = embedding self.collection_name = collection_name self.num_d...
def __init__(self, service_url: str, embedding: Embeddings, collection_name: str=_LANGCHAIN_DEFAULT_COLLECTION_NAME, num_dimensions: int= ADA_TOKEN_COUNT, distance_strategy: DistanceStrategy= DEFAULT_DISTANCE_STRATEGY, pre_delete_collection: bool=False, logger: Optional[logging.Logger]=None, relevance_s...
null
parse_dict_through_component
"""Parse a dictionary by creating a component and then turning it back into a dictionary. This helps with two things 1. Extract and format data from a dictionary according to schema 2. Provide a central place to do this in a fault-tolerant way """ try: return asdict(component.from_data(data)) ...
def parse_dict_through_component(data: dict, component: Type[Component], fault_tolerant: bool=False) ->Dict: """Parse a dictionary by creating a component and then turning it back into a dictionary. This helps with two things 1. Extract and format data from a dictionary according to schema 2. P...
Parse a dictionary by creating a component and then turning it back into a dictionary. This helps with two things 1. Extract and format data from a dictionary according to schema 2. Provide a central place to do this in a fault-tolerant way
test_dereference_refs_one_ref
schema = {'type': 'object', 'properties': {'first_name': {'$ref': '#/$defs/name'}}, '$defs': {'name': {'type': 'string'}}} expected = {'type': 'object', 'properties': {'first_name': {'type': 'string'}}, '$defs': {'name': {'type': 'string'}}} actual = dereference_refs(schema) assert actual == expected
def test_dereference_refs_one_ref() ->None: schema = {'type': 'object', 'properties': {'first_name': {'$ref': '#/$defs/name'}}, '$defs': {'name': {'type': 'string'}}} expected = {'type': 'object', 'properties': {'first_name': {'type': 'string'}}, '$defs': {'name': {'type': 'string'}}} actual...
null
dict
"""Return dictionary representation of output parser.""" output_parser_dict = super().dict(**kwargs) try: output_parser_dict['_type'] = self._type except NotImplementedError: pass return output_parser_dict
def dict(self, **kwargs: Any) ->Dict: """Return dictionary representation of output parser.""" output_parser_dict = super().dict(**kwargs) try: output_parser_dict['_type'] = self._type except NotImplementedError: pass return output_parser_dict
Return dictionary representation of output parser.
_get_messages
"""Get Chat Messages from inputs. Args: inputs: The input dictionary. Returns: A list of chat messages. Raises: InputFormatError: If the input format is invalid. """ if not inputs: raise InputFormatError('Inputs should not be empty.') if 'messages' in inputs: single_inp...
def _get_messages(inputs: Dict[str, Any]) ->List[BaseMessage]: """Get Chat Messages from inputs. Args: inputs: The input dictionary. Returns: A list of chat messages. Raises: InputFormatError: If the input format is invalid. """ if not inputs: raise InputFormatE...
Get Chat Messages from inputs. Args: inputs: The input dictionary. Returns: A list of chat messages. Raises: InputFormatError: If the input format is invalid.
create
...
@overload @staticmethod def create(messages: Sequence[Dict[str, Any]], *, provider: str= 'ChatOpenAI', stream: Literal[True], **kwargs: Any) ->Iterable: ...
null
get_format_instructions
return FORMAT_INSTRUCTIONS
def get_format_instructions(self) ->str: return FORMAT_INSTRUCTIONS
null
test_generation_chunk
assert GenerationChunk(text='Hello, ') + GenerationChunk(text='world!' ) == GenerationChunk(text='Hello, world!' ), 'GenerationChunk + GenerationChunk should be a GenerationChunk' assert GenerationChunk(text='Hello, ') + GenerationChunk(text='world!', generation_info={'foo': 'bar'}) == GenerationChunk(text=...
def test_generation_chunk() ->None: assert GenerationChunk(text='Hello, ') + GenerationChunk(text='world!' ) == GenerationChunk(text='Hello, world!' ), 'GenerationChunk + GenerationChunk should be a GenerationChunk' assert GenerationChunk(text='Hello, ') + GenerationChunk(text='world!', ...
null
batch
from langchain_core.beta.runnables.context import config_with_context from langchain_core.callbacks.manager import CallbackManager if not inputs: return [] configs = [config_with_context(c, self.steps) for c in get_config_list( config, len(inputs))] callback_managers = [CallbackManager.configure(inheritable_cal...
def batch(self, inputs: List[Input], config: Optional[Union[RunnableConfig, List[RunnableConfig]]]=None, *, return_exceptions: bool=False, **kwargs: Optional[Any]) ->List[Output]: from langchain_core.beta.runnables.context import config_with_context from langchain_core.callbacks.manager import CallbackM...
null
raise_deprecation
try: import numexpr except ImportError: raise ImportError( 'LLMMathChain requires the numexpr package. Please install it with `pip install numexpr`.' ) if 'llm' in values: warnings.warn( 'Directly instantiating an LLMMathChain with an llm is deprecated. Please instantiate with llm_ch...
@root_validator(pre=True) def raise_deprecation(cls, values: Dict) ->Dict: try: import numexpr except ImportError: raise ImportError( 'LLMMathChain requires the numexpr package. Please install it with `pip install numexpr`.' ) if 'llm' in values: warnings.warn...
null
_type
return 'json-agent'
@property def _type(self) ->str: return 'json-agent'
null
_is_gemini_model
return is_gemini_model(self.model_name)
@property def _is_gemini_model(self) ->bool: return is_gemini_model(self.model_name)
null
__init__
"""Initialize with dataframe object. Args: data_frame: DataFrame object. page_content_column: Name of the column containing the page content. Defaults to "text". """ self.data_frame = data_frame self.page_content_column = page_content_column
def __init__(self, data_frame: Any, *, page_content_column: str='text'): """Initialize with dataframe object. Args: data_frame: DataFrame object. page_content_column: Name of the column containing the page content. Defaults to "text". """ self.data_frame = ...
Initialize with dataframe object. Args: data_frame: DataFrame object. page_content_column: Name of the column containing the page content. Defaults to "text".
check_queries_required
if values.get('sequential_response') and not queries: raise ValueError( 'queries is required when sequential_response is set to True') return queries
@validator('queries', always=True) def check_queries_required(cls, queries: Optional[Mapping], values: Mapping [str, Any]) ->Optional[Mapping]: if values.get('sequential_response') and not queries: raise ValueError( 'queries is required when sequential_response is set to True') return qu...
null
deprecated_function
"""original doc""" return 'This is a deprecated function.'
@deprecated(since='2.0.0', removal='3.0.0', pending=False) def deprecated_function() ->str: """original doc""" return 'This is a deprecated function.'
original doc
save_context
"""Nothing should be saved or changed, my memory is set in stone.""" pass
def save_context(self, inputs: Dict[str, Any], outputs: Dict[str, str]) ->None: """Nothing should be saved or changed, my memory is set in stone.""" pass
Nothing should be saved or changed, my memory is set in stone.