method_name
stringlengths
1
78
method_body
stringlengths
3
9.66k
full_code
stringlengths
31
10.7k
docstring
stringlengths
4
4.74k
get_model
"""Download model. f From https://huggingface.co/Sosaka/Alpaca-native-4bit-ggml/, convert to new ggml format and return model path.""" model_url = ( 'https://huggingface.co/Sosaka/Alpaca-native-4bit-ggml/resolve/main/ggml-alpaca-7b-q4.bin' ) tokenizer_url = ( 'https://huggingface.co/decapoda-researc...
def get_model() ->str: """Download model. f From https://huggingface.co/Sosaka/Alpaca-native-4bit-ggml/, convert to new ggml format and return model path.""" model_url = ( 'https://huggingface.co/Sosaka/Alpaca-native-4bit-ggml/resolve/main/ggml-alpaca-7b-q4.bin' ) tokenizer_url = ( ...
Download model. f From https://huggingface.co/Sosaka/Alpaca-native-4bit-ggml/, convert to new ggml format and return model path.
on_tool_end
"""Run when tool ends running.""" self.metrics['step'] += 1 self.metrics['tool_ends'] += 1 self.metrics['ends'] += 1 tool_ends = self.metrics['tool_ends'] resp: Dict[str, Any] = {} resp.update({'action': 'on_tool_end', 'output': output}) resp.update(self.metrics) self.jsonf(resp, self.temp_dir, f'tool_end_{tool_ends}')
def on_tool_end(self, output: str, **kwargs: Any) ->None: """Run when tool ends running.""" self.metrics['step'] += 1 self.metrics['tool_ends'] += 1 self.metrics['ends'] += 1 tool_ends = self.metrics['tool_ends'] resp: Dict[str, Any] = {} resp.update({'action': 'on_tool_end', 'output': outpu...
Run when tool ends running.
_get_tmdb_api
tmdb_bearer_token = kwargs['tmdb_bearer_token'] chain = APIChain.from_llm_and_api_docs(llm, tmdb_docs.TMDB_DOCS, headers={ 'Authorization': f'Bearer {tmdb_bearer_token}'}, limit_to_domains=[ 'https://api.themoviedb.org/']) return Tool(name='TMDB-API', description= 'Useful for when you want to get informatio...
def _get_tmdb_api(llm: BaseLanguageModel, **kwargs: Any) ->BaseTool: tmdb_bearer_token = kwargs['tmdb_bearer_token'] chain = APIChain.from_llm_and_api_docs(llm, tmdb_docs.TMDB_DOCS, headers={'Authorization': f'Bearer {tmdb_bearer_token}'}, limit_to_domains=['https://api.themoviedb.org/']) re...
null
similarity_search_with_score
"""Pass through to `knn_search including score`""" return self.knn_search(query=query, k=k, **kwargs)
def similarity_search_with_score(self, query: str, k: int=10, **kwargs: Any ) ->List[Tuple[Document, float]]: """Pass through to `knn_search including score`""" return self.knn_search(query=query, k=k, **kwargs)
Pass through to `knn_search including score`
test_chat_google_genai_invoke_multimodal
messages: list = [HumanMessage(content=[{'type': 'text', 'text': "Guess what's in this picture! You have 3 guesses."}, {'type': 'image_url', 'image_url': 'data:image/png;base64,' + _B64_string}])] llm = ChatGoogleGenerativeAI(model=_VISION_MODEL) response = llm.invoke(messages) assert isinstance(response.conten...
def test_chat_google_genai_invoke_multimodal() ->None: messages: list = [HumanMessage(content=[{'type': 'text', 'text': "Guess what's in this picture! You have 3 guesses."}, {'type': 'image_url', 'image_url': 'data:image/png;base64,' + _B64_string}])] llm = ChatGoogleGenerativeAI(model=_VISION_M...
null
similarity_search_with_score_by_vector
"""Return docs most similar to query. Args: embedding: Embedding vector to look up documents similar to. k: Number of Documents to return. Defaults to 4. filter (Optional[Dict[str, Any]]): Filter by metadata. Defaults to None. fetch_k: (Optional[int]) Number of D...
def similarity_search_with_score_by_vector(self, embedding: List[float], *, k: int=4, filter: Optional[Dict[str, Any]]=None, fetch_k: int=20, ** kwargs: Any) ->List[Tuple[Document, float]]: """Return docs most similar to query. Args: embedding: Embedding vector to look up documents simi...
Return docs most similar to query. Args: embedding: Embedding vector to look up documents similar to. k: Number of Documents to return. Defaults to 4. filter (Optional[Dict[str, Any]]): Filter by metadata. Defaults to None. fetch_k: (Optional[int]) Number of Documents to fetch before filtering. ...
_create_chat_result
generations = [] for choice in response['choices']: message = _convert_dict_to_message(choice['messages']) generations.append(ChatGeneration(message=message)) token_usage = response['usage'] llm_output = {'token_usage': token_usage} return ChatResult(generations=generations, llm_output=llm_output)
def _create_chat_result(response: Mapping[str, Any]) ->ChatResult: generations = [] for choice in response['choices']: message = _convert_dict_to_message(choice['messages']) generations.append(ChatGeneration(message=message)) token_usage = response['usage'] llm_output = {'token_usage': t...
null
_get_next_response_in_sequence
queries = cast(Mapping, self.queries) response = queries[list(queries.keys())[self.response_index]] self.response_index = self.response_index + 1 return response
@property def _get_next_response_in_sequence(self) ->str: queries = cast(Mapping, self.queries) response = queries[list(queries.keys())[self.response_index]] self.response_index = self.response_index + 1 return response
null
output_keys
"""Output keys. :meta private: """ _output_keys = [self.output_key] return _output_keys
@property def output_keys(self) ->List[str]: """Output keys. :meta private: """ _output_keys = [self.output_key] return _output_keys
Output keys. :meta private:
__init__
"""Initialize with necessary components.""" self.embedding = embedding self.index = index self.docstore = docstore self.ids = ids
def __init__(self, embedding: Embeddings, index: Any, docstore: Docstore, ids: List[str]): """Initialize with necessary components.""" self.embedding = embedding self.index = index self.docstore = docstore self.ids = ids
Initialize with necessary components.
_create_prediction
try: import replicate as replicate_python except ImportError: raise ImportError( 'Could not import replicate python package. Please install it with `pip install replicate`.' ) if self.version_obj is None: model_str, version_str = self.model.split(':') model = replicate_python.models.get(...
def _create_prediction(self, prompt: str, **kwargs: Any) ->Prediction: try: import replicate as replicate_python except ImportError: raise ImportError( 'Could not import replicate python package. Please install it with `pip install replicate`.' ) if self.version_obj i...
null
_transform
prev_parsed = None acc_gen = None for chunk in input: if isinstance(chunk, BaseMessageChunk): chunk_gen: Generation = ChatGenerationChunk(message=chunk) elif isinstance(chunk, BaseMessage): chunk_gen = ChatGenerationChunk(message=BaseMessageChunk(**chunk. dict())) else: c...
def _transform(self, input: Iterator[Union[str, BaseMessage]]) ->Iterator[Any]: prev_parsed = None acc_gen = None for chunk in input: if isinstance(chunk, BaseMessageChunk): chunk_gen: Generation = ChatGenerationChunk(message=chunk) elif isinstance(chunk, BaseMessage): ...
null
_run
"""Use the tool.""" if self.sync_browser is None: raise ValueError(f'Synchronous browser not provided to {self.name}') page = get_current_page(self.sync_browser) response = page.goto(url) status = response.status if response else 'unknown' return f'Navigating to {url} returned status code {status}'
def _run(self, url: str, run_manager: Optional[CallbackManagerForToolRun]=None ) ->str: """Use the tool.""" if self.sync_browser is None: raise ValueError(f'Synchronous browser not provided to {self.name}') page = get_current_page(self.sync_browser) response = page.goto(url) status = res...
Use the tool.
test_embeddings_clustering_filter
texts = ['What happened to all of my cookies?', 'A cookie is a small, baked sweet treat and you can find it in the cookie', "monsters' jar.", 'Cookies are good.', 'I have nightmares about the cookie monster.', 'The most popular pizza styles are: Neapolitan, New York-style and', 'Chicago-style. You c...
def test_embeddings_clustering_filter() ->None: texts = ['What happened to all of my cookies?', 'A cookie is a small, baked sweet treat and you can find it in the cookie' , "monsters' jar.", 'Cookies are good.', 'I have nightmares about the cookie monster.', 'The most popular pizza s...
null
_get_chat_params
if len(prompts) > 1: raise ValueError( f'OpenAIChat currently only supports single prompt, got {prompts}') messages = self.prefix_messages + [{'role': 'user', 'content': prompts[0]}] params: Dict[str, Any] = {**{'model': self.model_name}, **self._default_params} if stop is not None: if 'stop' in params:...
def _get_chat_params(self, prompts: List[str], stop: Optional[List[str]]=None ) ->Tuple: if len(prompts) > 1: raise ValueError( f'OpenAIChat currently only supports single prompt, got {prompts}') messages = self.prefix_messages + [{'role': 'user', 'content': prompts[0]}] params: Dict...
null
lazy_load
"""Lazy load records.""" from sodapy import Socrata client = Socrata(self.city_id, None) results = client.get(self.dataset_id, limit=self.limit) for record in results: yield Document(page_content=str(record), metadata={'source': self. city_id + '_' + self.dataset_id})
def lazy_load(self) ->Iterator[Document]: """Lazy load records.""" from sodapy import Socrata client = Socrata(self.city_id, None) results = client.get(self.dataset_id, limit=self.limit) for record in results: yield Document(page_content=str(record), metadata={'source': self. cit...
Lazy load records.
_import_stackexchange
from langchain_community.utilities.stackexchange import StackExchangeAPIWrapper return StackExchangeAPIWrapper
def _import_stackexchange() ->Any: from langchain_community.utilities.stackexchange import StackExchangeAPIWrapper return StackExchangeAPIWrapper
null
test_docai_parser_invalid_processor_name
with patch('google.cloud.documentai.DocumentProcessorServiceClient'): with pytest.raises(ValueError): _ = DocAIParser(processor_name=processor_name, location='us')
@pytest.mark.requires('google.cloud', 'google.cloud.documentai') @pytest.mark.parametrize('processor_name', [ 'projects/123456/locations/us-central1/processors/ab123dfg:publish', 'ab123dfg']) def test_docai_parser_invalid_processor_name(processor_name: str) ->None: with patch('google.cloud.documentai.Docume...
null
test_api_key_masked
"""Test that the API key is masked""" azure_chat = request.getfixturevalue(fixture_name) print(azure_chat.endpoint_api_key, end='') captured = capsys.readouterr() assert str(azure_chat.endpoint_api_key) == '**********' and repr(azure_chat .endpoint_api_key ) == "SecretStr('**********')" and captured.out == '***...
def test_api_key_masked(self, fixture_name: str, request: FixtureRequest, capsys: CaptureFixture) ->None: """Test that the API key is masked""" azure_chat = request.getfixturevalue(fixture_name) print(azure_chat.endpoint_api_key, end='') captured = capsys.readouterr() assert str(azure_chat.endpo...
Test that the API key is masked
test_sitemap_block_only_one
"""Test sitemap loader.""" loader = SitemapLoader('https://api.python.langchain.com/sitemap.xml', blocksize=1000000, blocknum=0) documents = loader.load() assert len(documents) > 1 assert 'LangChain Python API' in documents[0].page_content
def test_sitemap_block_only_one() ->None: """Test sitemap loader.""" loader = SitemapLoader('https://api.python.langchain.com/sitemap.xml', blocksize=1000000, blocknum=0) documents = loader.load() assert len(documents) > 1 assert 'LangChain Python API' in documents[0].page_content
Test sitemap loader.
test_flat_vector_field_defaults
"""Test defaults for FlatVectorField.""" flat_vector_field_data = {'name': 'example', 'dims': 100, 'algorithm': 'FLAT'} flat_vector = FlatVectorField(**flat_vector_field_data) assert flat_vector.datatype == 'FLOAT32' assert flat_vector.distance_metric == 'COSINE' assert flat_vector.initial_cap is None assert flat_vecto...
def test_flat_vector_field_defaults() ->None: """Test defaults for FlatVectorField.""" flat_vector_field_data = {'name': 'example', 'dims': 100, 'algorithm': 'FLAT'} flat_vector = FlatVectorField(**flat_vector_field_data) assert flat_vector.datatype == 'FLOAT32' assert flat_vector.distance_m...
Test defaults for FlatVectorField.
test_self_hosted_embedding_documents
"""Test self-hosted huggingface instruct embeddings.""" documents = ['foo bar'] * 2 gpu = get_remote_instance() embedding = SelfHostedEmbeddings(model_load_fn=get_pipeline, hardware=gpu, inference_fn=inference_fn) output = embedding.embed_documents(documents) assert len(output) == 2 assert len(output[0]) == 50265
def test_self_hosted_embedding_documents() ->None: """Test self-hosted huggingface instruct embeddings.""" documents = ['foo bar'] * 2 gpu = get_remote_instance() embedding = SelfHostedEmbeddings(model_load_fn=get_pipeline, hardware= gpu, inference_fn=inference_fn) output = embedding.embed_d...
Test self-hosted huggingface instruct embeddings.
test_run_chat_model_all_formats
llm = FakeChatModel() _run_llm(llm, inputs, mock.MagicMock())
@pytest.mark.parametrize('inputs', _VALID_MESSAGES + _VALID_PROMPTS) def test_run_chat_model_all_formats(inputs: Dict[str, Any]) ->None: llm = FakeChatModel() _run_llm(llm, inputs, mock.MagicMock())
null
test_huggingface_pipeline_text_generation
"""Test valid call to HuggingFace text generation model.""" llm = HuggingFacePipeline.from_model_id(model_id='gpt2', task= 'text-generation', pipeline_kwargs={'max_new_tokens': 10}) output = llm('Say foo:') assert isinstance(output, str)
def test_huggingface_pipeline_text_generation() ->None: """Test valid call to HuggingFace text generation model.""" llm = HuggingFacePipeline.from_model_id(model_id='gpt2', task= 'text-generation', pipeline_kwargs={'max_new_tokens': 10}) output = llm('Say foo:') assert isinstance(output, str)
Test valid call to HuggingFace text generation model.
__init__
super().__init__() self.auto_close = auto_close self.include_names = include_names self.include_types = include_types self.include_tags = include_tags self.exclude_names = exclude_names self.exclude_types = exclude_types self.exclude_tags = exclude_tags send_stream: Any receive_stream: Any send_stream, receive_stream =...
def __init__(self, *, auto_close: bool=True, include_names: Optional[ Sequence[str]]=None, include_types: Optional[Sequence[str]]=None, include_tags: Optional[Sequence[str]]=None, exclude_names: Optional[ Sequence[str]]=None, exclude_types: Optional[Sequence[str]]=None, exclude_tags: Optional[Sequence[s...
null
attempt_parse_teams
"""Parse appropriate content from the list of teams.""" parsed_teams: Dict[str, List[dict]] = {'teams': []} for team in input_dict['teams']: try: team = parse_dict_through_component(team, Team, fault_tolerant=False) parsed_teams['teams'].append(team) except Exception as e: warnings.warn(...
def attempt_parse_teams(self, input_dict: dict) ->Dict[str, List[dict]]: """Parse appropriate content from the list of teams.""" parsed_teams: Dict[str, List[dict]] = {'teams': []} for team in input_dict['teams']: try: team = parse_dict_through_component(team, Team, fault_tolerant= ...
Parse appropriate content from the list of teams.
deprecated_method
"""original doc""" return 'This is a deprecated method.'
@deprecated(since='2.0.0', removal='3.0.0') def deprecated_method(self) ->str: """original doc""" return 'This is a deprecated method.'
original doc
log
pass
def log(self, event: TEvent) ->None: pass
null
_import_vectorstore_tool_VectorStoreQATool
from langchain_community.tools.vectorstore.tool import VectorStoreQATool return VectorStoreQATool
def _import_vectorstore_tool_VectorStoreQATool() ->Any: from langchain_community.tools.vectorstore.tool import VectorStoreQATool return VectorStoreQATool
null
get_output_schema
return self.runnable.get_output_schema(config)
def get_output_schema(self, config: Optional[RunnableConfig]=None) ->Type[ BaseModel]: return self.runnable.get_output_schema(config)
null
from_llm
"""Create a `CriteriaEvalChain` instance from an llm and criteria. Parameters ---------- llm : BaseLanguageModel The language model to use for evaluation. criteria : CRITERIA_TYPE - default=None for "helpfulness" The criteria to evaluate the runs against. It can ...
@classmethod def from_llm(cls, llm: BaseLanguageModel, criteria: Optional[CRITERIA_TYPE] =None, *, prompt: Optional[BasePromptTemplate]=None, **kwargs: Any ) ->CriteriaEvalChain: """Create a `CriteriaEvalChain` instance from an llm and criteria. Parameters ---------- llm : BaseLangu...
Create a `CriteriaEvalChain` instance from an llm and criteria. Parameters ---------- llm : BaseLanguageModel The language model to use for evaluation. criteria : CRITERIA_TYPE - default=None for "helpfulness" The criteria to evaluate the runs against. It can be: - a mapping of a criterion name to its...
lc_secrets
return {'credentials': 'GIGACHAT_CREDENTIALS', 'access_token': 'GIGACHAT_ACCESS_TOKEN', 'password': 'GIGACHAT_PASSWORD', 'key_file_password': 'GIGACHAT_KEY_FILE_PASSWORD'}
@property def lc_secrets(self) ->Dict[str, str]: return {'credentials': 'GIGACHAT_CREDENTIALS', 'access_token': 'GIGACHAT_ACCESS_TOKEN', 'password': 'GIGACHAT_PASSWORD', 'key_file_password': 'GIGACHAT_KEY_FILE_PASSWORD'}
null
__init__
super().__init__(*ops) self.state = state
def __init__(self, *ops: Dict[str, Any], state: RunState) ->None: super().__init__(*ops) self.state = state
null
_chain_type
return 'sql_database_sequential_chain'
@property def _chain_type(self) ->str: return 'sql_database_sequential_chain'
null
chain
...
@overload def chain(func: Callable[[Input], AsyncIterator[Output]]) ->Runnable[Input, Output]: ...
null
test_json_equality_evaluator_evaluation_name
assert json_equality_evaluator.evaluation_name == 'json_equality'
def test_json_equality_evaluator_evaluation_name(json_equality_evaluator: JsonEqualityEvaluator) ->None: assert json_equality_evaluator.evaluation_name == 'json_equality'
null
from_texts
"""Construct USearch wrapper from raw documents. This is a user friendly interface that: 1. Embeds documents. 2. Creates an in memory docstore 3. Initializes the USearch database This is intended to be a quick way to get started. Example: .. code-...
@classmethod def from_texts(cls, texts: List[str], embedding: Embeddings, metadatas: Optional[List[Dict]]=None, ids: Optional[np.ndarray]=None, metric: str= 'cos', **kwargs: Any) ->USearch: """Construct USearch wrapper from raw documents. This is a user friendly interface that: 1. Embeds...
Construct USearch wrapper from raw documents. This is a user friendly interface that: 1. Embeds documents. 2. Creates an in memory docstore 3. Initializes the USearch database This is intended to be a quick way to get started. Example: .. code-block:: python from langchain_community.vectorstor...
test_given_no_connection_or_engine_args_provided_default_engine_should_be_used
"""When no connection or engine arguments are provided then the default configuration must be used.""" pgvector.PGVector(connection_string=_CONNECTION_STRING, embedding_function= _EMBEDDING_FUNCTION) create_engine.assert_called_with(url=_CONNECTION_STRING)
@pytest.mark.requires('pgvector') @mock.patch('sqlalchemy.create_engine') def test_given_no_connection_or_engine_args_provided_default_engine_should_be_used( create_engine: Mock) ->None: """When no connection or engine arguments are provided then the default configuration must be used.""" pgvector.PGVector(...
When no connection or engine arguments are provided then the default configuration must be used.
batch
return self._batch_with_config(self._batch, inputs, config, return_exceptions=return_exceptions, **kwargs)
def batch(self, inputs: List[str], config: Optional[Union[RunnableConfig, List[RunnableConfig]]]=None, *, return_exceptions: bool=False, **kwargs: Any) ->List[str]: return self._batch_with_config(self._batch, inputs, config, return_exceptions=return_exceptions, **kwargs)
null
_diff
"""Convert parsed outputs into a diff format. The semantics of this are up to the output parser.""" raise NotImplementedError()
def _diff(self, prev: Optional[T], next: T) ->T: """Convert parsed outputs into a diff format. The semantics of this are up to the output parser.""" raise NotImplementedError()
Convert parsed outputs into a diff format. The semantics of this are up to the output parser.
test_analyticdb_with_filter_match
"""Test end to end construction and search.""" texts = ['foo', 'bar', 'baz'] metadatas = [{'page': str(i)} for i in range(len(texts))] docsearch = AnalyticDB.from_texts(texts=texts, collection_name= 'test_collection_filter', embedding=FakeEmbeddingsWithAdaDimension(), metadatas=metadatas, connection_string=CONN...
def test_analyticdb_with_filter_match() ->None: """Test end to end construction and search.""" texts = ['foo', 'bar', 'baz'] metadatas = [{'page': str(i)} for i in range(len(texts))] docsearch = AnalyticDB.from_texts(texts=texts, collection_name= 'test_collection_filter', embedding=FakeEmbedding...
Test end to end construction and search.
_run
try: dir_path_ = self.get_relative_path(dir_path) except FileValidationError: return INVALID_PATH_TEMPLATE.format(arg_name='dir_path', value=dir_path) try: entries = os.listdir(dir_path_) if entries: return '\n'.join(entries) else: return f'No files found in directory {dir_path}' exc...
def _run(self, dir_path: str='.', run_manager: Optional[ CallbackManagerForToolRun]=None) ->str: try: dir_path_ = self.get_relative_path(dir_path) except FileValidationError: return INVALID_PATH_TEMPLATE.format(arg_name='dir_path', value=dir_path ) try: entries = os.l...
null
__getitem__
return getattr(self, item)
def __getitem__(self, item: str) ->Any: return getattr(self, item)
null
_make_request
request = urllib.request.Request(url, headers=self.headers) with urllib.request.urlopen(request) as response: json_data = json.loads(response.read().decode()) text = stringify_dict(json_data) metadata = {'source': url} return [Document(page_content=text, metadata=metadata)]
def _make_request(self, url: str) ->List[Document]: request = urllib.request.Request(url, headers=self.headers) with urllib.request.urlopen(request) as response: json_data = json.loads(response.read().decode()) text = stringify_dict(json_data) metadata = {'source': url} return [D...
null
test_blob_mimetype_from_str_data
"""Test reading blob from a file path.""" content = b'Hello, World!' mimetype = 'text/html' blob = Blob.from_data(content, mime_type=mimetype) assert blob.mimetype == mimetype
def test_blob_mimetype_from_str_data() ->None: """Test reading blob from a file path.""" content = b'Hello, World!' mimetype = 'text/html' blob = Blob.from_data(content, mime_type=mimetype) assert blob.mimetype == mimetype
Test reading blob from a file path.
test_initialize_watsonxllm_cloud_bad_path
try: WatsonxLLM(model_id='google/flan-ul2', url= 'https://us-south.ml.cloud.ibm.com') except ValueError as e: assert 'WATSONX_APIKEY' in e.__str__()
def test_initialize_watsonxllm_cloud_bad_path() ->None: try: WatsonxLLM(model_id='google/flan-ul2', url= 'https://us-south.ml.cloud.ibm.com') except ValueError as e: assert 'WATSONX_APIKEY' in e.__str__()
null
similarity_search
"""Return pinecone documents most similar to query. Args: query: Text to look up documents similar to. k: Number of Documents to return. Defaults to 4. filter: Dictionary of argument(s) to filter on metadata namespace: Namespace to search in. Default will search ...
def similarity_search(self, query: str, k: int=4, filter: Optional[dict]= None, namespace: Optional[str]=None, **kwargs: Any) ->List[Document]: """Return pinecone documents most similar to query. Args: query: Text to look up documents similar to. k: Number of Documents to return...
Return pinecone documents most similar to query. Args: query: Text to look up documents similar to. k: Number of Documents to return. Defaults to 4. filter: Dictionary of argument(s) to filter on metadata namespace: Namespace to search in. Default will search in '' namespace. Returns: List of Docu...
is_lc_serializable
return True
@classmethod def is_lc_serializable(self) ->bool: return True
null
_get_invocation_params
params = self.dict() params['stop'] = stop return {**params, **kwargs}
def _get_invocation_params(self, stop: Optional[List[str]]=None, **kwargs: Any ) ->dict: params = self.dict() params['stop'] = stop return {**params, **kwargs}
null
on_chain_error
"""Do nothing when LLM chain outputs an error.""" pass
def on_chain_error(self, error: BaseException, **kwargs: Any) ->None: """Do nothing when LLM chain outputs an error.""" pass
Do nothing when LLM chain outputs an error.
_convert_lc_run_to_wb_span
"""Utility to convert any generic LangChain Run into a W&B Trace Span. :param run: The LangChain Run to convert. :return: The converted W&B Trace Span. """ if run.run_type == 'llm': return self._convert_llm_run_to_wb_span(run) elif run.run_type == 'chain': return self._convert_chain_run_...
def _convert_lc_run_to_wb_span(self, run: Run) ->'Span': """Utility to convert any generic LangChain Run into a W&B Trace Span. :param run: The LangChain Run to convert. :return: The converted W&B Trace Span. """ if run.run_type == 'llm': return self._convert_llm_run_to_wb_span(r...
Utility to convert any generic LangChain Run into a W&B Trace Span. :param run: The LangChain Run to convert. :return: The converted W&B Trace Span.
test_chat_model_on_kv_singleio_dataset
llm = ChatOpenAI(temperature=0) eval_config = RunEvalConfig(evaluators=[EvaluatorType.QA, EvaluatorType. CRITERIA]) run_on_dataset(dataset_name=kv_singleio_dataset_name, llm_or_chain_factory= llm, evaluation=eval_config, client=client, project_name= eval_project_name, tags=['shouldpass']) _check_all_feedbac...
def test_chat_model_on_kv_singleio_dataset(kv_singleio_dataset_name: str, eval_project_name: str, client: Client) ->None: llm = ChatOpenAI(temperature=0) eval_config = RunEvalConfig(evaluators=[EvaluatorType.QA, EvaluatorType .CRITERIA]) run_on_dataset(dataset_name=kv_singleio_dataset_name, ...
null
_get_bilibili_subs_and_info
try: from bilibili_api import sync, video except ImportError: raise ImportError( 'requests package not found, please install it with `pip install bilibili-api-python`' ) bvid = re.search('BV\\w+', url) if bvid is not None: v = video.Video(bvid=bvid.group()) else: aid = re.search('av[0-9]...
def _get_bilibili_subs_and_info(self, url: str) ->Tuple[str, dict]: try: from bilibili_api import sync, video except ImportError: raise ImportError( 'requests package not found, please install it with `pip install bilibili-api-python`' ) bvid = re.search('BV\\w+', url...
null
test_embaas_embed_documents
"""Test embaas embeddings with multiple texts.""" texts = ['foo bar', 'bar foo', 'foo'] embedding = EmbaasEmbeddings() output = embedding.embed_documents(texts) assert len(output) == 3 assert len(output[0]) == 1024 assert len(output[1]) == 1024 assert len(output[2]) == 1024
def test_embaas_embed_documents() ->None: """Test embaas embeddings with multiple texts.""" texts = ['foo bar', 'bar foo', 'foo'] embedding = EmbaasEmbeddings() output = embedding.embed_documents(texts) assert len(output) == 3 assert len(output[0]) == 1024 assert len(output[1]) == 1024 a...
Test embaas embeddings with multiple texts.
load_json
try: return json.loads(s) except Exception: return {}
def load_json(s): try: return json.loads(s) except Exception: return {}
null
delete_collection
self.logger.debug('Trying to delete collection') with Session(self._conn) as session: collection = self.get_collection(session) if not collection: self.logger.warning('Collection not found') return session.delete(collection) session.commit()
def delete_collection(self) ->None: self.logger.debug('Trying to delete collection') with Session(self._conn) as session: collection = self.get_collection(session) if not collection: self.logger.warning('Collection not found') return session.delete(collection) ...
null
_call
""" Compute the string distance between the prediction and the reference. Args: inputs (Dict[str, Any]): The input values. run_manager (Optional[CallbackManagerForChainRun]): The callback manager. Returns: Dict[str, Any]: The evaluation resul...
def _call(self, inputs: Dict[str, Any], run_manager: Optional[ CallbackManagerForChainRun]=None) ->Dict[str, Any]: """ Compute the string distance between the prediction and the reference. Args: inputs (Dict[str, Any]): The input values. run_manager (Optional[CallbackMan...
Compute the string distance between the prediction and the reference. Args: inputs (Dict[str, Any]): The input values. run_manager (Optional[CallbackManagerForChainRun]): The callback manager. Returns: Dict[str, Any]: The evaluation results containing the score.
uppercase_and_check_dtype
if v.upper() not in REDIS_VECTOR_DTYPE_MAP: raise ValueError( f'datatype must be one of {REDIS_VECTOR_DTYPE_MAP.keys()}. Got {v}') return v.upper()
@validator('datatype', pre=True) def uppercase_and_check_dtype(cls, v: str) ->str: if v.upper() not in REDIS_VECTOR_DTYPE_MAP: raise ValueError( f'datatype must be one of {REDIS_VECTOR_DTYPE_MAP.keys()}. Got {v}' ) return v.upper()
null
similarity_search
"""Return docs most similar to query.""" query_vector = self._embedding.embed_query(query) return [doc for doc, score in self.similarity_search_with_score_by_vector( query_vector, k, distance_func, **kwargs)]
def similarity_search(self, query: str, k: int=4, distance_func: Literal[ 'sqrt_euclid', 'neg_dot_prod', 'ned_cos']='sqrt_euclid', **kwargs: Any ) ->List[Document]: """Return docs most similar to query.""" query_vector = self._embedding.embed_query(query) return [doc for doc, score in self. ...
Return docs most similar to query.
test_anthropic_streaming
"""Test streaming tokens from anthropic.""" llm = Anthropic(model='claude-instant-1') generator = llm.stream("I'm Pickle Rick") assert isinstance(generator, Generator) for token in generator: assert isinstance(token, str)
def test_anthropic_streaming() ->None: """Test streaming tokens from anthropic.""" llm = Anthropic(model='claude-instant-1') generator = llm.stream("I'm Pickle Rick") assert isinstance(generator, Generator) for token in generator: assert isinstance(token, str)
Test streaming tokens from anthropic.
test_chroma_with_metadatas
"""Test end to end construction and search.""" texts = ['foo', 'bar', 'baz'] metadatas = [{'page': str(i)} for i in range(len(texts))] docsearch = Chroma.from_texts(collection_name='test_collection', texts= texts, embedding=FakeEmbeddings(), metadatas=metadatas) output = docsearch.similarity_search('foo', k=1) asse...
def test_chroma_with_metadatas() ->None: """Test end to end construction and search.""" texts = ['foo', 'bar', 'baz'] metadatas = [{'page': str(i)} for i in range(len(texts))] docsearch = Chroma.from_texts(collection_name='test_collection', texts= texts, embedding=FakeEmbeddings(), metadatas=met...
Test end to end construction and search.
embed_general_texts
"""Call out to Voyage Embedding endpoint for embedding general text. Args: texts: The list of texts to embed. input_type: Type of the input text. Default to None, meaning the type is unspecified. Other options: query, document. Returns: Embedding for...
def embed_general_texts(self, texts: List[str], *, input_type: Optional[str ]=None) ->List[List[float]]: """Call out to Voyage Embedding endpoint for embedding general text. Args: texts: The list of texts to embed. input_type: Type of the input text. Default to None, meaning the...
Call out to Voyage Embedding endpoint for embedding general text. Args: texts: The list of texts to embed. input_type: Type of the input text. Default to None, meaning the type is unspecified. Other options: query, document. Returns: Embedding for the text.
_invocation_params
api_key = cast(SecretStr, self.voyage_api_key).get_secret_value() params = {'url': self.voyage_api_base, 'headers': {'Authorization': f'Bearer {api_key}'}, 'json': {'model': self.model, 'input': input, 'input_type': input_type}, 'timeout': self.request_timeout} return params
def _invocation_params(self, input: List[str], input_type: Optional[str]=None ) ->Dict: api_key = cast(SecretStr, self.voyage_api_key).get_secret_value() params = {'url': self.voyage_api_base, 'headers': {'Authorization': f'Bearer {api_key}'}, 'json': {'model': self.model, 'input': input, 'i...
null
_identifying_params
"""Get the identifying parameters.""" return {**{'model': self.model}, **self._default_params}
@property def _identifying_params(self) ->Dict[str, Any]: """Get the identifying parameters.""" return {**{'model': self.model}, **self._default_params}
Get the identifying parameters.
test_unnamed_decorator
"""Test functionality with unnamed decorator.""" @tool def search_api(query: str) ->str: """Search the API for the query.""" return 'API result' assert isinstance(search_api, BaseTool) assert search_api.name == 'search_api' assert not search_api.return_direct assert search_api('test') == 'API result'
def test_unnamed_decorator() ->None: """Test functionality with unnamed decorator.""" @tool def search_api(query: str) ->str: """Search the API for the query.""" return 'API result' assert isinstance(search_api, BaseTool) assert search_api.name == 'search_api' assert not search_...
Test functionality with unnamed decorator.
test_agent_bad_action
"""Test react chain when bad action given.""" agent = _get_agent() output = agent.run('when was langchain made') assert output == 'curses foiled again'
def test_agent_bad_action() ->None: """Test react chain when bad action given.""" agent = _get_agent() output = agent.run('when was langchain made') assert output == 'curses foiled again'
Test react chain when bad action given.
_Global
self.fill('global ') interleave(lambda : self.write(', '), self.write, t.names)
def _Global(self, t): self.fill('global ') interleave(lambda : self.write(', '), self.write, t.names)
null
_import_ddg_search_tool_DuckDuckGoSearchRun
from langchain_community.tools.ddg_search.tool import DuckDuckGoSearchRun return DuckDuckGoSearchRun
def _import_ddg_search_tool_DuckDuckGoSearchRun() ->Any: from langchain_community.tools.ddg_search.tool import DuckDuckGoSearchRun return DuckDuckGoSearchRun
null
add_texts
"""Upload texts with metadata (properties) to Weaviate.""" from weaviate.util import get_valid_uuid ids = [] embeddings: Optional[List[List[float]]] = None if self._embedding: if not isinstance(texts, list): texts = list(texts) embeddings = self._embedding.embed_documents(texts) with self._client.batch ...
def add_texts(self, texts: Iterable[str], metadatas: Optional[List[dict]]= None, **kwargs: Any) ->List[str]: """Upload texts with metadata (properties) to Weaviate.""" from weaviate.util import get_valid_uuid ids = [] embeddings: Optional[List[List[float]]] = None if self._embedding: if ...
Upload texts with metadata (properties) to Weaviate.
astra_db_collection
from astrapy.db import AstraDB astra_db = AstraDB(token=ASTRA_DB_APPLICATION_TOKEN, api_endpoint= ASTRA_DB_API_ENDPOINT, namespace=ASTRA_DB_KEYSPACE) collection_name = f"lc_test_loader_{str(uuid.uuid4()).split('-')[0]}" collection = astra_db.create_collection(collection_name) yield collection astra_db.delete_collec...
@pytest.fixture def astra_db_collection(): from astrapy.db import AstraDB astra_db = AstraDB(token=ASTRA_DB_APPLICATION_TOKEN, api_endpoint= ASTRA_DB_API_ENDPOINT, namespace=ASTRA_DB_KEYSPACE) collection_name = f"lc_test_loader_{str(uuid.uuid4()).split('-')[0]}" collection = astra_db.create_coll...
null
return_values
"""Return values of the agent.""" return ['output']
@property def return_values(self) ->List[str]: """Return values of the agent.""" return ['output']
Return values of the agent.
read_schema
"""Reads in the index schema from a dict or yaml file. Check if it is a dict and return RedisModel otherwise, check if it's a path and read in the file assuming it's a yaml file and return a RedisModel """ if isinstance(index_schema, dict): return index_schema elif isinstance(index_schema, Path): w...
def read_schema(index_schema: Optional[Union[Dict[str, List[Any]], str, os. PathLike]]) ->Dict[str, Any]: """Reads in the index schema from a dict or yaml file. Check if it is a dict and return RedisModel otherwise, check if it's a path and read in the file assuming it's a yaml file and return a RedisM...
Reads in the index schema from a dict or yaml file. Check if it is a dict and return RedisModel otherwise, check if it's a path and read in the file assuming it's a yaml file and return a RedisModel
build_extra
"""Build extra kwargs from additional params that were passed in.""" all_required_field_names = {field.alias for field in cls.__fields__.values()} extra = values.get('model_kwargs', {}) for field_name in list(values): if field_name not in all_required_field_names: if field_name in extra: raise V...
@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 = {field.alias for field in cls.__fields__. values()} extra = values.get('model_kwargs', {}) for field_name in li...
Build extra kwargs from additional params that were passed in.
_auth
"""Authenticate with Microsoft Graph API""" if self.access_token != '': return if self.auth_with_token: with self.token_path.open('r') as token_file: self.access_token = token_file.read() else: try: from msal import ConfidentialClientApplication except ImportError as e: raise Imp...
def _auth(self) ->None: """Authenticate with Microsoft Graph API""" if self.access_token != '': return if self.auth_with_token: with self.token_path.open('r') as token_file: self.access_token = token_file.read() else: try: from msal import ConfidentialClie...
Authenticate with Microsoft Graph API
embed_query
return self._query([text])[0]
def embed_query(self, text: str) ->List[float]: return self._query([text])[0]
null
from_texts
"""Return VectorStore initialized from texts and embeddings.""" if not collection_name: raise ValueError('Collection name must be provided') if not vector_size: raise ValueError('Vector size must be provided') if not api_key: raise ValueError('API key must be provided') semadb = cls(collection_name, vector_...
@classmethod def from_texts(cls, texts: List[str], embedding: Embeddings, metadatas: Optional[List[dict]]=None, collection_name: str='', vector_size: int=0, api_key: str='', distance_strategy: DistanceStrategy=DistanceStrategy. EUCLIDEAN_DISTANCE, **kwargs: Any) ->'SemaDB': """Return VectorStore initial...
Return VectorStore initialized from texts and embeddings.
test_memory_with_message_store
"""Test the memory with a message store.""" message_history = CosmosDBChatMessageHistory(cosmos_endpoint=endpoint, cosmos_database='chat_history', cosmos_container='messages', credential =credential, session_id='my-test-session', user_id='my-test-user', ttl=10) message_history.prepare_cosmos() memory = Conversa...
def test_memory_with_message_store() ->None: """Test the memory with a message store.""" message_history = CosmosDBChatMessageHistory(cosmos_endpoint=endpoint, cosmos_database='chat_history', cosmos_container='messages', credential=credential, session_id='my-test-session', user_id= 'my-t...
Test the memory with a message store.
input_keys
""" Get the input keys. Returns: List[str]: The input keys. """ return ['reference', 'prediction']
@property def input_keys(self) ->List[str]: """ Get the input keys. Returns: List[str]: The input keys. """ return ['reference', 'prediction']
Get the input keys. Returns: List[str]: The input keys.
drop_tables
with self._conn.begin(): Base.metadata.drop_all(self._conn)
def drop_tables(self) ->None: with self._conn.begin(): Base.metadata.drop_all(self._conn)
null
from_gml
try: import networkx as nx except ImportError: raise ImportError( 'Could not import networkx python package. Please install it with `pip install networkx`.' ) graph = nx.read_gml(gml_path) return cls(graph)
@classmethod def from_gml(cls, gml_path: str) ->NetworkxEntityGraph: try: import networkx as nx except ImportError: raise ImportError( 'Could not import networkx python package. Please install it with `pip install networkx`.' ) graph = nx.read_gml(gml_path) return...
null
test_color_question_1
"""Test simple question.""" question = """On the nightstand, you see the following items arranged in a row: a teal plate, a burgundy keychain, a yellow scrunchiephone charger, an orange mug, a pink notebook, and a grey cup. How many non-orange items do you see to the l...
def test_color_question_1() ->None: """Test simple question.""" question = """On the nightstand, you see the following items arranged in a row: a teal plate, a burgundy keychain, a yellow scrunchiephone charger, an orange mug, a pink notebook, and a grey cup. How many non-orang...
Test simple question.
get_full_header
"""Return a full header of the agent's status, summary, and current time.""" now = datetime.now() if now is None else now summary = self.get_summary(force_refresh=force_refresh, now=now) current_time_str = now.strftime('%B %d, %Y, %I:%M %p') return f"""{summary} It is {current_time_str}. {self.name}'s status: {self.sta...
def get_full_header(self, force_refresh: bool=False, now: Optional[datetime ]=None) ->str: """Return a full header of the agent's status, summary, and current time.""" now = datetime.now() if now is None else now summary = self.get_summary(force_refresh=force_refresh, now=now) current_time_str = now...
Return a full header of the agent's status, summary, and current time.
test_openai_multiple_prompts
"""Test completion with multiple prompts.""" output = llm.generate(["I'm Pickle Rick", "I'm Pickle Rick"]) assert isinstance(output, LLMResult) assert isinstance(output.generations, list) assert len(output.generations) == 2
@pytest.mark.scheduled def test_openai_multiple_prompts(llm: AzureOpenAI) ->None: """Test completion with multiple prompts.""" output = llm.generate(["I'm Pickle Rick", "I'm Pickle Rick"]) assert isinstance(output, LLMResult) assert isinstance(output.generations, list) assert len(output.generations)...
Test completion with multiple prompts.
on_tool_error
"""Do nothing when tool outputs an error."""
def on_tool_error(self, error: BaseException, **kwargs: Any) ->None: """Do nothing when tool outputs an error."""
Do nothing when tool outputs an error.
test_alibabacloud_opensearch_with_text_and_meta_score_query
opensearch = create_alibabacloud_opensearch() output = opensearch.similarity_search_with_relevance_scores(query='foo', search_filter={'string_field': 'value1', 'int_field': 1, 'float_field': 1.0, 'double_field': 2.0}, k=1) assert output == [(Document(page_content='foo', metadata={'string_field': 'value1', '...
def test_alibabacloud_opensearch_with_text_and_meta_score_query() ->None: opensearch = create_alibabacloud_opensearch() output = opensearch.similarity_search_with_relevance_scores(query='foo', search_filter={'string_field': 'value1', 'int_field': 1, 'float_field': 1.0, 'double_field': 2.0}, k=1)...
null
test_aql_generation
"""Test that AQL statement is correctly generated and executed.""" db = get_arangodb_client() populate_arangodb_database(db) graph = ArangoGraph(db) chain = ArangoGraphQAChain.from_llm(OpenAI(temperature=0), graph=graph) chain.return_aql_result = True output = chain('Is Ned Stark alive?') assert output['aql_result'] ==...
def test_aql_generation() ->None: """Test that AQL statement is correctly generated and executed.""" db = get_arangodb_client() populate_arangodb_database(db) graph = ArangoGraph(db) chain = ArangoGraphQAChain.from_llm(OpenAI(temperature=0), graph=graph) chain.return_aql_result = True output...
Test that AQL statement is correctly generated and executed.
_identifying_params
"""Get the identifying parameters.""" return {}
@property def _identifying_params(self) ->Mapping[str, Any]: """Get the identifying parameters.""" return {}
Get the identifying parameters.
func
return idchain
def func(__input: dict) ->Runnable: return idchain
null
parse_sitemap
"""Parse sitemap xml and load into a list of dicts. Args: soup: BeautifulSoup object. Returns: List of dicts. """ els = [] for url in soup.find_all('url'): loc = url.find('loc') if not loc: continue loc_text = loc.text.strip() if self.restrict_to...
def parse_sitemap(self, soup: Any) ->List[dict]: """Parse sitemap xml and load into a list of dicts. Args: soup: BeautifulSoup object. Returns: List of dicts. """ els = [] for url in soup.find_all('url'): loc = url.find('loc') if not loc: ...
Parse sitemap xml and load into a list of dicts. Args: soup: BeautifulSoup object. Returns: List of dicts.
clear
"""Clear cache.""" self._cache = {}
def clear(self, **kwargs: Any) ->None: """Clear cache.""" self._cache = {}
Clear cache.
_llm_type
"""Return type of llm.""" return 'titan_takeoff_pro'
@property def _llm_type(self) ->str: """Return type of llm.""" return 'titan_takeoff_pro'
Return type of llm.
_format_output
return output['output']['choices'][0]['text']
def _format_output(self, output: dict) ->str: return output['output']['choices'][0]['text']
null
_type
return 'json_functions'
@property def _type(self) ->str: return 'json_functions'
null
_default_params
params: Dict[str, Any] = {'target_uri': self.target_uri, 'endpoint': self. endpoint, 'temperature': self.temperature, 'n': self.n, 'stop': self. stop, 'max_tokens': self.max_tokens, 'extra_params': self.extra_params} return params
@property def _default_params(self) ->Dict[str, Any]: params: Dict[str, Any] = {'target_uri': self.target_uri, 'endpoint': self.endpoint, 'temperature': self.temperature, 'n': self.n, 'stop': self.stop, 'max_tokens': self.max_tokens, 'extra_params': self. extra_params} return params
null
save
with open(self.persist_path, 'wb') as fp: fp.write(self.bson.dumps(data))
def save(self, data: Any) ->None: with open(self.persist_path, 'wb') as fp: fp.write(self.bson.dumps(data))
null
test_metadata_with_tags_and_frontmatter
"""Verify a doc with frontmatter and tags/dataview tags are all added to metadata.""" doc = next(doc for doc in docs if doc.metadata['source'] == 'tags_and_frontmatter.md') FRONTMATTER_FIELDS = {'aBool', 'aFloat', 'anInt', 'anArray', 'aString', 'aDict', 'tags'} DATAVIEW_FIELDS = {'dataview1', 'dataview2', '...
def test_metadata_with_tags_and_frontmatter() ->None: """Verify a doc with frontmatter and tags/dataview tags are all added to metadata.""" doc = next(doc for doc in docs if doc.metadata['source'] == 'tags_and_frontmatter.md') FRONTMATTER_FIELDS = {'aBool', 'aFloat', 'anInt', 'anArray', 'aString...
Verify a doc with frontmatter and tags/dataview tags are all added to metadata.
_format_reference
"""Format the reference text. Args: reference (str): The reference text. Returns: str: The formatted reference text. """ if not reference: return '' return f""" The following is the expected answer. Use this to measure correctness: [GROUND_TRUTH] {reference} [END_G...
@staticmethod def _format_reference(reference: Optional[str]) ->str: """Format the reference text. Args: reference (str): The reference text. Returns: str: The formatted reference text. """ if not reference: return '' return f""" The following is th...
Format the reference text. Args: reference (str): The reference text. Returns: str: The formatted reference text.
test_init
CohereRerank() CohereRerank(top_n=5, model='rerank-english_v2.0', cohere_api_key='foo', user_agent='bar')
@pytest.mark.requires('cohere') def test_init() ->None: CohereRerank() CohereRerank(top_n=5, model='rerank-english_v2.0', cohere_api_key='foo', user_agent='bar')
null
__init__
"""Initializes the JsonSchemaEvaluator. Args: **kwargs: Additional keyword arguments. Raises: ImportError: If the jsonschema package is not installed. """ super().__init__() try: import jsonschema except ImportError: raise ImportError( 'The JsonSchemaEva...
def __init__(self, **kwargs: Any) ->None: """Initializes the JsonSchemaEvaluator. Args: **kwargs: Additional keyword arguments. Raises: ImportError: If the jsonschema package is not installed. """ super().__init__() try: import jsonschema except ...
Initializes the JsonSchemaEvaluator. Args: **kwargs: Additional keyword arguments. Raises: ImportError: If the jsonschema package is not installed.
load
"""Load documents.""" psychic_docs = self.psychic.get_documents(connector_id=self.connector_id, account_id=self.account_id) return [Document(page_content=doc['content'], metadata={'title': doc[ 'title'], 'source': doc['uri']}) for doc in psychic_docs.documents]
def load(self) ->List[Document]: """Load documents.""" psychic_docs = self.psychic.get_documents(connector_id=self. connector_id, account_id=self.account_id) return [Document(page_content=doc['content'], metadata={'title': doc[ 'title'], 'source': doc['uri']}) for doc in psychic_docs.documen...
Load documents.