prompts.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import json
  2. from typing import Optional
  3. from shared.api.models.base import (
  4. WrappedBooleanResponse,
  5. WrappedGenericMessageResponse,
  6. )
  7. from shared.api.models.management.responses import (
  8. WrappedPromptResponse,
  9. WrappedPromptsResponse,
  10. )
  11. class PromptsSDK:
  12. def __init__(self, client):
  13. self.client = client
  14. async def create(
  15. self, name: str, template: str, input_types: dict
  16. ) -> WrappedGenericMessageResponse:
  17. """
  18. Create a new prompt.
  19. Args:
  20. name (str): The name of the prompt
  21. template (str): The template string for the prompt
  22. input_types (dict): A dictionary mapping input names to their types
  23. Returns:
  24. dict: Created prompt information
  25. """
  26. data = {"name": name, "template": template, "input_types": input_types}
  27. return await self.client._make_request(
  28. "POST",
  29. "prompts",
  30. json=data,
  31. version="v3",
  32. )
  33. async def list(self) -> WrappedPromptsResponse:
  34. """
  35. List all available prompts.
  36. Returns:
  37. dict: List of all available prompts
  38. """
  39. return await self.client._make_request(
  40. "GET",
  41. "prompts",
  42. version="v3",
  43. )
  44. async def retrieve(
  45. self,
  46. name: str,
  47. inputs: Optional[dict] = None,
  48. prompt_override: Optional[str] = None,
  49. ) -> WrappedPromptResponse:
  50. """
  51. Get a specific prompt by name, optionally with inputs and override.
  52. Args:
  53. name (str): The name of the prompt to retrieve
  54. inputs (Optional[dict]): JSON-encoded inputs for the prompt
  55. prompt_override (Optional[str]): An override for the prompt template
  56. Returns:
  57. dict: The requested prompt with applied inputs and/or override
  58. """
  59. params = {}
  60. if inputs:
  61. params["inputs"] = json.dumps(inputs)
  62. if prompt_override:
  63. params["prompt_override"] = prompt_override
  64. return await self.client._make_request(
  65. "POST",
  66. f"prompts/{name}",
  67. params=params,
  68. version="v3",
  69. )
  70. async def update(
  71. self,
  72. name: str,
  73. template: Optional[str] = None,
  74. input_types: Optional[dict] = None,
  75. ) -> WrappedGenericMessageResponse:
  76. """
  77. Update an existing prompt's template and/or input types.
  78. Args:
  79. name (str): The name of the prompt to update
  80. template (Optional[str]): The updated template string for the prompt
  81. input_types (Optional[dict]): The updated dictionary mapping input names to their types
  82. Returns:
  83. dict: The updated prompt details
  84. """
  85. data: dict = {}
  86. if template:
  87. data["template"] = template
  88. if input_types:
  89. data["input_types"] = json.dumps(input_types)
  90. return await self.client._make_request(
  91. "PUT",
  92. f"prompts/{name}",
  93. json=data,
  94. version="v3",
  95. )
  96. async def delete(self, name: str) -> WrappedBooleanResponse:
  97. """
  98. Delete a prompt by name.
  99. Args:
  100. name (str): The name of the prompt to delete
  101. Returns:
  102. bool: True if deletion was successful
  103. """
  104. return await self.client._make_request(
  105. "DELETE",
  106. f"prompts/{name}",
  107. version="v3",
  108. )