Execute the model with the given input. This method supports flexible argument passing. You can pass parameters usually, or pass a boolean as the second argument as a shorthand for streaming. Args: input (Any): The input data (text, URL, base64, etc).
(
self,
input: Any = None,
params: Union[Dict, bool, None] = None,
stream: bool = False
)
| 42 | self._ready = True |
| 43 | |
| 44 | def run( |
| 45 | self, |
| 46 | input: Any = None, |
| 47 | params: Union[Dict, bool, None] = None, |
| 48 | stream: bool = False |
| 49 | ): |
| 50 | """ |
| 51 | Execute the model with the given input. |
| 52 | |
| 53 | This method supports flexible argument passing. You can pass parameters usually, |
| 54 | or pass a boolean as the second argument as a shorthand for streaming. |
| 55 | |
| 56 | Args: |
| 57 | input (Any): The input data (text, URL, base64, etc). |
| 58 | params (Dict | bool, optional): Model parameters (e.g. temp, top_p) OR a boolean flag for streaming. |
| 59 | stream (bool, optional): Explicitly set the stream flag. |
| 60 | |
| 61 | Examples: |
| 62 | >>> # 1. Standard run |
| 63 | >>> model.run("Hello world") |
| 64 | |
| 65 | >>> # 2. With Parameters |
| 66 | >>> model.run("Hello world", {"temperature": 0.5}) |
| 67 | |
| 68 | >>> # 3. Shorthand Streaming (pass True as 2nd arg) |
| 69 | >>> model.run("Hello world", True) |
| 70 | |
| 71 | >>> # 4. Explicit Streaming with Parameters |
| 72 | >>> model.run("Hello world", {"temperature": 0.5}, stream=True) |
| 73 | |
| 74 | Returns: |
| 75 | dict | iter: Model output (JSON response or an iterable stream). |
| 76 | """ |
| 77 | if self._ready is False: |
| 78 | self._initialize() |
| 79 | |
| 80 | request_params = None |
| 81 | request_stream = stream |
| 82 | |
| 83 | # Check what the user passed into the second argument ('params') |
| 84 | if isinstance(params, bool): |
| 85 | # User passed `model.run(input, True)` -> Treat param as stream flag |
| 86 | request_stream = params |
| 87 | request_params = None |
| 88 | elif isinstance(params, dict): |
| 89 | # User passed `model.run(input, {...})` -> Standard usage |
| 90 | request_params = params |
| 91 | # We keep request_stream as whatever the 3rd arg is (default False) |
| 92 | else: |
| 93 | # User passed explicit None or something else; trust the named args |
| 94 | request_params = params |
| 95 | |
| 96 | post_body = { |
| 97 | "params": request_params, |
| 98 | "stream": request_stream, |
| 99 | "json": False if self._is_generating_media and request_stream else None, |
| 100 | } |
| 101 |