Skip to content

Commit

Permalink
fix: support running in wasm and improve performance of getting versi…
Browse files Browse the repository at this point in the history
…on of `openai`
  • Loading branch information
CNSeniorious000 committed Feb 6, 2024
1 parent 9c50c75 commit 5bc1291
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
pip install promplate
```

**Promplate** supports both CPython and PyPy, from `3.8` to `3.12`.
**Promplate** supports both CPython and PyPy, from `3.8` to `3.12`. It even supports running in browsers through wasm implementations of python.

## Documentation

Expand Down
4 changes: 3 additions & 1 deletion python/promplate/llm/openai/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ def bind(self, **run_config):

@cached_property
def _user_agent(self):
return get_user_agent(self, ("OpenAI", "openai"))
from openai.version import VERSION

return get_user_agent(self, ("OpenAI", VERSION))

@property
def _config(self): # type: ignore
Expand Down
14 changes: 12 additions & 2 deletions python/promplate/prompt/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from functools import cached_property, wraps
from functools import cache, cached_property, wraps
from inspect import currentframe, isclass
from re import compile
from typing import Any, Callable, ParamSpec, TypeVar
Expand Down Expand Up @@ -90,6 +90,16 @@ def get_builtins() -> dict[str, Any]:
return __builtins__ if isinstance(__builtins__, dict) else __builtins__.__dict__


@cache
def version(package: str):
from importlib.metadata import PackageNotFoundError, version

try:
return version(package)
except PackageNotFoundError:
return "-"


@cache_once
def get_user_agent(self, *additional_packages: tuple[str, str]):
from importlib.metadata import version
Expand All @@ -98,7 +108,7 @@ def get_user_agent(self, *additional_packages: tuple[str, str]):
return " ".join(
(
f"Promplate/{version('promplate')} ({self.__name__ if isclass(self) else self.__class__.__name__})",
*(f"{display_name}/{version(package)}" for display_name, package in additional_packages),
*(f"{name}/{v}" for name, v in additional_packages),
f"HTTPX/{version('httpx')}",
f"Python/{py_version.split()[0]}",
)
Expand Down
2 changes: 1 addition & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "promplate"
version = "0.3.3.1"
version = "0.3.3.2"
description = "cross-language prompt engineering framework"
homepage = "https://promplate.dev/"
documentation = "https://docs.py.promplate.dev/"
Expand Down

0 comments on commit 5bc1291

Please sign in to comment.