initial commit

This commit is contained in:
2026-05-11 12:36:20 +05:30
commit 384cbe8019
15377 changed files with 2360544 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
"""LangChain **Runnable** and the **LangChain Expression Language (LCEL)**.
The LangChain Expression Language (LCEL) offers a declarative method to build
production-grade programs that harness the power of LLMs.
Programs created using LCEL and LangChain Runnables inherently support
synchronous, asynchronous, batch, and streaming operations.
Support for **async** allows servers hosting the LCEL based programs
to scale better for higher concurrent loads.
**Batch** operations allow for processing multiple inputs in parallel.
**Streaming** of intermediate outputs, as they're being generated, allows for
creating more responsive UX.
This module contains non-core Runnable classes.
"""

View File

@@ -0,0 +1,42 @@
from typing import Any
from langchain_core.runnables.base import RunnableBindingBase
from langchain_core.runnables.utils import Input, Output
class HubRunnable(RunnableBindingBase[Input, Output]): # type: ignore[no-redef]
"""An instance of a runnable stored in the LangChain Hub."""
owner_repo_commit: str
def __init__(
self,
owner_repo_commit: str,
*,
api_url: str | None = None,
api_key: str | None = None,
**kwargs: Any,
) -> None:
"""Initialize the `HubRunnable`.
Args:
owner_repo_commit: The full name of the prompt to pull from in the format of
`owner/prompt_name:commit_hash` or `owner/prompt_name`
or just `prompt_name` if it's your own prompt.
api_url: The URL of the LangChain Hub API.
Defaults to the hosted API service if you have an api key set,
or a localhost instance if not.
api_key: The API key to use to authenticate with the LangChain Hub API.
**kwargs: Additional keyword arguments to pass to the parent class.
"""
from langchain_classic.hub import pull
pulled = pull(owner_repo_commit, api_url=api_url, api_key=api_key)
super_kwargs = {
"kwargs": {},
"config": {},
**kwargs,
"bound": pulled,
"owner_repo_commit": owner_repo_commit,
}
super().__init__(**super_kwargs)

View File

@@ -0,0 +1,54 @@
from collections.abc import Callable, Mapping
from operator import itemgetter
from typing import Any
from langchain_core.messages import BaseMessage
from langchain_core.output_parsers.openai_functions import JsonOutputFunctionsParser
from langchain_core.runnables import RouterRunnable, Runnable
from langchain_core.runnables.base import RunnableBindingBase
from typing_extensions import TypedDict
class OpenAIFunction(TypedDict):
"""A function description for `ChatOpenAI`."""
name: str
"""The name of the function."""
description: str
"""The description of the function."""
parameters: dict
"""The parameters to the function."""
class OpenAIFunctionsRouter(RunnableBindingBase[BaseMessage, Any]): # type: ignore[no-redef]
"""A runnable that routes to the selected function."""
functions: list[OpenAIFunction] | None
def __init__(
self,
runnables: Mapping[
str,
Runnable[dict, Any] | Callable[[dict], Any],
],
functions: list[OpenAIFunction] | None = None,
):
"""Initialize the `OpenAIFunctionsRouter`.
Args:
runnables: A mapping of function names to runnables.
functions: Optional list of functions to check against the runnables.
"""
if functions is not None:
if len(functions) != len(runnables):
msg = "The number of functions does not match the number of runnables."
raise ValueError(msg)
if not all(func["name"] in runnables for func in functions):
msg = "One or more function names are not found in runnables."
raise ValueError(msg)
router = (
JsonOutputFunctionsParser(args_only=False)
| {"key": itemgetter("name"), "input": itemgetter("arguments")}
| RouterRunnable(runnables)
)
super().__init__(bound=router, kwargs={}, functions=functions)