Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add templates module and update version #98

Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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=prompt_name, params=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