-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeout.ts
More file actions
88 lines (76 loc) · 2.42 KB
/
timeout.ts
File metadata and controls
88 lines (76 loc) · 2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* Timeout Example (real LLM call)
*
* Demonstrates:
* - Setting per-request and per-tool timeouts via `timeout`
* - Timeout as a first-class option (no manual AbortSignal needed)
*
* Usage: bun run dev timeout [request-timeout-ms]
* Default timeout: 5000ms
*/
import { z } from "zod";
import { createLLM, prompt, s } from "@/index";
const provider = (process.env.LLM_PROVIDER ?? "openai-compatible") as
| "openai-compatible"
| "anthropic-compatible";
const model = process.env.LLM_MODEL ?? "gpt-5-nano";
const baseURL = process.env.LLM_BASE_URL;
const apiKey = process.env.LLM_API_KEY;
const debugEnabled = process.env.STRUCTURED_DEBUG === "1";
if (!apiKey) {
console.error("Missing LLM_API_KEY in environment.");
console.error("Set it before running: bun run dev timeout");
process.exit(1);
}
const requestTimeoutMs = Number(process.argv[3] ?? "5000");
if (!Number.isFinite(requestTimeoutMs) || requestTimeoutMs <= 0) {
console.error(`Invalid timeout: "${process.argv[3] ?? ""}"`);
process.exit(1);
}
const llm = createLLM({
provider,
model,
transport: { baseURL, apiKey },
defaults: {
mode: "strict",
selfHeal: false,
debug: debugEnabled,
},
});
const SummarySchema = s.schema(
"Summary",
z.object({
topic: s.string().min(1).describe("The topic being summarized"),
summary: s.string().min(1).describe("A concise summary"),
}),
);
console.log(`Provider: ${provider}`);
console.log(`Model: ${model}`);
console.log(`Request timeout: ${requestTimeoutMs}ms`);
console.log("Launching generation...\n");
try {
const result = await llm.structured(
SummarySchema,
prompt`Summarize the concept of "structured data extraction from LLMs" in a few sentences.`,
{
timeout: {
request: requestTimeoutMs,
},
},
);
console.log("Result:", JSON.stringify(result.data, null, 2));
console.log(`\nTokens used: ${result.usage?.totalTokens ?? "unknown"}`);
} catch (error) {
if (isTimeoutError(error)) {
console.log(`Request timed out after ${requestTimeoutMs}ms.`);
console.log("Try a larger value, e.g. `bun run dev timeout 10000`.");
process.exit(0);
}
console.error("Request failed:", error);
process.exit(1);
}
function isTimeoutError(error: unknown): boolean {
if (!error || typeof error !== "object") return false;
const name = "name" in error ? (error as { name: unknown }).name : undefined;
return name === "AbortError" || name === "TimeoutError";
}