-
Notifications
You must be signed in to change notification settings - Fork 330
Expand file tree
/
Copy pathapp.js
More file actions
105 lines (90 loc) · 2.84 KB
/
Copy pathapp.js
File metadata and controls
105 lines (90 loc) · 2.84 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
// <complete_code>
// <imports>
import { ChatOpenAI } from "@langchain/openai";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { FoundryLocalManager } from 'foundry-local-sdk';
// </imports>
// Initialize the Foundry Local SDK
console.log('Initializing Foundry Local SDK...');
const endpointUrl = 'http://localhost:5764';
// <init>
const manager = FoundryLocalManager.create({
appName: 'foundry_local_samples',
logLevel: 'info',
webServiceUrls: endpointUrl
});
// </init>
console.log('✓ SDK initialized successfully');
// Download and register all execution providers.
let currentEp = '';
await manager.downloadAndRegisterEps((epName, percent) => {
if (epName !== currentEp) {
if (currentEp !== '') process.stdout.write('\n');
currentEp = epName;
}
process.stdout.write(`\r ${epName.padEnd(30)} ${percent.toFixed(1).padStart(5)}%`);
});
if (currentEp !== '') process.stdout.write('\n');
// Get the model object
const modelAlias = 'qwen2.5-0.5b'; // Using an available model from the list above
const model = await manager.catalog.getModel(modelAlias);
// Download the model
console.log(`\nDownloading model ${modelAlias}...`);
await model.download((progress) => {
process.stdout.write(`\rDownloading... ${progress.toFixed(2)}%`);
});
console.log('\n✓ Model downloaded');
// Load the model
console.log(`\nLoading model ${modelAlias}...`);
await model.load();
console.log('✓ Model loaded');
// Start the web service
console.log('\nStarting web service...');
manager.startWebService();
console.log('✓ Web service started');
// <langchain_setup>
// Configure ChatOpenAI to use your locally-running model
const llm = new ChatOpenAI({
model: model.id,
configuration: {
baseURL: endpointUrl + '/v1',
apiKey: 'notneeded'
},
temperature: 0.6,
streaming: false
});
// Create a translation prompt template
const prompt = ChatPromptTemplate.fromMessages([
{
role: "system",
content: "You are a helpful assistant that translates {input_language} to {output_language}."
},
{
role: "user",
content: "{input}"
}
]);
// Build a simple chain by connecting the prompt to the language model
const chain = prompt.pipe(llm);
// </langchain_setup>
// <chat_completion>
const input = "I love to code.";
console.log(`Translating '${input}' to French...`);
// Run the chain with your inputs
await chain.invoke({
input_language: "English",
output_language: "French",
input: input
}).then(aiMsg => {
// Print the result content
console.log(`Response: ${aiMsg.content}`);
}).catch(err => {
console.error("Error:", err);
});
// </chat_completion>
// Tidy up
console.log('Unloading model and stopping web service...');
await model.unload();
manager.stopWebService();
console.log(`✓ Model unloaded and web service stopped`);
// </complete_code>