From 49e4d688c6444b923607875a722324f5d01c86f0 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Tue, 4 Jul 2023 07:50:31 -0700 Subject: [PATCH] Removed .stream() method in favor of .prompt(stream=False) --- docs/python-api.md | 5 ++--- llm/cli.py | 8 +++----- llm/models.py | 14 +++++++------- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/docs/python-api.md b/docs/python-api.md index 1292c27..146094a 100644 --- a/docs/python-api.md +++ b/docs/python-api.md @@ -21,9 +21,8 @@ The `Model` class is an abstract base class that needs to be subclassed to provi Model instances provide the following methods: -- `prompt(prompt: str, ...options) -> Prompt` - a convenience wrapper which creates a `Prompt` instance and then executes it. This is the most common way to use LLM models. -- `stream(prompt: str) -> Response` - a convenient wrapper for `.execute(..., stream=True)`. -- `execute(prompt: Prompt, stream: bool) -> Response` - execute a prepared Prompt instance against the model and return a `Response`, streaming or not-streaming. +- `prompt(prompt: str, stream: bool, ...options) -> Prompt` - a convenience wrapper which creates a `Prompt` instance and then executes it. This is the most common way to use LLM models. +- `execute(prompt: Prompt, stream: bool) -> Response` - execute a prepared Prompt instance against the model and return a `Response`. Models usually return subclasses of `Response` that are specific to that model. diff --git a/llm/cli.py b/llm/cli.py index 7fcaf73..856f172 100644 --- a/llm/cli.py +++ b/llm/cli.py @@ -217,12 +217,10 @@ def prompt( raise click.ClickException(str(ex)) should_stream = model.can_stream and not no_stream - if should_stream: - method = model.stream - else: - method = model.prompt + if not should_stream: + validated_options["stream"] = False - response = method(prompt, system, **validated_options) + response = model.prompt(prompt, system, **validated_options) if should_stream: for chunk in response: diff --git a/llm/models.py b/llm/models.py index 00c1549..242aba1 100644 --- a/llm/models.py +++ b/llm/models.py @@ -144,18 +144,18 @@ class Model(ABC): return os.environ.get(self.key_env_var) return None - def prompt(self, prompt, system=None, **options): + def prompt( + self, + prompt: Optional[str], + system: Optional[str] = None, + stream: bool = False, + **options + ): return self.execute( Prompt(prompt, system=system, model=self, options=self.Options(**options)), stream=False, ) - def stream(self, prompt, system=None, **options): - return self.execute( - Prompt(prompt, system=system, model=self, options=self.Options(**options)), - stream=True, - ) - @abstractmethod def execute(self, prompt: Prompt, stream: bool = True) -> Response: pass