|
| 1 | +import requests |
| 2 | +from typing import Any, Dict, Optional |
| 3 | + |
| 4 | +from ..version import __version__ |
| 5 | + |
| 6 | + |
| 7 | +class Models: |
| 8 | + """Models lists all the models available in the Prediction Guard Platform. |
| 9 | +
|
| 10 | + Usage:: |
| 11 | +
|
| 12 | + import os |
| 13 | + import json |
| 14 | +
|
| 15 | + from predictionguard import PredictionGuard |
| 16 | +
|
| 17 | + # Set your Prediction Guard token as an environmental variable. |
| 18 | + os.environ["PREDICTIONGUARD_API_KEY"] = "<api key>" |
| 19 | +
|
| 20 | + client = PredictionGuard() |
| 21 | +
|
| 22 | + response = client.models.list() |
| 23 | +
|
| 24 | + print(json.dumps(response, sort_keys=True, indent=4, separators=(",", ": "))) |
| 25 | + """ |
| 26 | + |
| 27 | + def __init__(self, api_key, url): |
| 28 | + self.api_key = api_key |
| 29 | + self.url = url |
| 30 | + |
| 31 | + def list(self, capability: Optional[str] = "") -> Dict[str, Any]: |
| 32 | + """ |
| 33 | + Creates a models list request in the Prediction Guard REST API. |
| 34 | +
|
| 35 | + :param capability: The capability of models to list. |
| 36 | + :return: A dictionary containing the metadata of all the models. |
| 37 | + """ |
| 38 | + |
| 39 | + # Run _check_injection |
| 40 | + choices = self._list_models(capability) |
| 41 | + return choices |
| 42 | + |
| 43 | + def _list_models(self, capability): |
| 44 | + """ |
| 45 | + Function to list available models. |
| 46 | + """ |
| 47 | + |
| 48 | + capabilities = [ |
| 49 | + "chat-completion", "chat-with-image", "completion", |
| 50 | + "embedding", "embedding-with-image", "tokenize" |
| 51 | + ] |
| 52 | + |
| 53 | + headers = { |
| 54 | + "Content-Type": "application/json", |
| 55 | + "Authorization": "Bearer " + self.api_key, |
| 56 | + "User-Agent": "Prediction Guard Python Client: " + __version__, |
| 57 | + } |
| 58 | + |
| 59 | + models_path = "/models" |
| 60 | + if capability != "": |
| 61 | + if capability not in capabilities: |
| 62 | + raise ValueError( |
| 63 | + "If specifying a capability, please use one of the following: " |
| 64 | + + ", ".join(capabilities) |
| 65 | + ) |
| 66 | + else: |
| 67 | + models_path += "/" + capability |
| 68 | + |
| 69 | + response = requests.request( |
| 70 | + "GET", self.url + models_path, headers=headers |
| 71 | + ) |
| 72 | + |
| 73 | + if response.status_code == 200: |
| 74 | + ret = response.json() |
| 75 | + return ret |
| 76 | + elif response.status_code == 429: |
| 77 | + raise ValueError( |
| 78 | + "Could not connect to Prediction Guard API. " |
| 79 | + "Too many requests, rate limit or quota exceeded." |
| 80 | + ) |
| 81 | + else: |
| 82 | + # Check if there is a json body in the response. Read that in, |
| 83 | + # print out the error field in the json body, and raise an exception. |
| 84 | + err = "" |
| 85 | + try: |
| 86 | + err = response.json()["error"] |
| 87 | + except Exception: |
| 88 | + pass |
| 89 | + raise ValueError("Could not check for injection. " + err) |
0 commit comments