|
| 1 | +import requests |
| 2 | +from typing import Optional, Dict, Any, List |
| 3 | + |
| 4 | +class TeaScout: |
| 5 | + def __init__(self, url: str, key: Optional[str] = None): |
| 6 | + """ |
| 7 | + Initialize the TeaScout client. |
| 8 | + |
| 9 | + Args: |
| 10 | + url (str): The base URL of the TeaScout server (e.g., "http://localhost:5000"). |
| 11 | + key (str, optional): API Key if required by the server. |
| 12 | + """ |
| 13 | + self.base_url = url.rstrip('/') |
| 14 | + self.api_key = key |
| 15 | + self.session = requests.Session() |
| 16 | + if self.api_key: |
| 17 | + self.session.headers.update({'Authorization': f'Bearer {self.api_key}'}) |
| 18 | + |
| 19 | + def list_models(self) -> Dict[str, str]: |
| 20 | + """ |
| 21 | + List available models from the server. |
| 22 | + |
| 23 | + Returns: |
| 24 | + dict: A dictionary of model names and their descriptions. |
| 25 | + """ |
| 26 | + try: |
| 27 | + response = self.session.get(f"{self.base_url}/models") |
| 28 | + response.raise_for_status() |
| 29 | + return response.json() |
| 30 | + except requests.RequestException as e: |
| 31 | + raise ConnectionError(f"Failed to fetch models: {e}") |
| 32 | + |
| 33 | + def model(self, model_name: str) -> 'ModelContext': |
| 34 | + """ |
| 35 | + Select a model to work with. |
| 36 | + |
| 37 | + Args: |
| 38 | + model_name (str): The ID of the model to use. |
| 39 | + |
| 40 | + Returns: |
| 41 | + ModelContext: A context object for building the request. |
| 42 | + """ |
| 43 | + return ModelContext(self, model_name) |
| 44 | + |
| 45 | +class ModelContext: |
| 46 | + def __init__(self, client: TeaScout, model_name: str): |
| 47 | + self.client = client |
| 48 | + self.model_name = model_name |
| 49 | + self._text_content = None |
| 50 | + |
| 51 | + def text(self, content: str) -> 'ModelContext': |
| 52 | + """ |
| 53 | + Set the text content for inference. |
| 54 | + |
| 55 | + Args: |
| 56 | + content (str): The text to analyze. |
| 57 | + |
| 58 | + Returns: |
| 59 | + ModelContext: Returns self for chaining. |
| 60 | + """ |
| 61 | + self._text_content = content |
| 62 | + return self |
| 63 | + |
| 64 | + def inference(self) -> Dict[str, Any]: |
| 65 | + """ |
| 66 | + Execute the inference request. |
| 67 | + |
| 68 | + Returns: |
| 69 | + dict: The inference result from the server. |
| 70 | + |
| 71 | + Raises: |
| 72 | + ValueError: If text content is not set. |
| 73 | + ConnectionError: If the request fails. |
| 74 | + """ |
| 75 | + if self._text_content is None: |
| 76 | + raise ValueError("Text content must be set using .text() before calling .inference()") |
| 77 | + |
| 78 | + url = f"{self.client.base_url}/inference/{self.model_name}" |
| 79 | + payload = {"text": self._text_content} |
| 80 | + |
| 81 | + try: |
| 82 | + response = self.client.session.post(url, json=payload) |
| 83 | + response.raise_for_status() |
| 84 | + return response.json() |
| 85 | + except requests.RequestException as e: |
| 86 | + raise ConnectionError(f"Inference request failed: {e}") |
0 commit comments