ausername-12345 commited on
Commit
3edcc9b
·
1 Parent(s): b6479e4

use INFERENCE_API_URL as-is, no endpoint construction

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