From 152fb5ba1d6729cc928f17e69bbac36bc06b6c43 Mon Sep 17 00:00:00 2001 From: Piali Choudhury Date: Mon, 11 Dec 2023 17:37:29 -0800 Subject: [PATCH 1/5] Added launch and tasks json to enable debugging from root folder. --- .gitignore | 6 +- .vscode/launch.json | 240 ++++++++++++++++++++++++++++++++++++++++++++ .vscode/tasks.json | 59 +++++++++++ 3 files changed, 302 insertions(+), 3 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json diff --git a/.gitignore b/.gitignore index c5424f78..9fe202f7 100644 --- a/.gitignore +++ b/.gitignore @@ -9,9 +9,9 @@ node_modules/ # Local development and debugging .scratch/ -.vscode -**/.vscode/* +#.vscode +#**/.vscode/* **/tsconfig.debug.json -!**/.vscode/launch.json +#!**/.vscode/launch.json **/build.bat diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..df880b8a --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,240 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "sentiment-wsl", + "type": "node", + "request": "launch", + "program": "${workspaceFolder}/examples/sentiment/src/main.ts", + "stopOnEntry": false, + "args": [], + "cwd": "${workspaceFolder}/examples/sentiment", + "skipFiles": [ + "/**" + ], + "preLaunchTask": "build-sentiment", + "sourceMaps": true, + "outFiles": [ + "${workspaceFolder}/examples/sentiment/dist/**/*.js" + ], + "console": "integratedTerminal" + }, + { + "name": "sentiment", + "type": "node", + "request": "launch", + "program": "${workspaceFolder}/examples/sentiment/src/main.ts", + "stopOnEntry": false, + "args": [], + "cwd": "${workspaceFolder}/examples/sentiment", + "skipFiles": [ + "/**" + ], + "preLaunchTask": "build-sentiment", + "sourceMaps": true, + "outFiles": [ + "${workspaceFolder}/examples/sentiment/dist/**/*.js" + ], + "console": "externalTerminal" + }, + { + "name": "calendar-wsl", + "type": "node", + "request": "launch", + "program": "${workspaceFolder}/examples/calendar/src/main.ts", + "stopOnEntry": false, + "args": [], + "cwd": "${workspaceFolder}/examples/calendar", + "skipFiles": [ + "/**" + ], + "preLaunchTask": "build-calendar", + "sourceMaps": true, + "outFiles": [ + "${workspaceFolder}/examples/calendar/dist/**/*.js" + ], + "console": "integratedTerminal" + }, + { + "name": "calendar", + "type": "node", + "request": "launch", + "program": "${workspaceFolder}/examples/calendar/src/main.ts", + "stopOnEntry": false, + "args": [], + "cwd": "${workspaceFolder}/examples/calendar", + "skipFiles": [ + "/**" + ], + "preLaunchTask": "build-calendar", + "sourceMaps": true, + "outFiles": [ + "${workspaceFolder}/examples/calendar/dist/**/*.js" + ], + "console": "externalTerminal" + }, + { + "name": "math-wsl", + "type": "node", + "request": "launch", + "program": "${workspaceFolder}/examples/math/src/main.ts", + "stopOnEntry": false, + "args": [], + "cwd": "${workspaceFolder}/examples/math", + "skipFiles": [ + "/**" + ], + "preLaunchTask": "build-math", + "sourceMaps": true, + "outFiles": [ + "${workspaceFolder}/examples/math/dist/**/*.js" + ], + "console": "integratedTerminal" + }, + { + "name": "math", + "type": "node", + "request": "launch", + "program": "${workspaceFolder}/examples/math/src/main.ts", + "stopOnEntry": false, + "args": [], + "cwd": "${workspaceFolder}/examples/math", + "skipFiles": [ + "/**" + ], + "preLaunchTask": "build-math", + "sourceMaps": true, + "outFiles": [ + "${workspaceFolder}/examples/math/dist/**/*.js" + ], + "console": "externalTerminal" + }, + { + "name": "music-wsl", + "type": "node", + "request": "launch", + "program": "${workspaceFolder}/examples/music/src/main.ts", + "stopOnEntry": false, + "args": [], + "cwd": "${workspaceFolder}/examples/music", + "skipFiles": [ + "/**" + ], + "preLaunchTask": "build-music", + "sourceMaps": true, + "outFiles": [ + "${workspaceFolder}/examples/music/dist/**/*.js" + ], + "console": "integratedTerminal" + }, + { + "name": "music", + "type": "node", + "request": "launch", + "program": "${workspaceFolder}/examples/music/src/main.ts", + "stopOnEntry": false, + "args": [], + "cwd": "${workspaceFolder}/examples/music", + "skipFiles": [ + "/**" + ], + "preLaunchTask": "build-music", + "sourceMaps": true, + "outFiles": [ + "${workspaceFolder}/examples/music/dist/**/*.js" + ], + "console": "externalTerminal" + }, + { + "name": "restaurant-wsl", + "type": "node", + "request": "launch", + "program": "${workspaceFolder}/examples/restaurant/src/main.ts", + "stopOnEntry": false, + "args": [], + "cwd": "${workspaceFolder}/examples/restaurant", + "skipFiles": [ + "/**" + ], + "preLaunchTask": "build-restaurant", + "sourceMaps": true, + "outFiles": [ + "${workspaceFolder}/examples/restaurant/dist/**/*.js" + ], + "console": "integratedTerminal" + }, + { + "name": "restaurant", + "type": "node", + "request": "launch", + "program": "${workspaceFolder}/examples/restaurant/src/main.ts", + "stopOnEntry": false, + "args": [], + "cwd": "${workspaceFolder}/examples/restaurant", + "skipFiles": [ + "/**" + ], + "preLaunchTask": "build-restaurant", + "sourceMaps": true, + "outFiles": [ + "${workspaceFolder}/examples/restaurant/dist/**/*.js" + ], + "console": "externalTerminal" + }, + { + "name": "coffeeShop-wsl", + "type": "node", + "request": "launch", + "program": "${workspaceFolder}/examples/coffeeShop/src/main.ts", + "stopOnEntry": false, + "args": [], + "cwd": "${workspaceFolder}/examples/coffeeShop", + "skipFiles": [ + "/**" + ], + "preLaunchTask": "build-coffeeShop", + "sourceMaps": true, + "outFiles": [ + "${workspaceFolder}/examples/coffeeShop/dist/**/*.js" + ], + "console": "integratedTerminal" + }, + { + "name": "coffeeShop", + "type": "node", + "request": "launch", + "program": "${workspaceFolder}/examples/coffeeShop/src/main.ts", + "stopOnEntry": false, + "args": [], + "cwd": "${workspaceFolder}/examples/coffeeShop", + "skipFiles": [ + "/**" + ], + "preLaunchTask": "build-coffeeShop", + "sourceMaps": true, + "outFiles": [ + "${workspaceFolder}/examples/coffeeShop/dist/**/*.js" + ], + "console": "externalTerminal" + }, + ], + "compounds": [ + { + "name": "Debug All Projects", + "configurations": [ + "sentiment-wsl", + "calendar-wsl", + "music-wsl", + "math-wsl", + "restaurant-wsl", + "coffeeShop-wsl", + "sentiment", + "calendar", + "music", + "math", + "restaurant", + "coffeeShop", + ] + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 00000000..75346609 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,59 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build-sentiment", + "type": "npm", + "script": "build", + "path": "examples/sentiment", + "group": "build", + "problemMatcher": [], + "detail": "tsc -p src" + }, + { + "label": "build-calendar", + "type": "npm", + "script": "build", + "path": "examples/calendar", + "group": "build", + "problemMatcher": [], + "detail": "tsc -p src" + }, + { + "label": "build-math", + "type": "npm", + "script": "build", + "path": "examples/math", + "group": "build", + "problemMatcher": [], + "detail": "tsc -p src" + }, + { + "label": "build-music", + "type": "npm", + "script": "build", + "path": "examples/music", + "group": "build", + "problemMatcher": [], + "detail": "tsc -p src" + }, + { + "label": "build-restaurant", + "type": "npm", + "script": "build", + "path": "examples/restaurant", + "group": "build", + "problemMatcher": [], + "detail": "tsc -p src" + }, + { + "label": "build-coffeeShop", + "type": "npm", + "script": "build", + "path": "examples/coffeeShop", + "group": "build", + "problemMatcher": [], + "detail": "tsc -p src" + } + ] +} \ No newline at end of file From 893d426d31de34794ba0d1f36c87fd031bd2e91f Mon Sep 17 00:00:00 2001 From: Piali Choudhury Date: Thu, 4 Jan 2024 14:13:05 -0800 Subject: [PATCH 2/5] classification example --- examples/classification/.vscode/launch.json | 18 ++++++++ examples/classification/package.json | 22 ++++++++++ .../src/classificationSchema.ts | 8 ++++ examples/classification/src/input.txt | 4 ++ examples/classification/src/main.ts | 43 +++++++++++++++++++ examples/classification/src/tsconfig.json | 16 +++++++ 6 files changed, 111 insertions(+) create mode 100644 examples/classification/.vscode/launch.json create mode 100644 examples/classification/package.json create mode 100644 examples/classification/src/classificationSchema.ts create mode 100644 examples/classification/src/input.txt create mode 100644 examples/classification/src/main.ts create mode 100644 examples/classification/src/tsconfig.json diff --git a/examples/classification/.vscode/launch.json b/examples/classification/.vscode/launch.json new file mode 100644 index 00000000..91c64d7b --- /dev/null +++ b/examples/classification/.vscode/launch.json @@ -0,0 +1,18 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Launch Program", + "skipFiles": [ + "/**" + ], + "program": "${workspaceFolder}/dist/main.js", + "console": "externalTerminal" + } + ] +} \ No newline at end of file diff --git a/examples/classification/package.json b/examples/classification/package.json new file mode 100644 index 00000000..290d819e --- /dev/null +++ b/examples/classification/package.json @@ -0,0 +1,22 @@ +{ + "name": "classification", + "version": "0.0.1", + "private": true, + "description": "", + "main": "dist/main.js", + "scripts": { + "build": "tsc -p src", + "postbuild": "copyfiles -u 1 src/**/*Schema.ts src/**/*.txt dist" + }, + "author": "", + "license": "MIT", + "dependencies": { + "dotenv": "^16.3.1", + "typechat": "^0.0.10" + }, + "devDependencies": { + "@types/node": "^20.3.1", + "copyfiles": "^2.4.1", + "typescript": "^5.1.3" + } +} diff --git a/examples/classification/src/classificationSchema.ts b/examples/classification/src/classificationSchema.ts new file mode 100644 index 00000000..a4b6ae06 --- /dev/null +++ b/examples/classification/src/classificationSchema.ts @@ -0,0 +1,8 @@ +export type Class = { + name: string; + description: string; +}; + +export interface ClassificationResponse { + class: Class; +} diff --git a/examples/classification/src/input.txt b/examples/classification/src/input.txt new file mode 100644 index 00000000..95a149dc --- /dev/null +++ b/examples/classification/src/input.txt @@ -0,0 +1,4 @@ +I want to buy a Sherlock Holmes novel +I want to order a tall latte with extra foam +I want to buy Richard Feynman's book on physics +I want to buy Nurtec 75mg \ No newline at end of file diff --git a/examples/classification/src/main.ts b/examples/classification/src/main.ts new file mode 100644 index 00000000..a8ec9ec2 --- /dev/null +++ b/examples/classification/src/main.ts @@ -0,0 +1,43 @@ +import fs from "fs"; +import path from "path"; +import dotenv from "dotenv"; +import { createLanguageModel, createJsonTranslator, processRequests } from "typechat"; +import { Class, ClassificationResponse } from "./classificationSchema"; + +const classes: Class[] = [ + { + "name": "CoffeeShop", + "description": "Order Coffee Drinks (Italian names included) and Baked Goods" + }, +{ + "name": "Mystery Bookshop", + "description": "A bookstore that specializes in mystery books" +}, + { + "name": "Bookstore", + "description": "A bookstore that sells all kinds of books" + }, + { + "name": "Drugstore", + "description": "A drugstore that sells health and beauty products" + } +]; + +// TODO: use local .env file. +dotenv.config({ path: path.join(__dirname, "../../../.env") }); + +const model = createLanguageModel(process.env); +const schema = fs.readFileSync(path.join(__dirname, "classificationSchema.ts"), "utf8"); +const translator = createJsonTranslator(model, schema, "ClassificationResponse"); + +// Process requests interactively or from the input file specified on the command line +processRequests("🤗> ", process.argv[2], async (request) => { + const initClasses: string = JSON.stringify(classes, undefined); + const fullRequest: string = `Classify "${request}" using the following classification table:\n${initClasses}\n`; + const response = await translator.translate(request, [{role: "assistant", content:`${fullRequest}`}]); + if (!response.success) { + console.log(response.message); + return; + } + console.log(`The classification is ${response.data.class.name}`); +}); \ No newline at end of file diff --git a/examples/classification/src/tsconfig.json b/examples/classification/src/tsconfig.json new file mode 100644 index 00000000..0f8bbec6 --- /dev/null +++ b/examples/classification/src/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "target": "es2021", + "lib": ["es2021"], + "module": "node16", + "types": ["node"], + "outDir": "../dist", + "esModuleInterop": true, + "forceConsistentCasingInFileNames": true, + "strict": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "exactOptionalPropertyTypes": true, + "inlineSourceMap": true + } +} From 2f85bc3b8768a617d0beaf39a459aa5c2c3c303c Mon Sep 17 00:00:00 2001 From: Piali Choudhury Date: Thu, 4 Jan 2024 14:26:11 -0800 Subject: [PATCH 3/5] updating package-lock.json --- package-lock.json | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2e7b635d..cdbcfa38 100644 --- a/package-lock.json +++ b/package-lock.json @@ -121,6 +121,19 @@ "typescript": "^5.1.3" } }, + "examples/classification": { + "version": "0.0.1", + "license": "MIT", + "dependencies": { + "dotenv": "^16.3.1", + "typechat": "^0.0.10" + }, + "devDependencies": { + "@types/node": "^20.3.1", + "copyfiles": "^2.4.1", + "typescript": "^5.1.3" + } + }, "node_modules/@gar/promisify": { "version": "1.1.3", "resolved": "https://registry.npmjs.org/@gar/promisify/-/promisify-1.1.3.tgz", @@ -444,9 +457,9 @@ "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" }, "node_modules/axios": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/axios/-/axios-1.4.0.tgz", - "integrity": "sha512-S4XCWMEmzvo64T9GfvQDOXgYRDJ/wsSZc7Jvdgx5u1sd0JwsuPLqb3SYmusag+edF6ziyMensPVqLTSc1PiSEA==", + "version": "1.6.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.2.tgz", + "integrity": "sha512-7i24Ri4pmDRfJTR7LDBhsOTtcm+9kjX5WiY1X3wIisx6G9So3pfMkEiU7emUBe46oceVImccTEM3k6C5dbVW8A==", "dependencies": { "follow-redirects": "^1.15.0", "form-data": "^4.0.0", From 200edd216dca952443b551dc18ede790e8932ef9 Mon Sep 17 00:00:00 2001 From: Piali Choudhury Date: Thu, 4 Jan 2024 15:03:01 -0800 Subject: [PATCH 4/5] pusing package-lock.json after running npm install --- package-lock.json | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/package-lock.json b/package-lock.json index cdbcfa38..fe69ea0d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -36,6 +36,19 @@ "typescript": "^5.1.3" } }, + "examples/classification": { + "version": "0.0.1", + "license": "MIT", + "dependencies": { + "dotenv": "^16.3.1", + "typechat": "^0.0.10" + }, + "devDependencies": { + "@types/node": "^20.3.1", + "copyfiles": "^2.4.1", + "typescript": "^5.1.3" + } + }, "examples/code": { "version": "0.0.1", "extraneous": true, @@ -121,19 +134,6 @@ "typescript": "^5.1.3" } }, - "examples/classification": { - "version": "0.0.1", - "license": "MIT", - "dependencies": { - "dotenv": "^16.3.1", - "typechat": "^0.0.10" - }, - "devDependencies": { - "@types/node": "^20.3.1", - "copyfiles": "^2.4.1", - "typescript": "^5.1.3" - } - }, "node_modules/@gar/promisify": { "version": "1.1.3", "resolved": "https://registry.npmjs.org/@gar/promisify/-/promisify-1.1.3.tgz", @@ -601,6 +601,10 @@ "node": ">=10" } }, + "node_modules/classification": { + "resolved": "examples/classification", + "link": true + }, "node_modules/clean-stack": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/clean-stack/-/clean-stack-2.2.0.tgz", From 4f47dc44149658f5e3c9962fe758e6dd2252fddd Mon Sep 17 00:00:00 2001 From: Piali Choudhury Date: Thu, 4 Jan 2024 15:27:44 -0800 Subject: [PATCH 5/5] Incorporating code comments --- .vscode/launch.json | 53 ++++++++++++------- .vscode/tasks.json | 9 ++++ .../src/classificationSchema.ts | 4 +- examples/classification/src/main.ts | 8 +-- 4 files changed, 50 insertions(+), 24 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index df880b8a..4708be34 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -217,24 +217,41 @@ ], "console": "externalTerminal" }, - ], - "compounds": [ { - "name": "Debug All Projects", - "configurations": [ - "sentiment-wsl", - "calendar-wsl", - "music-wsl", - "math-wsl", - "restaurant-wsl", - "coffeeShop-wsl", - "sentiment", - "calendar", - "music", - "math", - "restaurant", - "coffeeShop", - ] + "name": "classification-wsl", + "type": "node", + "request": "launch", + "program": "${workspaceFolder}/examples/classification/src/main.ts", + "stopOnEntry": false, + "args": [], + "cwd": "${workspaceFolder}/examples/classification", + "skipFiles": [ + "/**" + ], + "preLaunchTask": "build-classification", + "sourceMaps": true, + "outFiles": [ + "${workspaceFolder}/examples/classification/dist/**/*.js" + ], + "console": "integratedTerminal" + }, + { + "name": "classification", + "type": "node", + "request": "launch", + "program": "${workspaceFolder}/examples/classification/src/main.ts", + "stopOnEntry": false, + "args": [], + "cwd": "${workspaceFolder}/examples/classification", + "skipFiles": [ + "/**" + ], + "preLaunchTask": "build-classification", + "sourceMaps": true, + "outFiles": [ + "${workspaceFolder}/examples/classification/dist/**/*.js" + ], + "console": "externalTerminal" } - ] + ], } \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 75346609..3eb039ff 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -54,6 +54,15 @@ "group": "build", "problemMatcher": [], "detail": "tsc -p src" + }, + { + "label": "build-classification", + "type": "npm", + "script": "build", + "path": "examples/classification", + "group": "build", + "problemMatcher": [], + "detail": "tsc -p src" } ] } \ No newline at end of file diff --git a/examples/classification/src/classificationSchema.ts b/examples/classification/src/classificationSchema.ts index a4b6ae06..871ea1c1 100644 --- a/examples/classification/src/classificationSchema.ts +++ b/examples/classification/src/classificationSchema.ts @@ -1,8 +1,8 @@ -export type Class = { +export type Classification = { name: string; description: string; }; export interface ClassificationResponse { - class: Class; + class: Classification; } diff --git a/examples/classification/src/main.ts b/examples/classification/src/main.ts index a8ec9ec2..fe990855 100644 --- a/examples/classification/src/main.ts +++ b/examples/classification/src/main.ts @@ -2,17 +2,17 @@ import fs from "fs"; import path from "path"; import dotenv from "dotenv"; import { createLanguageModel, createJsonTranslator, processRequests } from "typechat"; -import { Class, ClassificationResponse } from "./classificationSchema"; +import { Classification, ClassificationResponse } from "./classificationSchema"; -const classes: Class[] = [ +const classes: Classification[] = [ { "name": "CoffeeShop", "description": "Order Coffee Drinks (Italian names included) and Baked Goods" }, -{ + { "name": "Mystery Bookshop", "description": "A bookstore that specializes in mystery books" -}, + }, { "name": "Bookstore", "description": "A bookstore that sells all kinds of books"