llmjs2 1.0.0 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +39 -450
- package/grapes.jpg +0 -0
- package/index.d.ts +43 -0
- package/index.js +465 -0
- package/package.json +7 -47
- package/spec.txt +73 -0
- package/test-generate-tools-suite.js +100 -0
- package/test-generate-tools.js +57 -0
- package/test-generate.js +31 -0
- package/test.js +33 -0
- package/LICENSE +0 -21
- package/dist/agent.d.ts +0 -80
- package/dist/agent.d.ts.map +0 -1
- package/dist/agent.js +0 -189
- package/dist/agent.js.map +0 -1
- package/dist/index.d.ts +0 -74
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -191
- package/dist/index.js.map +0 -1
- package/dist/providers/base.d.ts +0 -58
- package/dist/providers/base.d.ts.map +0 -1
- package/dist/providers/base.js +0 -149
- package/dist/providers/base.js.map +0 -1
- package/dist/providers/index.d.ts +0 -8
- package/dist/providers/index.d.ts.map +0 -1
- package/dist/providers/index.js +0 -7
- package/dist/providers/index.js.map +0 -1
- package/dist/providers/ollama.d.ts +0 -42
- package/dist/providers/ollama.d.ts.map +0 -1
- package/dist/providers/ollama.js +0 -260
- package/dist/providers/ollama.js.map +0 -1
- package/dist/providers/openai.d.ts +0 -38
- package/dist/providers/openai.d.ts.map +0 -1
- package/dist/providers/openai.js +0 -289
- package/dist/providers/openai.js.map +0 -1
- package/dist/types.d.ts +0 -182
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -6
- package/dist/types.js.map +0 -1
- package/src/agent.ts +0 -285
- package/src/index.ts +0 -268
- package/src/providers/base.ts +0 -216
- package/src/providers/index.ts +0 -8
- package/src/providers/ollama.ts +0 -429
- package/src/providers/openai.ts +0 -485
- package/src/types.ts +0 -231
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { generate } from './index.js';
|
|
2
|
+
|
|
3
|
+
const MODEL = 'ollama/qwen3.5:397b-cloud';
|
|
4
|
+
|
|
5
|
+
const tools = [
|
|
6
|
+
{
|
|
7
|
+
name: 'get_weather',
|
|
8
|
+
description: 'Get the current weather for a location',
|
|
9
|
+
parameters: {
|
|
10
|
+
location: {
|
|
11
|
+
type: 'string',
|
|
12
|
+
required: true,
|
|
13
|
+
description: 'The city and state, e.g. San Francisco, CA',
|
|
14
|
+
},
|
|
15
|
+
unit: {
|
|
16
|
+
type: 'string',
|
|
17
|
+
enum: ['celsius', 'fahrenheit'],
|
|
18
|
+
description: 'Temperature unit',
|
|
19
|
+
},
|
|
20
|
+
},
|
|
21
|
+
handler: ({ location, unit = 'fahrenheit' }) => {
|
|
22
|
+
const weatherData = {
|
|
23
|
+
'San Francisco, CA': { temp: 72, condition: 'Sunny' },
|
|
24
|
+
'New York, NY': { temp: 45, condition: 'Cloudy' },
|
|
25
|
+
'London, UK': { temp: 48, condition: 'Rainy' },
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
const data = weatherData[location] || { temp: 70, condition: 'Unknown' };
|
|
29
|
+
const temp =
|
|
30
|
+
unit === 'celsius' ? Math.round((data.temp - 32) * (5 / 9)) : data.temp;
|
|
31
|
+
return `Weather in ${location}: ${temp}°${unit === 'celsius' ? 'C' : 'F'}, ${data.condition}`;
|
|
32
|
+
},
|
|
33
|
+
},
|
|
34
|
+
];
|
|
35
|
+
|
|
36
|
+
async function run() {
|
|
37
|
+
const input = {
|
|
38
|
+
model: MODEL,
|
|
39
|
+
userPrompt: 'Call get_weather for San Francisco, CA and return the result directly.',
|
|
40
|
+
images: [],
|
|
41
|
+
references: ['Use the weather tool if the user asks for weather.'],
|
|
42
|
+
systemPrompt: 'You are a tool-aware assistant. Use available tools when appropriate.',
|
|
43
|
+
tools,
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
console.log('Running generate() with tool support...');
|
|
48
|
+
const response = await generate(input);
|
|
49
|
+
console.log('\nFinal response:');
|
|
50
|
+
console.log(response);
|
|
51
|
+
} catch (error) {
|
|
52
|
+
console.error('generate() failed:', error?.message ?? error);
|
|
53
|
+
process.exit(1);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
run();
|
package/test-generate.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { generate } from './index.js';
|
|
2
|
+
|
|
3
|
+
const MODEL = 'ollama/qwen3.5:397b-cloud';
|
|
4
|
+
const PROMPT = 'what is in the image? make it concise';
|
|
5
|
+
|
|
6
|
+
const IMAGES = [
|
|
7
|
+
'https://thumbs.dreamstime.com/b/sample-jpeg-heartwarming-close-up-endearing-small-mouse-happily-munching-piece-cheese-357411130.jpg',
|
|
8
|
+
// './grapes.jpg',
|
|
9
|
+
];
|
|
10
|
+
|
|
11
|
+
const REFERENCES = [
|
|
12
|
+
// 'llmjs2 is a zero-dependency Node.js library for Ollama.',
|
|
13
|
+
//'https://en.wikipedia.org/wiki/History_of_Hong_Kong',
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
async function run() {
|
|
17
|
+
console.log('Testing generate() with prompt, images, and references...');
|
|
18
|
+
|
|
19
|
+
try {
|
|
20
|
+
const result = await generate(MODEL, PROMPT, IMAGES, REFERENCES);
|
|
21
|
+
console.log('generate() result:');
|
|
22
|
+
console.log(result);
|
|
23
|
+
} catch (error) {
|
|
24
|
+
console.error('generate() failed:', error?.message ?? error);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
run().catch((error) => {
|
|
29
|
+
console.error('Unexpected failure:', error);
|
|
30
|
+
process.exit(1);
|
|
31
|
+
});
|
package/test.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { completion } from './index.js';
|
|
2
|
+
|
|
3
|
+
const MODEL = process.env.TEST_MODEL || 'ollama/qwen3.5:397b-cloud';
|
|
4
|
+
const PROMPT = 'Hello~';
|
|
5
|
+
|
|
6
|
+
async function run() {
|
|
7
|
+
try {
|
|
8
|
+
console.log('Testing direct completion call...');
|
|
9
|
+
const result = await completion(MODEL, PROMPT);
|
|
10
|
+
console.log('Result:', result);
|
|
11
|
+
} catch (error) {
|
|
12
|
+
console.error('Direct call failed:', error.message);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
try {
|
|
16
|
+
console.log('\nTesting options object call...');
|
|
17
|
+
const result = await completion({
|
|
18
|
+
model: MODEL,
|
|
19
|
+
messages: [
|
|
20
|
+
{ role: 'system', content: 'You are a concise assistant.' },
|
|
21
|
+
{ role: 'user', content: PROMPT },
|
|
22
|
+
],
|
|
23
|
+
});
|
|
24
|
+
console.log('Result:', result);
|
|
25
|
+
} catch (error) {
|
|
26
|
+
console.error('Options call failed:', error.message);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
run().catch((error) => {
|
|
31
|
+
console.error('Unexpected error:', error);
|
|
32
|
+
process.exit(1);
|
|
33
|
+
});
|
package/LICENSE
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2024 littlellmjs
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
package/dist/agent.d.ts
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Agent - Stateful conversation manager with tool support
|
|
3
|
-
*/
|
|
4
|
-
import { CompletionResponse, CompletionChunk, Message, Tool } from './index.js';
|
|
5
|
-
/**
|
|
6
|
-
* Agent configuration
|
|
7
|
-
*/
|
|
8
|
-
export interface AgentConfig {
|
|
9
|
-
/** Model identifier (e.g., 'ollama/qwen3.5:397b-cloud') */
|
|
10
|
-
model: string;
|
|
11
|
-
/** API key for the provider (optional, reads from OLLAMA_CLOUD_API_KEY env by default) */
|
|
12
|
-
apiKey?: string;
|
|
13
|
-
/** Base URL for the API (optional, defaults to https://ollama.com) */
|
|
14
|
-
baseUrl?: string;
|
|
15
|
-
/** System instruction for the agent */
|
|
16
|
-
instruction?: string;
|
|
17
|
-
/** Available tools/functions */
|
|
18
|
-
tools?: Tool[];
|
|
19
|
-
/** Tool executor function - receives tool name and arguments, returns result string */
|
|
20
|
-
toolExecutor?: (toolName: string, args: Record<string, unknown>) => string;
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Agent generation request
|
|
24
|
-
*/
|
|
25
|
-
export interface AgentGenerateRequest {
|
|
26
|
-
/** User prompt/message */
|
|
27
|
-
userPrompt: string;
|
|
28
|
-
/** Optional images (base64 or URLs) */
|
|
29
|
-
images?: string[];
|
|
30
|
-
/** Optional reference documents or context */
|
|
31
|
-
references?: string[];
|
|
32
|
-
/** Additional context variables */
|
|
33
|
-
context?: Record<string, unknown>;
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Agent generation response
|
|
37
|
-
*/
|
|
38
|
-
export interface AgentGenerateResponse {
|
|
39
|
-
/** Generated response */
|
|
40
|
-
response: string;
|
|
41
|
-
/** Full completion response from provider */
|
|
42
|
-
completion: CompletionResponse;
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Stateful agent for conversations and tool use
|
|
46
|
-
*/
|
|
47
|
-
export declare class Agent {
|
|
48
|
-
private config;
|
|
49
|
-
private conversationHistory;
|
|
50
|
-
constructor(config: AgentConfig);
|
|
51
|
-
/**
|
|
52
|
-
* Generate a response for a user message
|
|
53
|
-
*/
|
|
54
|
-
generate(request: AgentGenerateRequest): Promise<AgentGenerateResponse>;
|
|
55
|
-
/**
|
|
56
|
-
* Get completion from the model
|
|
57
|
-
*/
|
|
58
|
-
private getCompletion;
|
|
59
|
-
/**
|
|
60
|
-
* Stream a response for a user message
|
|
61
|
-
*/
|
|
62
|
-
generateStream(request: AgentGenerateRequest): AsyncIterable<CompletionChunk>;
|
|
63
|
-
/**
|
|
64
|
-
* Get conversation history
|
|
65
|
-
*/
|
|
66
|
-
getHistory(): Message[];
|
|
67
|
-
/**
|
|
68
|
-
* Clear conversation history (keeps system instruction if set)
|
|
69
|
-
*/
|
|
70
|
-
clearHistory(): void;
|
|
71
|
-
/**
|
|
72
|
-
* Add a message to history
|
|
73
|
-
*/
|
|
74
|
-
addMessage(role: 'system' | 'user' | 'assistant', content: string): void;
|
|
75
|
-
/**
|
|
76
|
-
* Get the current configuration
|
|
77
|
-
*/
|
|
78
|
-
getConfig(): AgentConfig;
|
|
79
|
-
}
|
|
80
|
-
//# sourceMappingURL=agent.d.ts.map
|
package/dist/agent.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"agent.d.ts","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAIL,kBAAkB,EAClB,eAAe,EACf,OAAO,EACP,IAAI,EAEL,MAAM,YAAY,CAAC;AAEpB;;G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ACH,YAAY,IAAI,IAAI;IAapB;;OAEG;IACH,UAAU,CAAC,IAAI,EAAE,QAAQ,GAAG,MAAM,GAAG,WAAW,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI;IAOxE;;OAEG;IACH,SAAS,IAAI,WAAW;CAGzB"}
|
package/dist/agent.js
DELETED
|
@@ -1,189 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Agent - Stateful conversation manager with tool support
|
|
3
|
-
*/
|
|
4
|
-
import { completion, streamCompletion, LLMError, } from './index.js';
|
|
5
|
-
/**
|
|
6
|
-
* Stateful agent for conversations and tool use
|
|
7
|
-
*/
|
|
8
|
-
export class Agent {
|
|
9
|
-
constructor(config) {
|
|
10
|
-
this.conversationHistory = [];
|
|
11
|
-
if (!config.model) {
|
|
12
|
-
throw new LLMError('Model is required in agent config', 'MISSING_MODEL');
|
|
13
|
-
}
|
|
14
|
-
this.config = config;
|
|
15
|
-
// Initialize conversation with system instruction
|
|
16
|
-
if (config.instruction) {
|
|
17
|
-
this.conversationHistory.push({
|
|
18
|
-
role: 'system',
|
|
19
|
-
content: config.instruction,
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Generate a response for a user message
|
|
25
|
-
*/
|
|
26
|
-
async generate(request) {
|
|
27
|
-
if (!request.userPrompt) {
|
|
28
|
-
throw new LLMError('userPrompt is required', 'MISSING_USER_PROMPT');
|
|
29
|
-
}
|
|
30
|
-
// Build user message with context
|
|
31
|
-
let userMessage = request.userPrompt;
|
|
32
|
-
if (request.images && request.images.length > 0) {
|
|
33
|
-
userMessage += `\n\n[Images: ${request.images.length} image(s) attached]`;
|
|
34
|
-
}
|
|
35
|
-
if (request.references && request.references.length > 0) {
|
|
36
|
-
userMessage += '\n\nReferences:\n' + request.references.join('\n---\n');
|
|
37
|
-
}
|
|
38
|
-
if (request.context && Object.keys(request.context).length > 0) {
|
|
39
|
-
userMessage +=
|
|
40
|
-
'\n\nContext: ' + JSON.stringify(request.context, null, 2);
|
|
41
|
-
}
|
|
42
|
-
// Add user message to history
|
|
43
|
-
this.conversationHistory.push({
|
|
44
|
-
role: 'user',
|
|
45
|
-
content: userMessage,
|
|
46
|
-
});
|
|
47
|
-
// Get initial completion
|
|
48
|
-
let completion_result = await this.getCompletion();
|
|
49
|
-
let toolCalls = completion_result.toolCalls;
|
|
50
|
-
let maxIterations = 10;
|
|
51
|
-
let iterations = 0;
|
|
52
|
-
// Handle tool calls in a loop
|
|
53
|
-
while (toolCalls && toolCalls.length > 0 && iterations < maxIterations) {
|
|
54
|
-
iterations++;
|
|
55
|
-
// Execute all tool calls
|
|
56
|
-
const toolResults = [];
|
|
57
|
-
for (const toolCall of toolCalls) {
|
|
58
|
-
try {
|
|
59
|
-
let result;
|
|
60
|
-
if (this.config.toolExecutor) {
|
|
61
|
-
// Use provided tool executor
|
|
62
|
-
result = this.config.toolExecutor(toolCall.name, toolCall.arguments);
|
|
63
|
-
}
|
|
64
|
-
else {
|
|
65
|
-
// Fallback: return error
|
|
66
|
-
result = `Tool '${toolCall.name}' not configured`;
|
|
67
|
-
}
|
|
68
|
-
toolResults.push(`Tool: ${toolCall.name}\nResult: ${result}`);
|
|
69
|
-
}
|
|
70
|
-
catch (error) {
|
|
71
|
-
toolResults.push(`Tool: ${toolCall.name}\nError: ${error instanceof Error ? error.message : String(error)}`);
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
// Add tool results to conversation
|
|
75
|
-
this.conversationHistory.push({
|
|
76
|
-
role: 'user',
|
|
77
|
-
content: 'Tool Results:\n' + toolResults.join('\n\n'),
|
|
78
|
-
});
|
|
79
|
-
// Get next completion
|
|
80
|
-
completion_result = await this.getCompletion();
|
|
81
|
-
toolCalls = completion_result.toolCalls;
|
|
82
|
-
}
|
|
83
|
-
// Add final assistant response to history
|
|
84
|
-
this.conversationHistory.push({
|
|
85
|
-
role: 'assistant',
|
|
86
|
-
content: completion_result.content,
|
|
87
|
-
});
|
|
88
|
-
return {
|
|
89
|
-
response: completion_result.content,
|
|
90
|
-
completion: completion_result,
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* Get completion from the model
|
|
95
|
-
*/
|
|
96
|
-
async getCompletion() {
|
|
97
|
-
const completionRequest = {
|
|
98
|
-
model: this.config.model,
|
|
99
|
-
apiKey: this.config.apiKey,
|
|
100
|
-
baseUrl: this.config.baseUrl,
|
|
101
|
-
messages: this.conversationHistory,
|
|
102
|
-
tools: this.config.tools,
|
|
103
|
-
};
|
|
104
|
-
return completion(completionRequest);
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* Stream a response for a user message
|
|
108
|
-
*/
|
|
109
|
-
async *generateStream(request) {
|
|
110
|
-
if (!request.userPrompt) {
|
|
111
|
-
throw new LLMError('userPrompt is required', 'MISSING_USER_PROMPT');
|
|
112
|
-
}
|
|
113
|
-
// Build user message with context
|
|
114
|
-
let userMessage = request.userPrompt;
|
|
115
|
-
if (request.images && request.images.length > 0) {
|
|
116
|
-
userMessage += `\n\n[Images: ${request.images.length} image(s) attached]`;
|
|
117
|
-
}
|
|
118
|
-
if (request.references && request.references.length > 0) {
|
|
119
|
-
userMessage += '\n\nReferences:\n' + request.references.join('\n---\n');
|
|
120
|
-
}
|
|
121
|
-
if (request.context && Object.keys(request.context).length > 0) {
|
|
122
|
-
userMessage +=
|
|
123
|
-
'\n\nContext: ' + JSON.stringify(request.context, null, 2);
|
|
124
|
-
}
|
|
125
|
-
// Add user message to history
|
|
126
|
-
this.conversationHistory.push({
|
|
127
|
-
role: 'user',
|
|
128
|
-
content: userMessage,
|
|
129
|
-
});
|
|
130
|
-
// Create completion request
|
|
131
|
-
const completionRequest = {
|
|
132
|
-
model: this.config.model,
|
|
133
|
-
apiKey: this.config.apiKey,
|
|
134
|
-
baseUrl: this.config.baseUrl,
|
|
135
|
-
messages: this.conversationHistory,
|
|
136
|
-
tools: this.config.tools,
|
|
137
|
-
};
|
|
138
|
-
// Stream and collect response
|
|
139
|
-
let fullResponse = '';
|
|
140
|
-
const stream = streamCompletion(completionRequest);
|
|
141
|
-
for await (const chunk of stream) {
|
|
142
|
-
fullResponse += chunk.delta;
|
|
143
|
-
yield chunk;
|
|
144
|
-
}
|
|
145
|
-
// Add assistant response to history
|
|
146
|
-
this.conversationHistory.push({
|
|
147
|
-
role: 'assistant',
|
|
148
|
-
content: fullResponse,
|
|
149
|
-
});
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* Get conversation history
|
|
153
|
-
*/
|
|
154
|
-
getHistory() {
|
|
155
|
-
return [...this.conversationHistory];
|
|
156
|
-
}
|
|
157
|
-
/**
|
|
158
|
-
* Clear conversation history (keeps system instruction if set)
|
|
159
|
-
*/
|
|
160
|
-
clearHistory() {
|
|
161
|
-
if (this.config.instruction) {
|
|
162
|
-
this.conversationHistory = [
|
|
163
|
-
{
|
|
164
|
-
role: 'system',
|
|
165
|
-
content: this.config.instruction,
|
|
166
|
-
},
|
|
167
|
-
];
|
|
168
|
-
}
|
|
169
|
-
else {
|
|
170
|
-
this.conversationHistory = [];
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* Add a message to history
|
|
175
|
-
*/
|
|
176
|
-
addMessage(role, content) {
|
|
177
|
-
this.conversationHistory.push({
|
|
178
|
-
role,
|
|
179
|
-
content,
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
/**
|
|
183
|
-
* Get the current configuration
|
|
184
|
-
*/
|
|
185
|
-
getConfig() {
|
|
186
|
-
return { ...this.config };
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
//# sourceMappingURL=agent.js.map
|
package/dist/agent.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"agent.js","sourceRoot":"","sources":["../src/agent.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EACL,UAAU,EACV,gBAAgB,EAMhB,QAAQ,GACT,MAAM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,CAAC,KAAK,CAAC;YAC5B,MAAM,KAAK,CAAC;QACd,CAAC;QAED,oCAAoC;QACpC,IAAI,CAAC,mBAAmB,CAAC,IAAI,CAAC;YAC5B,IAAI,EAAE,WAAW;YACjB,OAAO,EAAE,YAAY;SACtB,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,UAAU;QACR,OAAO,CAAC,GAAG,IAAI,CAAC,mBAAmB,CAAC,CAAC;IACvC,CAAC;IAED;;OAEG;IACH,YAAY;QACV,IAAI,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE,CAAC;YAC5B,IAAI,CAAC,mBAAmB,GAAG;gBACzB;oBACE,IAAI,EAAE,QAAQ;oBACd,OAAO,EAAE,IAAI,CAAC,MAAM,CAAC,WAAW;iBACjC;aACF,CAAC;QACJ,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,mBAAmB,GAAG,EAAE,CAAC;QAChC,CAAC;IACH,CAAC;IAED;;OAEG;IACH,UAAU,CAAC,IAAqC,EAAE,OAAe;QAC/D,IAAI,CAAC,mBAAmB,CAAC,IAAI,CAAC;YAC5B,IAAI;YACJ,OAAO;SACR,CAAC,CAAC;IACL,CAAC;IAED;;OAEG;IACH,SAAS;QACP,OAAO,EAAE,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC;IAC5B,CAAC;CACF"}
|
package/dist/index.d.ts
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* llmjs2 - Enterprise-grade LLM abstraction layer
|
|
3
|
-
* Unified API for OpenAI and Ollama
|
|
4
|
-
*/
|
|
5
|
-
import { CompletionRequest, CompletionResponse, CompletionChunk, CompletionOptions } from './types.js';
|
|
6
|
-
import { LLMError } from './providers/base.js';
|
|
7
|
-
/**
|
|
8
|
-
* Configure global settings
|
|
9
|
-
*/
|
|
10
|
-
export declare function configure(options: CompletionOptions): void;
|
|
11
|
-
/**
|
|
12
|
-
* Create a completion request
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
* import { completion } from 'llmjs2';
|
|
16
|
-
*
|
|
17
|
-
* const result = await completion({
|
|
18
|
-
* model: 'openai/gpt-4',
|
|
19
|
-
* apiKey: 'sk-...',
|
|
20
|
-
* messages: [
|
|
21
|
-
* { role: 'user', content: 'Hello!' }
|
|
22
|
-
* ]
|
|
23
|
-
* });
|
|
24
|
-
*
|
|
25
|
-
* console.log(result.content);
|
|
26
|
-
*/
|
|
27
|
-
export declare function completion(request: CompletionRequest): Promise<CompletionResponse>;
|
|
28
|
-
/**
|
|
29
|
-
* Stream a completion request
|
|
30
|
-
*
|
|
31
|
-
* @example
|
|
32
|
-
* import { streamCompletion } from 'llmjs2';
|
|
33
|
-
*
|
|
34
|
-
* const stream = streamCompletion({
|
|
35
|
-
* model: 'openai/gpt-4',
|
|
36
|
-
* apiKey: 'sk-...',
|
|
37
|
-
* messages: [
|
|
38
|
-
* { role: 'user', content: 'Write a poem' }
|
|
39
|
-
* ]
|
|
40
|
-
* });
|
|
41
|
-
*
|
|
42
|
-
* for await (const chunk of stream) {
|
|
43
|
-
* process.stdout.write(chunk.delta);
|
|
44
|
-
* }
|
|
45
|
-
*/
|
|
46
|
-
export declare function streamCompletion(request: CompletionRequest): AsyncIterable<CompletionChunk>;
|
|
47
|
-
/**
|
|
48
|
-
* Validate provider connectivity and configuration
|
|
49
|
-
*/
|
|
50
|
-
export declare function validateProvider(model: string, apiKey?: string, baseUrl?: string): Promise<void>;
|
|
51
|
-
/**
|
|
52
|
-
* Clear provider cache
|
|
53
|
-
*/
|
|
54
|
-
export declare function clearProviderCache(): void;
|
|
55
|
-
/**
|
|
56
|
-
* Export types for consumers
|
|
57
|
-
*/
|
|
58
|
-
export type { CompletionRequest, CompletionResponse, CompletionChunk, CompletionOptions, Message, MessageRole, Tool, ProviderType, ProviderConfig, ProviderError, } from './types.js';
|
|
59
|
-
/**
|
|
60
|
-
* Export error class
|
|
61
|
-
*/
|
|
62
|
-
export { LLMError };
|
|
63
|
-
/**
|
|
64
|
-
* Export provider classes for advanced use cases
|
|
65
|
-
*/
|
|
66
|
-
export { OpenAIProvider } from './providers/openai.js';
|
|
67
|
-
export { OllamaProvider } from './providers/ollama.js';
|
|
68
|
-
export { BaseProvider } from './providers/base.js';
|
|
69
|
-
/**
|
|
70
|
-
* Export Agent for stateful conversations
|
|
71
|
-
*/
|
|
72
|
-
export { Agent } from './agent.js';
|
|
73
|
-
export type { AgentConfig, AgentGenerateRequest, AgentGenerateResponse } from './agent.js';
|
|
74
|
-
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EACL,iBAAiB,EACjB,kBAAkB,EAClB,eAAe,EACf,iBAAiB,EAGlB,MAAM,YAAY,CAAC;AAGpB,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAY/C;;GAEG;AACH,wBAAgB,SAAS,CAAC,OAAO,EAAE,iBAAiB,GAAG,IAAI,CAE1D;AAgFD;;;;;;;;;;;;;;;GAeG;AACH,wBAAsB,UAAU,CAC9B,OAAO,EAAE,iBAAiB,GACzB,OAAO,CAAC,kBAAkB,CAAC,CAqB7B;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAuB,gBAAgB,CACrC,OAAO,EAAE,iBAAiB,GACzB,aAAa,CAAC,eAAe,CAAC,CAqBhC;AAED;;GAEG;AACH,wBAAsB,gBAAgB,CACpC,KAAK,EAAE,MAAM,EACb,MAAM,CAAC,EAAE,MAAM,EACf,OAAO,CAAC,EAAE,MAAM,GACf,OAAO,CAAC,IAAI,CAAC,CAuBf;AAED;;GAEG;AACH,wBAAgB,kBAAkB,IAAI,IAAI,CAEzC;AAED;;GAEG;AACH,YAAY,EACV,iBAAiB,EACjB,kBAAkB,EAClB,eAAe,EACf,iBAAiB,EACjB,OAAO,EACP,WAAW,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