Skip to content

Commit

Permalink
chore: use filename as uri instead of key
Browse files Browse the repository at this point in the history
  • Loading branch information
phil65 committed Dec 2, 2024
1 parent 549c06f commit ff9409f
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/llmling/config/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class BaseResource(BaseModel):

resource_type: str = Field(init=False)
description: str = ""
uri: str | None = None # Add this field
processors: list[ProcessingStep] = Field(
default_factory=list
) # Optional with empty default
Expand Down
5 changes: 3 additions & 2 deletions src/llmling/config/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ async def resolve_resource_uri(self, uri_or_name: str) -> tuple[str, Resource]:
logger.debug("Trying as resource name")
resource = self._resource_registry[uri_or_name]
loader = self._loader_registry.get_loader(resource)
loader = loader.create(resource, uri_or_name) # Create instance
uri = loader.create_uri(name=uri_or_name)
except KeyError:
pass
Expand All @@ -298,13 +299,13 @@ async def resolve_resource_uri(self, uri_or_name: str) -> tuple[str, Resource]:
if "/" in uri_or_name or "\\" in uri_or_name or "." in uri_or_name:
try:
logger.debug("Trying as file path")
uri = PathResourceLoader.create_uri(name=uri_or_name)
resource = PathResource(path=uri_or_name)
loader = PathResourceLoader.create(resource, uri_or_name)
uri = loader.create_uri(name=uri_or_name)
except Exception as exc: # noqa: BLE001
logger.debug("Failed to create file URI: %s", exc)
else:
return uri, resource

msg = (
f"Could not resolve resource {uri_or_name!r}. Expected resource name or path."
)
Expand Down
6 changes: 2 additions & 4 deletions src/llmling/resources/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,7 @@ def get_name_from_uri(cls, uri: str) -> str:
else:
return normalized

@classmethod
def create_uri(cls, *, name: str) -> str:
def create_uri(self, *, name: str) -> str:
"""Create a valid URI for this resource type.
Args:
Expand All @@ -196,8 +195,7 @@ def create_uri(cls, *, name: str) -> str:
# Remove any existing scheme
if "://" in name:
name = name.split("://", 1)[1]

return cls.get_uri_template().format(name=name)
return self.get_uri_template().format(name=name)

def __repr__(self) -> str:
"""Show loader type and context."""
Expand Down
5 changes: 2 additions & 3 deletions src/llmling/resources/loaders/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@ def get_uri_template(cls) -> str:
"""Image URIs follow the same pattern as file URIs."""
return "image:///{name}"

@classmethod
def create_uri(cls, *, name: str) -> str:
def create_uri(self, *, name: str) -> str:
"""Handle image paths properly."""
normalized = name.replace("\\", "/").lstrip("/")
return cls.get_uri_template().format(name=normalized)
return self.get_uri_template().format(name=normalized)

@classmethod
def get_name_from_uri(cls, uri: str) -> str:
Expand Down
15 changes: 11 additions & 4 deletions src/llmling/resources/loaders/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from typing import TYPE_CHECKING, Any, ClassVar

import upath
from upath import UPath

from llmling.config.models import PathResource
Expand Down Expand Up @@ -39,10 +40,16 @@ def get_name_from_uri(cls, uri: str) -> str:
msg = f"Invalid URI: {uri}"
raise exceptions.LoaderError(msg) from exc

@classmethod
def create_uri(cls, *, name: str) -> str:
"""Create a URI from a path."""
def create_uri(self, *, name: str) -> str:
"""Create a URI based on resource path basename or explicit URI."""
try:
if self.context and self.context.resource:
if self.context.resource.uri:
return paths.path_to_uri(self.context.resource.uri)
# Use basename of the configured path
path = upath.UPath(self.context.resource.path)
return paths.path_to_uri(path.name)
# Fallback to name if no context
return paths.path_to_uri(name)
except ValueError as exc:
msg = f"Failed to create URI from {name}"
Expand Down Expand Up @@ -100,5 +107,5 @@ async def _load_impl(


if __name__ == "__main__":
uri = PathResourceLoader.create_uri(name="/path/to/file.txt")
uri = PathResourceLoader().create_uri(name="/path/to/file.txt")
print(uri) # file:///path/to/file.txt
1 change: 1 addition & 0 deletions src/llmling/resources/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ def get_uri(self, name: str) -> str:
"""Get URI for a resource by name."""
resource = self[name]
loader = self.loader_registry.get_loader(resource)
loader = loader.create(resource, name) # Create instance
return loader.create_uri(name=name)

@logfire.instrument("Loading resource {name}")
Expand Down
4 changes: 3 additions & 1 deletion tests/resources/test_loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,9 @@ def test_uri_creation(uri_template: str, name: str, expected: str) -> None:
(TextResourceLoader,),
{"get_uri_template": staticmethod(lambda: uri_template)},
)
assert test_loader.create_uri(name=name) == expected # type: ignore
# Create instance with no context
loader = test_loader(None)
assert loader.create_uri(name=name) == expected # type: ignore


def test_create_loaded_resource() -> None:
Expand Down

0 comments on commit ff9409f

Please sign in to comment.