_utils.py — langchain Source File
Architecture documentation for _utils.py, a python file in the langchain codebase. 4 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR f6007d80_a358_f7cd_14a5_9fbfbdf1edf2["_utils.py"] 66c6348c_7716_027c_42d7_71449bc64eeb["base64"] f6007d80_a358_f7cd_14a5_9fbfbdf1edf2 --> 66c6348c_7716_027c_42d7_71449bc64eeb c89186be_3766_27dd_efaa_6092bf0ccc74["urllib.parse"] f6007d80_a358_f7cd_14a5_9fbfbdf1edf2 --> c89186be_3766_27dd_efaa_6092bf0ccc74 1803c8c1_a347_1256_1454_9f04c3553d93["httpx"] f6007d80_a358_f7cd_14a5_9fbfbdf1edf2 --> 1803c8c1_a347_1256_1454_9f04c3553d93 e36ef4a1_87ee_d91e_3f75_05e353ec925c["ollama"] f6007d80_a358_f7cd_14a5_9fbfbdf1edf2 --> e36ef4a1_87ee_d91e_3f75_05e353ec925c style f6007d80_a358_f7cd_14a5_9fbfbdf1edf2 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
"""Utility function to validate Ollama models."""
from __future__ import annotations
import base64
from urllib.parse import unquote, urlparse
from httpx import ConnectError
from ollama import Client, ResponseError
def validate_model(client: Client, model_name: str) -> None:
"""Validate that a model exists in the local Ollama instance.
Args:
client: The Ollama client.
model_name: The name of the model to validate.
Raises:
ValueError: If the model is not found or if there's a connection issue.
"""
try:
response = client.list()
model_names: list[str] = [model["model"] for model in response["models"]]
if not any(
model_name == m or m.startswith(f"{model_name}:") for m in model_names
):
msg = (
f"Model `{model_name}` not found in Ollama. Please pull the "
f"model (using `ollama pull {model_name}`) or specify a valid "
f"model name. Available local models: {', '.join(model_names)}"
)
raise ValueError(msg)
except ConnectError as e:
msg = (
"Failed to connect to Ollama. Please check that Ollama is downloaded, "
"running and accessible. https://ollama.com/download"
)
raise ValueError(msg) from e
except ResponseError as e:
msg = (
"Received an error from the Ollama API. "
"Please check your Ollama server logs."
)
raise ValueError(msg) from e
def parse_url_with_auth(
url: str | None,
) -> tuple[str | None, dict[str, str] | None]:
"""Parse URL and extract `userinfo` credentials for headers.
Handles URLs of the form: `https://user:password@host:port/path`
Args:
url: The URL to parse.
Returns:
A tuple of `(cleaned_url, headers_dict)` where:
- `cleaned_url` is the URL without authentication credentials if any were
found. Otherwise, returns the original URL.
- `headers_dict` contains Authorization header if credentials were found.
"""
if not url:
return None, None
parsed = urlparse(url)
if not parsed.scheme or not parsed.netloc or not parsed.hostname:
return None, None
if not parsed.username:
return url, None
# Handle case where password might be empty string or None
password = parsed.password or ""
# Create basic auth header (decode percent-encoding)
username = unquote(parsed.username)
password = unquote(password)
credentials = f"{username}:{password}"
encoded_credentials = base64.b64encode(credentials.encode()).decode()
headers = {"Authorization": f"Basic {encoded_credentials}"}
# Strip credentials from URL
cleaned_netloc = parsed.hostname or ""
if parsed.port:
cleaned_netloc += f":{parsed.port}"
cleaned_url = f"{parsed.scheme}://{cleaned_netloc}"
if parsed.path:
cleaned_url += parsed.path
if parsed.query:
cleaned_url += f"?{parsed.query}"
if parsed.fragment:
cleaned_url += f"#{parsed.fragment}"
return cleaned_url, headers
def merge_auth_headers(
client_kwargs: dict,
auth_headers: dict[str, str] | None,
) -> None:
"""Merge authentication headers into client kwargs in-place.
Args:
client_kwargs: The client kwargs dict to update.
auth_headers: Headers to merge (typically from `parse_url_with_auth`).
"""
if auth_headers:
headers = client_kwargs.get("headers", {})
headers.update(auth_headers)
client_kwargs["headers"] = headers
Domain
Subdomains
Dependencies
- base64
- httpx
- ollama
- urllib.parse
Source
Frequently Asked Questions
What does _utils.py do?
_utils.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, RunnableInterface subdomain.
What functions are defined in _utils.py?
_utils.py defines 3 function(s): merge_auth_headers, parse_url_with_auth, validate_model.
What does _utils.py depend on?
_utils.py imports 4 module(s): base64, httpx, ollama, urllib.parse.
Where is _utils.py in the architecture?
_utils.py is located at libs/partners/ollama/langchain_ollama/_utils.py (domain: CoreAbstractions, subdomain: RunnableInterface, directory: libs/partners/ollama/langchain_ollama).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free