method_name
stringlengths
1
78
method_body
stringlengths
3
9.66k
full_code
stringlengths
31
10.7k
docstring
stringlengths
4
4.74k
test_import_storage
"""Attempt to import storage modules.""" from langchain_community.storage.redis import RedisStore
def test_import_storage() ->None: """Attempt to import storage modules.""" from langchain_community.storage.redis import RedisStore
Attempt to import storage modules.
test_memory_with_message_store
"""Test the memory with a message store.""" message_history = MongoDBChatMessageHistory(connection_string= connection_string, session_id='test-session') memory = ConversationBufferMemory(memory_key='baz', chat_memory= message_history, return_messages=True) memory.chat_memory.add_ai_message('This is me, the AI')...
def test_memory_with_message_store() ->None: """Test the memory with a message store.""" message_history = MongoDBChatMessageHistory(connection_string= connection_string, session_id='test-session') memory = ConversationBufferMemory(memory_key='baz', chat_memory= message_history, return_messa...
Test the memory with a message store.
embed_query
return self._query([text])[0]
def embed_query(self, text: str) ->List[float]: return self._query([text])[0]
null
on_text
""" Run when agent is ending. """ self.step += 1 self.text_ctr += 1 resp = self._init_resp() resp.update({'action': 'on_text', 'text': text}) resp.update(self.get_custom_callback_meta()) self.on_text_records.append(resp) self.action_records.append(resp) if self.stream_logs: self.logger.report_text(r...
def on_text(self, text: str, **kwargs: Any) ->None: """ Run when agent is ending. """ self.step += 1 self.text_ctr += 1 resp = self._init_resp() resp.update({'action': 'on_text', 'text': text}) resp.update(self.get_custom_callback_meta()) self.on_text_records.append(resp) ...
Run when agent is ending.
from_examples
"""Create k-shot example selector using example list and embeddings. Reshuffles examples dynamically based on query similarity. Args: examples: List of examples to use in the prompt. embeddings: An initialized embedding API interface, e.g. OpenAIEmbeddings(). vector...
@classmethod def from_examples(cls, examples: List[dict], embeddings: Embeddings, vectorstore_cls: Type[VectorStore], k: int=4, input_keys: Optional[List [str]]=None, **vectorstore_cls_kwargs: Any ) ->SemanticSimilarityExampleSelector: """Create k-shot example selector using example list and embeddings....
Create k-shot example selector using example list and embeddings. Reshuffles examples dynamically based on query similarity. Args: examples: List of examples to use in the prompt. embeddings: An initialized embedding API interface, e.g. OpenAIEmbeddings(). vectorstore_cls: A vector store DB interface clas...
stream
return self.transform(iter([input]), config, **kwargs)
def stream(self, input: Dict[str, Any], config: Optional[RunnableConfig]= None, **kwargs: Any) ->Iterator[Dict[str, Any]]: return self.transform(iter([input]), config, **kwargs)
null
_load_agent_from_file
"""Load agent from file.""" valid_suffixes = {'json', 'yaml'} if isinstance(file, str): file_path = Path(file) else: file_path = file if file_path.suffix[1:] == 'json': with open(file_path) as f: config = json.load(f) elif file_path.suffix[1:] == 'yaml': with open(file_path, 'r') as f: c...
def _load_agent_from_file(file: Union[str, Path], **kwargs: Any) ->Union[ BaseSingleActionAgent, BaseMultiActionAgent]: """Load agent from file.""" valid_suffixes = {'json', 'yaml'} if isinstance(file, str): file_path = Path(file) else: file_path = file if file_path.suffix[1:] ==...
Load agent from file.
save_context
"""Save context from this session for every memory.""" for memory in self.memories: memory.save_context(inputs, outputs)
def save_context(self, inputs: Dict[str, Any], outputs: Dict[str, str]) ->None: """Save context from this session for every memory.""" for memory in self.memories: memory.save_context(inputs, outputs)
Save context from this session for every memory.
get_number_of_documents
"""Helper to see the number of documents in the index Returns: int: The number of documents """ return self._client.index(self._index_name).get_stats()['numberOfDocuments']
def get_number_of_documents(self) ->int: """Helper to see the number of documents in the index Returns: int: The number of documents """ return self._client.index(self._index_name).get_stats()['numberOfDocuments' ]
Helper to see the number of documents in the index Returns: int: The number of documents
from_texts
vs = SKLearnVectorStore(embedding, persist_path=persist_path, **kwargs) vs.add_texts(texts, metadatas=metadatas, ids=ids) return vs
@classmethod def from_texts(cls, texts: List[str], embedding: Embeddings, metadatas: Optional[List[dict]]=None, ids: Optional[List[str]]=None, persist_path: Optional[str]=None, **kwargs: Any) ->'SKLearnVectorStore': vs = SKLearnVectorStore(embedding, persist_path=persist_path, **kwargs) vs.add_texts(tex...
null
test_arcee_api_key_is_secret_string
mock_response = mock_get.return_value mock_response.status_code = 200 mock_response.json.return_value = {'model_id': '', 'status': 'training_complete'} arcee_without_env_var = Arcee(model='DALM-PubMed', arcee_api_key= 'secret_api_key', arcee_api_url='https://localhost', arcee_api_version= 'version') assert ...
@patch('langchain_community.utilities.arcee.requests.get') def test_arcee_api_key_is_secret_string(mock_get: MagicMock) ->None: mock_response = mock_get.return_value mock_response.status_code = 200 mock_response.json.return_value = {'model_id': '', 'status': 'training_complete'} arcee_without_en...
null
_import_requests_tool_RequestsDeleteTool
from langchain_community.tools.requests.tool import RequestsDeleteTool return RequestsDeleteTool
def _import_requests_tool_RequestsDeleteTool() ->Any: from langchain_community.tools.requests.tool import RequestsDeleteTool return RequestsDeleteTool
null
_call
from bs4 import BeautifulSoup _run_manager = run_manager or CallbackManagerForChainRun.get_noop_manager() other_keys = {k: v for k, v in inputs.items() if k != self.input_key} url = inputs[self.input_key] res = self.requests_wrapper.get(url) soup = BeautifulSoup(res, 'html.parser') other_keys[self.requests_key] = soup....
def _call(self, inputs: Dict[str, Any], run_manager: Optional[ CallbackManagerForChainRun]=None) ->Dict[str, Any]: from bs4 import BeautifulSoup _run_manager = run_manager or CallbackManagerForChainRun.get_noop_manager() other_keys = {k: v for k, v in inputs.items() if k != self.input_key} url = inp...
null
_stream
"""Allows streaming to model!""" inputs = self.custom_preprocess(messages) for response in self.get_stream(inputs=inputs, stop=stop, labels=labels, ** kwargs): chunk = self._get_filled_chunk(self.custom_postprocess(response)) yield chunk if run_manager: run_manager.on_llm_new_token(chunk.text, c...
def _stream(self, messages: List[BaseMessage], stop: Optional[Sequence[str] ]=None, run_manager: Optional[CallbackManagerForLLMRun]=None, labels: Optional[dict]=None, **kwargs: Any) ->Iterator[ChatGenerationChunk]: """Allows streaming to model!""" inputs = self.custom_preprocess(messages) for respon...
Allows streaming to model!
_chain_type
return 'graph_aql_chain'
@property def _chain_type(self) ->str: return 'graph_aql_chain'
null
embeddings
return self.embedding
@property def embeddings(self) ->Embeddings: return self.embedding
null
_import_aleph_alpha
from langchain_community.llms.aleph_alpha import AlephAlpha return AlephAlpha
def _import_aleph_alpha() ->Any: from langchain_community.llms.aleph_alpha import AlephAlpha return AlephAlpha
null
__call__
"""Maps the Run and Example to a dictionary.""" if not example.outputs: raise ValueError( f'Example {example.id} has no outputs to use as areference label.') return self.map(example)
def __call__(self, example: Example) ->Dict[str, str]: """Maps the Run and Example to a dictionary.""" if not example.outputs: raise ValueError( f'Example {example.id} has no outputs to use as areference label.') return self.map(example)
Maps the Run and Example to a dictionary.
_run
from langchain.output_parsers.json import parse_json_markdown try: data = parse_json_markdown(text) except json.JSONDecodeError as e: raise e response = self.requests_wrapper.post(data['url'], data['data']) response = response[:self.response_length] return self.llm_chain.predict(response=response, instructions=...
def _run(self, text: str) ->str: from langchain.output_parsers.json import parse_json_markdown try: data = parse_json_markdown(text) except json.JSONDecodeError as e: raise e response = self.requests_wrapper.post(data['url'], data['data']) response = response[:self.response_length] ...
null
parse
"""Parse the request and error tags.""" json_match = re.search('```json(.*?)```', llm_output, re.DOTALL) if json_match: return self._load_json_block(json_match.group(1).strip()) message_match = re.search('```text(.*?)```', llm_output, re.DOTALL) if message_match: return f'MESSAGE: {message_match.group(1).strip(...
def parse(self, llm_output: str) ->str: """Parse the request and error tags.""" json_match = re.search('```json(.*?)```', llm_output, re.DOTALL) if json_match: return self._load_json_block(json_match.group(1).strip()) message_match = re.search('```text(.*?)```', llm_output, re.DOTALL) if mes...
Parse the request and error tags.
_import_spark_sql_tool_QueryCheckerTool
from langchain_community.tools.spark_sql.tool import QueryCheckerTool return QueryCheckerTool
def _import_spark_sql_tool_QueryCheckerTool() ->Any: from langchain_community.tools.spark_sql.tool import QueryCheckerTool return QueryCheckerTool
null
max_marginal_relevance_search
"""Return docs selected using the maximal marginal relevance. Maximal marginal relevance optimizes for similarity to query AND diversity among selected documents. Args: query: Text to look up documents similar to. k: Number of Documents to return. Defaults to 4. ...
def max_marginal_relevance_search(self, query: str, k: int=4, fetch_k: int= 20, lambda_mult: float=0.5, text_in_page_content: Optional[str]=None, meta_filter: Optional[dict]=None, **kwargs: Any) ->List[Document]: """Return docs selected using the maximal marginal relevance. Maximal marginal relevan...
Return docs selected using the maximal marginal relevance. Maximal marginal relevance optimizes for similarity to query AND diversity among selected documents. Args: query: Text to look up documents similar to. k: Number of Documents to return. Defaults to 4. fetch_k: Number of Documents to fetch to pass ...
_is_b64
return s.startswith('data:image')
def _is_b64(s: str) ->bool: return s.startswith('data:image')
null
test_openai_model_param
llm = ChatOpenAI(model='foo') assert llm.model_name == 'foo' llm = ChatOpenAI(model_name='foo') assert llm.model_name == 'foo'
@pytest.mark.requires('openai') def test_openai_model_param() ->None: llm = ChatOpenAI(model='foo') assert llm.model_name == 'foo' llm = ChatOpenAI(model_name='foo') assert llm.model_name == 'foo'
null
_chunk
for i in range(0, len(texts), size): yield texts[i:i + size]
def _chunk(texts: List[str], size: int) ->Iterator[List[str]]: for i in range(0, len(texts), size): yield texts[i:i + size]
null
load_tools
"""Load tools based on their name. Tools allow agents to interact with various resources and services like APIs, databases, file systems, etc. Please scope the permissions of each tools to the minimum required for the application. For example, if an application only needs to read from a database,...
def load_tools(tool_names: List[str], llm: Optional[BaseLanguageModel]=None, callbacks: Callbacks=None, **kwargs: Any) ->List[BaseTool]: """Load tools based on their name. Tools allow agents to interact with various resources and services like APIs, databases, file systems, etc. Please scope the p...
Load tools based on their name. Tools allow agents to interact with various resources and services like APIs, databases, file systems, etc. Please scope the permissions of each tools to the minimum required for the application. For example, if an application only needs to read from a database, the database tool shou...
__getattr__
"""Get attr name.""" if name == 'create_pandas_dataframe_agent': HERE = Path(__file__).parents[3] here = as_import_path(Path(__file__).parent, relative_to=HERE) old_path = 'langchain.' + here + '.' + name new_path = 'langchain_experimental.' + here + '.' + name raise ImportError( f"""This ag...
def __getattr__(name: str) ->Any: """Get attr name.""" if name == 'create_pandas_dataframe_agent': HERE = Path(__file__).parents[3] here = as_import_path(Path(__file__).parent, relative_to=HERE) old_path = 'langchain.' + here + '.' + name new_path = 'langchain_experimental.' + he...
Get attr name.
ignore_llm
"""Whether to ignore LLM callbacks.""" return self.ignore_llm_
@property def ignore_llm(self) ->bool: """Whether to ignore LLM callbacks.""" return self.ignore_llm_
Whether to ignore LLM callbacks.
_chunk_to_generation
return Generation(text=chunk.text, generation_info=chunk.generation_info)
@staticmethod def _chunk_to_generation(chunk: GenerationChunk) ->Generation: return Generation(text=chunk.text, generation_info=chunk.generation_info)
null
_uuid_key
return uuid.uuid4().hex
def _uuid_key() ->str: return uuid.uuid4().hex
null
from_nasa_api_wrapper
operations: List[Dict] = [{'mode': 'search_media', 'name': 'Search NASA Image and Video Library media', 'description': NASA_SEARCH_PROMPT}, {'mode': 'get_media_metadata_manifest', 'name': 'Get NASA Image and Video Library media metadata manifest', 'description': NASA_MANIFEST_PROMPT}, {'mode': 'get_...
@classmethod def from_nasa_api_wrapper(cls, nasa_api_wrapper: NasaAPIWrapper ) ->'NasaToolkit': operations: List[Dict] = [{'mode': 'search_media', 'name': 'Search NASA Image and Video Library media', 'description': NASA_SEARCH_PROMPT}, {'mode': 'get_media_metadata_manifest', 'name': 'Get...
null
embeddings
return self._embedding
@property def embeddings(self) ->Embeddings: return self._embedding
null
similarity_search_by_vector
"""Perform a similarity search with MyScale by vectors 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 Non...
def similarity_search_by_vector(self, embedding: List[float], k: int=4, where_str: Optional[str]=None, **kwargs: Any) ->List[Document]: """Perform a similarity search with MyScale by vectors Args: query (str): query string k (int, optional): Top K neighbors to retrieve. Defaults...
Perform a similarity search with MyScale by vectors 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...
_validate_uri
if self.target_uri == 'databricks': return if urlparse(self.target_uri).scheme != 'databricks': raise ValueError( 'Invalid target URI. The target URI must be a valid databricks URI.')
def _validate_uri(self) ->None: if self.target_uri == 'databricks': return if urlparse(self.target_uri).scheme != 'databricks': raise ValueError( 'Invalid target URI. The target URI must be a valid databricks URI.' )
null
test_del
mock_session_pool.return_value = MagicMock() nebula_graph = NebulaGraph(self.space, self.username, self.password, self. address, self.port, self.session_pool_size) nebula_graph.__del__() mock_session_pool.return_value.close.assert_called_once()
@patch('nebula3.gclient.net.SessionPool.SessionPool') def test_del(self, mock_session_pool: Any) ->None: mock_session_pool.return_value = MagicMock() nebula_graph = NebulaGraph(self.space, self.username, self.password, self.address, self.port, self.session_pool_size) nebula_graph.__del__() mock_...
null
consistent_fake_sparse_encoder
""" Generates a consistent fake sparse vector. Parameters: - query (str): The query string to make the function deterministic. - size (int): The size of the vector to generate. - density (float): The density of the vector to generate. Returns: - indices (list): List of indices where the no...
def consistent_fake_sparse_encoder(query: str, size: int=100, density: float=0.7) ->Tuple[List[int], List[float]]: """ Generates a consistent fake sparse vector. Parameters: - query (str): The query string to make the function deterministic. - size (int): The size of the vector to generate. ...
Generates a consistent fake sparse vector. Parameters: - query (str): The query string to make the function deterministic. - size (int): The size of the vector to generate. - density (float): The density of the vector to generate. Returns: - indices (list): List of indices where the non-zero elements are located. - v...
_generate
params, chat, message = self._prepare_chat(messages, stop=stop) response: genai.types.GenerateContentResponse = _chat_with_retry(content= message, **params, generation_method=chat.send_message) return _response_to_result(response)
def _generate(self, messages: List[BaseMessage], stop: Optional[List[str]]= None, run_manager: Optional[CallbackManagerForLLMRun]=None, **kwargs: Any ) ->ChatResult: params, chat, message = self._prepare_chat(messages, stop=stop) response: genai.types.GenerateContentResponse = _chat_with_retry(content ...
null
parse
raise NotImplementedError( 'This OutputParser can only be called by the `parse_with_prompt` method.')
def parse(self, completion: str) ->T: raise NotImplementedError( 'This OutputParser can only be called by the `parse_with_prompt` method.' )
null
get_name
"""Get the name of the runnable.""" name = name or self.name or self.__class__.__name__ if suffix: if name[0].isupper(): return name + suffix.title() else: return name + '_' + suffix.lower() else: return name
def get_name(self, suffix: Optional[str]=None, *, name: Optional[str]=None ) ->str: """Get the name of the runnable.""" name = name or self.name or self.__class__.__name__ if suffix: if name[0].isupper(): return name + suffix.title() else: return name + '_' + suff...
Get the name of the runnable.
delete_all_indexes
client = self.client() indexes = client.get_indexes() for index in indexes['results']: task = client.index(index.uid).delete() client.wait_for_task(task.task_uid)
def delete_all_indexes(self) ->None: client = self.client() indexes = client.get_indexes() for index in indexes['results']: task = client.index(index.uid).delete() client.wait_for_task(task.task_uid)
null
_scopes
"""Return required scopes."""
@property @abstractmethod def _scopes(self) ->List[str]: """Return required scopes."""
Return required scopes.
embed_query
"""Embed query text. Args: text: The text to embed. Returns: List[float]: Embeddings for the text. """ if not self.access_token: self._refresh_access_token_with_lock() resp = self._embedding({'input': [text]}) if resp.get('error_code'): if resp.get('error_code')...
def embed_query(self, text: str) ->List[float]: """Embed query text. Args: text: The text to embed. Returns: List[float]: Embeddings for the text. """ if not self.access_token: self._refresh_access_token_with_lock() resp = self._embedding({'input': [...
Embed query text. Args: text: The text to embed. Returns: List[float]: Embeddings for the text.
embed_documents
"""Embed a list of documents using AwaEmbedding. Args: texts: The list of texts need to be embedded Returns: List of embeddings, one for each text. """ return self.client.EmbeddingBatch(texts)
def embed_documents(self, texts: List[str]) ->List[List[float]]: """Embed a list of documents using AwaEmbedding. Args: texts: The list of texts need to be embedded Returns: List of embeddings, one for each text. """ return self.client.EmbeddingBatch(texts)
Embed a list of documents using AwaEmbedding. Args: texts: The list of texts need to be embedded Returns: List of embeddings, one for each text.
_call
if self.sequential_responses: return self._get_next_response_in_sequence if self.queries is not None: return self.queries[prompt] if stop is None: return 'foo' else: return 'bar'
def _call(self, prompt: str, stop: Optional[List[str]]=None, run_manager: Optional[CallbackManagerForLLMRun]=None, **kwargs: Any) ->str: if self.sequential_responses: return self._get_next_response_in_sequence if self.queries is not None: return self.queries[prompt] if stop is None: ...
null
nlp_request
""" Make an API request to the Diffbot NLP endpoint. Args: text (str): The text to be processed. Returns: Dict[str, Any]: The JSON response from the API. """ payload = {'content': text, 'lang': 'en'} FIELDS = 'facts' HOST = 'nl.diffbot.com' url = ( f'https:/...
def nlp_request(self, text: str) ->Dict[str, Any]: """ Make an API request to the Diffbot NLP endpoint. Args: text (str): The text to be processed. Returns: Dict[str, Any]: The JSON response from the API. """ payload = {'content': text, 'lang': 'en'} ...
Make an API request to the Diffbot NLP endpoint. Args: text (str): The text to be processed. Returns: Dict[str, Any]: The JSON response from the API.
_llm_type
return 'fake-list-chat-model'
@property def _llm_type(self) ->str: return 'fake-list-chat-model'
null
test_mdelete
key_value_pairs = [('key1', b'value1'), ('key2', b'value2')] file_store.mset(key_value_pairs) file_store.mdelete(['key1']) values = file_store.mget(['key1']) assert values == [None]
def test_mdelete(file_store: LocalFileStore) ->None: key_value_pairs = [('key1', b'value1'), ('key2', b'value2')] file_store.mset(key_value_pairs) file_store.mdelete(['key1']) values = file_store.mget(['key1']) assert values == [None]
null
test_vertex_generate_code
llm = VertexAI(temperature=0.3, n=2, model_name='code-bison@001') output = llm.generate(['generate a python method that says foo:']) assert isinstance(output, LLMResult) assert len(output.generations) == 1 assert len(output.generations[0]) == 2
@pytest.mark.scheduled def test_vertex_generate_code() ->None: llm = VertexAI(temperature=0.3, n=2, model_name='code-bison@001') output = llm.generate(['generate a python method that says foo:']) assert isinstance(output, LLMResult) assert len(output.generations) == 1 assert len(output.generations[0...
null
_get_score
match = re.search('grade:\\s*(correct|incorrect)', text.strip(), re.IGNORECASE) if match: if match.group(1).upper() == 'CORRECT': return 'CORRECT', 1 elif match.group(1).upper() == 'INCORRECT': return 'INCORRECT', 0 try: first_word = text.strip().split()[0].translate(str.maketrans('', '', ...
def _get_score(text: str) ->Optional[Tuple[str, int]]: match = re.search('grade:\\s*(correct|incorrect)', text.strip(), re. IGNORECASE) if match: if match.group(1).upper() == 'CORRECT': return 'CORRECT', 1 elif match.group(1).upper() == 'INCORRECT': return 'INCORR...
null
test_functions_call
chat = QianfanChatEndpoint(model='ERNIE-Bot') prompt = ChatPromptTemplate(messages=[HumanMessage(content= "What's the temperature in Shanghai today?"), AIMessage(content='', additional_kwargs={'function_call': {'name': 'get_current_temperature', 'thoughts': 'i will use get_current_temperature to resolve...
def test_functions_call() ->None: chat = QianfanChatEndpoint(model='ERNIE-Bot') prompt = ChatPromptTemplate(messages=[HumanMessage(content= "What's the temperature in Shanghai today?"), AIMessage(content='', additional_kwargs={'function_call': {'name': 'get_current_temperature', 'thought...
null
find_replies
""" Recursively find all replies to a given parent message ID. Args: parent_id (int): The parent message ID. reply_data (pd.DataFrame): A DataFrame containing reply messages. Returns: list: A list of message IDs that are replies to th...
def find_replies(parent_id: int, reply_data: pd.DataFrame) ->List[int]: """ Recursively find all replies to a given parent message ID. Args: parent_id (int): The parent message ID. reply_data (pd.DataFrame): A DataFrame containing reply messages. ...
Recursively find all replies to a given parent message ID. Args: parent_id (int): The parent message ID. reply_data (pd.DataFrame): A DataFrame containing reply messages. Returns: list: A list of message IDs that are replies to the parent message ID.
validate_environment
"""Validate that api key and endpoint exists in environment.""" azure_cogs_key = get_from_dict_or_env(values, 'azure_cogs_key', 'AZURE_COGS_KEY') azure_cogs_region = get_from_dict_or_env(values, 'azure_cogs_region', 'AZURE_COGS_REGION') try: import azure.cognitiveservices.speech as speechsdk values['spe...
@root_validator(pre=True) def validate_environment(cls, values: Dict) ->Dict: """Validate that api key and endpoint exists in environment.""" azure_cogs_key = get_from_dict_or_env(values, 'azure_cogs_key', 'AZURE_COGS_KEY') azure_cogs_region = get_from_dict_or_env(values, 'azure_cogs_region', ...
Validate that api key and endpoint exists in environment.
test_func_call
act = json.dumps([{'action_name': 'foo', 'action': {'param': 42}}]) msg = AIMessage(content='LLM thoughts.', additional_kwargs={'function_call': {'name': 'foo', 'arguments': f'{{"actions": {act}}}'}}) result = _parse_ai_message(msg) assert isinstance(result, list) assert len(result) == 1 action = result[0] assert i...
def test_func_call(self) ->None: act = json.dumps([{'action_name': 'foo', 'action': {'param': 42}}]) msg = AIMessage(content='LLM thoughts.', additional_kwargs={ 'function_call': {'name': 'foo', 'arguments': f'{{"actions": {act}}}'}} ) result = _parse_ai_message(msg) assert isinstance(re...
null
_import_google_serper
from langchain_community.utilities.google_serper import GoogleSerperAPIWrapper return GoogleSerperAPIWrapper
def _import_google_serper() ->Any: from langchain_community.utilities.google_serper import GoogleSerperAPIWrapper return GoogleSerperAPIWrapper
null
test_parse_history
system_input = "You're supposed to answer math questions." text_question1, text_answer1 = 'How much is 2+2?', '4' text_question2 = 'How much is 3+3?' system_message = SystemMessage(content=system_input) message1 = HumanMessage(content=text_question1) message2 = AIMessage(content=text_answer1) message3 = HumanMessage(co...
def test_parse_history() ->None: system_input = "You're supposed to answer math questions." text_question1, text_answer1 = 'How much is 2+2?', '4' text_question2 = 'How much is 3+3?' system_message = SystemMessage(content=system_input) message1 = HumanMessage(content=text_question1) message2 = A...
null
test_chroma_with_relevance_score_custom_normalization_fn
"""Test searching with relevance score and custom normalization function.""" texts = ['foo', 'bar', 'baz'] metadatas = [{'page': str(i)} for i in range(len(texts))] docsearch = Chroma.from_texts(collection_name='test_collection', texts= texts, embedding=FakeEmbeddings(), metadatas=metadatas, relevance_score_fn=...
def test_chroma_with_relevance_score_custom_normalization_fn() ->None: """Test searching with relevance score and custom normalization function.""" texts = ['foo', 'bar', 'baz'] metadatas = [{'page': str(i)} for i in range(len(texts))] docsearch = Chroma.from_texts(collection_name='test_collection', tex...
Test searching with relevance score and custom normalization function.
create_prompt
additional_prompts = [] if num_plates: num_plates_str = (f'are {num_plates} plates' if num_plates > 1 else 'is 1 plate') additional_prompts.append(NUM_PLATES_PROMPT.format(num_plates_str= num_plates_str)) if num_rows: additional_prompts.append(ROWS_PROMPT.format(num_rows=num_rows)) if num_co...
def create_prompt(num_plates: Optional[int]=None, num_rows: Optional[int]= None, num_cols: Optional[int]=None) ->str: additional_prompts = [] if num_plates: num_plates_str = (f'are {num_plates} plates' if num_plates > 1 else 'is 1 plate') additional_prompts.append(NUM_PLATES_PROM...
null
test_partial_functions_json_output_parser_diff
def input_iter(_: Any) ->Iterator[AIMessageChunk]: for token in STREAMED_TOKENS: yield AIMessageChunk(content='', additional_kwargs={'function_call': {'arguments': token}}) chain = input_iter | JsonOutputFunctionsParser(diff=True) assert list(chain.stream(None)) == EXPECTED_STREAMED_JSON_DIFF
def test_partial_functions_json_output_parser_diff() ->None: def input_iter(_: Any) ->Iterator[AIMessageChunk]: for token in STREAMED_TOKENS: yield AIMessageChunk(content='', additional_kwargs={ 'function_call': {'arguments': token}}) chain = input_iter | JsonOutputFunctions...
null
_call
rellm = import_rellm() from transformers import Text2TextGenerationPipeline pipeline = cast(Text2TextGenerationPipeline, self.pipeline) text = rellm.complete_re(prompt, self.regex, tokenizer=pipeline.tokenizer, model=pipeline.model, max_new_tokens=self.max_new_tokens) if stop is not None: text = enforce_stop_to...
def _call(self, prompt: str, stop: Optional[List[str]]=None, run_manager: Optional[CallbackManagerForLLMRun]=None, **kwargs: Any) ->str: rellm = import_rellm() from transformers import Text2TextGenerationPipeline pipeline = cast(Text2TextGenerationPipeline, self.pipeline) text = rellm.complete_re(pr...
null
from_default
"""Load with default LLMChain.""" llm = OpenAI(temperature=0.5, best_of=10, n=3, max_tokens=50) return cls.from_llm(llm, objective, **kwargs)
@classmethod def from_default(cls, objective: str, **kwargs: Any) ->NatBotChain: """Load with default LLMChain.""" llm = OpenAI(temperature=0.5, best_of=10, n=3, max_tokens=50) return cls.from_llm(llm, objective, **kwargs)
Load with default LLMChain.
on_text
""" Run when agent is ending. """ self.step += 1 self.text_ctr += 1 resp: Dict[str, Any] = {} resp.update({'action': 'on_text', 'text': text}) resp.update(self.get_custom_callback_meta()) self.deck.append(self.markdown_renderer().to_html('### On Text')) self.deck.append(self.table_renderer().to_html(sel...
def on_text(self, text: str, **kwargs: Any) ->None: """ Run when agent is ending. """ self.step += 1 self.text_ctr += 1 resp: Dict[str, Any] = {} resp.update({'action': 'on_text', 'text': text}) resp.update(self.get_custom_callback_meta()) self.deck.append(self.markdown_rende...
Run when agent is ending.
similarity_search_with_score
"""Return meilisearch documents most similar to the query, along with scores. Args: query (str): Query text for which to find similar documents. k (int): Number of documents to return. Defaults to 4. filter (Optional[Dict[str, str]]): Filter by metadata. Defa...
def similarity_search_with_score(self, query: str, k: int=4, filter: Optional[Dict[str, str]]=None, **kwargs: Any) ->List[Tuple[Document, float] ]: """Return meilisearch documents most similar to the query, along with scores. Args: query (str): Query text for which to find similar docum...
Return meilisearch documents most similar to the query, along with scores. Args: query (str): Query text for which to find similar documents. k (int): Number of documents to return. Defaults to 4. filter (Optional[Dict[str, str]]): Filter by metadata. Defaults to None. Returns: List[Document]:...
test_konko_streaming_model_name_test
"""Check model info during streaming.""" chat_instance = ChatKonko(max_tokens=10, streaming=True) msg = HumanMessage(content='Hi') llm_data = chat_instance.generate([[msg]]) assert llm_data.llm_output is not None assert llm_data.llm_output['model_name'] == chat_instance.model
def test_konko_streaming_model_name_test() ->None: """Check model info during streaming.""" chat_instance = ChatKonko(max_tokens=10, streaming=True) msg = HumanMessage(content='Hi') llm_data = chat_instance.generate([[msg]]) assert llm_data.llm_output is not None assert llm_data.llm_output['mode...
Check model info during streaming.
delete
""" Delete a Redis entry. Args: ids: List of ids (keys in redis) to delete. redis_url: Redis connection url. This should be passed in the kwargs or set as an environment variable: REDIS_URL. Returns: bool: Whether or not the deletions were su...
@staticmethod def delete(ids: Optional[List[str]]=None, **kwargs: Any) ->bool: """ Delete a Redis entry. Args: ids: List of ids (keys in redis) to delete. redis_url: Redis connection url. This should be passed in the kwargs or set as an environment variable: ...
Delete a Redis entry. Args: ids: List of ids (keys in redis) to delete. redis_url: Redis connection url. This should be passed in the kwargs or set as an environment variable: REDIS_URL. Returns: bool: Whether or not the deletions were successful. Raises: ValueError: If the redis python packa...
test_table_info_w_sample_rows
"""Test that table info is constructed properly.""" engine = create_engine('sqlite:///:memory:') metadata_obj.create_all(engine) values = [{'user_id': 13, 'user_name': 'Harrison', 'user_bio': 'bio'}, { 'user_id': 14, 'user_name': 'Chase', 'user_bio': 'bio'}] stmt = insert(user).values(values) with engine.begin() as...
def test_table_info_w_sample_rows() ->None: """Test that table info is constructed properly.""" engine = create_engine('sqlite:///:memory:') metadata_obj.create_all(engine) values = [{'user_id': 13, 'user_name': 'Harrison', 'user_bio': 'bio'}, {'user_id': 14, 'user_name': 'Chase', 'user_bio': 'b...
Test that table info is constructed properly.
test_all_imports
assert set(__all__) == set(EXPECTED_ALL)
def test_all_imports() ->None: assert set(__all__) == set(EXPECTED_ALL)
null
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.
_embed_query
if query is None or query == '' or query.isspace(): return None else: return self.embedding.embed_query(query)
def _embed_query(self, query: str) ->Optional[List[float]]: if query is None or query == '' or query.isspace(): return None else: return self.embedding.embed_query(query)
null
_prep_docs
embeddings: List[List[float]] = self._embed_fn.embed_documents(list(texts)) docs: List[TigrisDocument] = [] for t, m, e, _id in itertools.zip_longest(texts, metadatas or [], embeddings or [], ids or []): doc: TigrisDocument = {'text': t, 'embeddings': e or [], 'metadata': m or {}} if _id: d...
def _prep_docs(self, texts: Iterable[str], metadatas: Optional[List[dict]], ids: Optional[List[str]]) ->List[TigrisDocument]: embeddings: List[List[float]] = self._embed_fn.embed_documents(list(texts)) docs: List[TigrisDocument] = [] for t, m, e, _id in itertools.zip_longest(texts, metadatas or [], ...
null
plan
"""Given input, decided what to do. Args: intermediate_steps: Steps the LLM has taken to date, along with observations callbacks: Callbacks to run. **kwargs: User inputs. Returns: Action specifying what tool to use. """ full_input...
def plan(self, intermediate_steps: List[Tuple[AgentAction, str]], callbacks: Callbacks=None, **kwargs: Any) ->Union[AgentAction, AgentFinish]: """Given input, decided what to do. Args: intermediate_steps: Steps the LLM has taken to date, along with observations c...
Given input, decided what to do. Args: intermediate_steps: Steps the LLM has taken to date, along with observations callbacks: Callbacks to run. **kwargs: User inputs. Returns: Action specifying what tool to use.
_import_openllm
from langchain_community.llms.openllm import OpenLLM return OpenLLM
def _import_openllm() ->Any: from langchain_community.llms.openllm import OpenLLM return OpenLLM
null
get_client_info
"""Returns a custom user agent header. Args: module (Optional[str]): Optional. The module for a custom user agent header. Returns: google.api_core.gapic_v1.client_info.ClientInfo """ try: from google.api_core.gapic_v1.client_info import ClientInfo except ImportError as exc: ...
def get_client_info(module: Optional[str]=None) ->'ClientInfo': """Returns a custom user agent header. Args: module (Optional[str]): Optional. The module for a custom user agent header. Returns: google.api_core.gapic_v1.client_info.ClientInfo """ try: from google...
Returns a custom user agent header. Args: module (Optional[str]): Optional. The module for a custom user agent header. Returns: google.api_core.gapic_v1.client_info.ClientInfo
_llm_type
"""Return type of LLM.""" return 'nvidia-trt-llm'
@property def _llm_type(self) ->str: """Return type of LLM.""" return 'nvidia-trt-llm'
Return type of LLM.
stream
if type(self)._stream == BaseChatModel._stream: yield cast(BaseMessageChunk, self.invoke(input, config=config, stop= stop, **kwargs)) else: config = ensure_config(config) messages = self._convert_input(input).to_messages() params = self._get_invocation_params(stop=stop, **kwargs) options = {...
def stream(self, input: LanguageModelInput, config: Optional[RunnableConfig ]=None, *, stop: Optional[List[str]]=None, **kwargs: Any) ->Iterator[ BaseMessageChunk]: if type(self)._stream == BaseChatModel._stream: yield cast(BaseMessageChunk, self.invoke(input, config=config, stop =stop, ...
null
get_access_code_url
"""Get the URL to get an access code.""" url = f'https://app.clickup.com/api?client_id={oauth_client_id}' return f'{url}&redirect_uri={redirect_uri}'
@classmethod def get_access_code_url(cls, oauth_client_id: str, redirect_uri: str= 'https://google.com') ->str: """Get the URL to get an access code.""" url = f'https://app.clickup.com/api?client_id={oauth_client_id}' return f'{url}&redirect_uri={redirect_uri}'
Get the URL to get an access code.
test_jinachat_generate
"""Test JinaChat wrapper with generate.""" chat = JinaChat(max_tokens=10) 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) == 1 fo...
def test_jinachat_generate() ->None: """Test JinaChat wrapper with generate.""" chat = JinaChat(max_tokens=10) message = HumanMessage(content='Hello') response = chat.generate([[message], [message]]) assert isinstance(response, LLMResult) assert len(response.generations) == 2 for generations...
Test JinaChat wrapper with generate.
__hash__
return hash((self.id, tuple(self.options.keys()), self.default))
def __hash__(self) ->int: return hash((self.id, tuple(self.options.keys()), self.default))
null
get_user_agent
from langchain_community import __version__ return f'langchain/{__version__}'
@staticmethod def get_user_agent() ->str: from langchain_community import __version__ return f'langchain/{__version__}'
null
from_llm
"""Get the response parser.""" task_creation_template = ( 'You are an task creation AI that uses the result of an execution agent to create new tasks with the following objective: {objective}, The last completed task has the result: {result}. This result was based on this task description: {task_description}. These...
@classmethod def from_llm(cls, llm: BaseLanguageModel, verbose: bool=True) ->LLMChain: """Get the response parser.""" task_creation_template = ( 'You are an task creation AI that uses the result of an execution agent to create new tasks with the following objective: {objective}, The last completed task ...
Get the response parser.
_get_connection
try: import singlestoredb as s2 except ImportError: raise ImportError( 'Could not import singlestoredb python package. Please install it with `pip install singlestoredb`.' ) return s2.connect(**self.connection_kwargs)
def _get_connection(self) ->Any: try: import singlestoredb as s2 except ImportError: raise ImportError( 'Could not import singlestoredb python package. Please install it with `pip install singlestoredb`.' ) return s2.connect(**self.connection_kwargs)
null
_prepare_output
"""Prepare the output.""" parsed = result[self.output_key] if RUN_KEY in result: parsed[RUN_KEY] = result[RUN_KEY] return parsed
def _prepare_output(self, result: dict) ->dict: """Prepare the output.""" parsed = result[self.output_key] if RUN_KEY in result: parsed[RUN_KEY] = result[RUN_KEY] return parsed
Prepare the output.
test_each_simple
"""Test that each() works with a simple runnable.""" parser = FakeSplitIntoListParser() assert parser.invoke('first item, second item') == ['first item', 'second item' ] assert parser.map().invoke(['a, b', 'c']) == [['a', 'b'], ['c']] assert parser.map().map().invoke([['a, b', 'c'], ['c, e']]) == [[['a', 'b'], ...
def test_each_simple() ->None: """Test that each() works with a simple runnable.""" parser = FakeSplitIntoListParser() assert parser.invoke('first item, second item') == ['first item', 'second item'] assert parser.map().invoke(['a, b', 'c']) == [['a', 'b'], ['c']] assert parser.map().map().i...
Test that each() works with a simple runnable.
submit
"""Submit a function to the executor. Args: func (Callable[..., T]): The function to submit. *args (Any): The positional arguments to the function. **kwargs (Any): The keyword arguments to the function. Returns: Future[T]: The future for the function. ...
def submit(self, func: Callable[P, T], *args: P.args, **kwargs: P.kwargs ) ->Future[T]: """Submit a function to the executor. Args: func (Callable[..., T]): The function to submit. *args (Any): The positional arguments to the function. **kwargs (Any): The keyword arg...
Submit a function to the executor. Args: func (Callable[..., T]): The function to submit. *args (Any): The positional arguments to the function. **kwargs (Any): The keyword arguments to the function. Returns: Future[T]: The future for the function.
clean_pdf
"""Clean the PDF file. Args: contents: a PDF file contents. Returns: """ contents = '\n'.join([line for line in contents.split('\n') if not line. startswith('![]')]) contents = contents.replace('\\section{', '# ').replace('}', '') contents = contents.replace('\\$', '$').replac...
def clean_pdf(self, contents: str) ->str: """Clean the PDF file. Args: contents: a PDF file contents. Returns: """ contents = '\n'.join([line for line in contents.split('\n') if not line .startswith('![]')]) contents = contents.replace('\\section{', '# ').repla...
Clean the PDF file. Args: contents: a PDF file contents. Returns:
_get_google_jobs
return GoogleJobsQueryRun(api_wrapper=GoogleJobsAPIWrapper(**kwargs))
def _get_google_jobs(**kwargs: Any) ->BaseTool: return GoogleJobsQueryRun(api_wrapper=GoogleJobsAPIWrapper(**kwargs))
null
get_tools
"""Get the tools in the toolkit.""" return self.tools
def get_tools(self) ->List[BaseTool]: """Get the tools in the toolkit.""" return self.tools
Get the tools in the toolkit.
is_lc_serializable
return False
@classmethod def is_lc_serializable(cls) ->bool: return False
null
__init__
super().__init__() try: from trubrics import Trubrics except ImportError: raise ImportError( 'The TrubricsCallbackHandler requires installation of the trubrics package. Please install it with `pip install trubrics`.' ) self.trubrics = Trubrics(project=project, email=email or os.environ[ 'TRU...
def __init__(self, project: str='default', email: Optional[str]=None, password: Optional[str]=None, **kwargs: Any) ->None: super().__init__() try: from trubrics import Trubrics except ImportError: raise ImportError( 'The TrubricsCallbackHandler requires installation of the tr...
null
get_arangodb_client
"""Get the Arango DB client from credentials. Args: url: Arango DB url. Can be passed in as named arg or set as environment var ``ARANGODB_URL``. Defaults to "http://localhost:8529". dbname: Arango DB name. Can be passed in as named arg or set as environment var ``ARANGODB_D...
def get_arangodb_client(url: Optional[str]=None, dbname: Optional[str]=None, username: Optional[str]=None, password: Optional[str]=None) ->Any: """Get the Arango DB client from credentials. Args: url: Arango DB url. Can be passed in as named arg or set as environment var ``ARANGODB_URL`...
Get the Arango DB client from credentials. Args: url: Arango DB url. Can be passed in as named arg or set as environment var ``ARANGODB_URL``. Defaults to "http://localhost:8529". dbname: Arango DB name. Can be passed in as named arg or set as environment var ``ARANGODB_DBNAME``. Defaults to "_...
test_complex_question
"""Test complex question that should need python.""" question = 'What is the square root of 2?' output = fake_llm_math_chain.run(question) assert output == f'Answer: {2 ** 0.5}'
@pytest.mark.requires('numexpr') def test_complex_question(fake_llm_math_chain: LLMMathChain) ->None: """Test complex question that should need python.""" question = 'What is the square root of 2?' output = fake_llm_math_chain.run(question) assert output == f'Answer: {2 ** 0.5}'
Test complex question that should need python.
_get_run_type
if isinstance(run.run_type, str): return run.run_type elif hasattr(run.run_type, 'value'): return run.run_type.value else: return str(run.run_type)
def _get_run_type(run: 'Run') ->str: if isinstance(run.run_type, str): return run.run_type elif hasattr(run.run_type, 'value'): return run.run_type.value else: return str(run.run_type)
null
pending
return self.status == 'pending'
def pending(self) ->bool: return self.status == 'pending'
null
test_api_key_is_secret_string
llm = ChatJavelinAIGateway(gateway_uri='<javelin-ai-gateway-uri>', route= '<javelin-ai-gateway-chat-route>', javelin_api_key='secret-api-key', params={'temperature': 0.1}) assert isinstance(llm.javelin_api_key, SecretStr) assert llm.javelin_api_key.get_secret_value() == 'secret-api-key'
@pytest.mark.requires('javelin_sdk') def test_api_key_is_secret_string() ->None: llm = ChatJavelinAIGateway(gateway_uri='<javelin-ai-gateway-uri>', route='<javelin-ai-gateway-chat-route>', javelin_api_key= 'secret-api-key', params={'temperature': 0.1}) assert isinstance(llm.javelin_api_key, Secr...
null
add_documents
doc_ids = [str(uuid.uuid4()) for _ in doc_contents] summary_docs = [Document(page_content=s, metadata={id_key: doc_ids[i]}) for i, s in enumerate(doc_summaries)] retriever.vectorstore.add_documents(summary_docs) retriever.docstore.mset(list(zip(doc_ids, doc_contents)))
def add_documents(retriever, doc_summaries, doc_contents): doc_ids = [str(uuid.uuid4()) for _ in doc_contents] summary_docs = [Document(page_content=s, metadata={id_key: doc_ids[i]}) for i, s in enumerate(doc_summaries)] retriever.vectorstore.add_documents(summary_docs) retriever.docstore.mset(l...
null
test_replicate_input
llm = Replicate(model=TEST_MODEL, input={'max_length': 10}) assert llm.model_kwargs == {'max_length': 10}
def test_replicate_input() ->None: llm = Replicate(model=TEST_MODEL, input={'max_length': 10}) assert llm.model_kwargs == {'max_length': 10}
null
_get_dimension_values
"""Makes a call to Cube's REST API load endpoint to retrieve values for dimensions. These values can be used to achieve a more accurate filtering. """ logger.info('Loading dimension values for: {dimension_name}...') headers = {'Content-Type': 'application/json', 'Authorization': self. cube_...
def _get_dimension_values(self, dimension_name: str) ->List[str]: """Makes a call to Cube's REST API load endpoint to retrieve values for dimensions. These values can be used to achieve a more accurate filtering. """ logger.info('Loading dimension values for: {dimension_name}...') h...
Makes a call to Cube's REST API load endpoint to retrieve values for dimensions. These values can be used to achieve a more accurate filtering.
input_mapper
return [HumanMessage(content=d['some_input'])]
def input_mapper(d: dict) ->List[BaseMessage]: return [HumanMessage(content=d['some_input'])]
null
_import_portkey
from langchain_community.utilities.portkey import Portkey return Portkey
def _import_portkey() ->Any: from langchain_community.utilities.portkey import Portkey return Portkey
null
_stream
generation_config = kwargs.get('generation_config', {}) if stop: generation_config['stop_sequences'] = stop for stream_resp in _completion_with_retry(self, prompt, stream=True, is_gemini=True, run_manager=run_manager, generation_config= generation_config, **kwargs): chunk = GenerationChunk(text=stream_r...
def _stream(self, prompt: str, stop: Optional[List[str]]=None, run_manager: Optional[CallbackManagerForLLMRun]=None, **kwargs: Any) ->Iterator[ GenerationChunk]: generation_config = kwargs.get('generation_config', {}) if stop: generation_config['stop_sequences'] = stop for stream_resp in _co...
null
_default_params
"""Get the default parameters for calling Ollama.""" return {'model': self.model, 'format': self.format, 'options': {'mirostat': self.mirostat, 'mirostat_eta': self.mirostat_eta, 'mirostat_tau': self. mirostat_tau, 'num_ctx': self.num_ctx, 'num_gpu': self.num_gpu, 'num_thread': self.num_thread, 'repeat_last...
@property def _default_params(self) ->Dict[str, Any]: """Get the default parameters for calling Ollama.""" return {'model': self.model, 'format': self.format, 'options': { 'mirostat': self.mirostat, 'mirostat_eta': self.mirostat_eta, 'mirostat_tau': self.mirostat_tau, 'num_ctx': self.num_ctx, ...
Get the default parameters for calling Ollama.