(request: Input)
| 101 | ) |
| 102 | @modal.web_endpoint(method="POST", label=APP_NAME) |
| 103 | async def generate(request: Input) -> Output: |
| 104 | logging.info(f"Generating for model {request.model}") |
| 105 | |
| 106 | sample_params = SamplingParams( |
| 107 | n=request.n, |
| 108 | temperature=request.temperature, |
| 109 | max_tokens=request.max_tokens, |
| 110 | ) |
| 111 | |
| 112 | request_id = random_uuid() |
| 113 | |
| 114 | logging.info(f"Generating for request {request_id}") |
| 115 | |
| 116 | lora_request = None |
| 117 | if TESTING_LORA: |
| 118 | lora_request = LoRARequest( |
| 119 | request.model, abs(hash(request.model)), lora_model_cache_dir(MODEL_ID) |
| 120 | ) |
| 121 | |
| 122 | output_generator = engine.generate( |
| 123 | request.prompt, |
| 124 | sample_params, |
| 125 | request_id=request_id, |
| 126 | lora_request=lora_request, |
| 127 | ) |
| 128 | |
| 129 | final_output: Union[RequestOutput, None] = None |
| 130 | async for request_output in output_generator: |
| 131 | final_output = request_output |
| 132 | |
| 133 | if final_output is None: |
| 134 | raise Exception("No output generated") |
| 135 | |
| 136 | prompt_tokens = len(final_output.prompt_token_ids) |
| 137 | completion_tokens = sum(len(x.token_ids) for x in final_output.outputs) |
| 138 | |
| 139 | output = Output( |
| 140 | id=request_id, |
| 141 | choices=[ |
| 142 | Choice(text=choice.text, finish_reason=choice.finish_reason) |
| 143 | for choice in final_output.outputs |
| 144 | ], |
| 145 | usage=Usage( |
| 146 | prompt_tokens=prompt_tokens, |
| 147 | completion_tokens=completion_tokens, |
| 148 | ), |
| 149 | ) |
| 150 | |
| 151 | return output |
nothing calls this directly
no test coverage detected