ausername-12345 commited on
Commit
56dbcd5
·
1 Parent(s): 3ee7765

add 503 fallback: retry without tools then fall back to Gen endpoint

Browse files
Files changed (1) hide show
  1. src/inferenceClient.js +63 -18
src/inferenceClient.js CHANGED
@@ -1,8 +1,13 @@
1
- const API_URL = process.env.INFERENCE_API_URL;
2
  const API_KEY = process.env.INFERENCE_API_KEY;
3
  const FETCH_TIMEOUT = 30000;
4
 
 
 
 
 
5
  let currentModel = process.env.INFERENCE_MODEL || 'lightning';
 
6
 
7
  const AVAILABLE_MODELS = [
8
  'lightning',
@@ -20,13 +25,32 @@ function setModel(name) {
20
  }
21
  }
22
 
23
- if (!API_URL) {
24
  console.warn('[inferenceClient] INFERENCE_API_URL is not set - chat requests will fail.');
25
  }
26
  if (!API_KEY) {
27
  console.warn('[inferenceClient] INFERENCE_API_KEY is not set - chat requests will fail.');
28
  }
29
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  // messages: OpenAI-style chat messages array
31
  // tools: OpenAI-style tools array (optional)
32
  async function chat(messages, tools = []) {
@@ -34,25 +58,15 @@ async function chat(messages, tools = []) {
34
  model: currentModel,
35
  messages,
36
  };
37
- if (tools && tools.length) {
38
  body.tools = tools;
39
- body.tool_choice = 'auto';
40
  }
41
 
42
  let res;
 
 
43
  try {
44
- const controller = new AbortController();
45
- const timeout = setTimeout(() => controller.abort(), FETCH_TIMEOUT);
46
- res = await fetch(API_URL, {
47
- method: 'POST',
48
- headers: {
49
- 'Content-Type': 'application/json',
50
- Authorization: `Bearer ${API_KEY}`,
51
- },
52
- body: JSON.stringify(body),
53
- signal: controller.signal,
54
- });
55
- clearTimeout(timeout);
56
  } catch (e) {
57
  if (e.name === 'AbortError') {
58
  throw new Error(`Inference request timed out after ${FETCH_TIMEOUT / 1000}s`);
@@ -60,6 +74,33 @@ async function chat(messages, tools = []) {
60
  throw new Error(`Could not reach the Inference Port API: ${e.message}`);
61
  }
62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  if (!res.ok) {
64
  let detail = '';
65
  try {
@@ -67,7 +108,7 @@ async function chat(messages, tools = []) {
67
  } catch {
68
  /* ignore */
69
  }
70
- throw new Error(`Inference Port API returned ${res.status}: ${detail.slice(0, 500)}`);
71
  }
72
 
73
  const data = await res.json();
@@ -78,4 +119,8 @@ async function chat(messages, tools = []) {
78
  return choice.message;
79
  }
80
 
81
- module.exports = { chat, getModel, setModel, AVAILABLE_MODELS, API_URL };
 
 
 
 
 
1
+ const BASE_URL = process.env.INFERENCE_API_URL;
2
  const API_KEY = process.env.INFERENCE_API_KEY;
3
  const FETCH_TIMEOUT = 30000;
4
 
5
+ const GEN_ENDPOINT = (process.env.INFERENCE_GEN_URL || BASE_URL).replace(/\/+$/, '') + '/gen/chat/completions';
6
+ const V1_ENDPOINT = (process.env.INFERENCE_V1_URL || BASE_URL).replace(/\/+$/, '') + '/v1/chat/completions';
7
+
8
+ let currentEndpoint = V1_ENDPOINT;
9
  let currentModel = process.env.INFERENCE_MODEL || 'lightning';
10
+ let sendTools = process.env.SEND_TOOLS !== 'false';
11
 
12
  const AVAILABLE_MODELS = [
13
  'lightning',
 
25
  }
26
  }
27
 
28
+ if (!BASE_URL) {
29
  console.warn('[inferenceClient] INFERENCE_API_URL is not set - chat requests will fail.');
30
  }
31
  if (!API_KEY) {
32
  console.warn('[inferenceClient] INFERENCE_API_KEY is not set - chat requests will fail.');
33
  }
34
 
35
+ async function tryEndpoint(endpoint, body) {
36
+ const controller = new AbortController();
37
+ const timeout = setTimeout(() => controller.abort(), FETCH_TIMEOUT);
38
+ try {
39
+ const res = await fetch(endpoint, {
40
+ method: 'POST',
41
+ headers: {
42
+ 'Content-Type': 'application/json',
43
+ Authorization: `Bearer ${API_KEY}`,
44
+ },
45
+ body: JSON.stringify(body),
46
+ signal: controller.signal,
47
+ });
48
+ return res;
49
+ } finally {
50
+ clearTimeout(timeout);
51
+ }
52
+ }
53
+
54
  // messages: OpenAI-style chat messages array
55
  // tools: OpenAI-style tools array (optional)
56
  async function chat(messages, tools = []) {
 
58
  model: currentModel,
59
  messages,
60
  };
61
+ if (sendTools && tools && tools.length) {
62
  body.tools = tools;
 
63
  }
64
 
65
  let res;
66
+ let usedEndpoint = currentEndpoint;
67
+
68
  try {
69
+ res = await tryEndpoint(currentEndpoint, body);
 
 
 
 
 
 
 
 
 
 
 
70
  } catch (e) {
71
  if (e.name === 'AbortError') {
72
  throw new Error(`Inference request timed out after ${FETCH_TIMEOUT / 1000}s`);
 
74
  throw new Error(`Could not reach the Inference Port API: ${e.message}`);
75
  }
76
 
77
+ if (res.status === 503 && sendTools && tools.length) {
78
+ console.warn('[inferenceClient] 503 with tools - retrying without tools');
79
+ const strippedBody = { model: currentModel, messages };
80
+ try {
81
+ const retry = await tryEndpoint(currentEndpoint, strippedBody);
82
+ if (retry.ok) {
83
+ res = retry;
84
+ }
85
+ } catch {
86
+ /* ignore */
87
+ }
88
+ }
89
+
90
+ if (res.status === 503 && usedEndpoint === V1_ENDPOINT) {
91
+ console.warn('[inferenceClient] 503 on V1 endpoint - falling back to Gen endpoint');
92
+ const strippedBody = { model: currentModel, messages };
93
+ try {
94
+ const retry = await tryEndpoint(GEN_ENDPOINT, strippedBody);
95
+ if (retry.ok) {
96
+ res = retry;
97
+ usedEndpoint = GEN_ENDPOINT;
98
+ }
99
+ } catch {
100
+ /* ignore */
101
+ }
102
+ }
103
+
104
  if (!res.ok) {
105
  let detail = '';
106
  try {
 
108
  } catch {
109
  /* ignore */
110
  }
111
+ throw new Error(`Inference Port API (${res.url}) returned ${res.status}: ${detail.slice(0, 500)}`);
112
  }
113
 
114
  const data = await res.json();
 
119
  return choice.message;
120
  }
121
 
122
+ function useGenEndpoint() { currentEndpoint = GEN_ENDPOINT; }
123
+ function useV1Endpoint() { currentEndpoint = V1_ENDPOINT; }
124
+ function getEndpoint() { return currentEndpoint; }
125
+
126
+ module.exports = { chat, getModel, setModel, useGenEndpoint, useV1Endpoint, getEndpoint, AVAILABLE_MODELS, BASE_URL };