|
| 1 | +from typing import Any, Dict, List, Union, cast, Generator, Literal |
| 2 | +from typing_extensions import NotRequired, TypedDict |
| 3 | +from .request import Request, RequestConfig |
| 4 | +from .async_request import AsyncRequest |
| 5 | +from typing import List, Union |
| 6 | +from ._config import ClientConfig |
| 7 | +from .helpers import build_path |
| 8 | + |
| 9 | + |
| 10 | +class PromptEngineResult(TypedDict): |
| 11 | + prompt: str |
| 12 | + return_prompt: str |
| 13 | + id: str |
| 14 | + |
| 15 | + |
| 16 | +class PromptEngineRunParams(TypedDict): |
| 17 | + prompt: str |
| 18 | + inputs: NotRequired[List[object]] |
| 19 | + return_prompt: Union[str, List[object], Dict[str, str]] |
| 20 | + input_values: NotRequired[Dict[str, str]] |
| 21 | + stream: Union[bool, None] = False |
| 22 | + use_internet: Union[bool, None] = False |
| 23 | + prompt_guard: NotRequired[ |
| 24 | + Literal[ |
| 25 | + "defamation", |
| 26 | + "specialized_advice", |
| 27 | + "privacy", |
| 28 | + "intellectual_property", |
| 29 | + "indiscriminate_weapons", |
| 30 | + "hate", |
| 31 | + "sexual_content", |
| 32 | + "elections", |
| 33 | + "code_interpreter_abuse", |
| 34 | + ] |
| 35 | + ] |
| 36 | + |
| 37 | + |
| 38 | +class PromptEngineExecuteParams(TypedDict): |
| 39 | + id: str |
| 40 | + input_values: object |
| 41 | + stream: Union[bool, None] = False |
| 42 | + |
| 43 | + |
| 44 | +class PromptEngineRunResponse(TypedDict): |
| 45 | + success: bool |
| 46 | + result: Any |
| 47 | + |
| 48 | + |
| 49 | +class PromptEngineCreateParams(TypedDict): |
| 50 | + prompt: str |
| 51 | + inputs: NotRequired[List[object]] |
| 52 | + return_prompt: Union[str, List[object], Dict[str, str]] |
| 53 | + use_internet: Union[bool, None] = False |
| 54 | + optimize_prompt: Union[bool, None] = False |
| 55 | + prompt_guard: NotRequired[ |
| 56 | + Literal[ |
| 57 | + "defamation", |
| 58 | + "specialized_advice", |
| 59 | + "privacy", |
| 60 | + "intellectual_property", |
| 61 | + "indiscriminate_weapons", |
| 62 | + "hate", |
| 63 | + "sexual_content", |
| 64 | + "elections", |
| 65 | + "code_interpreter_abuse", |
| 66 | + ] |
| 67 | + ] |
| 68 | + |
| 69 | + |
| 70 | +class PromptEngineCreateResponse(TypedDict): |
| 71 | + success: bool |
| 72 | + prompt_engine_id: str |
| 73 | + |
| 74 | + |
| 75 | +class PromptEngineGetResponse(PromptEngineResult): |
| 76 | + success: bool |
| 77 | + |
| 78 | + |
| 79 | +class PromptEngineListResponse(TypedDict): |
| 80 | + success: bool |
| 81 | + prompt_engines: List[PromptEngineResult] |
| 82 | + |
| 83 | + |
| 84 | +class PromptEngineListParams(TypedDict): |
| 85 | + limit: str |
| 86 | + page: str |
| 87 | + |
| 88 | + |
| 89 | +class PromptEngineDeleteResponse(TypedDict): |
| 90 | + prompt_engine_id: str |
| 91 | + |
| 92 | + |
| 93 | +class PromptEngine(ClientConfig): |
| 94 | + config: RequestConfig |
| 95 | + |
| 96 | + def __init__( |
| 97 | + self, |
| 98 | + api_key: str, |
| 99 | + api_url: str, |
| 100 | + disable_request_logging: Union[bool, None] = False, |
| 101 | + ): |
| 102 | + super().__init__(api_key, api_url, disable_request_logging) |
| 103 | + self.config = RequestConfig( |
| 104 | + api_url=api_url, |
| 105 | + api_key=api_key, |
| 106 | + disable_request_logging=disable_request_logging, |
| 107 | + ) |
| 108 | + |
| 109 | + def create(self, params: PromptEngineCreateParams) -> PromptEngineCreateResponse: |
| 110 | + path = "/prompt_engine" |
| 111 | + resp = Request( |
| 112 | + config=self.config, |
| 113 | + path=path, |
| 114 | + params=cast(Dict[Any, Any], params), |
| 115 | + verb="post", |
| 116 | + ).perform_with_content() |
| 117 | + return resp |
| 118 | + |
| 119 | + def get(self, id: str) -> PromptEngineGetResponse: |
| 120 | + path = f"/prompt_engine/{id}" |
| 121 | + resp = Request( |
| 122 | + config=self.config, path=path, params={}, verb="get" |
| 123 | + ).perform_with_content() |
| 124 | + return resp |
| 125 | + |
| 126 | + def list( |
| 127 | + self, params: Union[PromptEngineListParams, None] = None |
| 128 | + ) -> PromptEngineListResponse: |
| 129 | + if params is None: |
| 130 | + params = {} |
| 131 | + |
| 132 | + # Default limit and page to 20 and 1 respectively |
| 133 | + if params.get("limit") is None: |
| 134 | + params["limit"] = 20 |
| 135 | + |
| 136 | + if params.get("page") is None: |
| 137 | + params["page"] = 0 |
| 138 | + |
| 139 | + path = build_path( |
| 140 | + base_path="/prompt_engine", |
| 141 | + params=params, |
| 142 | + ) |
| 143 | + resp = Request( |
| 144 | + config=self.config, path=path, params={}, verb="get" |
| 145 | + ).perform_with_content() |
| 146 | + return resp |
| 147 | + |
| 148 | + def delete(self, id: str) -> PromptEngineDeleteResponse: |
| 149 | + path = f"/prompt_engine/{id}" |
| 150 | + resp = Request( |
| 151 | + config=self.config, |
| 152 | + path=path, |
| 153 | + params={}, |
| 154 | + verb="DELETE", |
| 155 | + ).perform_with_content() |
| 156 | + return resp |
| 157 | + |
| 158 | + def run_prompt_direct( |
| 159 | + self, params: PromptEngineRunParams |
| 160 | + ) -> Union[PromptEngineRunResponse, Generator[Any, None, None]]: |
| 161 | + path = "/prompt_engine/run" |
| 162 | + stream = params.get("stream") |
| 163 | + if stream: |
| 164 | + resp = Request( |
| 165 | + config=self.config, |
| 166 | + path=path, |
| 167 | + params=cast(Dict[Any, Any], params), |
| 168 | + verb="post", |
| 169 | + ).perform_with_content_streaming() |
| 170 | + return resp |
| 171 | + |
| 172 | + resp = Request( |
| 173 | + config=self.config, |
| 174 | + path=path, |
| 175 | + params=cast(Dict[Any, Any], params), |
| 176 | + verb="post", |
| 177 | + ).perform_with_content() |
| 178 | + return resp |
| 179 | + |
| 180 | + def run( |
| 181 | + self, params: PromptEngineExecuteParams |
| 182 | + ) -> Union[PromptEngineRunResponse, Generator[Any, None, None]]: |
| 183 | + id = params.get("id") |
| 184 | + path = f"/prompt_engine/{id}" |
| 185 | + stream = params.get("stream") |
| 186 | + |
| 187 | + if stream: |
| 188 | + resp = Request( |
| 189 | + config=self.config, |
| 190 | + path=path, |
| 191 | + params=cast(Dict[Any, Any], params), |
| 192 | + verb="post", |
| 193 | + ).perform_with_content_streaming() |
| 194 | + return resp |
| 195 | + |
| 196 | + resp = Request( |
| 197 | + config=self.config, |
| 198 | + path=path, |
| 199 | + params=cast(Dict[Any, Any], params), |
| 200 | + verb="post", |
| 201 | + ).perform_with_content() |
| 202 | + return resp |
| 203 | + |
| 204 | + |
| 205 | +class AsyncPromptEngine(ClientConfig): |
| 206 | + config: RequestConfig |
| 207 | + |
| 208 | + def __init__( |
| 209 | + self, |
| 210 | + api_key: str, |
| 211 | + api_url: str, |
| 212 | + disable_request_logging: Union[bool, None] = False, |
| 213 | + ): |
| 214 | + super().__init__(api_key, api_url, disable_request_logging) |
| 215 | + self.config = RequestConfig( |
| 216 | + api_url=api_url, |
| 217 | + api_key=api_key, |
| 218 | + disable_request_logging=disable_request_logging, |
| 219 | + ) |
| 220 | + |
| 221 | + async def create( |
| 222 | + self, params: PromptEngineCreateParams |
| 223 | + ) -> PromptEngineCreateResponse: |
| 224 | + path = "/prompt_engine" |
| 225 | + resp = await AsyncRequest( |
| 226 | + config=self.config, |
| 227 | + path=path, |
| 228 | + params=cast(Dict[Any, Any], params), |
| 229 | + verb="post", |
| 230 | + ).perform_with_content() |
| 231 | + return resp |
| 232 | + |
| 233 | + async def get(self, id: str) -> PromptEngineGetResponse: |
| 234 | + path = f"/prompt_engine/{id}" |
| 235 | + resp = await AsyncRequest( |
| 236 | + config=self.config, path=path, params={}, verb="get" |
| 237 | + ).perform_with_content() |
| 238 | + return resp |
| 239 | + |
| 240 | + async def list( |
| 241 | + self, params: Union[PromptEngineListParams, None] = None |
| 242 | + ) -> PromptEngineListResponse: |
| 243 | + if params is None: |
| 244 | + params = {} |
| 245 | + |
| 246 | + # Default limit and page to 20 and 1 respectively |
| 247 | + if params.get("limit") is None: |
| 248 | + params["limit"] = 20 |
| 249 | + |
| 250 | + if params.get("page") is None: |
| 251 | + params["page"] = 0 |
| 252 | + |
| 253 | + path = build_path( |
| 254 | + base_path="/prompt_engine", |
| 255 | + params=params, |
| 256 | + ) |
| 257 | + resp = await AsyncRequest( |
| 258 | + config=self.config, path=path, params={}, verb="get" |
| 259 | + ).perform_with_content() |
| 260 | + return resp |
| 261 | + |
| 262 | + async def delete(self, id: str) -> PromptEngineDeleteResponse: |
| 263 | + path = f"/prompt_engine/{id}" |
| 264 | + resp = await AsyncRequest( |
| 265 | + config=self.config, |
| 266 | + path=path, |
| 267 | + params={}, |
| 268 | + verb="DELETE", |
| 269 | + ).perform_with_content() |
| 270 | + return resp |
| 271 | + |
| 272 | + async def run_prompt_direct( |
| 273 | + self, params: PromptEngineRunParams |
| 274 | + ) -> Union[PromptEngineRunResponse, Generator[Any, None, None]]: |
| 275 | + path = "/prompt_engine/run" |
| 276 | + stream = params.get("stream") |
| 277 | + if stream: |
| 278 | + resp = await AsyncRequest( |
| 279 | + config=self.config, |
| 280 | + path=path, |
| 281 | + params=cast(Dict[Any, Any], params), |
| 282 | + verb="post", |
| 283 | + ).perform_with_content_streaming() |
| 284 | + return resp |
| 285 | + resp = await AsyncRequest( |
| 286 | + config=self.config, |
| 287 | + path=path, |
| 288 | + params=cast(Dict[Any, Any], params), |
| 289 | + verb="post", |
| 290 | + ).perform_with_content() |
| 291 | + return resp |
| 292 | + |
| 293 | + async def run( |
| 294 | + self, params: PromptEngineExecuteParams |
| 295 | + ) -> Union[PromptEngineRunResponse, Generator[Any, None, None]]: |
| 296 | + id = params.get("id") |
| 297 | + path = f"/prompt_engine/{id}" |
| 298 | + stream = params.get("stream") |
| 299 | + |
| 300 | + if stream: |
| 301 | + resp = await AsyncRequest( |
| 302 | + config=self.config, |
| 303 | + path=path, |
| 304 | + params=cast(Dict[Any, Any], params), |
| 305 | + verb="post", |
| 306 | + ).perform_with_content_streaming() |
| 307 | + return resp |
| 308 | + |
| 309 | + resp = await AsyncRequest( |
| 310 | + config=self.config, |
| 311 | + path=path, |
| 312 | + params=cast(Dict[Any, Any], params), |
| 313 | + verb="post", |
| 314 | + ).perform_with_content() |
| 315 | + return resp |
0 commit comments