Skip to content

Commit 12a2bcb

Browse files
authored
Version 0.0.2 Changes (#4)
* copyrights * add splitter example * update splitter itself, add program map, update examples & exports * README update * bump to 0.0.2 * clean up other packages, small corrections * update w/ launch config + check on LangDev demo
1 parent 06061a2 commit 12a2bcb

25 files changed

+653
-90
lines changed

.vscode/launch.json

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
8+
{
9+
"type": "node",
10+
"request": "launch",
11+
"name": "Launch Example: Splitter",
12+
"skipFiles": [
13+
"<node_internals>/**"
14+
],
15+
"program": "${workspaceFolder}/packages/examples/example-dsl-evaluator/src/index.ts",
16+
"args": [
17+
"splitter"
18+
],
19+
"outFiles": [
20+
"${workspaceFolder}/**/*.js"
21+
]
22+
},
23+
{
24+
"type": "node",
25+
"request": "launch",
26+
"name": "Launch Example: Program Map",
27+
"skipFiles": [
28+
"<node_internals>/**"
29+
],
30+
"program": "${workspaceFolder}/packages/examples/example-dsl-evaluator/src/index.ts",
31+
"args": [
32+
"program-map"
33+
],
34+
"outFiles": [
35+
"${workspaceFolder}/**/*.js"
36+
]
37+
},
38+
{
39+
"type": "node",
40+
"request": "launch",
41+
"name": "Launch Example: LangDev Demo (with Ollama)",
42+
"skipFiles": [
43+
"<node_internals>/**"
44+
],
45+
"args": [
46+
"run-langdev"
47+
],
48+
"program": "${workspaceFolder}/packages/examples/example-dsl-evaluator/src/index.ts",
49+
"outFiles": [
50+
"${workspaceFolder}/**/*.js"
51+
]
52+
},
53+
{
54+
"type": "node",
55+
"request": "launch",
56+
"name": "Launch Example: Langium Demo (with Ollama)",
57+
"skipFiles": [
58+
"<node_internals>/**"
59+
],
60+
"program": "${workspaceFolder}/packages/examples/example-dsl-evaluator/src/index.ts",
61+
"outFiles": [
62+
"${workspaceFolder}/**/*.js"
63+
]
64+
}
65+
]
66+
}

package-lock.json

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/examples/example-dsl-evaluator/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"chromadb": "^1.9.2",
2121
"chromadb-default-embed": "^2.13.2",
2222
"dotenv": "^16.4.5",
23-
"langium-ai-tools": "^0.0.1",
23+
"langium-ai-tools": "^0.0.2",
2424
"ollama": "^0.5.9",
2525
"openai": "^4.67.3"
2626
},

packages/examples/example-dsl-evaluator/src/embedding-evaluator.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/******************************************************************************
2+
* Copyright 2024 - 2025 TypeFox GmbH
3+
* This program and the accompanying materials are made available under the
4+
* terms of the MIT License, which is available in the project root.
5+
******************************************************************************/
6+
17
/**
28
* Simple evaluator that computes the embedding for two strings, and returns the cosine similarity
39
*/

packages/examples/example-dsl-evaluator/src/eval-langdev.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1+
/******************************************************************************
2+
* Copyright 2024 - 2025 TypeFox GmbH
3+
* This program and the accompanying materials are made available under the
4+
* terms of the MIT License, which is available in the project root.
5+
******************************************************************************/
6+
17
import { EmptyFileSystem } from 'langium';
28
import { averageAcrossCases, Case, EvalMatrix, EvaluatorResult, generateRadarChart, LangiumEvaluator, LangiumEvaluatorResultData, loadLastResults, mergeEvaluators, Message, normalizeData, Runner } from 'langium-ai-tools/evaluator';
39
import { createLangiumGrammarServices } from 'langium/grammar';
410
import ollama from 'ollama';
511
import { EmbeddingEvaluatorResultData, OllamaEmbeddingEvaluator } from './embedding-evaluator.js';
12+
import * as readline from 'readline/promises';
13+
14+
const rl = readline.createInterface({
15+
input: process.stdin,
16+
output: process.stdout
17+
});
618

