Skip to content

Commit

Permalink
Add templates module and update version (#98)
Browse files Browse the repository at this point in the history
* Add templates module and update version

* Add import statement to __init__.py and remove blank lines from templates.py

* Change API request method from GET to POST in get_prompt_template function

* Refactor get_prompt_template function signature

* Refactor GetPromptTemplate class to remove optional types

* Refactor get() function signature in templates.py

Update get_prompt_template() function signature in utils.py
  • Loading branch information
abubakarsohail authored Jan 16, 2024
1 parent 81206f5 commit 87d2036
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 3 deletions.
6 changes: 4 additions & 2 deletions promptlayer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

from promptlayer.promptlayer import PromptLayerBase

from . import templates

api_key = os.environ.get("PROMPTLAYER_API_KEY")


def __getattr__(
name: Union[Literal["openai"], Literal["anthropic"], Literal["prompts"]]
name: Union[Literal["openai"], Literal["anthropic"], Literal["prompts"]],
):
if name == "openai":
import openai as openai_module
Expand Down Expand Up @@ -39,4 +41,4 @@ def __getattr__(
raise AttributeError(f"module {__name__} has no attribute {name}")


__all__ = ["api_key", "openai", "anthropic"]
__all__ = ["api_key", "openai", "anthropic", "templates"]
8 changes: 8 additions & 0 deletions promptlayer/templates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from typing import Union

from promptlayer.types.prompt_template import GetPromptTemplate
from promptlayer.utils import get_prompt_template


def get(prompt_name: str, params: Union[GetPromptTemplate, None] = None):
return get_prompt_template(prompt_name, params)
3 changes: 3 additions & 0 deletions promptlayer/types/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import prompt_template

__all__ = ["prompt_template"]
8 changes: 8 additions & 0 deletions promptlayer/types/prompt_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from typing import Dict, TypedDict


class GetPromptTemplate(TypedDict, total=False):
version: int
label: str
provider: str
input_variables: Dict[str, str]
25 changes: 25 additions & 0 deletions promptlayer/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import sys
import types
from copy import deepcopy
from typing import Union

import requests

import promptlayer
from promptlayer.types.prompt_template import GetPromptTemplate

URL_API_PROMPTLAYER = os.environ.setdefault(
"URL_API_PROMPTLAYER", "https://api.promptlayer.com"
Expand Down Expand Up @@ -514,3 +516,26 @@ def promptlayer_track_group(request_id, group_id):
raise Exception(
f"PromptLayer had the following error while tracking your group: {e}"
)


def get_prompt_template(
prompt_name: str, params: Union[GetPromptTemplate, None] = None
):
try:
json_body = {"api_key": get_api_key()}
if params:
json_body = {**json_body, **params}
response = requests.post(
f"{URL_API_PROMPTLAYER}/prompt-templates/{prompt_name}",
headers={"X-API-KEY": get_api_key()},
json=json_body,
)
if response.status_code != 200:
raise Exception(
f"PromptLayer had the following error while getting your prompt template: {response.text}"
)
return response.json()
except requests.exceptions.RequestException as e:
raise Exception(
f"PromptLayer had the following error while getting your prompt template: {e}"
)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "promptlayer"
version = "0.4.0"
version = "0.4.1"
description = "PromptLayer is a platform for prompt engineering and tracks your LLM requests."
authors = ["Magniv <[email protected]>"]
license = "Apache-2.0"
Expand Down

0 comments on commit 87d2036

Please sign in to comment.