viruPVS/langchain-docs huggingface dataset:
langchain_community.tools.gitlab.tool
langchain_community.tools.gmail.base
langchain_community.tools.gmail.create_draft
langchain_community.tools.gmail.get_message
langchain_community.tools.gmail.get_thread
langchain_community.tools.gmail.search
langchain_community.tools.gmail.send_message
langchain_community.tools.gmail.utils
langchain_community.tools.golden_query.tool
langchain_community.tools.google_cloud.texttospeech
langchain_community.tools.google_finance.tool
langchain_community.tools.google_jobs.tool
langchain_community.tools.google_lens.tool
langchain_community.tools.google_places.tool
langchain_community.tools.google_scholar.tool
langchain_community.tools.google_search.tool
langchain_community.tools.google_serper.tool
langchain_community.tools.google_trends.tool
langchain_community.tools.graphql.tool
langchain_community.tools.human.tool
langchain_community.tools.ifttt
langchain_community.tools.interaction.tool
langchain_community.tools.jira.tool
langchain_community.tools.json.tool
langchain_community.tools.memorize.tool
langchain_community.tools.merriam_webster.tool
langchain_community.tools.metaphor_search.tool
langchain_community.tools.multion.close_session
langchain_community.tools.multion.create_session
langchain_community.tools.multion.update_session
langchain_community.tools.nasa.tool
langchain_community.tools.nuclia.tool
langchain_community.tools.office365.base
langchain_community.tools.office365.create_draft_message
langchain_community.tools.office365.events_search
langchain_community.tools.office365.messages_search
langchain_community.tools.office365.send_event
langchain_community.tools.office365.send_message
langchain_community.tools.office365.utils
langchain_community.tools.openapi.utils.api_models
langchain_community.tools.openweathermap.tool
langchain_community.tools.playwright.base
langchain_community.tools.playwright.click
langchain_community.tools.playwright.current_page
langchain_community.tools.playwright.extract_hyperlinks
langchain_community.tools.playwright.extract_text
langchain_community.tools.playwright.extract_hyperlinks
langchain_community.tools.playwright.extract_text
langchain_community.tools.playwright.get_elements
langchain_community.tools.playwright.navigate
langchain_community.tools.playwright.navigate_back
langchain_community.tools.playwright.utils
langchain_community.tools.plugin
langchain_community.tools.powerbi.tool
langchain_community.tools.pubmed.tool
langchain_community.tools.reddit_search.tool
langchain_community.tools.render
langchain_community.tools.requests.tool
langchain_community.tools.scenexplain.tool
langchain_community.tools.searchapi.tool
langchain_community.tools.searx_search.tool
langchain_community.tools.shell.tool
langchain_community.tools.slack.base
langchain_community.tools.slack.get_channel
langchain_community.tools.slack.get_message
langchain_community.tools.slack.schedule_message
langchain_community.tools.slack.send_message
langchain_community.tools.slack.utils
langchain_community.tools.sleep.tool
langchain_community.tools.spark_sql.tool
langchain_community.tools.sql_database.tool
langchain_community.tools.stackexchange.tool
langchain_community.tools.steam.tool
langchain_community.tools.steamship_image_generation.tool
langchain_community.tools.steamship_image_generation.utils
langchain_community.tools.tavily_search.tool
langchain_community.tools.vectorstore.tool
langchain_community.tools.wikipedia.tool
langchain_community.tools.wolfram_alpha.tool
langchain_community.tools.yahoo_finance_news
langchain_community.tools.youtube.search
langchain_community.tools.zapier.tool
langchain_community.utilities.alpha_vantage
langchain_community.utilities.anthropic
langchain_community.utilities.apify
langchain_community.utilities.arcee
langchain_community.utilities.arxiv
langchain_community.utilities.awslambda
langchain_community.utilities.bibtex
langchain_community.utilities.bing_search
langchain_community.utilities.brave_search
langchain_community.utilities.clickup
langchain_community.utilities.dalle_image_generator
langchain_community.utilities.dataforseo_api_search
langchain_community.utilities.dalle_image_generator
langchain_community.utilities.dataforseo_api_search
langchain_community.utilities.duckduckgo_search
langchain_community.utilities.github
langchain_community.utilities.gitlab
langchain_community.utilities.golden_query
langchain_community.utilities.google_finance
langchain_community.utilities.google_jobs
langchain_community.utilities.google_lens
langchain_community.utilities.google_places_api
langchain_community.utilities.google_scholar
langchain_community.utilities.google_search
langchain_community.utilities.google_serper
langchain_community.utilities.google_trends
langchain_community.utilities.graphql
langchain_community.utilities.jira
langchain_community.utilities.max_compute
langchain_community.utilities.merriam_webster
langchain_community.utilities.metaphor_search
langchain_community.utilities.nasa
langchain_community.utilities.opaqueprompts
langchain_community.utilities.openapi
langchain_community.utilities.openweathermap
langchain_community.utilities.outline
langchain_community.utilities.portkey
langchain_community.utilities.powerbi
langchain_community.utilities.pubmed
langchain_community.utilities.python
langchain_community.utilities.reddit_search
langchain_community.utilities.redis
langchain_community.utilities.requests
langchain_community.utilities.scenexplain
langchain_community.utilities.searchapi
langchain_community.utilities.searx_search
langchain_community.utilities.serpapi
langchain_community.utilities.spark_sql
langchain_community.utilities.sql_database
langchain_community.utilities.stackexchange
langchain_community.utilities.steam
langchain_community.utilities.tavily_search
langchain_community.utilities.tensorflow_datasets
langchain_community.utilities.twilio
langchain_community.utilities.vertexai
langchain_community.utilities.wikipedia
langchain_community.utilities.wolfram_alpha
langchain_community.utilities.zapier
langchain_community.utils.math
langchain_community.utils.openai
langchain_community.utils.openai_functions
langchain_community.vectorstores.alibabacloud_opensearch
langchain_community.vectorstores.analyticdb
langchain_community.vectorstores.annoy
langchain_community.vectorstores.analyticdb
langchain_community.vectorstores.annoy
langchain_community.vectorstores.astradb
langchain_community.vectorstores.atlas
langchain_community.vectorstores.awadb
langchain_community.vectorstores.azure_cosmos_db
langchain_community.vectorstores.azuresearch
langchain_community.vectorstores.bageldb
langchain_community.vectorstores.baiducloud_vector_search
langchain_community.vectorstores.cassandra
langchain_community.vectorstores.chroma
langchain_community.vectorstores.clarifai
langchain_community.vectorstores.clickhouse
langchain_community.vectorstores.dashvector
langchain_community.vectorstores.databricks_vector_search
langchain_community.vectorstores.deeplake
langchain_community.vectorstores.dingo
langchain_community.vectorstores.docarray.base
langchain_community.vectorstores.docarray.hnsw
langchain_community.vectorstores.docarray.in_memory
langchain_community.vectorstores.elastic_vector_search
langchain_community.vectorstores.elasticsearch
langchain_community.vectorstores.epsilla
langchain_community.vectorstores.faiss
langchain_community.vectorstores.hippo
langchain_community.vectorstores.hologres
langchain_community.vectorstores.lancedb
langchain_community.vectorstores.llm_rails
langchain_community.vectorstores.marqo
langchain_community.vectorstores.matching_engine
langchain_community.vectorstores.meilisearch
langchain_community.vectorstores.milvus
langchain_community.vectorstores.momento_vector_index
langchain_community.vectorstores.mongodb_atlas
langchain_community.vectorstores.myscale
langchain_community.vectorstores.neo4j_vector
langchain_community.vectorstores.nucliadb
langchain_community.vectorstores.opensearch_vector_search
langchain_community.vectorstores.pgembedding
langchain_community.vectorstores.pgvecto_rs
langchain_community.vectorstores.pgvector
langchain_community.vectorstores.pinecone
langchain_community.vectorstores.qdrant
langchain_community.vectorstores.pinecone
langchain_community.vectorstores.qdrant
langchain_community.vectorstores.redis.base
langchain_community.vectorstores.redis.filters
langchain_community.vectorstores.redis.schema
langchain_community.vectorstores.rocksetdb
langchain_community.vectorstores.scann
langchain_community.vectorstores.semadb
langchain_community.vectorstores.singlestoredb
langchain_community.vectorstores.sklearn
langchain_community.vectorstores.sqlitevss
langchain_community.vectorstores.starrocks
langchain_community.vectorstores.supabase
langchain_community.vectorstores.surrealdb
langchain_community.vectorstores.tair
langchain_community.vectorstores.tencentvectordb
langchain_community.vectorstores.tigris
langchain_community.vectorstores.tiledb
langchain_community.vectorstores.timescalevector
langchain_community.vectorstores.typesense
langchain_community.vectorstores.usearch
langchain_community.vectorstores.utils
langchain_community.vectorstores.vald
langchain_community.vectorstores.vearch
langchain_community.vectorstores.vectara
langchain_community.vectorstores.vespa
langchain_community.vectorstores.weaviate
langchain_community.vectorstores.xata
langchain_community.vectorstores.yellowbrick
langchain_community.vectorstores.zep
langchain_community.vectorstores.zilliz
langchain_core.agents
langchain_core.beta.runnables.context
langchain_core.caches
langchain_core.callbacks.base
langchain_core.callbacks.manager
langchain_core.callbacks.stdout
langchain_core.callbacks.streaming_stdout
langchain_core.chat_history
langchain_core.chat_sessions
langchain_core.documents.base
langchain_core.documents.transformers
langchain_core.embeddings
langchain_core.example_selectors.base
langchain_core.example_selectors.length_based
langchain_core.example_selectors.semantic_similarity
langchain_core.exceptions
langchain_core.language_models.base
langchain_core.language_models.chat_models
langchain_core.language_models.llms
langchain_core.language_models.chat_models
langchain_core.language_models.llms
langchain_core.load.dump
langchain_core.load.load
langchain_core.load.serializable
langchain_core.memory
langchain_core.messages.ai
langchain_core.messages.base
langchain_core.messages.chat
langchain_core.messages.function
langchain_core.messages.human
langchain_core.messages.system
langchain_core.messages.tool
langchain_core.output_parsers.base
langchain_core.output_parsers.list
langchain_core.output_parsers.string
langchain_core.output_parsers.transform
langchain_core.outputs.chat_generation
langchain_core.outputs.chat_result
langchain_core.outputs.generation
langchain_core.outputs.llm_result
langchain_core.outputs.run_info
langchain_core.prompt_values
langchain_core.prompts.base
langchain_core.prompts.chat
langchain_core.prompts.few_shot
langchain_core.prompts.few_shot_with_templates
langchain_core.prompts.loading
langchain_core.prompts.pipeline
langchain_core.prompts.prompt
langchain_core.prompts.string
langchain_core.retrievers
langchain_core.runnables.base
langchain_core.runnables.branch
langchain_core.runnables.config
langchain_core.runnables.configurable
langchain_core.runnables.fallbacks
langchain_core.runnables.history
langchain_core.runnables.passthrough
langchain_core.runnables.retry
langchain_core.runnables.router
langchain_core.runnables.utils
langchain_core.stores
langchain_core.tools
langchain_core.tracers.base
langchain_core.tracers.context
langchain_core.tracers.evaluation
langchain_core.tracers.langchain
langchain_core.tracers.langchain_v1
langchain_core.tracers.log_stream
langchain_core.tracers.root_listeners
langchain_core.tracers.run_collector
langchain_core.tracers.schemas
langchain_core.tracers.run_collector
langchain_core.tracers.schemas
langchain_core.tracers.stdout
langchain_core.utils.aiter
langchain_core.utils.env
langchain_core.utils.formatting
langchain_core.utils.html
langchain_core.utils.input
langchain_core.utils.iter
langchain_core.utils.json_schema
langchain_core.utils.loading
langchain_core.utils.pydantic
langchain_core.utils.strings
langchain_core.utils.utils
langchain_core.vectorstores
langchain_experimental.agents.agent_toolkits.csv.base
langchain_experimental.agents.agent_toolkits.pandas.base
langchain_experimental.agents.agent_toolkits.python.base
langchain_experimental.agents.agent_toolkits.spark.base
langchain_experimental.agents.agent_toolkits.xorbits.base
langchain_experimental.autonomous_agents.autogpt.agent
langchain_experimental.autonomous_agents.autogpt.memory
langchain_experimental.autonomous_agents.autogpt.output_parser
langchain_experimental.autonomous_agents.autogpt.prompt
langchain_experimental.autonomous_agents.autogpt.prompt_generator
langchain_experimental.autonomous_agents.baby_agi.baby_agi
langchain_experimental.autonomous_agents.baby_agi.task_creation
langchain_experimental.autonomous_agents.baby_agi.task_execution
langchain_experimental.autonomous_agents.baby_agi.task_prioritization
langchain_experimental.autonomous_agents.hugginggpt.hugginggpt
langchain_experimental.autonomous_agents.hugginggpt.repsonse_generator
langchain_experimental.autonomous_agents.hugginggpt.task_executor
langchain_experimental.autonomous_agents.hugginggpt.task_planner
langchain_experimental.chat_models.llm_wrapper
langchain_experimental.comprehend_moderation.amazon_comprehend_moderation
langchain_experimental.comprehend_moderation.base_moderation
langchain_experimental.comprehend_moderation.base_moderation
langchain_experimental.comprehend_moderation.base_moderation_callbacks
langchain_experimental.comprehend_moderation.base_moderation_config
langchain_experimental.comprehend_moderation.base_moderation_exceptions
langchain_experimental.comprehend_moderation.pii
langchain_experimental.comprehend_moderation.prompt_safety
langchain_experimental.comprehend_moderation.toxicity
langchain_experimental.cpal.base
langchain_experimental.cpal.constants
langchain_experimental.cpal.models
langchain_experimental.data_anonymizer.base
langchain_experimental.data_anonymizer.deanonymizer_mapping
langchain_experimental.data_anonymizer.deanonymizer_matching_strategies
langchain_experimental.data_anonymizer.faker_presidio_mapping
langchain_experimental.data_anonymizer.presidio
langchain_experimental.fallacy_removal.base
langchain_experimental.fallacy_removal.models
langchain_experimental.generative_agents.generative_agent
langchain_experimental.generative_agents.memory
langchain_experimental.graph_transformers.diffbot
langchain_experimental.llm_bash.base
langchain_experimental.llm_bash.bash
langchain_experimental.llm_bash.prompt
langchain_experimental.llm_symbolic_math.base
langchain_experimental.llms.anthropic_functions
langchain_experimental.llms.jsonformer_decoder
langchain_experimental.llms.llamaapi
langchain_experimental.llms.lmformatenforcer_decoder
langchain_experimental.llms.ollama_functions
langchain_experimental.llms.rellm_decoder
langchain_experimental.open_clip.open_clip
langchain_experimental.pal_chain.base
langchain_experimental.plan_and_execute.agent_executor
langchain_experimental.plan_and_execute.executors.agent_executor
langchain_experimental.plan_and_execute.executors.agent_executor
langchain_experimental.plan_and_execute.executors.base
langchain_experimental.plan_and_execute.planners.base
langchain_experimental.plan_and_execute.planners.chat_planner
langchain_experimental.plan_and_execute.schema
langchain_experimental.prompt_injection_identifier.hugging_face_identifier
langchain_experimental.prompts.load
langchain_experimental.retrievers.vector_sql_database
langchain_experimental.rl_chain.base
langchain_experimental.rl_chain.metrics
langchain_experimental.rl_chain.model_repository
langchain_experimental.rl_chain.pick_best_chain
langchain_experimental.rl_chain.vw_logger
langchain_experimental.smart_llm.base
langchain_experimental.sql.base
langchain_experimental.sql.vector_sql
langchain_experimental.tabular_synthetic_data.base
langchain_experimental.tabular_synthetic_data.openai
langchain_experimental.tools.python.tool
langchain_experimental.tot.base
langchain_experimental.tot.checker
langchain_experimental.tot.controller
langchain_experimental.tot.memory
langchain_experimental.tot.prompts
langchain_experimental.tot.thought
langchain_experimental.tot.thought_generation
langchain_experimental.utilities.python
langchain_google_genai.chat_models
langchain_google_genai.embeddings
langchain_google_genai.llms
langchain_nvidia_ai_endpoints._common
langchain_nvidia_ai_endpoints.chat_models
langchain_nvidia_ai_endpoints.embeddings
pydantic.fields
pydantic.main
sqlalchemy.orm.decl_base
sqlalchemy.orm.instrumentation
string
typing
Source code for langchain_nvidia_ai_endpoints._common
from __future__ import annotations
import json
import logging
from typing import (
    Any,
    AsyncIterator,
    Callable,
    Dict,
    Generator,
    Iterator,
    List,
    Optional,
    Sequence,
    Tuple,
    Union,
)
import aiohttp
import requests
from langchain_core.messages import BaseMessage
from langchain_core.pydantic_v1 import (
    BaseModel,
    Field,
    PrivateAttr,
    SecretStr,
    root_validator,
)
from langchain_core.utils import get_from_dict_or_env
from requests.models import Response
logger = logging.getLogger(__name__)
class NVEModel(BaseModel):
    """
    Underlying Client for interacting with the AI Foundation Model Function API.
    Leveraged by the NVIDIABaseModel to provide a simple requests-oriented interface.
    Direct abstraction over NGC-recommended streaming/non-streaming Python solutions.
    NOTE: Models in the playground does not currently support raw text continuation.
    """
    ## Core defaults. These probably should not be changed
    fetch_url_format: str = Field("https://api.nvcf.nvidia.com/v2/nvcf/pexec/status/")
    call_invoke_base: str = Field("https://api.nvcf.nvidia.com/v2/nvcf/pexec/functions")
    get_session_fn: Callable = Field(requests.Session)
    get_asession_fn: Callable = Field(aiohttp.ClientSession)
    nvidia_api_key: SecretStr = Field(
        ...,
        description="API key for NVIDIA Foundation Endpoints. Starts with `nvapi-`",
    )
    is_staging: bool = Field(False, description="Whether to use staging API")
    ## Generation arguments
