wirejs-resources 0.1.146-llm → 0.1.148-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 +7 -1
- package/dist/services/llm.js +47 -24
- package/package.json +1 -1
package/dist/services/llm.d.ts
CHANGED
|
@@ -8,6 +8,12 @@ export type LLMChunk = {
|
|
|
8
8
|
message: LLMMessage;
|
|
9
9
|
done: boolean;
|
|
10
10
|
};
|
|
11
|
+
export type ContinueConversationOptions = {
|
|
12
|
+
history: LLMMessage[];
|
|
13
|
+
onChunk?: (chunk: LLMChunk) => void | Promise<void>;
|
|
14
|
+
timeoutSeconds?: number;
|
|
15
|
+
systemPrompt?: string;
|
|
16
|
+
};
|
|
11
17
|
export declare class LLM extends Resource {
|
|
12
18
|
models: string[];
|
|
13
19
|
systemPrompt: string | undefined;
|
|
@@ -19,5 +25,5 @@ export declare class LLM extends Resource {
|
|
|
19
25
|
private checkOllamaAvailable;
|
|
20
26
|
private checkModelExists;
|
|
21
27
|
private createStreamedString;
|
|
22
|
-
continueConversation(history
|
|
28
|
+
continueConversation({ history, onChunk, timeoutSeconds, systemPrompt, }: ContinueConversationOptions): Promise<LLMMessage>;
|
|
23
29
|
}
|
package/dist/services/llm.js
CHANGED
|
@@ -77,7 +77,7 @@ export class LLM extends Resource {
|
|
|
77
77
|
content: message
|
|
78
78
|
};
|
|
79
79
|
}
|
|
80
|
-
async continueConversation(history, onChunk) {
|
|
80
|
+
async continueConversation({ history, onChunk, timeoutSeconds, systemPrompt, }) {
|
|
81
81
|
const ollamaAvailable = await this.checkOllamaAvailable();
|
|
82
82
|
if (!ollamaAvailable) {
|
|
83
83
|
return this.createStreamedString('Ollama is not running locally. Please install and start Ollama:\n\n' +
|
|
@@ -92,31 +92,54 @@ export class LLM extends Resource {
|
|
|
92
92
|
if (!modelExists)
|
|
93
93
|
continue;
|
|
94
94
|
const stream = typeof onChunk === 'function';
|
|
95
|
-
const
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
95
|
+
const controller = new AbortController();
|
|
96
|
+
let timeoutId;
|
|
97
|
+
if (timeoutSeconds) {
|
|
98
|
+
timeoutId = setTimeout(() => {
|
|
99
|
+
controller.abort();
|
|
100
|
+
}, timeoutSeconds * 1000);
|
|
101
|
+
}
|
|
102
|
+
const finalSystemPrompt = systemPrompt ?? this.systemPrompt;
|
|
103
|
+
try {
|
|
104
|
+
const response = await fetch('http://localhost:11434/api/chat', {
|
|
105
|
+
method: 'POST',
|
|
106
|
+
headers: { 'Content-Type': 'application/json' },
|
|
107
|
+
body: JSON.stringify({
|
|
108
|
+
model: model,
|
|
109
|
+
messages: [
|
|
110
|
+
...(finalSystemPrompt ? [
|
|
111
|
+
{
|
|
112
|
+
role: 'system',
|
|
113
|
+
content: finalSystemPrompt
|
|
114
|
+
}
|
|
115
|
+
] : []),
|
|
116
|
+
...history
|
|
117
|
+
],
|
|
118
|
+
stream,
|
|
119
|
+
}),
|
|
120
|
+
signal: controller.signal
|
|
121
|
+
});
|
|
122
|
+
if (timeoutId) {
|
|
123
|
+
clearTimeout(timeoutId);
|
|
124
|
+
}
|
|
125
|
+
if (response.ok) {
|
|
126
|
+
if (stream) {
|
|
127
|
+
return this.stream(response, onChunk);
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
const chunk = await response.json();
|
|
131
|
+
return chunk.message;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
catch (error) {
|
|
136
|
+
if (timeoutId) {
|
|
137
|
+
clearTimeout(timeoutId);
|
|
115
138
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
return chunk.message;
|
|
139
|
+
if (error instanceof Error && error.name === 'AbortError') {
|
|
140
|
+
throw new Error(`Request timed out after ${timeoutSeconds} seconds`);
|
|
119
141
|
}
|
|
142
|
+
throw error;
|
|
120
143
|
}
|
|
121
144
|
}
|
|
122
145
|
// if nothing works, we want to tell the user (the dev) how to install the dep.
|