initial commit
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
"""
|
||||
**Utility functions** for LangChain.
|
||||
"""
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,51 @@
|
||||
from typing import Literal, Optional, Type, TypedDict
|
||||
|
||||
from langchain_core.utils.json_schema import dereference_refs
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class FunctionDescription(TypedDict):
|
||||
"""Representation of a callable function to the Ernie API."""
|
||||
|
||||
name: str
|
||||
"""The name of the function."""
|
||||
description: str
|
||||
"""A description of the function."""
|
||||
parameters: dict
|
||||
"""The parameters of the function."""
|
||||
|
||||
|
||||
class ToolDescription(TypedDict):
|
||||
"""Representation of a callable function to the Ernie API."""
|
||||
|
||||
type: Literal["function"]
|
||||
function: FunctionDescription
|
||||
|
||||
|
||||
def convert_pydantic_to_ernie_function(
|
||||
model: Type[BaseModel],
|
||||
*,
|
||||
name: Optional[str] = None,
|
||||
description: Optional[str] = None,
|
||||
) -> FunctionDescription:
|
||||
"""Convert a Pydantic model to a function description for the Ernie API."""
|
||||
schema = dereference_refs(model.schema())
|
||||
schema.pop("definitions", None)
|
||||
return {
|
||||
"name": name or schema["title"],
|
||||
"description": description or schema["description"],
|
||||
"parameters": schema,
|
||||
}
|
||||
|
||||
|
||||
def convert_pydantic_to_ernie_tool(
|
||||
model: Type[BaseModel],
|
||||
*,
|
||||
name: Optional[str] = None,
|
||||
description: Optional[str] = None,
|
||||
) -> ToolDescription:
|
||||
"""Convert a Pydantic model to a function description for the Ernie API."""
|
||||
function = convert_pydantic_to_ernie_function(
|
||||
model, name=name, description=description
|
||||
)
|
||||
return {"type": "function", "function": function}
|
||||
25
venv/Lib/site-packages/langchain_community/utils/google.py
Normal file
25
venv/Lib/site-packages/langchain_community/utils/google.py
Normal file
@@ -0,0 +1,25 @@
|
||||
"""Utilities to use Google provided components."""
|
||||
|
||||
from importlib import metadata
|
||||
from typing import Any, Optional
|
||||
|
||||
|
||||
def get_client_info(module: Optional[str] = None) -> Any:
|
||||
r"""Return 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
|
||||
"""
|
||||
from google.api_core.gapic_v1.client_info import ClientInfo
|
||||
|
||||
langchain_version = metadata.version("langchain")
|
||||
client_library_version = (
|
||||
f"{langchain_version}-{module}" if module else langchain_version
|
||||
)
|
||||
return ClientInfo(
|
||||
client_library_version=client_library_version,
|
||||
user_agent=f"langchain/{client_library_version}",
|
||||
)
|
||||
74
venv/Lib/site-packages/langchain_community/utils/math.py
Normal file
74
venv/Lib/site-packages/langchain_community/utils/math.py
Normal file
@@ -0,0 +1,74 @@
|
||||
"""Math utils."""
|
||||
|
||||
import logging
|
||||
from typing import List, Optional, Tuple, Union
|
||||
|
||||
import numpy as np
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
Matrix = Union[List[List[float]], List[np.ndarray], np.ndarray]
|
||||
|
||||
|
||||
def cosine_similarity(X: Matrix, Y: Matrix) -> np.ndarray:
|
||||
"""Row-wise cosine similarity between two equal-width matrices."""
|
||||
if len(X) == 0 or len(Y) == 0:
|
||||
return np.array([])
|
||||
|
||||
X = np.array(X)
|
||||
Y = np.array(Y)
|
||||
if X.shape[1] != Y.shape[1]:
|
||||
raise ValueError(
|
||||
f"Number of columns in X and Y must be the same. X has shape {X.shape} "
|
||||
f"and Y has shape {Y.shape}."
|
||||
)
|
||||
try:
|
||||
import simsimd as simd
|
||||
|
||||
X = np.array(X, dtype=np.float32)
|
||||
Y = np.array(Y, dtype=np.float32)
|
||||
Z = 1 - np.array(simd.cdist(X, Y, metric="cosine"))
|
||||
return Z
|
||||
except ImportError:
|
||||
logger.debug(
|
||||
"Unable to import simsimd, defaulting to NumPy implementation. If you want "
|
||||
"to use simsimd please install with `pip install simsimd`."
|
||||
)
|
||||
X_norm = np.linalg.norm(X, axis=1)
|
||||
Y_norm = np.linalg.norm(Y, axis=1)
|
||||
# Ignore divide by zero errors run time warnings as those are handled below.
|
||||
with np.errstate(divide="ignore", invalid="ignore"):
|
||||
similarity = np.dot(X, Y.T) / np.outer(X_norm, Y_norm)
|
||||
similarity[np.isnan(similarity) | np.isinf(similarity)] = 0.0
|
||||
return similarity
|
||||
|
||||
|
||||
def cosine_similarity_top_k(
|
||||
X: Matrix,
|
||||
Y: Matrix,
|
||||
top_k: Optional[int] = 5,
|
||||
score_threshold: Optional[float] = None,
|
||||
) -> Tuple[List[Tuple[int, int]], List[float]]:
|
||||
"""Row-wise cosine similarity with optional top-k and score threshold filtering.
|
||||
|
||||
Args:
|
||||
X: Matrix.
|
||||
Y: Matrix, same width as X.
|
||||
top_k: Max number of results to return.
|
||||
score_threshold: Minimum cosine similarity of results.
|
||||
|
||||
Returns:
|
||||
Tuple of two lists. First contains two-tuples of indices (X_idx, Y_idx),
|
||||
second contains corresponding cosine similarities.
|
||||
"""
|
||||
if len(X) == 0 or len(Y) == 0:
|
||||
return [], []
|
||||
score_array = cosine_similarity(X, Y)
|
||||
score_threshold = score_threshold or -1.0
|
||||
score_array[score_array < score_threshold] = 0
|
||||
top_k = int(min(top_k or len(score_array), int(np.count_nonzero(score_array))))
|
||||
top_k_idxs = np.argpartition(score_array, -top_k, axis=None)[-top_k:]
|
||||
top_k_idxs = top_k_idxs[np.argsort(score_array.ravel()[top_k_idxs])][::-1]
|
||||
ret_idxs = np.unravel_index(top_k_idxs, score_array.shape)
|
||||
scores = score_array.ravel()[top_k_idxs].tolist()
|
||||
return list(zip(*ret_idxs)), scores # type: ignore[return-value,unused-ignore]
|
||||
13
venv/Lib/site-packages/langchain_community/utils/openai.py
Normal file
13
venv/Lib/site-packages/langchain_community/utils/openai.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import functools
|
||||
from importlib.metadata import version
|
||||
|
||||
from packaging.version import parse
|
||||
|
||||
|
||||
@functools.cache
|
||||
def is_openai_v1() -> bool:
|
||||
"""Return whether OpenAI API is v1 or more."""
|
||||
_version = parse(version("openai"))
|
||||
return _version.major >= 1
|
||||
@@ -0,0 +1,19 @@
|
||||
# these stubs are just for backwards compatibility
|
||||
|
||||
from langchain_core.utils.function_calling import (
|
||||
FunctionDescription,
|
||||
ToolDescription,
|
||||
)
|
||||
from langchain_core.utils.function_calling import (
|
||||
convert_to_openai_function as convert_pydantic_to_openai_function,
|
||||
)
|
||||
from langchain_core.utils.function_calling import (
|
||||
convert_to_openai_tool as convert_pydantic_to_openai_tool,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"FunctionDescription",
|
||||
"ToolDescription",
|
||||
"convert_pydantic_to_openai_function",
|
||||
"convert_pydantic_to_openai_tool",
|
||||
]
|
||||
@@ -0,0 +1,16 @@
|
||||
import logging
|
||||
import os
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def get_user_agent() -> str:
|
||||
"""Get user agent from environment variable."""
|
||||
env_user_agent = os.environ.get("USER_AGENT")
|
||||
if not env_user_agent:
|
||||
log.warning(
|
||||
"USER_AGENT environment variable not set, "
|
||||
"consider setting it to identify your requests."
|
||||
)
|
||||
return "DefaultLangchainUserAgent"
|
||||
return env_user_agent
|
||||
Reference in New Issue
Block a user