## Generation arguments
    max_tries: int = Field(5, ge=1)
    headers_tmpl: dict = Field(
        ...,
        description="Headers template for API calls."
        " Should contain `call` and `stream` keys.",
    )
    _available_functions: Optional[List[dict]] = PrivateAttr(default=None)
    _available_models: Optional[dict] = PrivateAttr(default=None)
    @property
    def headers(self) -> dict:
        """Return headers with API key injected"""
        headers_ = self.headers_tmpl.copy()
        for header in headers_.values():
            if "{nvidia_api_key}" in header["Authorization"]:
                header["Authorization"] = header["Authorization"].format(
                    nvidia_api_key=self.nvidia_api_key.get_secret_value(),
                )
        return headers_
    @root_validator(pre=True)
    def validate_model(cls, values: Dict[str, Any]) -> Dict[str, Any]:
        """Validate and update model arguments, including API key and formatting"""
        values["nvidia_api_key"] = get_from_dict_or_env(
            values,
            "nvidia_api_key",
            "NVIDIA_API_KEY",
        )
        if "nvapi-" not in values.get("nvidia_api_key", ""):
            raise ValueError("Invalid NVAPI key detected. Should start with `nvapi-`")
        is_staging = "nvapi-stg-" in values["nvidia_api_key"]
        values["is_staging"] = is_staging
        if "headers_tmpl" not in values:
            values["headers_tmpl"] = {
                "call": {
                    "Authorization": "Bearer {nvidia_api_key}",
                    "Accept": "application/json",
                },
                "stream": {
"Accept": "application/json",
                },
                "stream": {
                    "Authorization": "Bearer {nvidia_api_key}",
                    "Accept": "text/event-stream",
                    "content-type": "application/json",
                },
            }
        values["fetch_url_format"] = cls._stagify(
            is_staging,
            values.get(
                "fetch_url_format", "https://api.nvcf.nvidia.com/v2/nvcf/pexec/status/"
            ),
        )
        values["call_invoke_base"] = cls._stagify(
            is_staging,
            values.get(
                "call_invoke_base",
                "https://api.nvcf.nvidia.com/v2/nvcf/pexec/functions",
            ),
        )
        return values
    @property
    def available_models(self) -> dict:
        """List the available models that can be invoked."""
        if self._available_models is not None:
            return self._available_models
        live_fns = [v for v in self.available_functions if v.get("status") == "ACTIVE"]
        self._available_models = {v["name"]: v["id"] for v in live_fns}
        return self._available_models
    @property
    def available_functions(self) -> List[dict]:
        """List the available functions that can be invoked."""
        if self._available_functions is not None:
            return self._available_functions
        invoke_url = self._stagify(
            self.is_staging, "https://api.nvcf.nvidia.com/v2/nvcf/functions"
        )
        query_res = self.query(invoke_url)
        if "functions" not in query_res:
            raise ValueError(
if "functions" not in query_res:
            raise ValueError(
                f"Unexpected response when querying {invoke_url}\n{query_res}"
            )
        self._available_functions = query_res["functions"]
        return self._available_functions
    @classmethod
    def _stagify(cls, is_staging: bool, path: str) -> str:
        """Helper method to switch between staging and production endpoints"""
        if is_staging and "stg.api" not in path:
            return path.replace("api.", "stg.api.")
        if not is_staging and "stg.api" in path:
            return path.replace("stg.api.", "api.")
        return path
    ####################################################################################
    ## Core utilities for posting and getting from NV Endpoints
    def _post(self, invoke_url: str, payload: dict = {}) -> Tuple[Response, Any]:
        """Method for posting to the AI Foundation Model Function API."""
        call_inputs = {
            "url": invoke_url,
            "headers": self.headers["call"],
            "json": payload,
            "stream": False,
        }
        session = self.get_session_fn()
        response = session.post(**call_inputs)
        self._try_raise(response)
        return response, session
    def _get(self, invoke_url: str, payload: dict = {}) -> Tuple[Response, Any]:
        """Method for getting from the AI Foundation Model Function API."""
        last_inputs = {
            "url": invoke_url,
            "headers": self.headers["call"],
            "json": payload,
            "stream": False,
        }
        session = self.get_session_fn()
        last_response = session.get(**last_inputs)
        self._try_raise(last_response)
        return last_response, session