-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllmClient.js
More file actions
148 lines (127 loc) · 4.22 KB
/
Copy pathllmClient.js
File metadata and controls
148 lines (127 loc) · 4.22 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
'use strict';
const log4js = require('ep_etherpad-lite/node_modules/log4js');
const logger = log4js.getLogger('ep_ai_core:llm');
/**
* Detect provider from config.
* @param {object} config
* @returns {'anthropic'|'openai'}
*/
const detectProvider = (config) => {
if (config.provider) return config.provider;
if (config.apiBaseUrl && config.apiBaseUrl.includes('anthropic.com')) return 'anthropic';
return 'openai';
};
/**
* Send a request to the Anthropic Messages API.
*/
const completeAnthropic = async (baseUrl, apiKey, model, messages, maxTokens) => {
const url = `${baseUrl}/messages`;
// Anthropic separates system prompt from messages
let system;
const userMessages = [];
for (const msg of messages) {
if (msg.role === 'system') {
system = system ? `${system}\n\n${msg.content}` : msg.content;
} else {
userMessages.push({role: msg.role, content: msg.content});
}
}
const body = {
model,
max_tokens: maxTokens || 4096,
messages: userMessages,
};
if (system) body.system = system;
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': apiKey,
'anthropic-version': '2023-06-01',
},
body: JSON.stringify(body),
});
if (!response.ok) {
const text = await response.text();
let errorMsg;
try {
const errorJson = JSON.parse(text);
errorMsg = errorJson.error?.message || text;
} catch {
errorMsg = text;
}
throw new Error(`LLM API error ${response.status}: ${errorMsg}`);
}
const data = await response.json();
// Anthropic returns content as array of blocks
const content = (data.content || [])
.filter((b) => b.type === 'text')
.map((b) => b.text)
.join('');
const usage = data.usage
? {
prompt_tokens: data.usage.input_tokens,
completion_tokens: data.usage.output_tokens,
total_tokens: (data.usage.input_tokens || 0) + (data.usage.output_tokens || 0),
}
: {};
return {content, usage};
};
/**
* Send a request to an OpenAI-compatible chat completions API.
*/
const completeOpenAI = async (baseUrl, apiKey, model, messages, maxTokens) => {
const url = `${baseUrl}/chat/completions`;
const body = {model, messages};
if (maxTokens) body.max_tokens = maxTokens;
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`,
},
body: JSON.stringify(body),
});
if (!response.ok) {
const text = await response.text();
let errorMsg;
try {
const errorJson = JSON.parse(text);
errorMsg = errorJson.error?.message || text;
} catch {
errorMsg = text;
}
throw new Error(`LLM API error ${response.status}: ${errorMsg}`);
}
const data = await response.json();
const content = data.choices?.[0]?.message?.content || '';
const usage = data.usage || {};
return {content, usage};
};
/**
* Create an LLM client. Supports both OpenAI-compatible and Anthropic APIs.
*
* @param {object} config
* @param {string} config.apiBaseUrl - e.g. "https://api.anthropic.com/v1" or "http://localhost:11434/v1"
* @param {string} config.apiKey
* @param {string} config.model - e.g. "claude-sonnet-4-20250514" or "gpt-4o"
* @param {number} [config.maxTokens]
* @param {string} [config.provider] - "anthropic" or "openai" (auto-detected from URL if omitted)
* @returns {{complete: function}}
*/
const create = (config) => {
const {apiBaseUrl, apiKey, model, maxTokens: defaultMaxTokens} = config;
const baseUrl = apiBaseUrl.replace(/\/+$/, '');
const provider = detectProvider(config);
const complete = async (messages, options = {}) => {
const maxTokens = options.maxTokens || defaultMaxTokens;
logger.debug(`LLM request: ${messages.length} messages, model=${model}, provider=${provider}`);
const result = provider === 'anthropic'
? await completeAnthropic(baseUrl, apiKey, model, messages, maxTokens)
: await completeOpenAI(baseUrl, apiKey, model, messages, maxTokens);
logger.debug(`LLM response: ${result.content.length} chars, tokens=${result.usage.total_tokens || '?'}`);
return result;
};
return {complete};
};
exports.create = create;