method_name
stringlengths
1
78
method_body
stringlengths
3
9.66k
full_code
stringlengths
31
10.7k
docstring
stringlengths
4
4.74k
json
return self.json_data
def json(self) ->Dict: return self.json_data
null
_parse_generation
if self.prompt.output_parser is not None: return [self.prompt.output_parser.parse_folder(res[self.output_key]) for res in generation] else: return generation
def _parse_generation(self, generation: List[Dict[str, str]]) ->Sequence[Union [str, List[str], Dict[str, str]]]: if self.prompt.output_parser is not None: return [self.prompt.output_parser.parse_folder(res[self.output_key] ) for res in generation] else: return generation
null
normalize
"""Normalize vectors to unit length.""" x /= np.clip(np.linalg.norm(x, axis=-1, keepdims=True), 1e-12, None) return x
def normalize(x: np.ndarray) ->np.ndarray: """Normalize vectors to unit length.""" x /= np.clip(np.linalg.norm(x, axis=-1, keepdims=True), 1e-12, None) return x
Normalize vectors to unit length.
_persist_run
pass
def _persist_run(self, run: Run) ->None: pass
null
test_visit_structured_query
query = 'What is the capital of France?' structured_query = StructuredQuery(query=query, filter=None) expected: Tuple[str, Dict] = (query, {}) actual = DEFAULT_TRANSLATOR.visit_structured_query(structured_query) assert expected == actual comp = Comparison(comparator=Comparator.LT, attribute='foo', value=['1', '2']) str...
def test_visit_structured_query() ->None: query = 'What is the capital of France?' structured_query = StructuredQuery(query=query, filter=None) expected: Tuple[str, Dict] = (query, {}) actual = DEFAULT_TRANSLATOR.visit_structured_query(structured_query) assert expected == actual comp = Compariso...
null
_import_docarray_inmemory
from langchain_community.vectorstores.docarray import DocArrayInMemorySearch return DocArrayInMemorySearch
def _import_docarray_inmemory() ->Any: from langchain_community.vectorstores.docarray import DocArrayInMemorySearch return DocArrayInMemorySearch
null
from_llm
"""Load QA Eval Chain from LLM. Args: llm (BaseLanguageModel): the base language model to use. prompt (PromptTemplate): A prompt template containing the input_variables: 'input', 'answer' and 'result' that will be used as the prompt for evaluation. D...
@classmethod def from_llm(cls, llm: BaseLanguageModel, prompt: Optional[PromptTemplate]= None, **kwargs: Any) ->QAEvalChain: """Load QA Eval Chain from LLM. Args: llm (BaseLanguageModel): the base language model to use. prompt (PromptTemplate): A prompt template containing the ...
Load QA Eval Chain from LLM. Args: llm (BaseLanguageModel): the base language model to use. prompt (PromptTemplate): A prompt template containing the input_variables: 'input', 'answer' and 'result' that will be used as the prompt for evaluation. Defaults to PROMPT. **kwargs: additional keywor...
_prepare_input
"""Prepare the input for the chain. Args: prediction (str): The output string from the first model. prediction_b (str): The output string from the second model. input (str, optional): The input or task string. reference (str, optional): The reference string, if a...
def _prepare_input(self, prediction: str, input: Optional[str], reference: Optional[str]) ->dict: """Prepare the input for the chain. Args: prediction (str): The output string from the first model. prediction_b (str): The output string from the second model. input (s...
Prepare the input for the chain. Args: prediction (str): The output string from the first model. prediction_b (str): The output string from the second model. input (str, optional): The input or task string. reference (str, optional): The reference string, if any. Returns: dict: The prepared input ...
test_shell_tool_init
placeholder = PlaceholderProcess() shell_tool = ShellTool(process=placeholder) assert shell_tool.name == 'terminal' assert isinstance(shell_tool.description, str) assert shell_tool.args_schema == ShellInput assert shell_tool.process is not None
def test_shell_tool_init() ->None: placeholder = PlaceholderProcess() shell_tool = ShellTool(process=placeholder) assert shell_tool.name == 'terminal' assert isinstance(shell_tool.description, str) assert shell_tool.args_schema == ShellInput assert shell_tool.process is not None
null
_run
raise self.exception
def _run(self) ->str: raise self.exception
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
embed_documents
"""Return simple embeddings.""" return [([float(1.0)] * (ADA_TOKEN_COUNT - 1) + [float(i)]) for i in range( len(texts))]
def embed_documents(self, texts: List[str]) ->List[List[float]]: """Return simple embeddings.""" return [([float(1.0)] * (ADA_TOKEN_COUNT - 1) + [float(i)]) for i in range(len(texts))]
Return simple embeddings.
similarity_search_by_vector
"""Return docs most similar to embedding vector. Args: embedding: Embedding to look up documents similar to. k: Number of Documents to return. Defaults to 4. metadata: Optional, metadata filter Returns: List of Documents most similar to the query vector....
def similarity_search_by_vector(self, embedding: List[float], k: int=4, metadata: Optional[Dict[str, Any]]=None, **kwargs: Any) ->List[Document]: """Return docs most similar to embedding vector. Args: embedding: Embedding to look up documents similar to. k: Number of Documents t...
Return docs most similar to embedding vector. Args: embedding: Embedding to look up documents similar to. k: Number of Documents to return. Defaults to 4. metadata: Optional, metadata filter Returns: List of Documents most similar to the query vector.
lazy_load
"""Lazily load weather data for the given locations.""" for place in self.places: metadata = {'queried_at': datetime.now()} content = self.client.run(place) yield Document(page_content=content, metadata=metadata)
def lazy_load(self) ->Iterator[Document]: """Lazily load weather data for the given locations.""" for place in self.places: metadata = {'queried_at': datetime.now()} content = self.client.run(place) yield Document(page_content=content, metadata=metadata)
Lazily load weather data for the given locations.
mock_lakefs_client_no_presign_not_local
with patch('langchain_community.document_loaders.lakefs.LakeFSClient' ) as mock_lakefs_client: mock_lakefs_client.return_value.ls_objects.return_value = [( 'path_bla.txt', 'https://physical_address_bla')] mock_lakefs_client.return_value.is_presign_supported.return_value = False yield mock_lakefs...
@pytest.fixture def mock_lakefs_client_no_presign_not_local() ->Any: with patch('langchain_community.document_loaders.lakefs.LakeFSClient' ) as mock_lakefs_client: mock_lakefs_client.return_value.ls_objects.return_value = [( 'path_bla.txt', 'https://physical_address_bla')] (mock_...
null
similarity_search_with_score_by_vector
"""Return docs most similar to embedding vector. Args: embedding: Embedding vector to look up documents similar to. k: Number of Documents to return. Defaults to 4. filter: Filter by metadata. Defaults to None. search_params: Additional search params ...
def similarity_search_with_score_by_vector(self, embedding: List[float], k: int=4, filter: Optional[MetadataFilter]=None, search_params: Optional[ common_types.SearchParams]=None, offset: int=0, score_threshold: Optional[float]=None, consistency: Optional[common_types. ReadConsistency]=None, **kwargs: A...
Return docs most similar to embedding vector. Args: embedding: Embedding vector to look up documents similar to. k: Number of Documents to return. Defaults to 4. filter: Filter by metadata. Defaults to None. search_params: Additional search params offset: Offset of the first result to retur...
download_audio_from_url
"""Download audio from url to local.""" ext = audio_url.split('.')[-1] response = requests.get(audio_url, stream=True) response.raise_for_status() with tempfile.NamedTemporaryFile(mode='wb', suffix=f'.{ext}', delete=False ) as f: for chunk in response.iter_content(chunk_size=8192): f.write(chunk) return...
def download_audio_from_url(audio_url: str) ->str: """Download audio from url to local.""" ext = audio_url.split('.')[-1] response = requests.get(audio_url, stream=True) response.raise_for_status() with tempfile.NamedTemporaryFile(mode='wb', suffix=f'.{ext}', delete=False ) as f: for...
Download audio from url to local.
lazy_load
if self.global_id and 'GL' in self.global_id: for d in self._load_pdf(): yield d elif self.global_id and 'SD' in self.global_id: for d in self._load_structured_doc(): yield d elif self.global_id and self.global_id[0:2] in ['FL', 'NB']: for d in self._load_folder_tree(): yield d else:...
def lazy_load(self) ->Iterator[Document]: if self.global_id and 'GL' in self.global_id: for d in self._load_pdf(): yield d elif self.global_id and 'SD' in self.global_id: for d in self._load_structured_doc(): yield d elif self.global_id and self.global_id[0:2] in ['FL...
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.
_create_empty_doc
"""Creates or replaces a document for this message history with no messages""" self.client.Documents.add_documents(collection=self.collection, workspace= self.workspace, data=[{'_id': self.session_id, self.messages_key: []}])
def _create_empty_doc(self) ->None: """Creates or replaces a document for this message history with no messages""" self.client.Documents.add_documents(collection=self.collection, workspace=self.workspace, data=[{'_id': self.session_id, self. messages_key: []}])
Creates or replaces a document for this message history with no messages
create_schema
"""Create the database schema.""" if isinstance(self.engine, AsyncEngine): raise AssertionError('This method is not supported for async engines.') Base.metadata.create_all(self.engine)
def create_schema(self) ->None: """Create the database schema.""" if isinstance(self.engine, AsyncEngine): raise AssertionError('This method is not supported for async engines.') Base.metadata.create_all(self.engine)
Create the database schema.
_validate_example_inputs_for_language_model
if input_mapper: prompt_input = input_mapper(first_example.inputs) if not isinstance(prompt_input, str) and not (isinstance(prompt_input, list) and all(isinstance(msg, BaseMessage) for msg in prompt_input)): raise InputFormatError( f"""When using an input_mapper to prepare dataset ex...
def _validate_example_inputs_for_language_model(first_example: Example, input_mapper: Optional[Callable[[Dict], Any]]) ->None: if input_mapper: prompt_input = input_mapper(first_example.inputs) if not isinstance(prompt_input, str) and not (isinstance( prompt_input, list) and all(isin...
null
format
if self.auto_embed: return self.format_auto_embed_on(event) else: return self.format_auto_embed_off(event)
def format(self, event: PickBestEvent) ->str: if self.auto_embed: return self.format_auto_embed_on(event) else: return self.format_auto_embed_off(event)
null
_is_visible
return not any(part.startswith('.') for part in path.parts)
@staticmethod def _is_visible(path: Path) ->bool: return not any(part.startswith('.') for part in path.parts)
null
_bes_vector_store
index_name = kwargs.get('index_name') if index_name is None: raise ValueError('Please provide an index_name.') bes_url = kwargs.get('bes_url') if bes_url is None: raise ValueError('Please provided a valid bes connection url') return BESVectorStore(embedding=embedding, **kwargs)
@staticmethod def _bes_vector_store(embedding: Optional[Embeddings]=None, **kwargs: Any ) ->'BESVectorStore': index_name = kwargs.get('index_name') if index_name is None: raise ValueError('Please provide an index_name.') bes_url = kwargs.get('bes_url') if bes_url is None: raise Value...
null
test_run_success_all_meta
api_client.load_all_available_meta = True responses.add(responses.POST, api_client.outline_instance_url + api_client. outline_search_endpoint, json=OUTLINE_SUCCESS_RESPONSE, status=200) docs = api_client.run('Testing') assert_docs(docs, all_meta=True)
@responses.activate def test_run_success_all_meta(api_client: OutlineAPIWrapper) ->None: api_client.load_all_available_meta = True responses.add(responses.POST, api_client.outline_instance_url + api_client.outline_search_endpoint, json=OUTLINE_SUCCESS_RESPONSE, status=200) docs = api_client....
null
time_weighted_retriever
vectorstore = MockVectorStore() return TimeWeightedVectorStoreRetriever(vectorstore=vectorstore, memory_stream=_get_example_memories())
@pytest.fixture def time_weighted_retriever() ->TimeWeightedVectorStoreRetriever: vectorstore = MockVectorStore() return TimeWeightedVectorStoreRetriever(vectorstore=vectorstore, memory_stream=_get_example_memories())
null
_import_elastic_vector_search
from langchain_community.vectorstores.elastic_vector_search import ElasticVectorSearch return ElasticVectorSearch
def _import_elastic_vector_search() ->Any: from langchain_community.vectorstores.elastic_vector_search import ElasticVectorSearch return ElasticVectorSearch
null
_If
self.fill('if ') self.dispatch(t.test) self.enter() self.dispatch(t.body) self.leave() while t.orelse and len(t.orelse) == 1 and isinstance(t.orelse[0], ast.If): t = t.orelse[0] self.fill('elif ') self.dispatch(t.test) self.enter() self.dispatch(t.body) self.leave() if t.orelse: self.fill('e...
def _If(self, t): self.fill('if ') self.dispatch(t.test) self.enter() self.dispatch(t.body) self.leave() while t.orelse and len(t.orelse) == 1 and isinstance(t.orelse[0], ast.If): t = t.orelse[0] self.fill('elif ') self.dispatch(t.test) self.enter() self.d...
null
test_bedrock_invoke
"""Test invoke tokens from BedrockChat.""" result = chat.invoke("I'm Pickle Rick", config=dict(tags=['foo'])) assert isinstance(result.content, str)
@pytest.mark.scheduled def test_bedrock_invoke(chat: BedrockChat) ->None: """Test invoke tokens from BedrockChat.""" result = chat.invoke("I'm Pickle Rick", config=dict(tags=['foo'])) assert isinstance(result.content, str)
Test invoke tokens from BedrockChat.
_import_baidu_qianfan_endpoint
from langchain_community.llms.baidu_qianfan_endpoint import QianfanLLMEndpoint return QianfanLLMEndpoint
def _import_baidu_qianfan_endpoint() ->Any: from langchain_community.llms.baidu_qianfan_endpoint import QianfanLLMEndpoint return QianfanLLMEndpoint
null
_identifying_params
return {'model_name': self.model}
@property def _identifying_params(self) ->Dict[str, Any]: return {'model_name': self.model}
null
test_infer_variables
messages = [HumanMessagePromptTemplate.from_template('{foo}')] prompt = ChatPromptTemplate(messages=messages) assert prompt.input_variables == ['foo']
def test_infer_variables() ->None: messages = [HumanMessagePromptTemplate.from_template('{foo}')] prompt = ChatPromptTemplate(messages=messages) assert prompt.input_variables == ['foo']
null
memory_variables
"""Will always return list of memory variables. :meta private: """ return [self.memory_key]
@property def memory_variables(self) ->List[str]: """Will always return list of memory variables. :meta private: """ return [self.memory_key]
Will always return list of memory variables. :meta private:
test_dereference_refs_no_refs
schema = {'type': 'object', 'properties': {'first_name': {'type': 'string'}}} actual = dereference_refs(schema) assert actual == schema
def test_dereference_refs_no_refs() ->None: schema = {'type': 'object', 'properties': {'first_name': {'type': 'string'}}} actual = dereference_refs(schema) assert actual == schema
null
_prepare_request
return {'url': 'https://www.searchapi.io/api/v1/search', 'headers': { 'Authorization': f'Bearer {self.searchapi_api_key}'}, 'params': { 'engine': self.engine, 'q': query, **{key: value for key, value in kwargs.items() if value is not None}}}
def _prepare_request(self, query: str, **kwargs: Any) ->dict: return {'url': 'https://www.searchapi.io/api/v1/search', 'headers': { 'Authorization': f'Bearer {self.searchapi_api_key}'}, 'params': { 'engine': self.engine, 'q': query, **{key: value for key, value in kwargs.items() if value is ...
null
test_ip
"""Test inner product distance.""" docsearch = Redis.from_texts(texts, FakeEmbeddings(), redis_url= TEST_REDIS_URL, vector_schema=ip_schema) output = docsearch.similarity_search_with_score('far', k=2) _, score = output[1] assert score == IP_SCORE assert drop(docsearch.index_name)
def test_ip(texts: List[str]) ->None: """Test inner product distance.""" docsearch = Redis.from_texts(texts, FakeEmbeddings(), redis_url= TEST_REDIS_URL, vector_schema=ip_schema) output = docsearch.similarity_search_with_score('far', k=2) _, score = output[1] assert score == IP_SCORE ass...
Test inner product distance.
create_qa_with_structure_chain
"""Create a question answering chain that returns an answer with sources based on schema. Args: llm: Language model to use for the chain. schema: Pydantic schema to use for the output. output_parser: Output parser to use. Should be one of `pydantic` or `base`. Default to `b...
def create_qa_with_structure_chain(llm: BaseLanguageModel, schema: Union[ dict, Type[BaseModel]], output_parser: str='base', prompt: Optional[ Union[PromptTemplate, ChatPromptTemplate]]=None, verbose: bool=False ) ->LLMChain: """Create a question answering chain that returns an answer with sources ...
Create a question answering chain that returns an answer with sources based on schema. Args: llm: Language model to use for the chain. schema: Pydantic schema to use for the output. output_parser: Output parser to use. Should be one of `pydantic` or `base`. Default to `base`. prompt: Optional ...
stream
return self.transform(iter([input]), config, **kwargs)
def stream(self, input: Other, config: Optional[RunnableConfig]=None, ** kwargs: Any) ->Iterator[Other]: return self.transform(iter([input]), config, **kwargs)
null
embed_query
"""Embed query text.""" raise NotImplementedError
def embed_query(self, text: str) ->List[float]: """Embed query text.""" raise NotImplementedError
Embed query text.
on_retriever_start
self.on_retriever_start_common()
def on_retriever_start(self, *args: Any, **kwargs: Any) ->Any: self.on_retriever_start_common()
null
_get_builtin_translator
"""Get the translator class corresponding to the vector store class.""" BUILTIN_TRANSLATORS: Dict[Type[VectorStore], Type[Visitor]] = {Pinecone: PineconeTranslator, Chroma: ChromaTranslator, DashVector: DashvectorTranslator, Weaviate: WeaviateTranslator, Vectara: VectaraTranslator, Qdrant: QdrantTranslator,...
def _get_builtin_translator(vectorstore: VectorStore) ->Visitor: """Get the translator class corresponding to the vector store class.""" BUILTIN_TRANSLATORS: Dict[Type[VectorStore], Type[Visitor]] = {Pinecone: PineconeTranslator, Chroma: ChromaTranslator, DashVector: DashvectorTranslator, Weavia...
Get the translator class corresponding to the vector store class.
test_serialize_llmchain_chat
llm = ChatOpenAI(model='davinci', temperature=0.5, openai_api_key='hello') prompt = ChatPromptTemplate.from_messages([HumanMessagePromptTemplate. from_template('hello {name}!')]) chain = LLMChain(llm=llm, prompt=prompt) assert dumps(chain, pretty=True) == snapshot import os has_env = 'OPENAI_API_KEY' in os.environ ...
@pytest.mark.requires('openai') def test_serialize_llmchain_chat(snapshot: Any) ->None: llm = ChatOpenAI(model='davinci', temperature=0.5, openai_api_key='hello') prompt = ChatPromptTemplate.from_messages([HumanMessagePromptTemplate. from_template('hello {name}!')]) chain = LLMChain(llm=llm, prompt=...
null
test_parse_case_matched_and_final_answer
llm_output = """I can use the `foo` tool to achieve the goal. Action: ```json { "action": "Final Answer", "action_input": "This is the final answer" } ``` """ output, log = get_action_and_input(llm_output) assert output == 'This is the final answer' assert log == llm_output
def test_parse_case_matched_and_final_answer() ->None: llm_output = """I can use the `foo` tool to achieve the goal. Action: ```json { "action": "Final Answer", "action_input": "This is the final answer" } ``` """ output, log = get_action_and_input(llm_output) assert out...
null
_wait
"""Wait for a response from API after an initial response is made.""" i = 1 while response.status_code == 202: request_id = response.headers.get('NVCF-REQID', '') response = session.get(self.fetch_url_format + request_id, headers=self .headers['call']) if response.status_code == 202: try: ...
def _wait(self, response: Response, session: Any) ->Response: """Wait for a response from API after an initial response is made.""" i = 1 while response.status_code == 202: request_id = response.headers.get('NVCF-REQID', '') response = session.get(self.fetch_url_format + request_id, headers=...
Wait for a response from API after an initial response is made.
on_tool_start
self.on_tool_start_common()
def on_tool_start(self, *args: Any, **kwargs: Any) ->Any: self.on_tool_start_common()
null
get_custom_callback_meta
return {'step': self.step, 'starts': self.starts, 'ends': self.ends, 'errors': self.errors, 'text_ctr': self.text_ctr, 'chain_starts': self. chain_starts, 'chain_ends': self.chain_ends, 'llm_starts': self. llm_starts, 'llm_ends': self.llm_ends, 'llm_streams': self.llm_streams, 'tool_starts': self.tool_s...
def get_custom_callback_meta(self) ->Dict[str, Any]: return {'step': self.step, 'starts': self.starts, 'ends': self.ends, 'errors': self.errors, 'text_ctr': self.text_ctr, 'chain_starts': self.chain_starts, 'chain_ends': self.chain_ends, 'llm_starts': self.llm_starts, 'llm_ends': self.llm_en...
null
_call
try: import openllm except ImportError as e: raise ImportError( "Could not import openllm. Make sure to install it with 'pip install openllm'." ) from e copied = copy.deepcopy(self.llm_kwargs) copied.update(kwargs) config = openllm.AutoConfig.for_model(self._identifying_params['model_name' ]...
def _call(self, prompt: str, stop: Optional[List[str]]=None, run_manager: Optional[CallbackManagerForLLMRun]=None, **kwargs: Any) ->str: try: import openllm except ImportError as e: raise ImportError( "Could not import openllm. Make sure to install it with 'pip install openllm'."...
null
_identifying_params
return self._default_params
@property def _identifying_params(self) ->Mapping[str, Any]: return self._default_params
null
authenticate
"""Authenticate using the AIN Blockchain""" try: from ain.ain import Ain except ImportError as e: raise ImportError( 'Cannot import ain-py related modules. Please install the package with `pip install ain-py`.' ) from e if network == 'mainnet': provider_url = 'https://mainnet-api.ainetwork.a...
def authenticate(network: Optional[Literal['mainnet', 'testnet']]='testnet' ) ->Ain: """Authenticate using the AIN Blockchain""" try: from ain.ain import Ain except ImportError as e: raise ImportError( 'Cannot import ain-py related modules. Please install the package with `pi...
Authenticate using the AIN Blockchain
_url
return f'https://api.deepinfra.com/v1/inference/{self.model_id}'
def _url(self) ->str: return f'https://api.deepinfra.com/v1/inference/{self.model_id}'
null
test_saving_loading_llm
"""Test saving/loading an promptlayer OpenAPI LLM.""" llm = PromptLayerOpenAIChat(max_tokens=10) llm.save(file_path=tmp_path / 'openai.yaml') loaded_llm = load_llm(tmp_path / 'openai.yaml') assert loaded_llm == llm
def test_saving_loading_llm(tmp_path: Path) ->None: """Test saving/loading an promptlayer OpenAPI LLM.""" llm = PromptLayerOpenAIChat(max_tokens=10) llm.save(file_path=tmp_path / 'openai.yaml') loaded_llm = load_llm(tmp_path / 'openai.yaml') assert loaded_llm == llm
Test saving/loading an promptlayer OpenAPI LLM.
visit_structured_query
try: from qdrant_client.http import models as rest except ImportError as e: raise ImportError( 'Cannot import qdrant_client. Please install with `pip install qdrant-client`.' ) from e if structured_query.filter is None: kwargs = {} else: filter = structured_query.filter.accept(self) ...
def visit_structured_query(self, structured_query: StructuredQuery) ->Tuple[ str, dict]: try: from qdrant_client.http import models as rest except ImportError as e: raise ImportError( 'Cannot import qdrant_client. Please install with `pip install qdrant-client`.' ) fr...
null
__init__
"""Initialize with necessary components.""" self.embedding = embedding self.embedding_function = embedding.embed_query self.index_uri = index_uri self.metric = metric self.config = config tiledb_vs, tiledb = dependable_tiledb_import() with tiledb.scope_ctx(ctx_or_config=config): index_group = tiledb.Group(self.inde...
def __init__(self, embedding: Embeddings, index_uri: str, metric: str, *, vector_index_uri: str='', docs_array_uri: str='', config: Optional[ Mapping[str, Any]]=None, timestamp: Any=None, **kwargs: Any): """Initialize with necessary components.""" self.embedding = embedding self.embedding_function =...
Initialize with necessary components.
lc_secrets
return {'replicate_api_token': 'REPLICATE_API_TOKEN'}
@property def lc_secrets(self) ->Dict[str, str]: return {'replicate_api_token': 'REPLICATE_API_TOKEN'}
null
resolve_criteria
"""Resolve the criteria to evaluate. Parameters ---------- criteria : CRITERIA_TYPE The criteria to evaluate the runs against. It can be: - a mapping of a criterion name to its description - a single criterion name present in one of the default criteria - a si...
def resolve_criteria(criteria: Optional[Union[CRITERIA_TYPE, str]]) ->Dict[ str, str]: """Resolve the criteria to evaluate. Parameters ---------- criteria : CRITERIA_TYPE The criteria to evaluate the runs against. It can be: - a mapping of a criterion name to its description ...
Resolve the criteria to evaluate. Parameters ---------- criteria : CRITERIA_TYPE The criteria to evaluate the runs against. It can be: - a mapping of a criterion name to its description - a single criterion name present in one of the default criteria - a single `ConstitutionalPrinciple` ...
test_ideation
responses = ['Idea 1', 'Idea 2', 'Idea 3'] llm = FakeListLLM(responses=responses) prompt = PromptTemplate(input_variables=['product'], template= 'What is a good name for a company that makes {product}?') chain = SmartLLMChain(llm=llm, prompt=prompt) prompt_value, _ = chain.prep_prompts({'product': 'socks'}) chain.h...
def test_ideation() ->None: responses = ['Idea 1', 'Idea 2', 'Idea 3'] llm = FakeListLLM(responses=responses) prompt = PromptTemplate(input_variables=['product'], template= 'What is a good name for a company that makes {product}?') chain = SmartLLMChain(llm=llm, prompt=prompt) prompt_value, ...
null
test_chroma_mmr_by_vector
"""Test end to end construction and search.""" texts = ['foo', 'bar', 'baz'] embeddings = FakeEmbeddings() docsearch = Chroma.from_texts(collection_name='test_collection', texts= texts, embedding=embeddings) embedded_query = embeddings.embed_query('foo') output = docsearch.max_marginal_relevance_search_by_vector(em...
def test_chroma_mmr_by_vector() ->None: """Test end to end construction and search.""" texts = ['foo', 'bar', 'baz'] embeddings = FakeEmbeddings() docsearch = Chroma.from_texts(collection_name='test_collection', texts= texts, embedding=embeddings) embedded_query = embeddings.embed_query('foo...
Test end to end construction and search.
step
"""Take step."""
@abstractmethod def step(self, inputs: dict, callbacks: Callbacks=None, **kwargs: Any ) ->StepResponse: """Take step."""
Take step.
_get_mock_confluence_loader
confluence_loader = ConfluenceLoader(self.CONFLUENCE_URL, username=self. MOCK_USERNAME, api_key=self.MOCK_API_TOKEN) confluence_loader.confluence = mock_confluence return confluence_loader
def _get_mock_confluence_loader(self, mock_confluence: MagicMock ) ->ConfluenceLoader: confluence_loader = ConfluenceLoader(self.CONFLUENCE_URL, username=self .MOCK_USERNAME, api_key=self.MOCK_API_TOKEN) confluence_loader.confluence = mock_confluence return confluence_loader
null
_get_functions_multi_prompt
if suffix is not None: suffix_to_use = suffix if include_df_in_prompt: dfs_head = '\n\n'.join([d.head(number_of_head_rows).to_markdown() for d in dfs]) suffix_to_use = suffix_to_use.format(dfs_head=dfs_head) elif include_df_in_prompt: dfs_head = '\n\n'.join([d.head(number_of_head...
def _get_functions_multi_prompt(dfs: Any, prefix: Optional[str]=None, suffix: Optional[str]=None, include_df_in_prompt: Optional[bool]=True, number_of_head_rows: int=5) ->Tuple[BasePromptTemplate, List[ PythonAstREPLTool]]: if suffix is not None: suffix_to_use = suffix if include_df_in_p...
null
test_non_zero_distance_pairwise
eval_chain = PairwiseStringDistanceEvalChain(distance=distance) prediction = 'I like to eat apples.' reference = 'I like apples.' result = eval_chain.evaluate_string_pairs(prediction=prediction, prediction_b=reference) assert 'score' in result assert 0 < result['score'] < 1.0
@pytest.mark.requires('rapidfuzz') @pytest.mark.parametrize('distance', valid_distances) def test_non_zero_distance_pairwise(distance: StringDistance) ->None: eval_chain = PairwiseStringDistanceEvalChain(distance=distance) prediction = 'I like to eat apples.' reference = 'I like apples.' result = eval_c...
null
validate_environment
"""Validate that api key and python package exists in environment.""" huggingfacehub_api_token = get_from_dict_or_env(values, 'huggingfacehub_api_token', 'HUGGINGFACEHUB_API_TOKEN') try: from huggingface_hub.inference_api import InferenceApi repo_id = values['repo_id'] client = InferenceApi(repo_id=repo...
@root_validator() def validate_environment(cls, values: Dict) ->Dict: """Validate that api key and python package exists in environment.""" huggingfacehub_api_token = get_from_dict_or_env(values, 'huggingfacehub_api_token', 'HUGGINGFACEHUB_API_TOKEN') try: from huggingface_hub.inference_api ...
Validate that api key and python package exists in environment.
validate_environment
"""Validate that api key and python package exists in environment.""" octoai_api_token = get_from_dict_or_env(values, 'octoai_api_token', 'OCTOAI_API_TOKEN') values['endpoint_url'] = get_from_dict_or_env(values, 'endpoint_url', 'ENDPOINT_URL') values['octoai_api_token'] = octoai_api_token return values
@root_validator(allow_reuse=True) def validate_environment(cls, values: Dict) ->Dict: """Validate that api key and python package exists in environment.""" octoai_api_token = get_from_dict_or_env(values, 'octoai_api_token', 'OCTOAI_API_TOKEN') values['endpoint_url'] = get_from_dict_or_env(values, 'e...
Validate that api key and python package exists in environment.
test_load_returns_list_of_documents
import xorbits.pandas as pd data = {'text': ['Hello', 'World'], 'author': ['Alice', 'Bob'], 'date': [ '2022-01-01', '2022-01-02']} loader = XorbitsLoader(pd.DataFrame(data)) docs = loader.load() assert isinstance(docs, list) assert all(isinstance(doc, Document) for doc in docs) assert len(docs) == 2
@pytest.mark.skipif(not xorbits_installed, reason='xorbits not installed') def test_load_returns_list_of_documents() ->None: import xorbits.pandas as pd data = {'text': ['Hello', 'World'], 'author': ['Alice', 'Bob'], 'date': ['2022-01-01', '2022-01-02']} loader = XorbitsLoader(pd.DataFrame(data)) ...
null
test_llamacpp_embedding_query
"""Test llamacpp embeddings.""" document = 'foo bar' model_path = get_model() embedding = LlamaCppEmbeddings(model_path=model_path) output = embedding.embed_query(document) assert len(output) == 512
def test_llamacpp_embedding_query() ->None: """Test llamacpp embeddings.""" document = 'foo bar' model_path = get_model() embedding = LlamaCppEmbeddings(model_path=model_path) output = embedding.embed_query(document) assert len(output) == 512
Test llamacpp embeddings.
_attribute_to_filter_field
if attribute in [tf.name for tf in self._schema.text]: return RedisText(attribute) elif attribute in [tf.name for tf in self._schema.tag or []]: return RedisTag(attribute) elif attribute in [tf.name for tf in self._schema.numeric or []]: return RedisNum(attribute) else: raise ValueError( f"""Inv...
def _attribute_to_filter_field(self, attribute: str) ->RedisFilterField: if attribute in [tf.name for tf in self._schema.text]: return RedisText(attribute) elif attribute in [tf.name for tf in self._schema.tag or []]: return RedisTag(attribute) elif attribute in [tf.name for tf in self._sche...
null
_create_retry_decorator
""" Creates and returns a preconfigured tenacity retry decorator. The retry decorator is configured to handle specific Google API exceptions such as ResourceExhausted and ServiceUnavailable. It uses an exponential backoff strategy for retries. Returns: Callable[[Any], Any]: A retry decorat...
def _create_retry_decorator() ->Callable[[Any], Any]: """ Creates and returns a preconfigured tenacity retry decorator. The retry decorator is configured to handle specific Google API exceptions such as ResourceExhausted and ServiceUnavailable. It uses an exponential backoff strategy for retries. ...
Creates and returns a preconfigured tenacity retry decorator. The retry decorator is configured to handle specific Google API exceptions such as ResourceExhausted and ServiceUnavailable. It uses an exponential backoff strategy for retries. Returns: Callable[[Any], Any]: A retry decorator configured for handling s...
texts_metadatas_as_zep_documents
from zep_python.document import Document as ZepDocument return [ZepDocument(content='Test Document', metadata={'key': 'value'}) for _ in range(2)]
@pytest.fixture def texts_metadatas_as_zep_documents() ->List['ZepDocument']: from zep_python.document import Document as ZepDocument return [ZepDocument(content='Test Document', metadata={'key': 'value'}) for _ in range(2)]
null
_generate
if self.streaming: generation: Optional[GenerationChunk] = None for chunk in self._stream(prompts[0], stop, run_manager, **kwargs): if generation is None: generation = chunk else: generation += chunk assert generation is not None return LLMResult(generations=[[gen...
def _generate(self, prompts: List[str], stop: Optional[List[str]]=None, run_manager: Optional[CallbackManagerForLLMRun]=None, **kwargs: Any ) ->LLMResult: if self.streaming: generation: Optional[GenerationChunk] = None for chunk in self._stream(prompts[0], stop, run_manager, **kwargs): ...
null
_get_invocation_params
"""Get the parameters used to invoke the model.""" return {'model': self.model, **super()._get_invocation_params(stop=stop), **self._default_params, **kwargs}
def _get_invocation_params(self, stop: Optional[List[str]]=None, **kwargs: Any ) ->Dict[str, Any]: """Get the parameters used to invoke the model.""" return {'model': self.model, **super()._get_invocation_params(stop=stop ), **self._default_params, **kwargs}
Get the parameters used to invoke the model.
on_llm_start
"""Start a trace for an LLM run.""" parent_run_id_ = str(parent_run_id) if parent_run_id else None execution_order = self._get_execution_order(parent_run_id_) start_time = datetime.now(timezone.utc) if metadata: kwargs.update({'metadata': metadata}) llm_run = Run(id=run_id, parent_run_id=parent_run_id, serialized=s...
def on_llm_start(self, serialized: Dict[str, Any], prompts: List[str], *, run_id: UUID, tags: Optional[List[str]]=None, parent_run_id: Optional[ UUID]=None, metadata: Optional[Dict[str, Any]]=None, name: Optional[str ]=None, **kwargs: Any) ->Run: """Start a trace for an LLM run.""" parent_run_id_ = ...
Start a trace for an LLM run.
from_llm
llm_chain = LLMChain(llm=llm, prompt=prompt) if selection_scorer is SENTINEL: selection_scorer = base.AutoSelectionScorer(llm=llm_chain.llm) return PickBest(llm_chain=llm_chain, prompt=prompt, selection_scorer= selection_scorer, **kwargs)
@classmethod def from_llm(cls: Type[PickBest], llm: BaseLanguageModel, prompt: BasePromptTemplate, selection_scorer: Union[base.AutoSelectionScorer, object]=SENTINEL, **kwargs: Any) ->PickBest: llm_chain = LLMChain(llm=llm, prompt=prompt) if selection_scorer is SENTINEL: selection_scorer = base....
null
test_api_key_masked_when_passed_via_constructor
"""Test initialization with an API key provided via the initializer""" llm = Anyscale(anyscale_api_key='secret-api-key', anyscale_api_base='test', model_name='test') print(llm.anyscale_api_key, end='') captured = capsys.readouterr() assert captured.out == '**********'
@pytest.mark.requires('openai') def test_api_key_masked_when_passed_via_constructor(capsys: CaptureFixture ) ->None: """Test initialization with an API key provided via the initializer""" llm = Anyscale(anyscale_api_key='secret-api-key', anyscale_api_base= 'test', model_name='test') print(llm.an...
Test initialization with an API key provided via the initializer
_call
if self.be_correct: return {'bar': 'baz'} else: return {'baz': 'bar'}
def _call(self, inputs: Dict[str, str], run_manager: Optional[ CallbackManagerForChainRun]=None) ->Dict[str, str]: if self.be_correct: return {'bar': 'baz'} else: return {'baz': 'bar'}
null
_call
"""Call out to an AzureML Managed Online 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. Example: .. code-block:: python ...
def _call(self, prompt: str, stop: Optional[List[str]]=None, run_manager: Optional[CallbackManagerForLLMRun]=None, **kwargs: Any) ->str: """Call out to an AzureML Managed Online endpoint. Args: prompt: The prompt to pass into the model. stop: Optional list of stop words to use wh...
Call out to an AzureML Managed Online 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. Example: .. code-block:: python response = azureml_model("Tell me a joke.")
config_specs
return self.bound.config_specs
@property def config_specs(self) ->List[ConfigurableFieldSpec]: return self.bound.config_specs
null
test_awadb_with_metadatas
"""Test end to end construction and search.""" texts = ['foo', 'bar', 'baz'] metadatas = [{'page': i} for i in range(len(texts))] docsearch = AwaDB.from_texts(table_name='test_awadb', texts=texts, embedding=FakeEmbeddings(), metadatas=metadatas) output = docsearch.similarity_search('foo', k=1) assert output == [Doc...
def test_awadb_with_metadatas() ->None: """Test end to end construction and search.""" texts = ['foo', 'bar', 'baz'] metadatas = [{'page': i} for i in range(len(texts))] docsearch = AwaDB.from_texts(table_name='test_awadb', texts=texts, embedding=FakeEmbeddings(), metadatas=metadatas) output...
Test end to end construction and search.
load
"""Eagerly load the content.""" return list(self.lazy_load())
def load(self) ->List[Document]: """Eagerly load the content.""" return list(self.lazy_load())
Eagerly load the content.
lazy_load
ps = list(Path(self.file_path).glob('**/*.md')) for p in ps: with open(p, encoding=self.encoding) as f: text = f.read() front_matter = self._parse_front_matter(text) text = self._remove_front_matter(text) text = self._process_acreom_content(text) metadata = {'source': str(p.name), 'path': st...
def lazy_load(self) ->Iterator[Document]: ps = list(Path(self.file_path).glob('**/*.md')) for p in ps: with open(p, encoding=self.encoding) as f: text = f.read() front_matter = self._parse_front_matter(text) text = self._remove_front_matter(text) text = self._process_...
null
always_verbose
"""Whether to call verbose callbacks even if verbose is False.""" return True
@property def always_verbose(self) ->bool: """Whether to call verbose callbacks even if verbose is False.""" return True
Whether to call verbose callbacks even if verbose is False.
_identifying_params
"""Get the identifying parameters.""" return {'model': self.model, 'model_type': self.model_type, 'model_file': self.model_file, 'config': self.config}
@property def _identifying_params(self) ->Dict[str, Any]: """Get the identifying parameters.""" return {'model': self.model, 'model_type': self.model_type, 'model_file': self.model_file, 'config': self.config}
Get the identifying parameters.
test_chat_openai_generate
"""Test AzureChatOpenAI wrapper with generate.""" chat = _get_llm(max_tokens=10, n=2) message = HumanMessage(content='Hello') response = chat.generate([[message], [message]]) assert isinstance(response, LLMResult) assert len(response.generations) == 2 for generations in response.generations: assert len(generations)...
@pytest.mark.scheduled def test_chat_openai_generate() ->None: """Test AzureChatOpenAI wrapper with generate.""" chat = _get_llm(max_tokens=10, n=2) message = HumanMessage(content='Hello') response = chat.generate([[message], [message]]) assert isinstance(response, LLMResult) assert len(response...
Test AzureChatOpenAI wrapper with generate.
test_forefrontai_api_key_is_secret_string
"""Test that the API key is stored as a SecretStr.""" llm = ForefrontAI(forefrontai_api_key='secret-api-key', temperature=0.2) assert isinstance(llm.forefrontai_api_key, SecretStr)
def test_forefrontai_api_key_is_secret_string() ->None: """Test that the API key is stored as a SecretStr.""" llm = ForefrontAI(forefrontai_api_key='secret-api-key', temperature=0.2) assert isinstance(llm.forefrontai_api_key, SecretStr)
Test that the API key is stored as a SecretStr.
is_lc_serializable
"""Return whether this model can be serialized by Langchain.""" return True
@classmethod def is_lc_serializable(cls) ->bool: """Return whether this model can be serialized by Langchain.""" return True
Return whether this model can be serialized by Langchain.
clear
"""Clear memory contents."""
def clear(self) ->None: """Clear memory contents."""
Clear memory contents.
on_chain_error
"""Run when chain errors.""" self.step += 1 self.errors += 1
def on_chain_error(self, error: BaseException, **kwargs: Any) ->None: """Run when chain errors.""" self.step += 1 self.errors += 1
Run when chain errors.
_get_relevant_documents
from pinecone_text.hybrid import hybrid_convex_scale sparse_vec = self.sparse_encoder.encode_queries(query) dense_vec = self.embeddings.embed_query(query) dense_vec, sparse_vec = hybrid_convex_scale(dense_vec, sparse_vec, self.alpha) sparse_vec['values'] = [float(s1) for s1 in sparse_vec['values']] result = self.index....
def _get_relevant_documents(self, query: str, *, run_manager: CallbackManagerForRetrieverRun) ->List[Document]: from pinecone_text.hybrid import hybrid_convex_scale sparse_vec = self.sparse_encoder.encode_queries(query) dense_vec = self.embeddings.embed_query(query) dense_vec, sparse_vec = hybrid_co...
null
_get_functions_prompt_and_tools
try: import pandas as pd pd.set_option('display.max_columns', None) except ImportError: raise ImportError( 'pandas package not found, please install with `pip install pandas`') if input_variables is not None: raise ValueError('`input_variables` is not supported at the moment.') if include_df_in_...
def _get_functions_prompt_and_tools(df: Any, prefix: Optional[str]=None, suffix: Optional[str]=None, input_variables: Optional[List[str]]=None, include_df_in_prompt: Optional[bool]=True, number_of_head_rows: int=5 ) ->Tuple[BasePromptTemplate, List[PythonAstREPLTool]]: try: import pandas as pd ...
null
clear
def scan_and_delete(cursor: int) ->int: cursor, keys_to_delete = self.redis_client.scan(cursor, f'{self.full_key_prefix}:*') self.redis_client.delete(*keys_to_delete) return cursor cursor = scan_and_delete(0) while cursor != 0: scan_and_delete(cursor)
def clear(self) ->None: def scan_and_delete(cursor: int) ->int: cursor, keys_to_delete = self.redis_client.scan(cursor, f'{self.full_key_prefix}:*') self.redis_client.delete(*keys_to_delete) return cursor cursor = scan_and_delete(0) while cursor != 0: scan_and_de...
null
_invocation_params
params = self._default_params if self.stop_sequences is not None and stop_sequences is not None: raise ValueError('`stop` found in both the input and default params.') elif self.stop_sequences is not None: params['stop'] = self.stop_sequences else: params['stop'] = stop_sequences if self.model_kwargs: p...
def _invocation_params(self, stop_sequences: Optional[List[str]], **kwargs: Any ) ->dict: params = self._default_params if self.stop_sequences is not None and stop_sequences is not None: raise ValueError('`stop` found in both the input and default params.') elif self.stop_sequences is not None: ...
null
on_tool_end
"""Run when tool ends running.""" self.step += 1 self.tool_ends += 1 self.ends += 1 resp: Dict[str, Any] = {} resp.update({'action': 'on_tool_end', 'output': output}) resp.update(self.get_custom_callback_meta()) self.deck.append(self.markdown_renderer().to_html('### Tool End')) self.deck.append(self.table_renderer().to...
def on_tool_end(self, output: str, **kwargs: Any) ->None: """Run when tool ends running.""" self.step += 1 self.tool_ends += 1 self.ends += 1 resp: Dict[str, Any] = {} resp.update({'action': 'on_tool_end', 'output': output}) resp.update(self.get_custom_callback_meta()) self.deck.append(s...
Run when tool ends running.
load
return list(self.lazy_load())
def load(self) ->List[Document]: return list(self.lazy_load())
null
_import_clarifai
from langchain_community.llms.clarifai import Clarifai return Clarifai
def _import_clarifai() ->Any: from langchain_community.llms.clarifai import Clarifai return Clarifai
null
_load_documents_from_folder
"""Load documents from a folder.""" from googleapiclient.discovery import build creds = self._load_credentials() service = build('drive', 'v3', credentials=creds) files = self._fetch_files_recursive(service, folder_id) if file_types: _files = [f for f in files if f['mimeType'] in file_types] else: _files = file...
def _load_documents_from_folder(self, folder_id: str, *, file_types: Optional[Sequence[str]]=None) ->List[Document]: """Load documents from a folder.""" from googleapiclient.discovery import build creds = self._load_credentials() service = build('drive', 'v3', credentials=creds) files = self._fe...
Load documents from a folder.
__init__
self.inputs = inputs self.selected = selected
def __init__(self, inputs: Dict[str, Any], selected: Optional[TSelected]=None): self.inputs = inputs self.selected = selected
null
get_parameters_for_operation
"""Get the components for a given operation.""" from openapi_pydantic import Reference parameters = [] if operation.parameters: for parameter in operation.parameters: if isinstance(parameter, Reference): parameter = self._get_root_referenced_parameter(parameter) parameters.append(paramet...
def get_parameters_for_operation(self, operation: Operation) ->List[Parameter]: """Get the components for a given operation.""" from openapi_pydantic import Reference parameters = [] if operation.parameters: for parameter in operation.parameters: if isinstance(parameter, Reference): ...
Get the components for a given operation.
_response_json
"""Use requests to run request to DataForSEO SERP API and return results.""" request_details = self._prepare_request(url) response = requests.post(request_details['url'], headers=request_details[ 'headers'], json=request_details['data']) response.raise_for_status() return self._check_response(response.json())
def _response_json(self, url: str) ->dict: """Use requests to run request to DataForSEO SERP API and return results.""" request_details = self._prepare_request(url) response = requests.post(request_details['url'], headers= request_details['headers'], json=request_details['data']) response.raise_...
Use requests to run request to DataForSEO SERP API and return results.
test_forefrontai_call
"""Test valid call to forefrontai.""" llm = ForefrontAI(length=10) output = llm('Say foo:') assert isinstance(output, str)
def test_forefrontai_call() ->None: """Test valid call to forefrontai.""" llm = ForefrontAI(length=10) output = llm('Say foo:') assert isinstance(output, str)
Test valid call to forefrontai.
ignore_agent
"""Whether to ignore agent callbacks.""" return self.ignore_agent_
@property def ignore_agent(self) ->bool: """Whether to ignore agent callbacks.""" return self.ignore_agent_
Whether to ignore agent callbacks.