(args: ModalInput)
| 52 | }.modal.run`; |
| 53 | |
| 54 | export const runInference = async (args: ModalInput) => { |
| 55 | const resp = await fetch(inferenceBase, { |
| 56 | method: "POST", |
| 57 | body: JSON.stringify(args), |
| 58 | headers: { |
| 59 | "Content-Type": "application/json", |
| 60 | }, |
| 61 | }); |
| 62 | |
| 63 | const respText = await resp.text(); |
| 64 | if (!resp.ok) { |
| 65 | captureException("Failed to run inference.", { |
| 66 | extra: { |
| 67 | response: respText, |
| 68 | ...pick(args, ["model", "n", "max_tokens", "temperature"]), |
| 69 | }, |
| 70 | }); |
| 71 | throw new Error("Failed to run inference"); |
| 72 | } |
| 73 | |
| 74 | let json; |
| 75 | try { |
| 76 | json = JSON.parse(respText); |
| 77 | } catch (e) { |
| 78 | // captureException |
| 79 | captureException("Failed to parse response from modal.", { |
| 80 | extra: { |
| 81 | response: respText, |
| 82 | ...pick(args, ["model", "n", "max_tokens", "temperature"]), |
| 83 | }, |
| 84 | }); |
| 85 | throw new Error("Failed to parse LLM response"); |
| 86 | } |
| 87 | const output = outputSchema.safeParse(json); |
| 88 | if (output.success) { |
| 89 | return output.data; |
| 90 | } else { |
| 91 | captureException("Failed to validate output from modal.", { |
| 92 | extra: { |
| 93 | response: respText, |
| 94 | ...pick(args, ["model", "n", "max_tokens", "temperature"]), |
| 95 | }, |
| 96 | }); |
| 97 | throw new Error("Failed to validate LLM response"); |
| 98 | } |
| 99 | }; |
no outgoing calls
no test coverage detected