Skip to content

Commit

Permalink
chore: cache llm calls
Browse files Browse the repository at this point in the history
  • Loading branch information
phil65 committed Nov 16, 2024
1 parent b7dcb78 commit 7c4e11b
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion mknodes/templatenodes/mkllm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import functools
from typing import Any, TYPE_CHECKING

from jinjarope import llmfilters
Expand All @@ -17,6 +18,16 @@
logger = log.get_logger(__name__)


@functools.cache
def complete_llm(user_prompt: str, system_prompt: str, model: str, context: str) -> str:
return llmfilters.llm_complete(
user_prompt,
system_prompt,
model=model,
context=context,
)


class MkLlm(mktext.MkText):
"""Node for LLM-based text generation."""

Expand Down Expand Up @@ -88,7 +99,7 @@ def text(self) -> str:
"\n".join(filter(None, [self._context, *context_items])) or None
)

return llmfilters.llm_complete(
return complete_llm(
self.user_prompt,
self.system_prompt,
model=self._model,
Expand Down

0 comments on commit 7c4e11b

Please sign in to comment.