method_name
stringlengths
1
78
method_body
stringlengths
3
9.66k
full_code
stringlengths
31
10.7k
docstring
stringlengths
4
4.74k
_unbatch
if len(batch_of_texts) == 1 and len(batch_of_texts[0]) == 1: return batch_of_texts[0] texts = [] for sublist in batch_of_texts: texts.extend(sublist) return texts
@staticmethod def _unbatch(batch_of_texts: List[List[Any]]) ->List[Any]: if len(batch_of_texts) == 1 and len(batch_of_texts[0]) == 1: return batch_of_texts[0] texts = [] for sublist in batch_of_texts: texts.extend(sublist) return texts
null
_convert_dict_to_message
role = _dict['role'] content = _dict['content'] if role == 'user': return HumanMessage(content=content) elif role == 'assistant': return AIMessage(content=content) elif role == 'system': return SystemMessage(content=content) else: return ChatMessage(content=content, role=role)
@staticmethod def _convert_dict_to_message(_dict: Mapping[str, Any]) ->BaseMessage: role = _dict['role'] content = _dict['content'] if role == 'user': return HumanMessage(content=content) elif role == 'assistant': return AIMessage(content=content) elif role == 'system': retur...
null
process_response
""" Transform the Diffbot NLP response into a GraphDocument. Args: payload (Dict[str, Any]): The JSON response from Diffbot's NLP API. document (Document): The original document. Returns: GraphDocument: The transformed document as a graph. """ if 'fa...
def process_response(self, payload: Dict[str, Any], document: Document ) ->GraphDocument: """ Transform the Diffbot NLP response into a GraphDocument. Args: payload (Dict[str, Any]): The JSON response from Diffbot's NLP API. document (Document): The original document. ...
Transform the Diffbot NLP response into a GraphDocument. Args: payload (Dict[str, Any]): The JSON response from Diffbot's NLP API. document (Document): The original document. Returns: GraphDocument: The transformed document as a graph.
_llm_type
"""Return type of llm.""" return 'manifest'
@property def _llm_type(self) ->str: """Return type of llm.""" return 'manifest'
Return type of llm.
test_runnable_sequence_transform
llm = FakeStreamingListLLM(responses=['foo-lish']) chain: Runnable = llm | StrOutputParser() stream = chain.transform(llm.stream('Hi there!')) chunks = [] for chunk in stream: chunks.append(chunk) assert len(chunks) == len('foo-lish') assert ''.join(chunks) == 'foo-lish'
def test_runnable_sequence_transform() ->None: llm = FakeStreamingListLLM(responses=['foo-lish']) chain: Runnable = llm | StrOutputParser() stream = chain.transform(llm.stream('Hi there!')) chunks = [] for chunk in stream: chunks.append(chunk) assert len(chunks) == len('foo-lish') as...
null
_load_json_block
try: response_content = json.loads(serialized_block, strict=False) return response_content.get('response', 'ERROR parsing response.') except json.JSONDecodeError: return 'ERROR parsing response.' except: raise
def _load_json_block(self, serialized_block: str) ->str: try: response_content = json.loads(serialized_block, strict=False) return response_content.get('response', 'ERROR parsing response.') except json.JSONDecodeError: return 'ERROR parsing response.' except: raise
null
_get_elements
from unstructured.partition.auto import partition local_prefix = 'local://' if self.presign: with tempfile.TemporaryDirectory() as temp_dir: file_path = f"{temp_dir}/{self.path.split('/')[-1]}" os.makedirs(os.path.dirname(file_path), exist_ok=True) response = requests.get(self.url) r...
def _get_elements(self) ->List: from unstructured.partition.auto import partition local_prefix = 'local://' if self.presign: with tempfile.TemporaryDirectory() as temp_dir: file_path = f"{temp_dir}/{self.path.split('/')[-1]}" os.makedirs(os.path.dirname(file_path), exist_ok=T...
null
test_user_defined_scorer
llm, PROMPT = setup() class CustomSelectionScorer(rl_chain.SelectionScorer): def score_response(self, inputs: Dict[str, Any], llm_response: str, event: pick_best_chain.PickBestEvent) ->float: score = 200 return score chain = pick_best_chain.PickBest.from_llm(llm=llm, prompt=PROMPT, sele...
@pytest.mark.requires('vowpal_wabbit_next', 'sentence_transformers') def test_user_defined_scorer() ->None: llm, PROMPT = setup() class CustomSelectionScorer(rl_chain.SelectionScorer): def score_response(self, inputs: Dict[str, Any], llm_response: str, event: pick_best_chain.PickBestEvent...
null
convert_message_to_dict
"""Convert a message to a dictionary that can be passed to the API.""" message_dict: Dict[str, Any] if isinstance(message, ChatMessage): message_dict = {'role': message.role, 'content': message.content} elif isinstance(message, HumanMessage): message_dict = {'role': 'user', 'content': message.content} elif isin...
def convert_message_to_dict(message: BaseMessage) ->dict: """Convert a message to a dictionary that can be passed to the API.""" message_dict: Dict[str, Any] if isinstance(message, ChatMessage): message_dict = {'role': message.role, 'content': message.content} elif isinstance(message, HumanMessa...
Convert a message to a dictionary that can be passed to the API.
_chain_type
return 'vector_db_qa_with_sources_chain'
@property def _chain_type(self) ->str: return 'vector_db_qa_with_sources_chain'
null
test_all_imports
assert set(__all__) == set(EXPECTED_ALL)
def test_all_imports() ->None: assert set(__all__) == set(EXPECTED_ALL)
null
compress_documents
"""Compress retrieved documents given the query context."""
@abstractmethod def compress_documents(self, documents: Sequence[Document], query: str, callbacks: Optional[Callbacks]=None) ->Sequence[Document]: """Compress retrieved documents given the query context."""
Compress retrieved documents given the query context.
test_neo4jvector_with_metadatas_with_scores
"""Test end to end construction and search.""" metadatas = [{'page': str(i)} for i in range(len(texts))] docsearch = Neo4jVector.from_texts(texts=texts, embedding= FakeEmbeddingsWithOsDimension(), metadatas=metadatas, url=url, username =username, password=password, pre_delete_collection=True) output = docsearch...
def test_neo4jvector_with_metadatas_with_scores() ->None: """Test end to end construction and search.""" metadatas = [{'page': str(i)} for i in range(len(texts))] docsearch = Neo4jVector.from_texts(texts=texts, embedding= FakeEmbeddingsWithOsDimension(), metadatas=metadatas, url=url, usernam...
Test end to end construction and search.
_print_keys
if isinstance(keys, str): return f"'{keys}'" else: return ', '.join(f"'{k}'" for k in keys)
def _print_keys(keys: Union[str, Sequence[str]]) ->str: if isinstance(keys, str): return f"'{keys}'" else: return ', '.join(f"'{k}'" for k in keys)
null
test_chat_bedrock_streaming_generation_info
"""Test that generation info is preserved when streaming.""" class _FakeCallback(FakeCallbackHandler): saved_things: dict = {} def on_llm_end(self, *args: Any, **kwargs: Any) ->Any: self.saved_things['generation'] = args[0] callback = _FakeCallback() callback_manager = CallbackManager([callback]) chat ...
@pytest.mark.scheduled def test_chat_bedrock_streaming_generation_info() ->None: """Test that generation info is preserved when streaming.""" class _FakeCallback(FakeCallbackHandler): saved_things: dict = {} def on_llm_end(self, *args: Any, **kwargs: Any) ->Any: self.saved_things[...
Test that generation info is preserved when streaming.
memory_variables
"""Will always return list of memory variables. :meta private: """ return [self.memory_key]
@property def memory_variables(self) ->List[str]: """Will always return list of memory variables. :meta private: """ return [self.memory_key]
Will always return list of memory variables. :meta private:
load_agent_executor
""" Load an agent executor. Args: llm: BaseLanguageModel tools: List[BaseTool] verbose: bool. Defaults to False. include_task_in_prompt: bool. Defaults to False. Returns: ChainExecutor """ input_variables = ['previous_steps', 'current_step', 'agent_scratchpad'] ...
def load_agent_executor(llm: BaseLanguageModel, tools: List[BaseTool], verbose: bool=False, include_task_in_prompt: bool=False) ->ChainExecutor: """ Load an agent executor. Args: llm: BaseLanguageModel tools: List[BaseTool] verbose: bool. Defaults to False. include_task_...
Load an agent executor. Args: llm: BaseLanguageModel tools: List[BaseTool] verbose: bool. Defaults to False. include_task_in_prompt: bool. Defaults to False. Returns: ChainExecutor
test_json_distance_evaluator_evaluate_strings_complex_diff
prediction = '{"a":1, "b": {"c": 2, "d": 3}}' reference = '{"a": 1, "b": {"c": 2, "d": 4}}' result = json_distance_evaluator._evaluate_strings(prediction=prediction, reference=reference) pytest.approx(1 / len(reference.replace(' ', '')), result['score'])
@pytest.mark.requires('rapidfuzz') def test_json_distance_evaluator_evaluate_strings_complex_diff( json_distance_evaluator: JsonEditDistanceEvaluator) ->None: prediction = '{"a":1, "b": {"c": 2, "d": 3}}' reference = '{"a": 1, "b": {"c": 2, "d": 4}}' result = json_distance_evaluator._evaluate_strings(pr...
null
test_amazontextract_loader
if create_client: import boto3 textract_client = boto3.client('textract', region_name='us-east-2') loader = AmazonTextractPDFLoader(file_path, textract_features=features, client=textract_client) else: loader = AmazonTextractPDFLoader(file_path, textract_features=features) docs = loader.load() pr...
@pytest.mark.parametrize('file_path, features, docs_length, create_client', [( 'https://amazon-textract-public-content.s3.us-east-2.amazonaws.com/langchain/alejandro_rosalez_sample_1.jpg' , ['FORMS', 'TABLES', 'LAYOUT'], 1, False), ( 'https://amazon-textract-public-content.s3.us-east-2.amazonaws.com/lan...
null
_get_client
try: from langchainhub import Client except ImportError as e: raise ImportError( 'Could not import langchainhub, please install with `pip install langchainhub`.' ) from e return Client(api_url, api_key=api_key)
def _get_client(api_url: Optional[str]=None, api_key: Optional[str]=None ) ->Client: try: from langchainhub import Client except ImportError as e: raise ImportError( 'Could not import langchainhub, please install with `pip install langchainhub`.' ) from e return C...
null
_persist_run
"""Persist a run.""" self.runs.append(run)
def _persist_run(self, run: Run) ->None: """Persist a run.""" self.runs.append(run)
Persist a run.
messages
return [SystemMessage(content='You are a test user.'), HumanMessage(content ='Hello, I am a test user.')]
@pytest.fixture def messages() ->list: return [SystemMessage(content='You are a test user.'), HumanMessage( content='Hello, I am a test user.')]
null
test_default_chat_anthropic
base_model = AnthropicFunctions(model='claude-2') self.assertIsInstance(base_model.model, ChatAnthropic) model = base_model.bind(functions=[{'name': 'get_current_weather', 'description': 'Get the current weather in a given location', 'parameters': {'type': 'object', 'properties': {'location': {'type': 'stri...
def test_default_chat_anthropic(self) ->None: base_model = AnthropicFunctions(model='claude-2') self.assertIsInstance(base_model.model, ChatAnthropic) model = base_model.bind(functions=[{'name': 'get_current_weather', 'description': 'Get the current weather in a given location', 'parameters'...
null
update
""" Update the graph. """ from rdflib.exceptions import ParserError try: self.graph.update(query) except ParserError as e: raise ValueError(f'Generated SPARQL statement is invalid\n{e}') if self.local_copy: self.graph.serialize(destination=self.local_copy, format=self. local_copy.spl...
def update(self, query: str) ->None: """ Update the graph. """ from rdflib.exceptions import ParserError try: self.graph.update(query) except ParserError as e: raise ValueError(f'Generated SPARQL statement is invalid\n{e}') if self.local_copy: self.graph.seria...
Update the graph.
get_available_models
"""Get available models from Anyscale API.""" try: anyscale_api_key = anyscale_api_key or os.environ['ANYSCALE_API_KEY'] except KeyError as e: raise ValueError( 'Anyscale API key must be passed as keyword argument or set in environment variable ANYSCALE_API_KEY.' ) from e models_url = f'{anyscal...
@staticmethod def get_available_models(anyscale_api_key: Optional[str]=None, anyscale_api_base: str=DEFAULT_API_BASE) ->Set[str]: """Get available models from Anyscale API.""" try: anyscale_api_key = anyscale_api_key or os.environ['ANYSCALE_API_KEY'] except KeyError as e: raise ValueErro...
Get available models from Anyscale API.
query
return {'query': {'bool': {'must': [{'text_expansion': { f'{vector_query_field}.tokens': {'model_id': self.model_id, 'model_text': query}}}], 'filter': filter}}}
def query(self, query_vector: Union[List[float], None], query: Union[str, None], k: int, fetch_k: int, vector_query_field: str, text_field: str, filter: List[dict], similarity: Union[DistanceStrategy, None]) ->Dict: return {'query': {'bool': {'must': [{'text_expansion': { f'{vector_query_field}.toke...
null
test_remove_style
html2text_transformer = Html2TextTransformer() with_style_html = ( '<html><style>my_funky_style</style><p>First paragraph.</p></html>') documents = [Document(page_content=with_style_html)] docs_transformed = html2text_transformer.transform_documents(documents) assert docs_transformed[0].page_content == 'First parag...
@pytest.mark.requires('html2text') def test_remove_style() ->None: html2text_transformer = Html2TextTransformer() with_style_html = ( '<html><style>my_funky_style</style><p>First paragraph.</p></html>') documents = [Document(page_content=with_style_html)] docs_transformed = html2text_transformer...
null
from_texts
""" Class method that returns a ZepVectorStore instance initialized from texts. If the collection does not exist, it will be created. Args: texts (List[str]): The list of texts to add to the vectorstore. embedding (Optional[Embeddings]): Optional embedding function to u...
@classmethod def from_texts(cls, texts: List[str], embedding: Optional[Embeddings]=None, metadatas: Optional[List[dict]]=None, collection_name: str='', api_url: str='', api_key: Optional[str]=None, config: Optional[CollectionConfig] =None, **kwargs: Any) ->ZepVectorStore: """ Class method that r...
Class method that returns a ZepVectorStore instance initialized from texts. If the collection does not exist, it will be created. Args: texts (List[str]): The list of texts to add to the vectorstore. embedding (Optional[Embeddings]): Optional embedding function to use to embed the texts. metadatas ...
_get_ddg_search
return DuckDuckGoSearchRun(api_wrapper=DuckDuckGoSearchAPIWrapper(**kwargs))
def _get_ddg_search(**kwargs: Any) ->BaseTool: return DuckDuckGoSearchRun(api_wrapper=DuckDuckGoSearchAPIWrapper(**kwargs) )
null
load
"""Load sitemap.""" if self.is_local: try: import bs4 except ImportError: raise ImportError( 'beautifulsoup4 package not found, please install it with `pip install beautifulsoup4`' ) fp = open(self.web_path) soup = bs4.BeautifulSoup(fp, 'xml') else: soup = sel...
def load(self) ->List[Document]: """Load sitemap.""" if self.is_local: try: import bs4 except ImportError: raise ImportError( 'beautifulsoup4 package not found, please install it with `pip install beautifulsoup4`' ) fp = open(self.w...
Load sitemap.
__init__
self.message = message super().__init__(self.message)
def __init__(self, message: str= 'The prompt contains PII entities and cannot be processed'): self.message = message super().__init__(self.message)
null
combine_docs
"""Combine documents in a map reduce manner. Combine by mapping first chain over all documents, then reducing the results. This reducing can be done recursively if needed (if there are many documents). """ map_results = self.llm_chain.apply([{self.document_variable_name: d. page_content, **...
def combine_docs(self, docs: List[Document], token_max: Optional[int]=None, callbacks: Callbacks=None, **kwargs: Any) ->Tuple[str, dict]: """Combine documents in a map reduce manner. Combine by mapping first chain over all documents, then reducing the results. This reducing can be done recursiv...
Combine documents in a map reduce manner. Combine by mapping first chain over all documents, then reducing the results. This reducing can be done recursively if needed (if there are many documents).
_import_sleep_tool
from langchain_community.tools.sleep.tool import SleepTool return SleepTool
def _import_sleep_tool() ->Any: from langchain_community.tools.sleep.tool import SleepTool return SleepTool
null
_copy
"""Copy a run.""" try: return run.copy(deep=True) except TypeError: return run.copy()
def _copy(run: Run) ->Run: """Copy a run.""" try: return run.copy(deep=True) except TypeError: return run.copy()
Copy a run.
add_texts
""" Add texts through the embeddings and add to the vectorstore. Args: texts: list of text strings to add to the jaguar vector store. metadatas: Optional list of metadatas associated with the texts. [{"m1": "v11", "m2": "v12", "m3": "v13", "filecol": "path_file1.jpg" }, ...
def add_texts(self, texts: List[str], metadatas: Optional[List[dict]]=None, **kwargs: Any) ->List[str]: """ Add texts through the embeddings and add to the vectorstore. Args: texts: list of text strings to add to the jaguar vector store. metadatas: Optional list of metadatas...
Add texts through the embeddings and add to the vectorstore. Args: texts: list of text strings to add to the jaguar vector store. metadatas: Optional list of metadatas associated with the texts. [{"m1": "v11", "m2": "v12", "m3": "v13", "filecol": "path_file1.jpg" }, {"m1": "v21", "m2": "v22", "m3": "v23",...
__init__
try: import sqlite3 except ImportError: raise ImportError( 'Could not import sqlite3 python package. Please install it with `pip install sqlite3`.' ) super().__init__(*args, **kwargs) self.conn = sqlite3.connect(db_file) self.session_id = session_id self.table_name = table_name self._create_tabl...
def __init__(self, session_id: str='default', db_file: str='entities.db', table_name: str='memory_store', *args: Any, **kwargs: Any): try: import sqlite3 except ImportError: raise ImportError( 'Could not import sqlite3 python package. Please install it with `pip install sqlite3`....
null
on_chain_start
"""Run when chain starts running.""" self.step += 1 self.chain_starts += 1 self.starts += 1 resp = self._init_resp() resp.update({'action': 'on_chain_start'}) resp.update(flatten_dict(serialized)) resp.update(self.get_custom_callback_meta()) for chain_input_key, chain_input_val in inputs.items(): if isinstance(chai...
def on_chain_start(self, serialized: Dict[str, Any], inputs: Dict[str, Any], **kwargs: Any) ->None: """Run when chain starts running.""" self.step += 1 self.chain_starts += 1 self.starts += 1 resp = self._init_resp() resp.update({'action': 'on_chain_start'}) resp.update(flatten_dict(seri...
Run when chain starts running.
test_octoai_embedding_query
"""Test octoai embeddings.""" document = 'foo bar' embedding = OctoAIEmbeddings(endpoint_url='<endpoint_url>', octoai_api_token='<octoai_api_token>', embed_instruction= 'Represent this input: ', query_instruction='Represent this input: ', model_kwargs=None) output = embedding.embed_query(document) assert le...
def test_octoai_embedding_query() ->None: """Test octoai embeddings.""" document = 'foo bar' embedding = OctoAIEmbeddings(endpoint_url='<endpoint_url>', octoai_api_token='<octoai_api_token>', embed_instruction= 'Represent this input: ', query_instruction= 'Represent this input: ', mo...
Test octoai embeddings.
test_meilisearch
"""Test end to end construction and search.""" texts = ['foo', 'bar', 'baz'] vectorstore = Meilisearch.from_texts(texts=texts, embedding=FakeEmbeddings( ), url=TEST_MEILI_HTTP_ADDR, api_key=TEST_MEILI_MASTER_KEY, index_name= INDEX_NAME) self._wait_last_task() output = vectorstore.similarity_search('foo', k=1) a...
def test_meilisearch(self) ->None: """Test end to end construction and search.""" texts = ['foo', 'bar', 'baz'] vectorstore = Meilisearch.from_texts(texts=texts, embedding= FakeEmbeddings(), url=TEST_MEILI_HTTP_ADDR, api_key= TEST_MEILI_MASTER_KEY, index_name=INDEX_NAME) self._wait_last_...
Test end to end construction and search.
_url_to_pil
if PIL is None: raise ImportError( 'PIL is required to load images. Please install it with `pip install pillow`' ) try: if isinstance(image_source, IMAGE_TYPES): return image_source elif _is_url(image_source): if image_source.startswith('gs://'): return _load_imag...
def _url_to_pil(image_source: str) ->Image: if PIL is None: raise ImportError( 'PIL is required to load images. Please install it with `pip install pillow`' ) try: if isinstance(image_source, IMAGE_TYPES): return image_source elif _is_url(image_source)...
null
_parse_list
"""Parse a newline-separated string into a list of strings.""" lines = re.split('\\n', text.strip()) lines = [line for line in lines if line.strip()] return [re.sub('^\\s*\\d+\\.\\s*', '', line).strip() for line in lines]
@staticmethod def _parse_list(text: str) ->List[str]: """Parse a newline-separated string into a list of strings.""" lines = re.split('\\n', text.strip()) lines = [line for line in lines if line.strip()] return [re.sub('^\\s*\\d+\\.\\s*', '', line).strip() for line in lines]
Parse a newline-separated string into a list of strings.
_select_relevance_score_fn
""" The 'correct' relevance function may differ depending on a few things, including: - the distance / similarity metric used by the VectorStore - the scale of your embeddings (OpenAI's are unit normed. Many others are not!) - embedding dimensionality - etc. """ i...
def _select_relevance_score_fn(self) ->Callable[[float], float]: """ The 'correct' relevance function may differ depending on a few things, including: - the distance / similarity metric used by the VectorStore - the scale of your embeddings (OpenAI's are unit normed. Many others are ...
The 'correct' relevance function may differ depending on a few things, including: - the distance / similarity metric used by the VectorStore - the scale of your embeddings (OpenAI's are unit normed. Many others are not!) - embedding dimensionality - etc.
test_summary_buffer_memory_summary
"""Test ConversationSummaryBufferMemory when only buffer.""" memory = ConversationSummaryBufferMemory(llm=FakeLLM(), memory_key='baz', max_token_limit=13) memory.save_context({'input': 'bar'}, {'output': 'foo'}) memory.save_context({'input': 'bar1'}, {'output': 'foo1'}) assert memory.buffer == ["""Human: bar1 AI: f...
def test_summary_buffer_memory_summary() ->None: """Test ConversationSummaryBufferMemory when only buffer.""" memory = ConversationSummaryBufferMemory(llm=FakeLLM(), memory_key= 'baz', max_token_limit=13) memory.save_context({'input': 'bar'}, {'output': 'foo'}) memory.save_context({'input': 'bar...
Test ConversationSummaryBufferMemory when only buffer.
run_async
"""Run an async coroutine. Args: coro: The coroutine to run. Coroutine[Any, Any, T] Returns: T: The result of the coroutine. """ event_loop = asyncio.get_event_loop() return event_loop.run_until_complete(coro)
def run_async(coro: Coroutine[Any, Any, T]) ->T: """Run an async coroutine. Args: coro: The coroutine to run. Coroutine[Any, Any, T] Returns: T: The result of the coroutine. """ event_loop = asyncio.get_event_loop() return event_loop.run_until_complete(coro)
Run an async coroutine. Args: coro: The coroutine to run. Coroutine[Any, Any, T] Returns: T: The result of the coroutine.
_construct_path
"""Construct the path from the deserialized input.""" path = self.api_operation.base_url + self.api_operation.path for param in self.param_mapping.path_params: path = path.replace(f'{{{param}}}', str(args.pop(param, ''))) return path
def _construct_path(self, args: Dict[str, str]) ->str: """Construct the path from the deserialized input.""" path = self.api_operation.base_url + self.api_operation.path for param in self.param_mapping.path_params: path = path.replace(f'{{{param}}}', str(args.pop(param, ''))) return path
Construct the path from the deserialized input.
_load_generations_from_json
"""Load generations from json. Args: generations_json (str): A string of json representing a list of generations. Raises: ValueError: Could not decode json string to list of generations. Returns: RETURN_VAL_TYPE: A list of generations. Warning: would not work well with arbitr...
def _load_generations_from_json(generations_json: str) ->RETURN_VAL_TYPE: """Load generations from json. Args: generations_json (str): A string of json representing a list of generations. Raises: ValueError: Could not decode json string to list of generations. Returns: RETURN_...
Load generations from json. Args: generations_json (str): A string of json representing a list of generations. Raises: ValueError: Could not decode json string to list of generations. Returns: RETURN_VAL_TYPE: A list of generations. Warning: would not work well with arbitrary subclasses of `Generation`
message_history
from momento import CacheClient, Configurations, CredentialProvider cache_name = f'langchain-test-cache-{random_string()}' client = CacheClient(Configurations.Laptop.v1(), CredentialProvider. from_environment_variable('MOMENTO_API_KEY'), default_ttl=timedelta( seconds=30)) try: chat_message_history = Moment...
@pytest.fixture(scope='function') def message_history() ->Iterator[MomentoChatMessageHistory]: from momento import CacheClient, Configurations, CredentialProvider cache_name = f'langchain-test-cache-{random_string()}' client = CacheClient(Configurations.Laptop.v1(), CredentialProvider. from_environm...
null
get_result_from_sqldb
result = db._execute(cmd, fetch='all') return result
def get_result_from_sqldb(db: SQLDatabase, cmd: str) ->Sequence[Dict[str, Any] ]: result = db._execute(cmd, fetch='all') return result
null
test_message_chunks
assert AIMessageChunk(content='I am') + AIMessageChunk(content=' indeed.' ) == AIMessageChunk(content='I am indeed.' ), 'MessageChunk + MessageChunk should be a MessageChunk' assert AIMessageChunk(content='I am') + HumanMessageChunk(content=' indeed.' ) == AIMessageChunk(content='I am indeed.' ), 'Messa...
def test_message_chunks() ->None: assert AIMessageChunk(content='I am') + AIMessageChunk(content=' indeed.' ) == AIMessageChunk(content='I am indeed.' ), 'MessageChunk + MessageChunk should be a MessageChunk' assert AIMessageChunk(content='I am') + HumanMessageChunk(content= ' indeed.') ...
null
_load
"""Load the collection if available.""" from pymilvus import Collection if isinstance(self.col, Collection) and self._get_index() is not None: self.col.load()
def _load(self) ->None: """Load the collection if available.""" from pymilvus import Collection if isinstance(self.col, Collection) and self._get_index() is not None: self.col.load()
Load the collection if available.
__init__
merged = {**__steps} if __steps is not None else {} merged.update(kwargs) super().__init__(steps={key: coerce_to_runnable(r) for key, r in merged. items()})
def __init__(self, __steps: Optional[Mapping[str, Union[Runnable[Input, Any ], Callable[[Input], Any], Mapping[str, Union[Runnable[Input, Any], Callable[[Input], Any]]]]]]=None, **kwargs: Union[Runnable[Input, Any], Callable[[Input], Any], Mapping[str, Union[Runnable[Input, Any], Callable[[Input], Any]]...
null
test_chat_openai_extra_kwargs
"""Test extra kwargs to chat openai.""" llm = ChatOpenAI(foo=3, max_tokens=10) assert llm.max_tokens == 10 assert llm.model_kwargs == {'foo': 3} llm = ChatOpenAI(foo=3, model_kwargs={'bar': 2}) assert llm.model_kwargs == {'foo': 3, 'bar': 2} with pytest.raises(ValueError): ChatOpenAI(foo=3, model_kwargs={'foo': 2})...
def test_chat_openai_extra_kwargs() ->None: """Test extra kwargs to chat openai.""" llm = ChatOpenAI(foo=3, max_tokens=10) assert llm.max_tokens == 10 assert llm.model_kwargs == {'foo': 3} llm = ChatOpenAI(foo=3, model_kwargs={'bar': 2}) assert llm.model_kwargs == {'foo': 3, 'bar': 2} with p...
Test extra kwargs to chat openai.
test__get_hours_passed
time1 = datetime(2023, 4, 14, 14, 30) time2 = datetime(2023, 4, 14, 12, 0) expected_hours_passed = 2.5 hours_passed = _get_hours_passed(time1, time2) assert hours_passed == expected_hours_passed
def test__get_hours_passed() ->None: time1 = datetime(2023, 4, 14, 14, 30) time2 = datetime(2023, 4, 14, 12, 0) expected_hours_passed = 2.5 hours_passed = _get_hours_passed(time1, time2) assert hours_passed == expected_hours_passed
null
_llm_type
"""Return type of llm.""" return 'anthropic-llm'
@property def _llm_type(self) ->str: """Return type of llm.""" return 'anthropic-llm'
Return type of llm.
test_qdrant_add_texts_stores_duplicated_texts
"""Test end to end Qdrant.add_texts stores duplicated texts separately.""" from qdrant_client import QdrantClient from qdrant_client.http import models as rest client = QdrantClient(':memory:') collection_name = uuid.uuid4().hex vectors_config = rest.VectorParams(size=10, distance=rest.Distance.COSINE) if vector_name i...
@pytest.mark.parametrize('vector_name', [None, 'my-vector']) def test_qdrant_add_texts_stores_duplicated_texts(vector_name: Optional[str] ) ->None: """Test end to end Qdrant.add_texts stores duplicated texts separately.""" from qdrant_client import QdrantClient from qdrant_client.http import models as r...
Test end to end Qdrant.add_texts stores duplicated texts separately.
test_find_all_links_drop_fragment
html = 'href="foobar.com/woah#section_one"' actual = find_all_links(html) assert actual == ['foobar.com/woah']
def test_find_all_links_drop_fragment() ->None: html = 'href="foobar.com/woah#section_one"' actual = find_all_links(html) assert actual == ['foobar.com/woah']
null
messages
"""Messages in this chat history.""" return messages_from_dict(self._query( f""" SELECT * FROM UNNEST (( SELECT "{self.messages_key}" FROM {self.location} WHERE _id = :session_id )) ...
@property def messages(self) ->List[BaseMessage]: """Messages in this chat history.""" return messages_from_dict(self._query( f""" SELECT * FROM UNNEST (( SELECT "{self.messages_key}" FROM {self.location} ...
Messages in this chat history.
_import_gmail_GmailGetMessage
from langchain_community.tools.gmail import GmailGetMessage return GmailGetMessage
def _import_gmail_GmailGetMessage() ->Any: from langchain_community.tools.gmail import GmailGetMessage return GmailGetMessage
null
test_unstructured_loader_with_post_processor
def add_the_end(text: str) ->str: return text + 'THE END!' file_path = os.path.join(EXAMPLE_DOCS_DIRECTORY, 'layout-parser-paper.pdf') loader = UnstructuredFileLoader(file_path=file_path, post_processors=[ add_the_end], strategy='fast', mode='elements') docs = loader.load() assert len(docs) > 1 assert docs[0].p...
def test_unstructured_loader_with_post_processor() ->None: def add_the_end(text: str) ->str: return text + 'THE END!' file_path = os.path.join(EXAMPLE_DOCS_DIRECTORY, 'layout-parser-paper.pdf') loader = UnstructuredFileLoader(file_path=file_path, post_processors=[ add_the_end], strategy='fa...
null
fake_llm_sudoku
"""This is a fake LLM that responds to the sudoku problem.""" queries = {i: next_step.strip() for i, next_step in enumerate(solutions)} return FakeLLM(queries=queries, sequential_responses=True)
@pytest.fixture def fake_llm_sudoku() ->FakeLLM: """This is a fake LLM that responds to the sudoku problem.""" queries = {i: next_step.strip() for i, next_step in enumerate(solutions)} return FakeLLM(queries=queries, sequential_responses=True)
This is a fake LLM that responds to the sudoku problem.
_llm_type
"""Return type of llm.""" return 'stochasticai'
@property def _llm_type(self) ->str: """Return type of llm.""" return 'stochasticai'
Return type of llm.
get_history
input.pop('question') window = 3 data = graph.query( """ MATCH (u:User {id:$user_id})-[:HAS_SESSION]->(s:Session {id:$session_id}), (s)-[:LAST_MESSAGE]->(last_message) MATCH p=(last_message)<-[:NEXT*0..""" + str(window) + """]-() WITH p, length(p) AS length ORDER BY l...
def get_history(input: Dict[str, Any]) ->ChatMessageHistory: input.pop('question') window = 3 data = graph.query( """ MATCH (u:User {id:$user_id})-[:HAS_SESSION]->(s:Session {id:$session_id}), (s)-[:LAST_MESSAGE]->(last_message) MATCH p=(last_message)<-[:NEXT*0..""" ...
null
run
"""Run query through GoogleSearch and parse result.""" results = self._google_serper_api_results(query, gl=self.gl, hl=self.hl, num=self.k, tbs=self.tbs, search_type=self.type, **kwargs) return self._parse_results(results)
def run(self, query: str, **kwargs: Any) ->str: """Run query through GoogleSearch and parse result.""" results = self._google_serper_api_results(query, gl=self.gl, hl=self.hl, num=self.k, tbs=self.tbs, search_type=self.type, **kwargs) return self._parse_results(results)
Run query through GoogleSearch and parse result.
fn
return attrdict({'resources': {'123': attrdict({'fields': {'456': attrdict( {'paragraphs': {'123/t/text/0-14': attrdict({'text': 'This is a test', 'order': 0})}})}, 'data': {'texts': {'text': {'body': 'This is a test'} }}, 'extra': attrdict({'metadata': {'some': 'metadata'}})})}})
def fn(self: Any, **kwargs: Any) ->Any: return attrdict({'resources': {'123': attrdict({'fields': {'456': attrdict({'paragraphs': {'123/t/text/0-14': attrdict({'text': 'This is a test', 'order': 0})}})}, 'data': {'texts': {'text': { 'body': 'This is a test'}}}, 'extra': attrdict({'metadata':...
null
add_texts
"""Add more texts to the vectorstore. Args: texts (Iterable[str]): Iterable of strings/text to add to the vectorstore. metadatas (Optional[List[dict]], optional): Optional list of metadatas. Defaults to None. embeddings (Optional[List[List[float]]], optional)...
def add_texts(self, texts: Iterable[str], metadatas: Optional[List[dict]]= None, embeddings: Optional[List[List[float]]]=None, **kwargs: Any) ->List[ str]: """Add more texts to the vectorstore. Args: texts (Iterable[str]): Iterable of strings/text to add to the vectorstore. ...
Add more texts to the vectorstore. Args: texts (Iterable[str]): Iterable of strings/text to add to the vectorstore. metadatas (Optional[List[dict]], optional): Optional list of metadatas. Defaults to None. embeddings (Optional[List[List[float]]], optional): Optional pre-generated embeddings...
load_memory_variables
"""Return history buffer.""" if self.return_messages: buffer: Any = [self.summary_message_cls(content=self.buffer)] else: buffer = self.buffer return {self.memory_key: buffer}
def load_memory_variables(self, inputs: Dict[str, Any]) ->Dict[str, Any]: """Return history buffer.""" if self.return_messages: buffer: Any = [self.summary_message_cls(content=self.buffer)] else: buffer = self.buffer return {self.memory_key: buffer}
Return history buffer.
test_zilliz_with_score
"""Test end to end construction and search with scores and IDs.""" texts = ['foo', 'bar', 'baz'] metadatas = [{'page': i} for i in range(len(texts))] docsearch = _zilliz_from_texts(metadatas=metadatas) output = docsearch.similarity_search_with_score('foo', k=3) docs = [o[0] for o in output] scores = [o[1] for o in outp...
def test_zilliz_with_score() ->None: """Test end to end construction and search with scores and IDs.""" texts = ['foo', 'bar', 'baz'] metadatas = [{'page': i} for i in range(len(texts))] docsearch = _zilliz_from_texts(metadatas=metadatas) output = docsearch.similarity_search_with_score('foo', k=3) ...
Test end to end construction and search with scores and IDs.
InputType
return self.first.InputType
@property def InputType(self) ->Type[Input]: return self.first.InputType
null
test_modelscope_embedding_query
"""Test modelscope embeddings for query.""" document = 'foo bar' embedding = ModelScopeEmbeddings() output = embedding.embed_query(document) assert len(output) == 512
def test_modelscope_embedding_query() ->None: """Test modelscope embeddings for query.""" document = 'foo bar' embedding = ModelScopeEmbeddings() output = embedding.embed_query(document) assert len(output) == 512
Test modelscope embeddings for query.
on_chain_error
"""Handle an error for a chain run.""" chain_run = self._get_run(run_id) chain_run.error = self._get_stacktrace(error) chain_run.end_time = datetime.now(timezone.utc) chain_run.events.append({'name': 'error', 'time': chain_run.end_time}) if inputs is not None: chain_run.inputs = inputs if isinstance(inputs, dict) e...
def on_chain_error(self, error: BaseException, *, inputs: Optional[Dict[str, Any]]=None, run_id: UUID, **kwargs: Any) ->Run: """Handle an error for a chain run.""" chain_run = self._get_run(run_id) chain_run.error = self._get_stacktrace(error) chain_run.end_time = datetime.now(timezone.utc) chai...
Handle an error for a chain run.
embed_documents
""" Generates embeddings for a list of documents. Args: texts (List[str]): The documents to generate embeddings for. Returns: A list of embeddings, one for each document. """ return [self.nlp(text).vector.tolist() for text in texts]
def embed_documents(self, texts: List[str]) ->List[List[float]]: """ Generates embeddings for a list of documents. Args: texts (List[str]): The documents to generate embeddings for. Returns: A list of embeddings, one for each document. """ return [self.n...
Generates embeddings for a list of documents. Args: texts (List[str]): The documents to generate embeddings for. Returns: A list of embeddings, one for each document.
test_qdrant_similarity_search_by_vector
"""Test end to end construction and search.""" texts = ['foo', 'bar', 'baz'] docsearch = Qdrant.from_texts(texts, ConsistentFakeEmbeddings(), location= ':memory:', content_payload_key=content_payload_key, metadata_payload_key=metadata_payload_key, batch_size=batch_size, vector_name=vector_name) embeddings =...
@pytest.mark.parametrize('batch_size', [1, 64]) @pytest.mark.parametrize('content_payload_key', [Qdrant.CONTENT_KEY, 'foo']) @pytest.mark.parametrize('metadata_payload_key', [Qdrant.METADATA_KEY, 'bar']) @pytest.mark.parametrize('vector_name', [None, 'my-vector']) def test_qdrant_similarity_search_by_vector(batch_size:...
Test end to end construction and search.
similarity_search_with_score
"""Return docs most similar to query. Uses cosine similarity. Args: query: Text to look up documents similar to. k: Number of Documents to return. Defaults to 4. filter: A dictionary of metadata fields and values to filter by. Defaults to None. R...
def similarity_search_with_score(self, query: str, k: int=4, filter: Optional[dict]=None) ->List[Tuple[Document, float]]: """Return docs most similar to query. Uses cosine similarity. Args: query: Text to look up documents similar to. k: Number of Documents to return. Defaults t...
Return docs most similar to query. Uses cosine similarity. Args: query: Text to look up documents similar to. k: Number of Documents to return. Defaults to 4. filter: A dictionary of metadata fields and values to filter by. Defaults to None. Returns: List of Documents most similar to the q...
__init__
""" Initialize with a ready session and a keyspace name. Args: session (cassandra.cluster.Session): an open Cassandra session keyspace (str): the keyspace to use for storing the cache table_name (str): name of the Cassandra table to use as cache ttl_second...
def __init__(self, session: Optional[CassandraSession]=None, keyspace: Optional[str]=None, table_name: str=CASSANDRA_CACHE_DEFAULT_TABLE_NAME, ttl_seconds: Optional[int]=CASSANDRA_CACHE_DEFAULT_TTL_SECONDS, skip_provisioning: bool=False): """ Initialize with a ready session and a keyspace name. ...
Initialize with a ready session and a keyspace name. Args: session (cassandra.cluster.Session): an open Cassandra session keyspace (str): the keyspace to use for storing the cache table_name (str): name of the Cassandra table to use as cache ttl_seconds (optional int): time-to-live for cache entries ...
to_document
"""Convert the DocumentWithState to a Document.""" return Document(page_content=self.page_content, metadata=self.metadata)
def to_document(self) ->Document: """Convert the DocumentWithState to a Document.""" return Document(page_content=self.page_content, metadata=self.metadata)
Convert the DocumentWithState to a Document.
mock_feature_layer
feature_layer = MagicMock() feature_layer.query.return_value = [MagicMock(as_dict={'attributes': { 'field': 'value'}})] feature_layer.url = 'https://example.com/layer_url' feature_layer.properties = {'description': '<html><body>Some HTML content</body></html>', 'name': 'test', 'serviceItemId': 'testItemId'}...
@pytest.fixture def mock_feature_layer(): feature_layer = MagicMock() feature_layer.query.return_value = [MagicMock(as_dict={'attributes': { 'field': 'value'}})] feature_layer.url = 'https://example.com/layer_url' feature_layer.properties = {'description': '<html><body>Some HTML content<...
null
_headers
return {'Authorization': f'Bearer {self.api_key}'}
@property def _headers(self) ->dict: return {'Authorization': f'Bearer {self.api_key}'}
null
create_react_agent
"""Create an agent that uses ReAct prompting. Examples: .. code-block:: python from langchain import hub from langchain_community.llms import OpenAI from langchain.agents import AgentExecutor, create_react_agent prompt = hub.pull("hwchase17/react") ...
def create_react_agent(llm: BaseLanguageModel, tools: Sequence[BaseTool], prompt: BasePromptTemplate) ->Runnable: """Create an agent that uses ReAct prompting. Examples: .. code-block:: python from langchain import hub from langchain_community.llms import OpenAI ...
Create an agent that uses ReAct prompting. Examples: .. code-block:: python from langchain import hub from langchain_community.llms import OpenAI from langchain.agents import AgentExecutor, create_react_agent prompt = hub.pull("hwchase17/react") ...
test_nlpcloud_api_key
"""Test that nlpcloud api key is a secret key.""" assert isinstance(NLPCloud(nlpcloud_api_key='1').nlpcloud_api_key, SecretStr) monkeypatch.setenv('NLPCLOUD_API_KEY', 'secret-api-key') llm = NLPCloud() assert isinstance(llm.nlpcloud_api_key, SecretStr) assert cast(SecretStr, llm.nlpcloud_api_key).get_secret_value( ...
def test_nlpcloud_api_key(monkeypatch: MonkeyPatch, capsys: CaptureFixture ) ->None: """Test that nlpcloud api key is a secret key.""" assert isinstance(NLPCloud(nlpcloud_api_key='1').nlpcloud_api_key, SecretStr) monkeypatch.setenv('NLPCLOUD_API_KEY', 'secret-api-key') llm = NLPCloud() a...
Test that nlpcloud api key is a secret key.
on_chat_model_start
assert all(isinstance(m, BaseMessage) for m in chain(*messages)) self.on_chat_model_start_common()
def on_chat_model_start(self, serialized: Dict[str, Any], messages: List[ List[BaseMessage]], *, run_id: UUID, parent_run_id: Optional[UUID]=None, **kwargs: Any) ->Any: assert all(isinstance(m, BaseMessage) for m in chain(*messages)) self.on_chat_model_start_common()
null
embedding_openai
if not os.environ.get('OPENAI_API_KEY'): raise ValueError('OPENAI_API_KEY is not set') return OpenAIEmbeddings()
@pytest.fixture(scope='module') def embedding_openai() ->OpenAIEmbeddings: if not os.environ.get('OPENAI_API_KEY'): raise ValueError('OPENAI_API_KEY is not set') return OpenAIEmbeddings()
null
test__convert_dict_to_message_ai
message_dict = {'role': 'assistant', 'content': 'foo'} result = _convert_dict_to_message(message_dict) expected_output = AIMessage(content='foo') assert result == expected_output
def test__convert_dict_to_message_ai() ->None: message_dict = {'role': 'assistant', 'content': 'foo'} result = _convert_dict_to_message(message_dict) expected_output = AIMessage(content='foo') assert result == expected_output
null
add_documents
"""Add documents to vectorstore.""" current_time = kwargs.get('current_time') if current_time is None: current_time = datetime.datetime.now() dup_docs = [deepcopy(d) for d in documents] for i, doc in enumerate(dup_docs): if 'last_accessed_at' not in doc.metadata: doc.metadata['last_accessed_at'] = curre...
def add_documents(self, documents: List[Document], **kwargs: Any) ->List[str]: """Add documents to vectorstore.""" current_time = kwargs.get('current_time') if current_time is None: current_time = datetime.datetime.now() dup_docs = [deepcopy(d) for d in documents] for i, doc in enumerate(dup...
Add documents to vectorstore.
lc_attributes
attributes: Dict[str, Any] = {} if self.region_name: attributes['region_name'] = self.region_name return attributes
@property def lc_attributes(self) ->Dict[str, Any]: attributes: Dict[str, Any] = {} if self.region_name: attributes['region_name'] = self.region_name return attributes
null
_import_analyzer_engine
try: from presidio_analyzer import AnalyzerEngine except ImportError as e: raise ImportError( 'Could not import presidio_analyzer, please install with `pip install presidio-analyzer`. You will also need to download a spaCy model to use the analyzer, e.g. `python -m spacy download en_core_web_lg`.' ...
def _import_analyzer_engine() ->'AnalyzerEngine': try: from presidio_analyzer import AnalyzerEngine except ImportError as e: raise ImportError( 'Could not import presidio_analyzer, please install with `pip install presidio-analyzer`. You will also need to download a spaCy model to us...
null
on_retry_common
self.retries += 1
def on_retry_common(self) ->None: self.retries += 1
null
test_pairwise_embedding_distance_eval_chain_embedding_distance
"""Test the embedding distance.""" result = pairwise_embedding_distance_eval_chain.evaluate_string_pairs( prediction='A single cat', prediction_b='A single cat') assert np.isclose(result['score'], 0.0)
@pytest.mark.requires('openai', 'tiktoken') def test_pairwise_embedding_distance_eval_chain_embedding_distance( pairwise_embedding_distance_eval_chain: PairwiseEmbeddingDistanceEvalChain ) ->None: """Test the embedding distance.""" result = pairwise_embedding_distance_eval_chain.evaluate_string_pairs( ...
Test the embedding distance.
test_whole_class_deprecation
"""Test whole class deprecation.""" @deprecated(since='2.0.0', removal='3.0.0') class DeprecatedClass: def __init__(self) ->None: """original doc""" pass @deprecated(since='2.0.0', removal='3.0.0') def deprecated_method(self) ->str: """original doc""" return 'This is a depr...
def test_whole_class_deprecation() ->None: """Test whole class deprecation.""" @deprecated(since='2.0.0', removal='3.0.0') class DeprecatedClass: def __init__(self) ->None: """original doc""" pass @deprecated(since='2.0.0', removal='3.0.0') def deprecated_...
Test whole class deprecation.
test_md_header_text_splitter_preserve_headers_2
"""Test markdown splitter by header: Preserve Headers.""" markdown_document = """# Foo ## Bar Hi this is Jim Hi this is Joe ### Boo Hi this is Lance ## Baz Hi this is Molly ## Buz # Bop""" headers_to_split_on = [('#', 'Header 1'), ('##', 'Header 2'), ('###', 'Header 3')] markdown_splitter = Markdown...
def test_md_header_text_splitter_preserve_headers_2() ->None: """Test markdown splitter by header: Preserve Headers.""" markdown_document = """# Foo ## Bar Hi this is Jim Hi this is Joe ### Boo Hi this is Lance ## Baz Hi this is Molly ## Buz # Bop""" headers_to_split_on = [('#', 'Header 1'),...
Test markdown splitter by header: Preserve Headers.
run
"""Get the current weather information for a specified location.""" mgr = self.owm.weather_manager() observation = mgr.weather_at_place(location) w = observation.weather return self._format_weather_info(location, w)
def run(self, location: str) ->str: """Get the current weather information for a specified location.""" mgr = self.owm.weather_manager() observation = mgr.weather_at_place(location) w = observation.weather return self._format_weather_info(location, w)
Get the current weather information for a specified location.
get_resized_images
""" Resize images from base64-encoded strings. :param docs: A list of base64-encoded image to be resized. :return: Dict containing a list of resized base64-encoded strings. """ b64_images = [] for doc in docs: if isinstance(doc, Document): doc = doc.page_content b64_images.append(doc) r...
def get_resized_images(docs): """ Resize images from base64-encoded strings. :param docs: A list of base64-encoded image to be resized. :return: Dict containing a list of resized base64-encoded strings. """ b64_images = [] for doc in docs: if isinstance(doc, Document): d...
Resize images from base64-encoded strings. :param docs: A list of base64-encoded image to be resized. :return: Dict containing a list of resized base64-encoded strings.
visit_operation
"""Translate an Operation."""
@abstractmethod def visit_operation(self, operation: Operation) ->Any: """Translate an Operation."""
Translate an Operation.
__init__
"""Initialize with necessary components.""" try: import qdrant_client except ImportError: raise ImportError( 'Could not import qdrant-client python package. Please install it with `pip install qdrant-client`.' ) if not isinstance(client, qdrant_client.QdrantClient): raise ValueError( ...
def __init__(self, client: Any, collection_name: str, embeddings: Optional[ Embeddings]=None, content_payload_key: str=CONTENT_KEY, metadata_payload_key: str=METADATA_KEY, distance_strategy: str='COSINE', vector_name: Optional[str]=VECTOR_NAME, async_client: Optional[Any]= None, embedding_function: Opti...
Initialize with necessary components.
get_boolean_env_var
"""Retrieve the boolean value of an environment variable. Args: var_name (str): The name of the environment variable to retrieve. default_value (bool): The default value to return if the variable is not found. Returns: bool: The value of the environment variable, interpreted as a boolean. ...
def get_boolean_env_var(var_name, default_value=False): """Retrieve the boolean value of an environment variable. Args: var_name (str): The name of the environment variable to retrieve. default_value (bool): The default value to return if the variable is not found. Returns: bool: The value...
Retrieve the boolean value of an environment variable. Args: var_name (str): The name of the environment variable to retrieve. default_value (bool): The default value to return if the variable is not found. Returns: bool: The value of the environment variable, interpreted as a boolean.
_to_args_and_kwargs
"""Convert tool input to pydantic model.""" args, kwargs = super()._to_args_and_kwargs(tool_input) all_args = list(args) + list(kwargs.values()) if len(all_args) != 1: raise ToolException( f'Too many arguments to single-input tool {self.name}. Args: {all_args}' ) return tuple(all_args), {}
def _to_args_and_kwargs(self, tool_input: Union[str, Dict]) ->Tuple[Tuple, Dict ]: """Convert tool input to pydantic model.""" args, kwargs = super()._to_args_and_kwargs(tool_input) all_args = list(args) + list(kwargs.values()) if len(all_args) != 1: raise ToolException( f'Too ma...
Convert tool input to pydantic model.
_extract_code
start = node.lineno - 1 end = node.end_lineno return '\n'.join(self.source_lines[start:end])
def _extract_code(self, node: Any) ->str: start = node.lineno - 1 end = node.end_lineno return '\n'.join(self.source_lines[start:end])
null
delete
"""Deleting IDs from in memory dictionary.""" overlapping = set(ids).intersection(self._dict) if not overlapping: raise ValueError(f'Tried to delete ids that does not exist: {ids}') for _id in ids: self._dict.pop(_id)
def delete(self, ids: List) ->None: """Deleting IDs from in memory dictionary.""" overlapping = set(ids).intersection(self._dict) if not overlapping: raise ValueError(f'Tried to delete ids that does not exist: {ids}') for _id in ids: self._dict.pop(_id)
Deleting IDs from in memory dictionary.
__init__
zone = os.environ.get('NUCLIA_ZONE', 'europe-1') self._config['BACKEND'] = f'https://{zone}.nuclia.cloud/api/v1' key = os.environ.get('NUCLIA_NUA_KEY') if not key: raise ValueError('NUCLIA_NUA_KEY environment variable not set') else: self._config['NUA_KEY'] = key self._config['enable_ml'] = enable_ml super().__...
def __init__(self, enable_ml: bool=False) ->None: zone = os.environ.get('NUCLIA_ZONE', 'europe-1') self._config['BACKEND'] = f'https://{zone}.nuclia.cloud/api/v1' key = os.environ.get('NUCLIA_NUA_KEY') if not key: raise ValueError('NUCLIA_NUA_KEY environment variable not set') else: ...
null
test_load_returns_limited_docs
"""Test that returns several docs""" expected_docs = 2 api_client = ArxivAPIWrapper(load_max_docs=expected_docs) docs = api_client.load('ChatGPT') assert len(docs) == expected_docs assert_docs(docs)
def test_load_returns_limited_docs() ->None: """Test that returns several docs""" expected_docs = 2 api_client = ArxivAPIWrapper(load_max_docs=expected_docs) docs = api_client.load('ChatGPT') assert len(docs) == expected_docs assert_docs(docs)
Test that returns several docs
test_nvai_play_embedding_documents_multiple
"""Test NVIDIA embeddings for multiple documents.""" documents = ['foo bar', 'bar foo', 'foo'] embedding = NVIDIAEmbeddings(model='nvolveqa_40k') output = embedding.embed_documents(documents) assert len(output) == 3 assert all(len(doc) == 1024 for doc in output)
def test_nvai_play_embedding_documents_multiple() ->None: """Test NVIDIA embeddings for multiple documents.""" documents = ['foo bar', 'bar foo', 'foo'] embedding = NVIDIAEmbeddings(model='nvolveqa_40k') output = embedding.embed_documents(documents) assert len(output) == 3 assert all(len(doc) ==...
Test NVIDIA embeddings for multiple documents.