Skip to content

Commit

Permalink
chore: lazy importing
Browse files Browse the repository at this point in the history
  • Loading branch information
phil65 committed Jan 30, 2025
1 parent 95d0a20 commit 079c2e9
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/llmling_agent/agent/conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from llmling import BasePrompt, PromptMessage, StaticPrompt
from llmling.config.models import BaseResource
from psygnal import Signal
from toprompt import AnyPromptType, to_prompt
from upath import UPath

from llmling_agent.log import get_logger
Expand All @@ -27,6 +26,7 @@

from llmling.config.models import Resource
from llmling.prompts import PromptType
from toprompt import AnyPromptType

from llmling_agent.agent.agent import Agent
from llmling_agent.common_types import MessageRole, SessionIdType, StrPath
Expand Down Expand Up @@ -367,6 +367,8 @@ async def temporary_state(
replace_history: If True, only use provided history. If False, append
to existing history.
"""
from toprompt import to_prompt

old_history = self.chat_messages.copy()

try:
Expand Down
3 changes: 2 additions & 1 deletion src/llmling_agent/mcp_server/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

from llmling import LLMCallableTool
from llmling.prompts import PromptMessage, StaticPrompt
from mcp.types import EmbeddedResource, ImageContent

from llmling_agent.log import get_logger
from llmling_agent.mcp_server.client import MCPClient
Expand All @@ -31,6 +30,8 @@

async def convert_mcp_prompt(client: MCPClient, prompt: MCPPrompt) -> StaticPrompt:
"""Convert MCP prompt to StaticPrompt."""
from mcp.types import EmbeddedResource, ImageContent

result = await client.get_prompt(prompt.name)
return StaticPrompt(
name=prompt.name,
Expand Down
3 changes: 2 additions & 1 deletion src/llmling_agent/models/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from toprompt import render_prompt
from typing_extensions import TypeVar
from upath.core import UPath
import yamling

from llmling_agent.common_types import EndStrategy # noqa: TC001
from llmling_agent.config import Capabilities, Knowledge
Expand Down Expand Up @@ -699,6 +698,8 @@ def from_file(cls, path: StrPath) -> Self:
Raises:
ValueError: If loading fails
"""
import yamling

try:
data = yamling.load_yaml_file(path, resolve_inherit=True)
# Set identifier as name if not set
Expand Down
2 changes: 1 addition & 1 deletion src/llmling_agent/models/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from pydantic import BaseModel
import tokonomics
from typing_extensions import TypeVar
import yamling

from llmling_agent.common_types import JsonObject, MessageRole # noqa: TC001
from llmling_agent.log import get_logger
Expand Down Expand Up @@ -245,6 +244,7 @@ def format(
or if style is invalid
"""
from jinja2 import Environment
import yamling

env = Environment(trim_blocks=True, lstrip_blocks=True)
env.filters["to_yaml"] = yamling.dump_yaml
Expand Down
5 changes: 3 additions & 2 deletions src/llmling_agent/models/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
from platformdirs import user_data_dir
from pydantic import BaseModel, ConfigDict, Field
from upath import UPath
import yamling


LogFormat = Literal["chronological", "conversations"]
FilterMode = Literal["and", "override"]
SupportedFormats = Literal["yaml", "toml", "json", "ini"]
FormatType = SupportedFormats | Literal["auto"]

APP_NAME: Final = "llmling-agent"
APP_AUTHOR: Final = "llmling"
Expand Down Expand Up @@ -89,7 +90,7 @@ class FileStorageConfig(BaseStorageProviderConfig):
path: str
"""Path to storage file (extension determines format unless specified)"""

format: yamling.FormatType = "auto"
format: FormatType = "auto"
"""Storage format (auto=detect from extension)"""

encoding: str = "utf-8"
Expand Down
3 changes: 2 additions & 1 deletion src/llmling_agent_commands/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from slashed import Command, CommandContext, CommandError
from slashed.completers import CallbackCompleter
import yamling

from llmling_agent.agent import Agent, AnyAgent
from llmling_agent_commands.completers import get_available_agents
Expand Down Expand Up @@ -123,6 +122,8 @@ async def show_agent(
ctx: CommandContext[AgentContext], args: list[str], kwargs: dict[str, str]
):
"""Show current agent's configuration."""
import yamling

if not ctx.context.agent.context:
await ctx.output.print("No agent context available")
return
Expand Down
5 changes: 4 additions & 1 deletion src/llmling_agent_storage/file_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from tokonomics.toko_types import TokenUsage
from upath import UPath
import yamling

from llmling_agent.common_types import JsonValue, MessageRole
from llmling_agent.log import get_logger
Expand Down Expand Up @@ -94,6 +93,8 @@ def __init__(self, config: FileStorageConfig):

def _load(self):
"""Load data from file if it exists."""
import yamling

if self.path.exists():
self._data = yamling.load_file(
self.path,
Expand All @@ -104,6 +105,8 @@ def _load(self):

def _save(self):
"""Save current data to file."""
import yamling

self.path.parent.mkdir(parents=True, exist_ok=True)
yamling.dump_file(self._data, self.path, mode=self.format) # pyright: ignore

Expand Down

0 comments on commit 079c2e9

Please sign in to comment.