mirror of
https://github.com/run-llama/llama_cloud_services.git
synced 2026-07-20 00:54:09 -04:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| aa7631a010 | |||
| c3a4f4de45 | |||
| 761b21ba18 | |||
| 7d139c3169 |
@@ -2,6 +2,7 @@ import asyncio
|
||||
import mimetypes
|
||||
import os
|
||||
import time
|
||||
import warnings
|
||||
from contextlib import asynccontextmanager
|
||||
from copy import deepcopy
|
||||
from enum import Enum
|
||||
@@ -13,12 +14,18 @@ from urllib.parse import urlparse
|
||||
import httpx
|
||||
from fsspec import AbstractFileSystem
|
||||
from llama_index.core.async_utils import asyncio_run, run_jobs
|
||||
from llama_index.core.bridge.pydantic import Field, PrivateAttr, field_validator
|
||||
from llama_index.core.bridge.pydantic import (
|
||||
Field,
|
||||
PrivateAttr,
|
||||
field_validator,
|
||||
model_validator,
|
||||
)
|
||||
from llama_index.core.constants import DEFAULT_BASE_URL
|
||||
from llama_index.core.readers.base import BasePydanticReader
|
||||
from llama_index.core.readers.file.base import get_default_fs
|
||||
from llama_index.core.schema import Document
|
||||
|
||||
from llama_cloud_services.utils import check_extra_params
|
||||
from llama_cloud_services.parse.types import JobResult
|
||||
from llama_cloud_services.parse.utils import (
|
||||
SUPPORTED_FILE_TYPES,
|
||||
@@ -498,6 +505,21 @@ class LlamaParse(BasePydanticReader):
|
||||
description="If set, documents will automatically be partitioned into segments containing the specified number of pages at most. Parsing will be split into separate jobs for each partition segment. Can be used in combination with targetPages and maxPages.",
|
||||
)
|
||||
|
||||
@model_validator(mode="before")
|
||||
@classmethod
|
||||
def warn_extra_params(cls, data: Dict[str, Any]) -> Dict[str, Any]:
|
||||
extra_params, suggestions = check_extra_params(cls, data)
|
||||
if extra_params:
|
||||
suggestions = [f"\n - {suggestion}" for suggestion in suggestions]
|
||||
suggestions_str = "".join(suggestions)
|
||||
warnings.warn(
|
||||
"The following parameters are unused: "
|
||||
+ ", ".join(extra_params)
|
||||
+ f".\n{suggestions_str}",
|
||||
)
|
||||
|
||||
return data
|
||||
|
||||
@field_validator("api_key", mode="before", check_fields=True)
|
||||
@classmethod
|
||||
def validate_api_key(cls, v: str) -> str:
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
import difflib
|
||||
from pydantic import BaseModel
|
||||
from typing import Any, Dict, List, Tuple, Type
|
||||
|
||||
|
||||
def check_extra_params(
|
||||
model_cls: Type[BaseModel], data: Dict[str, Any]
|
||||
) -> Tuple[List[str], List[str]]:
|
||||
# check if one of the parameters is unused, and warn the user
|
||||
model_attributes = set(model_cls.model_fields.keys())
|
||||
extra_params = [param for param in data.keys() if param not in model_attributes]
|
||||
|
||||
suggestions: List[str] = []
|
||||
if extra_params:
|
||||
# for each unused parameter, check if it is similar to a valid parameter and suggest a typo correction, else suggest to check the documentation / update the package
|
||||
for param in extra_params:
|
||||
similar_params = difflib.get_close_matches(
|
||||
param, model_attributes, n=1, cutoff=0.8
|
||||
)
|
||||
if similar_params:
|
||||
suggestions.append(
|
||||
f"'{param}' is not a valid parameter. Did you mean '{similar_params[0]}' instead of '{param}'?"
|
||||
)
|
||||
else:
|
||||
suggestions.append(
|
||||
f"'{param}' is not a valid parameter. Please check the documentation or update the package."
|
||||
)
|
||||
|
||||
return extra_params, suggestions
|
||||
@@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"
|
||||
|
||||
[tool.poetry]
|
||||
name = "llama-parse"
|
||||
version = "0.6.31"
|
||||
version = "0.6.32"
|
||||
description = "Parse files into RAG-Optimized formats."
|
||||
authors = ["Logan Markewich <logan@llamaindex.ai>"]
|
||||
license = "MIT"
|
||||
@@ -13,7 +13,7 @@ packages = [{include = "llama_parse"}]
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = ">=3.9,<4.0"
|
||||
llama-cloud-services = ">=0.6.31"
|
||||
llama-cloud-services = ">=0.6.32"
|
||||
|
||||
[tool.poetry.group.dev.dependencies]
|
||||
pytest = "^8.0.0"
|
||||
|
||||
+1
-1
@@ -8,7 +8,7 @@ python_version = "3.10"
|
||||
|
||||
[tool.poetry]
|
||||
name = "llama-cloud-services"
|
||||
version = "0.6.31"
|
||||
version = "0.6.32"
|
||||
description = "Tailored SDK clients for LlamaCloud services."
|
||||
authors = ["Logan Markewich <logan@runllama.ai>"]
|
||||
license = "MIT"
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
from pydantic import BaseModel
|
||||
from llama_cloud_services.utils import check_extra_params
|
||||
|
||||
|
||||
class MyModel(BaseModel):
|
||||
name: str
|
||||
age: int
|
||||
email: str
|
||||
is_active: bool
|
||||
|
||||
|
||||
def test_check_extra_params_no_extra():
|
||||
"""Test when all parameters are valid - should return empty lists."""
|
||||
data = {"name": "John", "age": 25, "email": "john@example.com", "is_active": True}
|
||||
|
||||
extra_params, suggestions = check_extra_params(MyModel, data)
|
||||
|
||||
assert extra_params == []
|
||||
assert suggestions == []
|
||||
|
||||
|
||||
def test_check_extra_params_with_typos():
|
||||
"""Test when there are extra parameters that are close to valid ones (typos)."""
|
||||
data = {
|
||||
"name": "John",
|
||||
"age": 25,
|
||||
"emial": "john@example.com", # typo: emial instead of email
|
||||
"is_activ": True, # typo: is_activ instead of is_active
|
||||
"address": "123 Main St", # completely different parameter
|
||||
}
|
||||
|
||||
extra_params, suggestions = check_extra_params(MyModel, data)
|
||||
|
||||
assert len(extra_params) == 3
|
||||
assert "emial" in extra_params
|
||||
assert "is_activ" in extra_params
|
||||
assert "address" in extra_params
|
||||
|
||||
# Check that typo suggestions are provided
|
||||
assert len(suggestions) == 3
|
||||
assert "Did you mean 'email' instead of 'emial'?" in suggestions[0]
|
||||
assert "Did you mean 'is_active' instead of 'is_activ'?" in suggestions[1]
|
||||
assert "check the documentation or update the package" in suggestions[2]
|
||||
|
||||
|
||||
def test_check_extra_params_completely_invalid():
|
||||
"""Test when there are extra parameters with no close matches."""
|
||||
data = {
|
||||
"name": "John",
|
||||
"xyz": "invalid",
|
||||
"random_field": 123,
|
||||
"completely_different": True,
|
||||
}
|
||||
|
||||
extra_params, suggestions = check_extra_params(MyModel, data)
|
||||
|
||||
assert len(extra_params) == 3
|
||||
assert "xyz" in extra_params
|
||||
assert "random_field" in extra_params
|
||||
assert "completely_different" in extra_params
|
||||
|
||||
# All suggestions should be generic (no close matches)
|
||||
assert len(suggestions) == 3
|
||||
for suggestion in suggestions:
|
||||
assert "check the documentation or update the package" in suggestion
|
||||
assert "Did you mean" not in suggestion
|
||||
Reference in New Issue
Block a user