wirejs-resources 0.1.148-llm → 0.1.150-llm
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/dist/services/llm.d.ts +66 -2
- package/dist/services/llm.js +36 -3
- package/package.json +1 -1
package/dist/services/llm.d.ts
CHANGED
|
@@ -1,29 +1,93 @@
|
|
|
1
1
|
import { Resource } from '../resource.js';
|
|
2
|
+
export type ToolCall = {
|
|
3
|
+
/** Unique identifier for this tool call */
|
|
4
|
+
id?: string;
|
|
5
|
+
/** Function to be called */
|
|
6
|
+
function: {
|
|
7
|
+
/** Name of the function to call */
|
|
8
|
+
name: string;
|
|
9
|
+
/** Arguments to pass to the function (JSON object) */
|
|
10
|
+
arguments: Record<string, any>;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
2
13
|
export type LLMMessage = {
|
|
3
|
-
|
|
14
|
+
/** Standard user or assistant message */
|
|
15
|
+
role: 'user' | 'assistant';
|
|
16
|
+
/** Text content of the message */
|
|
17
|
+
content: string;
|
|
18
|
+
/** Tool calls requested by the assistant (only for assistant messages) */
|
|
19
|
+
tool_calls?: ToolCall[];
|
|
20
|
+
} | {
|
|
21
|
+
/** Tool execution result message */
|
|
22
|
+
role: 'tool';
|
|
23
|
+
/** Result content from the tool execution */
|
|
4
24
|
content: string;
|
|
25
|
+
/** Name of the tool that was executed */
|
|
26
|
+
tool_name: string;
|
|
27
|
+
/** ID linking this result to the original tool call */
|
|
28
|
+
tool_call_id: string;
|
|
5
29
|
};
|
|
6
30
|
export type LLMChunk = {
|
|
7
31
|
created_at: string;
|
|
8
32
|
message: LLMMessage;
|
|
9
33
|
done: boolean;
|
|
10
34
|
};
|
|
35
|
+
type ToolParameterProperty = {
|
|
36
|
+
/** The property's data type */
|
|
37
|
+
type: string;
|
|
38
|
+
/** Human-readable description of this property */
|
|
39
|
+
description?: string;
|
|
40
|
+
/** Allowed values for this property (for constrained inputs) */
|
|
41
|
+
enum?: string[] | number[];
|
|
42
|
+
/** Schema for array elements when this property's type is 'array' */
|
|
43
|
+
items?: {
|
|
44
|
+
type: string;
|
|
45
|
+
description?: string;
|
|
46
|
+
};
|
|
47
|
+
/** Minimum value for numeric properties */
|
|
48
|
+
minimum?: number;
|
|
49
|
+
/** Maximum value for numeric properties */
|
|
50
|
+
maximum?: number;
|
|
51
|
+
};
|
|
52
|
+
export type ToolDefinition = {
|
|
53
|
+
/** The name of the tool function */
|
|
54
|
+
name: string;
|
|
55
|
+
/** Human-readable description of what the tool does */
|
|
56
|
+
description: string;
|
|
57
|
+
/** JSON Schema definition for the tool's parameters - defines named function parameters */
|
|
58
|
+
parameters: {
|
|
59
|
+
/** Always 'object' - functions must have named parameters */
|
|
60
|
+
type: 'object';
|
|
61
|
+
/** Named function parameters and their schemas */
|
|
62
|
+
properties: Record<string, ToolParameterProperty>;
|
|
63
|
+
/** Array of parameter names that must be provided */
|
|
64
|
+
required?: string[];
|
|
65
|
+
};
|
|
66
|
+
};
|
|
11
67
|
export type ContinueConversationOptions = {
|
|
12
68
|
history: LLMMessage[];
|
|
13
69
|
onChunk?: (chunk: LLMChunk) => void | Promise<void>;
|
|
14
70
|
timeoutSeconds?: number;
|
|
15
71
|
systemPrompt?: string;
|
|
72
|
+
models?: string[];
|
|
73
|
+
tools?: ToolDefinition[];
|
|
74
|
+
targetContextSize?: number;
|
|
16
75
|
};
|
|
17
76
|
export declare class LLM extends Resource {
|
|
18
77
|
models: string[];
|
|
19
78
|
systemPrompt: string | undefined;
|
|
79
|
+
targetContextSize: number;
|
|
80
|
+
tools: ToolDefinition[];
|
|
20
81
|
constructor(scope: Resource | string, id: string, options: {
|
|
21
82
|
models: string[];
|
|
22
83
|
systemPrompt?: string;
|
|
84
|
+
targetContextSize?: number;
|
|
85
|
+
tools?: ToolDefinition[];
|
|
23
86
|
});
|
|
24
87
|
private stream;
|
|
25
88
|
private checkOllamaAvailable;
|
|
26
89
|
private checkModelExists;
|
|
27
90
|
private createStreamedString;
|
|
28
|
-
continueConversation({ history, onChunk, timeoutSeconds, systemPrompt, }: ContinueConversationOptions): Promise<LLMMessage>;
|
|
91
|
+
continueConversation({ history, onChunk, timeoutSeconds, systemPrompt, targetContextSize, models, tools, }: ContinueConversationOptions): Promise<LLMMessage>;
|
|
29
92
|
}
|
|
93
|
+
export {};
|
package/dist/services/llm.js
CHANGED
|
@@ -2,10 +2,14 @@ import { Resource } from '../resource.js';
|
|
|
2
2
|
export class LLM extends Resource {
|
|
3
3
|
models;
|
|
4
4
|
systemPrompt;
|
|
5
|
+
targetContextSize;
|
|
6
|
+
tools;
|
|
5
7
|
constructor(scope, id, options) {
|
|
6
8
|
super(scope, id);
|
|
7
9
|
this.models = options.models;
|
|
8
10
|
this.systemPrompt = options.systemPrompt;
|
|
11
|
+
this.targetContextSize = options.targetContextSize ?? 16384;
|
|
12
|
+
this.tools = options.tools ?? [];
|
|
9
13
|
}
|
|
10
14
|
async stream(response, onChunk) {
|
|
11
15
|
if (!response.ok || !response.body) {
|
|
@@ -15,6 +19,7 @@ export class LLM extends Resource {
|
|
|
15
19
|
const decoder = new TextDecoder('utf-8');
|
|
16
20
|
let role = 'assistant';
|
|
17
21
|
let content = '';
|
|
22
|
+
let tool_calls;
|
|
18
23
|
while (true) {
|
|
19
24
|
const { value, done } = await reader.read();
|
|
20
25
|
if (done)
|
|
@@ -30,8 +35,22 @@ export class LLM extends Resource {
|
|
|
30
35
|
}
|
|
31
36
|
role = chunk.message.role;
|
|
32
37
|
content += chunk.message.content;
|
|
38
|
+
// Capture tool calls if present in the chunk
|
|
39
|
+
if ('tool_calls' in chunk.message && chunk.message.tool_calls) {
|
|
40
|
+
tool_calls = chunk.message.tool_calls;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
// Ensure we have a valid role, default to assistant if unexpected
|
|
44
|
+
const finalRole = (role === 'user' || role === 'assistant') ? role : 'assistant';
|
|
45
|
+
// Build the result message
|
|
46
|
+
const result = {
|
|
47
|
+
role: finalRole,
|
|
48
|
+
content
|
|
49
|
+
};
|
|
50
|
+
if (tool_calls) {
|
|
51
|
+
result.tool_calls = tool_calls;
|
|
33
52
|
}
|
|
34
|
-
return
|
|
53
|
+
return result;
|
|
35
54
|
}
|
|
36
55
|
async checkOllamaAvailable() {
|
|
37
56
|
try {
|
|
@@ -77,7 +96,7 @@ export class LLM extends Resource {
|
|
|
77
96
|
content: message
|
|
78
97
|
};
|
|
79
98
|
}
|
|
80
|
-
async continueConversation({ history, onChunk, timeoutSeconds, systemPrompt, }) {
|
|
99
|
+
async continueConversation({ history, onChunk, timeoutSeconds, systemPrompt, targetContextSize, models, tools, }) {
|
|
81
100
|
const ollamaAvailable = await this.checkOllamaAvailable();
|
|
82
101
|
if (!ollamaAvailable) {
|
|
83
102
|
return this.createStreamedString('Ollama is not running locally. Please install and start Ollama:\n\n' +
|
|
@@ -87,7 +106,7 @@ export class LLM extends Resource {
|
|
|
87
106
|
'Models to try installing: ' + this.models.join(', '), onChunk);
|
|
88
107
|
}
|
|
89
108
|
// models should be in priority order. so, first one that works is the one we want.
|
|
90
|
-
for (const model of this.models) {
|
|
109
|
+
for (const model of models ?? this.models) {
|
|
91
110
|
const modelExists = await this.checkModelExists(model);
|
|
92
111
|
if (!modelExists)
|
|
93
112
|
continue;
|
|
@@ -100,6 +119,16 @@ export class LLM extends Resource {
|
|
|
100
119
|
}, timeoutSeconds * 1000);
|
|
101
120
|
}
|
|
102
121
|
const finalSystemPrompt = systemPrompt ?? this.systemPrompt;
|
|
122
|
+
const finalTools = tools ?? this.tools;
|
|
123
|
+
// Transform our ToolDefinition format to Ollama's expected format
|
|
124
|
+
const ollamaTools = finalTools.map(tool => ({
|
|
125
|
+
type: "function",
|
|
126
|
+
function: {
|
|
127
|
+
name: tool.name,
|
|
128
|
+
description: tool.description,
|
|
129
|
+
parameters: tool.parameters
|
|
130
|
+
}
|
|
131
|
+
}));
|
|
103
132
|
try {
|
|
104
133
|
const response = await fetch('http://localhost:11434/api/chat', {
|
|
105
134
|
method: 'POST',
|
|
@@ -116,6 +145,10 @@ export class LLM extends Resource {
|
|
|
116
145
|
...history
|
|
117
146
|
],
|
|
118
147
|
stream,
|
|
148
|
+
...(ollamaTools.length > 0 ? { tools: ollamaTools } : {}),
|
|
149
|
+
options: {
|
|
150
|
+
num_ctx: targetContextSize ?? this.targetContextSize
|
|
151
|
+
}
|
|
119
152
|
}),
|
|
120
153
|
signal: controller.signal
|
|
121
154
|
});
|