719
/**
820
* Create services for the Langium grammar language.
@@ -101,11 +113,33 @@ const langiumAndEmbeddingEvaluator = mergeEvaluators(
101113
new LangiumEvaluator(langiumServices.grammar),
102114

103115
// then run the Ollama embedding evaluator to compare expected vs. actual
104-
new OllamaEmbeddingEvaluator('mxbai-embed-large')
116+
new OllamaEmbeddingEvaluator('nomic-embed-text')
105117
);
106118

107119
export async function runLangDevDemo() {
108120

121+
// check if all the necessary models are installed via ollama
122+
const models = ['llama3.2:latest', 'codellama:latest', 'codegemma:latest', 'nomic-embed-text:latest'];
123+
const listedModels = (await ollama.list()).models;
124+
console.log('Available models: ', listedModels.map(m => m.name));
125+
const missingModels = models.filter(model => !listedModels.some(m => m.name === model));
126+
if (missingModels.length > 0) {
127+
console.error(`The following models are missing: ${missingModels.join(', ')}.`);
128+
// prompt to install
129+
const answer = await rl.question(`Do you want to install these missing models for this demo? (y/n) `);
130+
if (answer.toLowerCase() === 'y') {
131+
for (const model of missingModels) {
132+
console.log(`Installing model ${model}...`);
133+
await ollama.pull({
134+
model: model
135+
});
136+
}
137+
} else {
138+
console.error('Please install missing models and try again.');
139+
return;
140+
}
141+
}
142+
109143
const eMat = new EvalMatrix({
110144

111145
// basic configuration

packages/examples/example-dsl-evaluator/src/eval-langium.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/******************************************************************************
2+
* Copyright 2024 - 2025 TypeFox GmbH
3+
* This program and the accompanying materials are made available under the
4+
* terms of the MIT License, which is available in the project root.
5+
******************************************************************************/
6+
17
import { config } from 'dotenv';
28
import { EmptyFileSystem } from 'langium';
39
import { averageAcrossCases, averageAcrossRunners, EditDistanceEvaluator, EditDistanceEvaluatorResultData, EvalMatrix, generateHistogram, generateHistoricalChart, generateRadarChart, LangiumEvaluator, LangiumEvaluatorResultData, loadLastResults, mergeEvaluators, normalizeData } from 'langium-ai-tools/evaluator';
@@ -43,7 +49,7 @@ export async function runLangiumEvals() {
4349
eval: mergeEvaluators(
4450
new LangiumEvaluator(langiumServices.grammar),
4551
new EditDistanceEvaluator(),
46-
new OllamaEmbeddingEvaluator('mxbai-embed-large')
52+
new OllamaEmbeddingEvaluator('nomic-embed-text')
4753
)
4854
}
4955
],
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/******************************************************************************
2+
* Copyright 2025 TypeFox GmbH
3+
* This program and the accompanying materials are made available under the
4+
* terms of the MIT License, which is available in the project root.
5+
******************************************************************************/
6+
7+
import { ProgramMapper } from "langium-ai-tools";
8+
import { createLangiumGrammarServices } from "langium/grammar";
9+
import { NodeFileSystem } from "langium/node";
10+
11+
export function runExampleProgramMap() {
12+
13+
// simple langium grammar, as an example
14+
const exampleLangiumDoc = `
15+
grammar Test
16+
17+
entry Model: A | B | C | D | E;
18+
19+
A: 'A' ID;
20+
21+
/**
22+
* Info about B (one line above)
23+
*/
24+
25+
B: 'B' ID;
26+
27+
/**
28+
* Info about C
29+
*/
30+
C: 'C' ID;
31+
32+
33+
// info about D (one line above)
34+
35+
D: 'D' ID;
36+
37+
// info about E
38+
E: 'E' ID;
39+
40+
// Datatype rule
41+
DT returns string: ID;
42+
43+
hidden terminal WS: /\s+/;
44+
terminal ID: /[_a-zA-Z][\w_]*/;
45+
`;
46+
47+
// setup your language services (langium's, in this case)
48+
const langiumServices = createLangiumGrammarServices(NodeFileSystem);
49+
const grammarServices = langiumServices.grammar;
50+
51+
// instantiate
52+
const mapper = new ProgramMapper(grammarServices, {
53+
mappingRules: [
54+
{
55+
predicate: (node) => node.$type === 'ParserRule',
56+
map: (node: any) => {
57+
const ruleName = node.name;
58+
const modifiers = [
59+
node.entry ? 'entry' : undefined,
60+
node.fragment ? 'fragment' : undefined,
61+
node.definesHiddenTokens ? 'hidden' : undefined,
62+
node.dataType ? 'datatype' : undefined
63+
].filter(v => v !== undefined);
64+
const modifierString = modifiers.length > 0 ? `(${modifiers.join(', ')}) ` : '';
65+
return `${modifierString}rule ${ruleName}`;
66+
}
67+
},
68+
{
69+
predicate: (node) => node.$type === 'TerminalRule',
70+
map: (node: any) => {
71+
const modifiers = [
72+
node.fragment ? 'fragment' : undefined,
73+
node.hidden ? 'hidden' : undefined,
74+
].filter(v => v !== undefined);
75+
const modifierString = modifiers.length > 0 ? `(${modifiers.join(', ')}) ` : '';
76+
return `${modifierString}terminal ${node.name}`;
77+
}
78+
}
79+
]
80+
});
81+
const programMap = mapper.map(exampleLangiumDoc);
82+
console.log('Program Map Output:');
83+
console.log(programMap.join('\n'));
84+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/******************************************************************************
2+
* Copyright 2024 - 2025 TypeFox GmbH
3+
* This program and the accompanying materials are made available under the
4+
* terms of the MIT License, which is available in the project root.
5+
******************************************************************************/
6+
7+
import { NodeFileSystem } from "langium/node";
8+
import { splitByNode } from "langium-ai-tools/splitter";
9+
import { createLangiumGrammarServices } from "langium/grammar";
10+
import { LangiumServices } from "langium/lsp";
11+
12+
/**
13+
* An example of utilizing the splitter in Langium AI
14+
*/
15+
16+
export function runSplitterExample() {
17+
const exampleLangiumDoc = `
18+
grammar Test
19+
20+
entry Model: A | B | C | D | E;
21+
22+
A: 'A' ID;
23+
24+
/**
25+
* Info about B (one line above)
26+
*/
27+
28+
B: 'B' ID;
29+
30+
/**
31+
* Info about C
32+
*/
33+
C: 'C' ID;
34+
35+
36+
// info about D (one line above)
37+
38+
D: 'D' ID;
39+
40+
// info about E
41+
E: 'E' ID;
42+
43+
hidden terminal WS: /\s+/;
44+
terminal ID: /[_a-zA-Z][\w_]*/;
45+
`;
46+
47+
const langiumServices = createLangiumGrammarServices(NodeFileSystem);
48+
49+
// split by ParserRule (w/ comments included)
50+
const splits = splitByNode(
51+
exampleLangiumDoc,
52+
[(node) => node.$type === "ParserRule"],
53+
langiumServices.grammar,
54+
);
55+
56+
console.log("Split by ParserRule w/ comments:");
57+
console.dir(splits);
58+
59+
// split by ParserRule (w/ comments excluded)
60+
const splitsNoComments = splitByNode(
61+
exampleLangiumDoc,
62+
[(node) => node.$type === "ParserRule"],
63+
langiumServices.grammar,
64+
{ commentRuleNames: [] },
65+
);
66+
67+
console.log("Split by ParserRule without comments:");
68+
console.dir(splitsNoComments);
69+
}

packages/examples/example-dsl-evaluator/src/helloworld-cases.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/******************************************************************************
2+
* Copyright 2024 - 2025 TypeFox GmbH
3+
* This program and the accompanying materials are made available under the
4+
* terms of the MIT License, which is available in the project root.
5+
******************************************************************************/
6+
17
import { Case } from "langium-ai-tools";
28

39
export const cases: Case[] = [

0 commit comments

Comments
 (0)