method_name
stringlengths
1
78
method_body
stringlengths
3
9.66k
full_code
stringlengths
31
10.7k
docstring
stringlengths
4
4.74k
test__convert_message_to_dict_ai
message = AIMessage(content='foo') result = _convert_message_to_dict(message) expected_output = {'role': 'assistant', 'content': 'foo'} assert result == expected_output
def test__convert_message_to_dict_ai() ->None: message = AIMessage(content='foo') result = _convert_message_to_dict(message) expected_output = {'role': 'assistant', 'content': 'foo'} assert result == expected_output
null
__init__
"""Initialize with a file path.""" self.file_path = file_path
def __init__(self, file_path: str): """Initialize with a file path.""" self.file_path = file_path
Initialize with a file path.
get_default_prompt
base_prompt = ChatPromptTemplate.from_template(agent_instructions) return base_prompt + AIMessagePromptTemplate.from_template( '{intermediate_steps}')
@staticmethod def get_default_prompt() ->ChatPromptTemplate: base_prompt = ChatPromptTemplate.from_template(agent_instructions) return base_prompt + AIMessagePromptTemplate.from_template( '{intermediate_steps}')
null
test_llamacpp_model_kwargs
llm = LlamaCpp(model_path=get_model(), model_kwargs={'n_gqa': None}) assert llm.model_kwargs == {'n_gqa': None}
def test_llamacpp_model_kwargs() ->None: llm = LlamaCpp(model_path=get_model(), model_kwargs={'n_gqa': None}) assert llm.model_kwargs == {'n_gqa': None}
null
load
"""Load documents.""" p = Path(self.file_path) text_content = '' with open(p, encoding='utf8') as f: lines = f.readlines() message_line_regex = """ \\[? ( \\d{1,4} [\\/.] \\d{1,2} [\\/.] \\d{1,4} ...
def load(self) ->List[Document]: """Load documents.""" p = Path(self.file_path) text_content = '' with open(p, encoding='utf8') as f: lines = f.readlines() message_line_regex = """ \\[? ( \\d{1,4} [\\/.] \\d{1,2} ...
Load documents.
_import_vllm_openai
from langchain_community.llms.vllm import VLLMOpenAI return VLLMOpenAI
def _import_vllm_openai() ->Any: from langchain_community.llms.vllm import VLLMOpenAI return VLLMOpenAI
null
max_marginal_relevance_search_by_vector
"""Return docs selected using the maximal marginal relevance. Maximal marginal relevance optimizes for similarity to query AND diversity among selected documents. Args: embedding: Embedding to look up documents similar to. k: Number of Documents to return. Defaults to 4...
def max_marginal_relevance_search_by_vector(self, embedding: List[float], k: int=4, fetch_k: int=20, lambda_mult: float=0.5, **kwargs: Any) ->List[ Document]: """Return docs selected using the maximal marginal relevance. Maximal marginal relevance optimizes for similarity to query AND diversity ...
Return docs selected using the maximal marginal relevance. Maximal marginal relevance optimizes for similarity to query AND diversity among selected documents. Args: embedding: Embedding to look up documents similar to. k: Number of Documents to return. Defaults to 4. fetch_k: Number of Documents to fetch...
__init__
"""Define a parser that uses mime-types to determine how to parse a blob. Args: handlers: A mapping from mime-types to functions that take a blob, parse it and return a document. fallback_parser: A fallback_parser parser to use if the mime-type is not ...
def __init__(self, handlers: Mapping[str, BaseBlobParser], *, fallback_parser: Optional[BaseBlobParser]=None) ->None: """Define a parser that uses mime-types to determine how to parse a blob. Args: handlers: A mapping from mime-types to functions that take a blob, parse it ...
Define a parser that uses mime-types to determine how to parse a blob. Args: handlers: A mapping from mime-types to functions that take a blob, parse it and return a document. fallback_parser: A fallback_parser parser to use if the mime-type is not found in the handlers. If p...
test_johnsnowlabs_embed_query
"""Test johnsnowlabs embeddings.""" document = 'foo bar' embedding = JohnSnowLabsEmbeddings() output = embedding.embed_query(document) assert len(output) == 128
def test_johnsnowlabs_embed_query() ->None: """Test johnsnowlabs embeddings.""" document = 'foo bar' embedding = JohnSnowLabsEmbeddings() output = embedding.embed_query(document) assert len(output) == 128
Test johnsnowlabs embeddings.
_load_document_from_id
"""Load a document from an ID.""" from io import BytesIO from googleapiclient.discovery import build from googleapiclient.errors import HttpError from googleapiclient.http import MediaIoBaseDownload creds = self._load_credentials() service = build('drive', 'v3', credentials=creds) file = service.files().get(fileId=id, ...
def _load_document_from_id(self, id: str) ->Document: """Load a document from an ID.""" from io import BytesIO from googleapiclient.discovery import build from googleapiclient.errors import HttpError from googleapiclient.http import MediaIoBaseDownload creds = self._load_credentials() servic...
Load a document from an ID.
_lambda
if x == 1: raise ValueError('x is 1') elif x == 2: raise RuntimeError('x is 2') else: return x
def _lambda(x: int) ->Union[int, Runnable]: if x == 1: raise ValueError('x is 1') elif x == 2: raise RuntimeError('x is 2') else: return x
null
_import_titan_takeoff_pro
from langchain_community.llms.titan_takeoff_pro import TitanTakeoffPro return TitanTakeoffPro
def _import_titan_takeoff_pro() ->Any: from langchain_community.llms.titan_takeoff_pro import TitanTakeoffPro return TitanTakeoffPro
null
__init__
"""Initialize with a file path.""" try: from pdfminer.high_level import extract_text_to_fp except ImportError: raise ImportError( '`pdfminer` package not found, please install it with `pip install pdfminer.six`' ) super().__init__(file_path, headers=headers)
def __init__(self, file_path: str, *, headers: Optional[Dict]=None): """Initialize with a file path.""" try: from pdfminer.high_level import extract_text_to_fp except ImportError: raise ImportError( '`pdfminer` package not found, please install it with `pip install pdfminer.six`'...
Initialize with a file path.
_import_python_tool_PythonAstREPLTool
raise ImportError( "This tool has been moved to langchain experiment. This tool has access to a python REPL. For best practices make sure to sandbox this tool. Read https://github.com/langchain-ai/langchain/blob/master/SECURITY.md To keep using this code as is, install langchain experimental and update relevant imp...
def _import_python_tool_PythonAstREPLTool() ->Any: raise ImportError( "This tool has been moved to langchain experiment. This tool has access to a python REPL. For best practices make sure to sandbox this tool. Read https://github.com/langchain-ai/langchain/blob/master/SECURITY.md To keep using this code as...
null
_import_yellowbrick
from langchain_community.vectorstores.yellowbrick import Yellowbrick return Yellowbrick
def _import_yellowbrick() ->Any: from langchain_community.vectorstores.yellowbrick import Yellowbrick return Yellowbrick
null
on_llm_start
"""Log the prompts to Infino, and set start time and error flag.""" for prompt in prompts: self._send_to_infino('prompt', prompt, is_ts=False) self.error = 0 self.start_time = time.time()
def on_llm_start(self, serialized: Dict[str, Any], prompts: List[str], ** kwargs: Any) ->None: """Log the prompts to Infino, and set start time and error flag.""" for prompt in prompts: self._send_to_infino('prompt', prompt, is_ts=False) self.error = 0 self.start_time = time.time()
Log the prompts to Infino, and set start time and error flag.
add_texts
from qdrant_client import QdrantClient added_ids = [] client = cast(QdrantClient, self.client) for batch_ids, points in self._generate_rest_batches(texts, metadatas, ids, batch_size): client.upsert(self.collection_name, points=points, **kwargs) added_ids.extend(batch_ids) return added_ids
def add_texts(self, texts: Iterable[str], metadatas: Optional[List[dict]]= None, ids: Optional[Sequence[str]]=None, batch_size: int=64, **kwargs: Any ) ->List[str]: from qdrant_client import QdrantClient added_ids = [] client = cast(QdrantClient, self.client) for batch_ids, points in self._gener...
null
test_convert_pydantic_to_openai_function
class Data(BaseModel): """The data to return.""" key: str = Field(..., description='API key') days: int = Field(default=0, description='Number of days to forecast') actual = convert_pydantic_to_openai_function(Data) expected = {'name': 'Data', 'description': 'The data to return.', 'parameters': {'title'...
def test_convert_pydantic_to_openai_function() ->None: class Data(BaseModel): """The data to return.""" key: str = Field(..., description='API key') days: int = Field(default=0, description='Number of days to forecast') actual = convert_pydantic_to_openai_function(Data) expected = ...
null
test_spliting_answer_into_answer_and_sources
qa_chain = QAWithSourcesChain.from_llm(FakeLLM()) generated_answer, generated_sources = qa_chain._split_sources(text) assert generated_answer == answer assert generated_sources == sources
@pytest.mark.parametrize('text,answer,sources', [( """This Agreement is governed by English law. SOURCES: 28-pl""", 'This Agreement is governed by English law.\n', '28-pl'), ( """This Agreement is governed by English law. Sources: 28-pl""", 'This Agreement is governed by English law.\n', '28-pl'), ( ...
null
_parse_list
"""Parse a newline-separated string into a list of strings.""" lines = re.split('\\n', text.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()) return [re.sub('^\\s*\\d+\\.\\s*', '', line).strip() for line in lines]
Parse a newline-separated string into a list of strings.
regex_match_string_evaluator_ignore_case
"""Create a RegexMatchStringEvaluator with IGNORECASE flag.""" return RegexMatchStringEvaluator(flags=re.IGNORECASE)
@pytest.fixture def regex_match_string_evaluator_ignore_case() ->RegexMatchStringEvaluator: """Create a RegexMatchStringEvaluator with IGNORECASE flag.""" return RegexMatchStringEvaluator(flags=re.IGNORECASE)
Create a RegexMatchStringEvaluator with IGNORECASE flag.
test_load_fail_wrong_split_name
"""Test that fails to load""" with pytest.raises(ValidationError) as exc_info: TensorflowDatasets(dataset_name='mlqa/en', split_name= 'wrong_split_name', load_max_docs=MAX_DOCS, sample_to_document_function=mlqaen_example_to_document) assert 'Unknown split' in str(exc_info.value)
def test_load_fail_wrong_split_name() ->None: """Test that fails to load""" with pytest.raises(ValidationError) as exc_info: TensorflowDatasets(dataset_name='mlqa/en', split_name= 'wrong_split_name', load_max_docs=MAX_DOCS, sample_to_document_function=mlqaen_example_to_document) ...
Test that fails to load
_identifying_params
"""Get the identifying parameters.""" return {**{'models_priority_list': self.models_priority_list}, **self. _default_params}
@property def _identifying_params(self) ->Dict[str, Any]: """Get the identifying parameters.""" return {**{'models_priority_list': self.models_priority_list}, **self. _default_params}
Get the identifying parameters.
get_function_first_arg_dict_keys
"""Get the keys of the first argument of a function if it is a dict.""" try: code = inspect.getsource(func) tree = ast.parse(textwrap.dedent(code)) visitor = IsFunctionArgDict() visitor.visit(tree) return list(visitor.keys) if visitor.keys else None except (SyntaxError, TypeError, OSError): retu...
def get_function_first_arg_dict_keys(func: Callable) ->Optional[List[str]]: """Get the keys of the first argument of a function if it is a dict.""" try: code = inspect.getsource(func) tree = ast.parse(textwrap.dedent(code)) visitor = IsFunctionArgDict() visitor.visit(tree) ...
Get the keys of the first argument of a function if it is a dict.
from_url
"""Instantiate AIPlugin from a URL.""" response = requests.get(url).json() return cls(**response)
@classmethod def from_url(cls, url: str) ->AIPlugin: """Instantiate AIPlugin from a URL.""" response = requests.get(url).json() return cls(**response)
Instantiate AIPlugin from a URL.
_parse_attributedBody
""" Parse the attributedBody field of the message table for the text content of the message. The attributedBody field is a binary blob that contains the message content after the byte string b"NSString": 5 bytes 1-3 bytes `len` bytes ... | ...
def _parse_attributedBody(self, attributedBody: bytes) ->str: """ Parse the attributedBody field of the message table for the text content of the message. The attributedBody field is a binary blob that contains the message content after the byte string b"NSString": ...
Parse the attributedBody field of the message table for the text content of the message. The attributedBody field is a binary blob that contains the message content after the byte string b"NSString": 5 bytes 1-3 bytes `len` bytes ... | b"NSString" | preamble | `len` | contents...
input_keys
"""Input keys. :meta private: """ return [self.input_key]
@property def input_keys(self) ->List[str]: """Input keys. :meta private: """ return [self.input_key]
Input keys. :meta private:
_import_clickhouse
from langchain_community.vectorstores.clickhouse import Clickhouse return Clickhouse
def _import_clickhouse() ->Any: from langchain_community.vectorstores.clickhouse import Clickhouse return Clickhouse
null
api_client
return WikipediaAPIWrapper()
@pytest.fixture def api_client() ->WikipediaAPIWrapper: return WikipediaAPIWrapper()
null
_Tuple
self.write('(') if len(t.elts) == 1: elt = t.elts[0] self.dispatch(elt) self.write(',') else: interleave(lambda : self.write(', '), self.dispatch, t.elts) self.write(')')
def _Tuple(self, t): self.write('(') if len(t.elts) == 1: elt = t.elts[0] self.dispatch(elt) self.write(',') else: interleave(lambda : self.write(', '), self.dispatch, t.elts) self.write(')')
null
validate_environment
"""Validate that api key exists in environment.""" _ = get_from_dict_or_env(values, 'eleven_api_key', 'ELEVEN_API_KEY') return values
@root_validator(pre=True) def validate_environment(cls, values: Dict) ->Dict: """Validate that api key exists in environment.""" _ = get_from_dict_or_env(values, 'eleven_api_key', 'ELEVEN_API_KEY') return values
Validate that api key exists in environment.
__init__
""" Initialize an instance of UpstashRedisCache. This method initializes an object with Upstash Redis caching capabilities. It takes a `redis_` parameter, which should be an instance of an Upstash Redis client class, allowing the object to interact with Upstash Redis server for ...
def __init__(self, redis_: Any, *, ttl: Optional[int]=None): """ Initialize an instance of UpstashRedisCache. This method initializes an object with Upstash Redis caching capabilities. It takes a `redis_` parameter, which should be an instance of an Upstash Redis client class, allow...
Initialize an instance of UpstashRedisCache. This method initializes an object with Upstash Redis caching capabilities. It takes a `redis_` parameter, which should be an instance of an Upstash Redis client class, allowing the object to interact with Upstash Redis server for caching purposes. Parameters: redis_: A...
test_openllm_with_kwargs
llm = OpenLLM(model_name='flan-t5', model_id='google/flan-t5-small', temperature=0.84) output = llm('Say bar:') assert isinstance(output, str)
def test_openllm_with_kwargs() ->None: llm = OpenLLM(model_name='flan-t5', model_id='google/flan-t5-small', temperature=0.84) output = llm('Say bar:') assert isinstance(output, str)
null
create_hnsw_index
create_index_query = sqlalchemy.text( 'CREATE INDEX IF NOT EXISTS langchain_pg_embedding_idx ON langchain_pg_embedding USING hnsw (embedding) WITH (maxelements = {}, dims = {}, m = {}, efconstruction = {}, efsearch = {});' .format(max_elements, dims, m, ef_construction, ef_search)) try: with Session(self._c...
def create_hnsw_index(self, max_elements: int=10000, dims: int= ADA_TOKEN_COUNT, m: int=8, ef_construction: int=16, ef_search: int=16 ) ->None: create_index_query = sqlalchemy.text( 'CREATE INDEX IF NOT EXISTS langchain_pg_embedding_idx ON langchain_pg_embedding USING hnsw (embedding) WITH (maxeleme...
null
toxicity_callback
return self.on_after_toxicity.__func__ is not BaseModerationCallbackHandler.on_after_toxicity
@property def toxicity_callback(self) ->bool: return (self.on_after_toxicity.__func__ is not BaseModerationCallbackHandler.on_after_toxicity)
null
_validate_tools
super()._validate_tools(tools) validate_tools_single_input(cls.__name__, tools)
@classmethod def _validate_tools(cls, tools: Sequence[BaseTool]) ->None: super()._validate_tools(tools) validate_tools_single_input(cls.__name__, tools)
null
inputs
self._inputs = self.agent_executor.prep_inputs(inputs)
@inputs.setter def inputs(self, inputs: Any) ->None: self._inputs = self.agent_executor.prep_inputs(inputs)
null
test_gpt2_call
"""Test valid call to GPT2.""" llm = AzureMLOnlineEndpoint(endpoint_api_key=os.getenv( 'OSS_ENDPOINT_API_KEY'), endpoint_url=os.getenv('OSS_ENDPOINT_URL'), deployment_name=os.getenv('OSS_DEPLOYMENT_NAME'), content_formatter= OSSContentFormatter()) output = llm('Foo') assert isinstance(output, str)
def test_gpt2_call() ->None: """Test valid call to GPT2.""" llm = AzureMLOnlineEndpoint(endpoint_api_key=os.getenv( 'OSS_ENDPOINT_API_KEY'), endpoint_url=os.getenv('OSS_ENDPOINT_URL'), deployment_name=os.getenv('OSS_DEPLOYMENT_NAME'), content_formatter =OSSContentFormatter()) output ...
Test valid call to GPT2.
__init__
""" Initialize a new LangSmithChatDatasetLoader instance. :param dataset_name: The name of the LangSmith dataset. :param client: An instance of LangSmith client; if not provided, a new client instance will be created. """ try: from langsmith.client import Client except I...
def __init__(self, *, dataset_name: str, client: Optional['Client']=None): """ Initialize a new LangSmithChatDatasetLoader instance. :param dataset_name: The name of the LangSmith dataset. :param client: An instance of LangSmith client; if not provided, a new client instance wil...
Initialize a new LangSmithChatDatasetLoader instance. :param dataset_name: The name of the LangSmith dataset. :param client: An instance of LangSmith client; if not provided, a new client instance will be created.
mock_post
with patch('langchain_community.tools.edenai.edenai_base_tool.requests.post' ) as mock: yield mock
@pytest.fixture def mock_post() ->Generator: with patch( 'langchain_community.tools.edenai.edenai_base_tool.requests.post' ) as mock: yield mock
null
test_no_message
with pytest.raises(ValueError) as info: model.predict_messages([]) assert info.value.args[0] == 'at least one HumanMessage must be provided'
def test_no_message(model: Llama2Chat) ->None: with pytest.raises(ValueError) as info: model.predict_messages([]) assert info.value.args[0] == 'at least one HumanMessage must be provided'
null
_get_example_memories
return [Document(page_content='foo', metadata={'buffer_idx': i, 'last_accessed_at': datetime(2023, 4, 14, 12, 0)}) for i in range(k)]
def _get_example_memories(k: int=4) ->List[Document]: return [Document(page_content='foo', metadata={'buffer_idx': i, 'last_accessed_at': datetime(2023, 4, 14, 12, 0)}) for i in range(k)]
null
convert_pydantic_to_ernie_function
"""Converts a Pydantic model to a function description for the Ernie API.""" schema = dereference_refs(model.schema()) schema.pop('definitions', None) return {'name': name or schema['title'], 'description': description or schema['description'], 'parameters': schema}
def convert_pydantic_to_ernie_function(model: Type[BaseModel], *, name: Optional[str]=None, description: Optional[str]=None) ->FunctionDescription: """Converts a Pydantic model to a function description for the Ernie API.""" schema = dereference_refs(model.schema()) schema.pop('definitions', None) r...
Converts a Pydantic model to a function description for the Ernie API.
_build_youtube_client
try: from googleapiclient.discovery import build from youtube_transcript_api import YouTubeTranscriptApi except ImportError: raise ImportError( 'You must run`pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib youtube-transcript-api` to use the Google Drive loade...
def _build_youtube_client(self, creds: Any) ->Any: try: from googleapiclient.discovery import build from youtube_transcript_api import YouTubeTranscriptApi except ImportError: raise ImportError( 'You must run`pip install --upgrade google-api-python-client google-auth-httplib2...
null
score_response
...
@abstractmethod def score_response(self, inputs: Dict[str, Any], llm_response: str, event: TEvent) ->float: ...
null
test_get_relevant_documents
retriever = YouRetriever() actual = retriever.get_relevant_documents('test') assert len(actual) > 0
def test_get_relevant_documents(self) ->None: retriever = YouRetriever() actual = retriever.get_relevant_documents('test') assert len(actual) > 0
null
config_specs
return get_unique_config_specs(spec for dep in self.deps for spec in dep. config_specs)
@property def config_specs(self) ->List[ConfigurableFieldSpec]: return get_unique_config_specs(spec for dep in self.deps for spec in dep.config_specs)
null
on_llm_end
"""On LLM end, send data to Arthur.""" try: import pytz except ImportError as e: raise ImportError( "Could not import pytz. Please install it with 'pip install pytz'." ) from e run_id = kwargs['run_id'] try: run_map_data = self.run_map[run_id] except KeyError as e: raise KeyError( ...
def on_llm_end(self, response: LLMResult, **kwargs: Any) ->None: """On LLM end, send data to Arthur.""" try: import pytz except ImportError as e: raise ImportError( "Could not import pytz. Please install it with 'pip install pytz'." ) from e run_id = kwargs['run_i...
On LLM end, send data to Arthur.
run
plan = self.chat_planner.plan(inputs={'input': input, 'hf_tools': self.tools}) self.task_executor = TaskExecutor(plan) self.task_executor.run() response = self.response_generator.generate({'task_execution': self. task_executor}) return response
def run(self, input: str) ->str: plan = self.chat_planner.plan(inputs={'input': input, 'hf_tools': self. tools}) self.task_executor = TaskExecutor(plan) self.task_executor.run() response = self.response_generator.generate({'task_execution': self. task_executor}) return response
null
_select_relevance_score_fn
if self.relevance_score_fn: return self.relevance_score_fn metric_map = {'COSINE': self._cosine_relevance_score_fn, 'IP': self. _max_inner_product_relevance_score_fn, 'L2': self. _euclidean_relevance_score_fn} try: return metric_map[self._schema.content_vector.distance_metric] except KeyError: retur...
def _select_relevance_score_fn(self) ->Callable[[float], float]: if self.relevance_score_fn: return self.relevance_score_fn metric_map = {'COSINE': self._cosine_relevance_score_fn, 'IP': self. _max_inner_product_relevance_score_fn, 'L2': self. _euclidean_relevance_score_fn} try: ...
null
test_calls_convert_agent_action_to_messages
additional_kwargs1 = {'tool_calls': [{'id': 'call_abcd12345', 'function': { 'arguments': '{"a": 3, "b": 5}', 'name': 'add'}, 'type': 'function'}]} message1 = AIMessage(content='', additional_kwargs=additional_kwargs1) actions1 = parse_ai_message_to_openai_tool_action(message1) additional_kwargs2 = {'tool_calls': [{...
def test_calls_convert_agent_action_to_messages() ->None: additional_kwargs1 = {'tool_calls': [{'id': 'call_abcd12345', 'function': {'arguments': '{"a": 3, "b": 5}', 'name': 'add'}, 'type': 'function'}]} message1 = AIMessage(content='', additional_kwargs=additional_kwargs1) actions1 = parse_...
null
split_text_from_file
"""Split HTML file Args: file: HTML file """ try: from lxml import etree except ImportError as e: raise ImportError( 'Unable to import lxml, please install with `pip install lxml`.' ) from e parser = etree.HTMLParser() tree = etree.parse_folder(file, parser) xslt_pat...
def split_text_from_file(self, file: Any) ->List[Document]: """Split HTML file Args: file: HTML file """ try: from lxml import etree except ImportError as e: raise ImportError( 'Unable to import lxml, please install with `pip install lxml`.' ...
Split HTML file Args: file: HTML file
Embed
if isinstance(anything, _ToSelectFrom): return ToSelectFrom(Embed(anything.value, keep=keep)) elif isinstance(anything, _BasedOn): return BasedOn(Embed(anything.value, keep=keep)) if isinstance(anything, list): return [Embed(v, keep=keep) for v in anything] elif isinstance(anything, dict): return {k: Em...
def Embed(anything: Any, keep: bool=False) ->Any: if isinstance(anything, _ToSelectFrom): return ToSelectFrom(Embed(anything.value, keep=keep)) elif isinstance(anything, _BasedOn): return BasedOn(Embed(anything.value, keep=keep)) if isinstance(anything, list): return [Embed(v, keep=k...
null
test_sql_database_sequential_chain_run
"""Test that commands can be run successfully SEQUENTIALLY and returned in correct format.""" engine = create_engine('sqlite:///:memory:') metadata_obj.create_all(engine) stmt = insert(user).values(user_id=13, user_name='Harrison', user_company='Foo' ) with engine.connect() as conn: conn.execute(stmt) db = ...
def test_sql_database_sequential_chain_run() ->None: """Test that commands can be run successfully SEQUENTIALLY and returned in correct format.""" engine = create_engine('sqlite:///:memory:') metadata_obj.create_all(engine) stmt = insert(user).values(user_id=13, user_name='Harrison', user_co...
Test that commands can be run successfully SEQUENTIALLY and returned in correct format.
output_keys
"""Will always return text key. :meta private: """ if self.return_final_only: return [self.output_key] else: return [self.output_key, 'full_generation']
@property def output_keys(self) ->List[str]: """Will always return text key. :meta private: """ if self.return_final_only: return [self.output_key] else: return [self.output_key, 'full_generation']
Will always return text key. :meta private:
delete
"""Delete by vector IDs or filter. Args: ids: List of ids to delete. filter: Dictionary of conditions to filter vectors to delete. """ if namespace is None: namespace = self._namespace if delete_all: self._index.delete(delete_all=True, namespace=namespace, **kwargs) elif ...
def delete(self, ids: Optional[List[str]]=None, delete_all: Optional[bool]= None, namespace: Optional[str]=None, filter: Optional[dict]=None, ** kwargs: Any) ->None: """Delete by vector IDs or filter. Args: ids: List of ids to delete. filter: Dictionary of conditions to filte...
Delete by vector IDs or filter. Args: ids: List of ids to delete. filter: Dictionary of conditions to filter vectors to delete.
_file_types
"""Return supported file types.""" return _FileType.DOC, _FileType.DOCX, _FileType.PDF
@property def _file_types(self) ->Sequence[_FileType]: """Return supported file types.""" return _FileType.DOC, _FileType.DOCX, _FileType.PDF
Return supported file types.
get_input_schema
"""The tool's input schema.""" if self.args_schema is not None: return self.args_schema else: return create_schema_from_function(self.name, self._run)
def get_input_schema(self, config: Optional[RunnableConfig]=None) ->Type[ BaseModel]: """The tool's input schema.""" if self.args_schema is not None: return self.args_schema else: return create_schema_from_function(self.name, self._run)
The tool's input schema.
empty_tool_input
"""Return a constant.""" return 'the empty result'
@tool def empty_tool_input() ->str: """Return a constant.""" return 'the empty result'
Return a constant.
similarity_search_with_relevance_scores
"""Perform a similarity search with StarRocks Args: query (str): query string k (int, optional): Top K neighbors to retrieve. Defaults to 4. where_str (Optional[str], optional): where condition string. Defaults to None. ...
def similarity_search_with_relevance_scores(self, query: str, k: int=4, where_str: Optional[str]=None, **kwargs: Any) ->List[Tuple[Document, float] ]: """Perform a similarity search with StarRocks Args: query (str): query string k (int, optional): Top K neighbors to retrieve...
Perform a similarity search with StarRocks Args: query (str): query string k (int, optional): Top K neighbors to retrieve. Defaults to 4. where_str (Optional[str], optional): where condition string. Defaults to None. NOTE: Please do not let end-user to fill thi...
test_visit_operation
from timescale_vector import client op = Operation(operator=Operator.AND, arguments=[Comparison(comparator= Comparator.LT, attribute='foo', value=2), Comparison(comparator= Comparator.EQ, attribute='bar', value='baz'), Comparison(comparator= Comparator.GT, attribute='abc', value=2.0)]) expected = client.Pre...
@pytest.mark.requires('timescale_vector') def test_visit_operation() ->None: from timescale_vector import client op = Operation(operator=Operator.AND, arguments=[Comparison(comparator= Comparator.LT, attribute='foo', value=2), Comparison(comparator= Comparator.EQ, attribute='bar', value='baz'), ...
null
get_api_chain
"""Fake LLM API chain for testing.""" data = get_test_api_data() test_api_docs = data['api_docs'] test_question = data['question'] test_url = data['api_url'] test_api_response = data['api_response'] test_api_summary = data['api_summary'] api_url_query_prompt = API_URL_PROMPT.format(api_docs=test_api_docs, question=...
def get_api_chain(**kwargs: Any) ->APIChain: """Fake LLM API chain for testing.""" data = get_test_api_data() test_api_docs = data['api_docs'] test_question = data['question'] test_url = data['api_url'] test_api_response = data['api_response'] test_api_summary = data['api_summary'] api_u...
Fake LLM API chain for testing.
similarity_search
"""Return MongoDB documents most similar to the given query. Uses the knnBeta Operator available in MongoDB Atlas Search. This feature is in early access and available only for evaluation purposes, to validate functionality, and to gather feedback from a small closed group of early acce...
def similarity_search(self, query: str, k: int=4, pre_filter: Optional[Dict ]=None, post_filter_pipeline: Optional[List[Dict]]=None, **kwargs: Any ) ->List[Document]: """Return MongoDB documents most similar to the given query. Uses the knnBeta Operator available in MongoDB Atlas Search. Th...
Return MongoDB documents most similar to the given query. Uses the knnBeta Operator available in MongoDB Atlas Search. This feature is in early access and available only for evaluation purposes, to validate functionality, and to gather feedback from a small closed group of early access users. It is not recommended for...
test_causal_mediator
""" Test CPAL approach on causal mediator. """ narrative_input = ( 'jan has three times the number of pets as marcia.marcia has two more pets than cindy.If marcia has ten pets, how many pets does jan have?' ) llm = OpenAI(temperature=0, max_tokens=512) cpal_chain = CPALChain.from_univariate_prom...
def test_causal_mediator(self) ->None: """ Test CPAL approach on causal mediator. """ narrative_input = ( 'jan has three times the number of pets as marcia.marcia has two more pets than cindy.If marcia has ten pets, how many pets does jan have?' ) llm = OpenAI(temperature=0, ...
Test CPAL approach on causal mediator.
test_load_returns_limited_docs
"""Test that returns several docs""" expected_docs = 2 loader = ArxivLoader(query='ChatGPT', load_max_docs=expected_docs) docs = loader.load() assert len(docs) == expected_docs assert_docs(docs)
def test_load_returns_limited_docs() ->None: """Test that returns several docs""" expected_docs = 2 loader = ArxivLoader(query='ChatGPT', load_max_docs=expected_docs) docs = loader.load() assert len(docs) == expected_docs assert_docs(docs)
Test that returns several docs
_stream
"""Call out to Titan Takeoff stream endpoint. Args: prompt: The prompt to pass into the model. stop: Optional list of stop words to use when generating. Returns: The string generated by the model. Yields: A dictionary like object containing a st...
def _stream(self, prompt: str, stop: Optional[List[str]]=None, run_manager: Optional[CallbackManagerForLLMRun]=None, **kwargs: Any) ->Iterator[ GenerationChunk]: """Call out to Titan Takeoff stream endpoint. Args: prompt: The prompt to pass into the model. stop: Optional lis...
Call out to Titan Takeoff stream endpoint. Args: prompt: The prompt to pass into the model. stop: Optional list of stop words to use when generating. Returns: The string generated by the model. Yields: A dictionary like object containing a string token. Example: .. code-block:: python p...
_identifying_params
"""Get the identifying parameters.""" return {'model_name': self.model_name, 'name': self.name, 'cpu': self.cpu, 'memory': self.memory, 'gpu': self.gpu, 'python_version': self. python_version, 'python_packages': self.python_packages, 'max_length': self.max_length, 'model_kwargs': self.model_kwargs}
@property def _identifying_params(self) ->Mapping[str, Any]: """Get the identifying parameters.""" return {'model_name': self.model_name, 'name': self.name, 'cpu': self. cpu, 'memory': self.memory, 'gpu': self.gpu, 'python_version': self .python_version, 'python_packages': self.python_packages, ...
Get the identifying parameters.
similarity_search_with_score
"""Return docs most similar to query. Args: query: Text to look up documents similar to. k: Number of Documents to return. Defaults to 4. filter (Optional[Dict[str, str]]): Filter by metadata. Defaults to None. fetch_k: (Optional[int]) Number of Documents to fetc...
def similarity_search_with_score(self, query: str, 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: query: Text to look up documents similar to. k: Number of Document...
Return docs most similar to query. Args: query: Text to look up documents similar to. k: Number of Documents to return. Defaults to 4. filter (Optional[Dict[str, str]]): Filter by metadata. Defaults to None. fetch_k: (Optional[int]) Number of Documents to fetch before filtering. Defaults ...
lazy_parse
"""Lazily parse the blob.""" import io try: from pydub import AudioSegment except ImportError: raise ImportError( 'pydub package not found, please install it with `pip install pydub`') try: import librosa except ImportError: raise ImportError( 'librosa package not found, please install i...
def lazy_parse(self, blob: Blob) ->Iterator[Document]: """Lazily parse the blob.""" import io try: from pydub import AudioSegment except ImportError: raise ImportError( 'pydub package not found, please install it with `pip install pydub`' ) try: import...
Lazily parse the blob.
test_api_request_body_from_request_body_with_schema
"""Test instantiating APIRequestBody from RequestBody with a schema.""" from openapi_pydantic import MediaType, RequestBody, Schema request_body = RequestBody(content={'application/json': MediaType(schema= Schema(type='object', properties={'foo': Schema(type='string')}))}) api_request_body = APIRequestBody.from_req...
@pytest.mark.requires('openapi_pydantic') def test_api_request_body_from_request_body_with_schema(raw_spec: OpenAPISpec ) ->None: """Test instantiating APIRequestBody from RequestBody with a schema.""" from openapi_pydantic import MediaType, RequestBody, Schema request_body = RequestBody(content={'appli...
Test instantiating APIRequestBody from RequestBody with a schema.
test_all_imports
assert set(__all__) == set(EXPECTED_ALL)
def test_all_imports() ->None: assert set(__all__) == set(EXPECTED_ALL)
null
_start_chat
if not self.is_codey_model: return self.client.start_chat(context=history.context, message_history= history.history, **kwargs) else: return self.client.start_chat(message_history=history.history, **kwargs)
def _start_chat(self, history: _ChatHistory, **kwargs: Any) ->Union[ ChatSession, CodeChatSession]: if not self.is_codey_model: return self.client.start_chat(context=history.context, message_history=history.history, **kwargs) else: return self.client.start_chat(message_history=hi...
null
test_embedding_documents
"""Test embeddings for documents.""" documents = ['foo', 'bar'] embedding = VolcanoEmbeddings() output = embedding.embed_documents(documents) assert len(output) == 2 assert len(output[0]) == 1024
def test_embedding_documents() ->None: """Test embeddings for documents.""" documents = ['foo', 'bar'] embedding = VolcanoEmbeddings() output = embedding.embed_documents(documents) assert len(output) == 2 assert len(output[0]) == 1024
Test embeddings for documents.
test_qdrant_from_texts_raises_error_on_different_dimensionality
"""Test if Qdrant.from_texts raises an exception if dimensionality does not match""" collection_name = uuid.uuid4().hex with tempfile.TemporaryDirectory() as tmpdir: vec_store = Qdrant.from_texts(['lorem', 'ipsum', 'dolor', 'sit', 'amet' ], ConsistentFakeEmbeddings(dimensionality=10), collection_name= ...
@pytest.mark.parametrize('vector_name', [None, 'custom-vector']) def test_qdrant_from_texts_raises_error_on_different_dimensionality(vector_name : Optional[str]) ->None: """Test if Qdrant.from_texts raises an exception if dimensionality does not match""" collection_name = uuid.uuid4().hex with tempfile....
Test if Qdrant.from_texts raises an exception if dimensionality does not match
test_system_message_multiple_tools
prompt: Any = StructuredChatAgent.create_prompt([Tool(name='foo', description='Test tool FOO', func=lambda x: x), Tool(name='bar', description='Test tool BAR', func=lambda x: x)]) actual = prompt.messages[0].prompt.format() expected = dedent( """ Respond to the human as helpfully and accurately ...
def test_system_message_multiple_tools(self) ->None: prompt: Any = StructuredChatAgent.create_prompt([Tool(name='foo', description='Test tool FOO', func=lambda x: x), Tool(name='bar', description='Test tool BAR', func=lambda x: x)]) actual = prompt.messages[0].prompt.format() expected = dede...
null
_insert
_insert_query = self._build_insert_sql(transac, column_names) debug_output(_insert_query) get_named_result(self.connection, _insert_query)
def _insert(self, transac: Iterable, column_names: Iterable[str]) ->None: _insert_query = self._build_insert_sql(transac, column_names) debug_output(_insert_query) get_named_result(self.connection, _insert_query)
null
visit_Call
if isinstance(node.func, ast.Attribute) and isinstance(node.func.value, ast .Name ) and node.func.value.id == self.name and node.func.attr == 'get' and len( node.args) in (1, 2) and isinstance(node.args[0], ast.Constant ) and isinstance(node.args[0].value, str): self.keys.add(node.args[0].value)
def visit_Call(self, node: ast.Call) ->Any: if isinstance(node.func, ast.Attribute) and isinstance(node.func.value, ast.Name ) and node.func.value.id == self.name and node.func.attr == 'get' and len( node.args) in (1, 2) and isinstance(node.args[0], ast.Constant ) and isinstance(node...
null
from_llm
"""Initialize from LLM.""" qa_chain = LLMChain(llm=llm, prompt=qa_prompt) entity_chain = LLMChain(llm=llm, prompt=entity_prompt) return cls(qa_chain=qa_chain, entity_extraction_chain=entity_chain, **kwargs)
@classmethod def from_llm(cls, llm: BaseLanguageModel, qa_prompt: BasePromptTemplate= GRAPH_QA_PROMPT, entity_prompt: BasePromptTemplate= ENTITY_EXTRACTION_PROMPT, **kwargs: Any) ->GraphQAChain: """Initialize from LLM.""" qa_chain = LLMChain(llm=llm, prompt=qa_prompt) entity_chain = LLMChain(llm=llm...
Initialize from LLM.
_scopes
"""Return required scopes.""" return ['offline_access', 'Files.Read.All']
@property def _scopes(self) ->List[str]: """Return required scopes.""" return ['offline_access', 'Files.Read.All']
Return required scopes.
set_text
"""Set the text attribute to be the contents of the message.""" try: values['text'] = values['message'].content except (KeyError, AttributeError) as e: raise ValueError('Error while initializing ChatGeneration') from e return values
@root_validator def set_text(cls, values: Dict[str, Any]) ->Dict[str, Any]: """Set the text attribute to be the contents of the message.""" try: values['text'] = values['message'].content except (KeyError, AttributeError) as e: raise ValueError('Error while initializing ChatGeneration') from...
Set the text attribute to be the contents of the message.
get_multi_vector_retriever
"""Create the composed retriever object.""" vectorstore = get_vectorstore() store = get_docstore() return MultiVectorRetriever(vectorstore=vectorstore, byte_store=store, id_key=docstore_id_key)
def get_multi_vector_retriever(docstore_id_key: str): """Create the composed retriever object.""" vectorstore = get_vectorstore() store = get_docstore() return MultiVectorRetriever(vectorstore=vectorstore, byte_store=store, id_key=docstore_id_key)
Create the composed retriever object.
ideation_prompt_inputs
return {'question': self.question}
def ideation_prompt_inputs(self) ->Dict[str, Any]: return {'question': self.question}
null
_import_openweathermap_tool
from langchain_community.tools.openweathermap.tool import OpenWeatherMapQueryRun return OpenWeatherMapQueryRun
def _import_openweathermap_tool() ->Any: from langchain_community.tools.openweathermap.tool import OpenWeatherMapQueryRun return OpenWeatherMapQueryRun
null
_search
from youtube_search import YoutubeSearch results = YoutubeSearch(person, num_results).to_json() data = json.loads(results) url_suffix_list = [('https://www.youtube.com' + video['url_suffix']) for video in data['videos']] return str(url_suffix_list)
def _search(self, person: str, num_results: int) ->str: from youtube_search import YoutubeSearch results = YoutubeSearch(person, num_results).to_json() data = json.loads(results) url_suffix_list = [('https://www.youtube.com' + video['url_suffix']) for video in data['videos']] return str(url_...
null
add_texts
"""Run more texts through the embeddings and add to the vectorstore. Args: texts (Iterable[str]): Texts to add to the vectorstore. metadatas (Optional[List[dict]], optional): Optional list of metadatas. ids (Optional[List[str]]): An optional list of ids. refresh(...
def add_texts(self, texts: Iterable[str], metadatas: Optional[List[dict]]= None, ids: Optional[List[str]]=None, refresh: bool=True, **kwargs: Any ) ->List[str]: """Run more texts through the embeddings and add to the vectorstore. Args: texts (Iterable[str]): Texts to add to the vectorst...
Run more texts through the embeddings and add to the vectorstore. Args: texts (Iterable[str]): Texts to add to the vectorstore. metadatas (Optional[List[dict]], optional): Optional list of metadatas. ids (Optional[List[str]]): An optional list of ids. refresh(bool): Whether or not to refresh indices wi...
_import_deepinfra
from langchain_community.llms.deepinfra import DeepInfra return DeepInfra
def _import_deepinfra() ->Any: from langchain_community.llms.deepinfra import DeepInfra return DeepInfra
null
test_output_messages
runnable = RunnableLambda(lambda input: [AIMessage(content='you said: ' + '\n'.join([str(m.content) for m in input['history'] if isinstance(m, HumanMessage)] + [input['input']]))]) get_session_history = _get_get_session_history() with_history = RunnableWithMessageHistory(runnable, get_session_history, input...
def test_output_messages() ->None: runnable = RunnableLambda(lambda input: [AIMessage(content='you said: ' + '\n'.join([str(m.content) for m in input['history'] if isinstance(m, HumanMessage)] + [input['input']]))]) get_session_history = _get_get_session_history() with_history = RunnableWith...
null
_llm_type
"""Return type of llm.""" return 'azureml_endpoint'
@property def _llm_type(self) ->str: """Return type of llm.""" return 'azureml_endpoint'
Return type of llm.
_import_usearch
from langchain_community.vectorstores.usearch import USearch return USearch
def _import_usearch() ->Any: from langchain_community.vectorstores.usearch import USearch return USearch
null
test_bad_inputs
"""Test errors are raised if input keys are not found.""" chain = FakeChain() with pytest.raises(ValueError): chain({'foobar': 'baz'})
def test_bad_inputs() ->None: """Test errors are raised if input keys are not found.""" chain = FakeChain() with pytest.raises(ValueError): chain({'foobar': 'baz'})
Test errors are raised if input keys are not found.
_parse
return text.strip('**')
def _parse(text): return text.strip('**')
null
test_yaml_output_parser_fail
"""Test YamlOutputParser where completion result fails schema validation.""" yaml_parser: YamlOutputParser[TestModel] = YamlOutputParser(pydantic_object =TestModel) try: yaml_parser.parse_folder(DEF_RESULT_FAIL) except OutputParserException as e: print('parse_result:', e) assert 'Failed to parse TestMod...
def test_yaml_output_parser_fail() ->None: """Test YamlOutputParser where completion result fails schema validation.""" yaml_parser: YamlOutputParser[TestModel] = YamlOutputParser(pydantic_object =TestModel) try: yaml_parser.parse_folder(DEF_RESULT_FAIL) except OutputParserException as e...
Test YamlOutputParser where completion result fails schema validation.
_llm_type
"""Return type of llm.""" return 'fireworks-chat'
@property def _llm_type(self) ->str: """Return type of llm.""" return 'fireworks-chat'
Return type of llm.
_embed_with_retry
response = embeddings.client.create(**kwargs) return _check_response(response, skip_empty=embeddings.skip_empty)
@retry_decorator def _embed_with_retry(**kwargs: Any) ->Any: response = embeddings.client.create(**kwargs) return _check_response(response, skip_empty=embeddings.skip_empty)
null
test_huggingfacehub_embedding_query
"""Test huggingfacehub embeddings.""" document = 'foo bar' embedding = HuggingFaceHubEmbeddings() output = embedding.embed_query(document) assert len(output) == 768
def test_huggingfacehub_embedding_query() ->None: """Test huggingfacehub embeddings.""" document = 'foo bar' embedding = HuggingFaceHubEmbeddings() output = embedding.embed_query(document) assert len(output) == 768
Test huggingfacehub embeddings.
_clean_results
cleaned_results = [] for result in raw_search_results: cleaned_results.append({'title': result.get('title', 'Unknown Title'), 'url': result.get('url', 'Unknown URL'), 'author': result.get( 'author', 'Unknown Author'), 'published_date': result.get( 'publishedDate', 'Unknown Date')}) return cl...
def _clean_results(self, raw_search_results: List[Dict]) ->List[Dict]: cleaned_results = [] for result in raw_search_results: cleaned_results.append({'title': result.get('title', 'Unknown Title'), 'url': result.get('url', 'Unknown URL'), 'author': result.get('author', 'Unknown Au...
null
test_call
"""Test that call returns a URL in the output.""" search = DallEAPIWrapper() output = search.run('volcano island') assert 'https://oaidalleapi' in output
def test_call() ->None: """Test that call returns a URL in the output.""" search = DallEAPIWrapper() output = search.run('volcano island') assert 'https://oaidalleapi' in output
Test that call returns a URL in the output.
test_atlas_with_metadatas
"""Test end to end construction and search.""" texts = ['foo', 'bar', 'baz'] metadatas = [{'page': str(i)} for i in range(len(texts))] docsearch = AtlasDB.from_texts(name='langchain_test_project' + str(time. time()), texts=texts, api_key=ATLAS_TEST_API_KEY, embedding= FakeEmbeddings(), metadatas=metadatas, rese...
def test_atlas_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 = AtlasDB.from_texts(name='langchain_test_project' + str(time .time()), texts=texts, api_key=ATLAS_TEST_API_KEY...
Test end to end construction and search.
_FormattedValue
self.write('f') string = io.StringIO() self._fstring_FormattedValue(t, string.write) self.write(repr(string.getvalue()))
def _FormattedValue(self, t): self.write('f') string = io.StringIO() self._fstring_FormattedValue(t, string.write) self.write(repr(string.getvalue()))
null
validate
""" Check and validate the safety of the given prompt text. Args: prompt_value (str): The input text to be checked for unsafe text. config (Dict[str, Any]): Configuration settings for prompt safety checks. Raises: ValueError: If unsafe prompt is found in the...
def validate(self, prompt_value: str, config: Any=None) ->str: """ Check and validate the safety of the given prompt text. Args: prompt_value (str): The input text to be checked for unsafe text. config (Dict[str, Any]): Configuration settings for prompt safety checks. ...
Check and validate the safety of the given prompt text. Args: prompt_value (str): The input text to be checked for unsafe text. config (Dict[str, Any]): Configuration settings for prompt safety checks. Raises: ValueError: If unsafe prompt is found in the prompt text based on the specified threshold. ...