diff --git a/JS/edgechains/README.md b/JS/edgechains/README.md index 2b4baa1b7..81237e662 100644 --- a/JS/edgechains/README.md +++ b/JS/edgechains/README.md @@ -2,4 +2,4 @@ Installation ``` npm install arakoodev -``` \ No newline at end of file +``` diff --git a/JS/edgechains/arakooserver/tsconfig.json b/JS/edgechains/arakooserver/tsconfig.json index 6e01456b4..0827b39c8 100644 --- a/JS/edgechains/arakooserver/tsconfig.json +++ b/JS/edgechains/arakooserver/tsconfig.json @@ -13,7 +13,7 @@ "moduleResolution": "NodeNext", "declaration": true, "outDir": "./dist", - "rootDir": "./src", + "rootDir": "./src" }, "exclude": ["node_modules", "dist", "./**/*.test.ts"] } diff --git a/JS/edgechains/db/dist/index.js b/JS/edgechains/db/dist/index.js index 02b19ad58..22020dd58 100644 --- a/JS/edgechains/db/dist/index.js +++ b/JS/edgechains/db/dist/index.js @@ -2,4 +2,9 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.PostgresClient = void 0; var PostgresClient_js_1 = require("./lib/postgres-client/PostgresClient.js"); -Object.defineProperty(exports, "PostgresClient", { enumerable: true, get: function () { return PostgresClient_js_1.PostgresClient; } }); +Object.defineProperty(exports, "PostgresClient", { + enumerable: true, + get: function () { + return PostgresClient_js_1.PostgresClient; + }, +}); diff --git a/JS/edgechains/db/tsconfig.json b/JS/edgechains/db/tsconfig.json index 6e01456b4..0827b39c8 100644 --- a/JS/edgechains/db/tsconfig.json +++ b/JS/edgechains/db/tsconfig.json @@ -13,7 +13,7 @@ "moduleResolution": "NodeNext", "declaration": true, "outDir": "./dist", - "rootDir": "./src", + "rootDir": "./src" }, "exclude": ["node_modules", "dist", "./**/*.test.ts"] } diff --git a/JS/edgechains/document-loader/dist/index.js b/JS/edgechains/document-loader/dist/index.js index 41ae0e33b..ad41a645a 100644 --- a/JS/edgechains/document-loader/dist/index.js +++ b/JS/edgechains/document-loader/dist/index.js @@ -2,4 +2,9 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.PdfLoader = void 0; var pdfLoader_js_1 = require("./lib/pdf-loader/pdfLoader.js"); -Object.defineProperty(exports, "PdfLoader", { enumerable: true, get: function () { return pdfLoader_js_1.PdfLoader; } }); +Object.defineProperty(exports, "PdfLoader", { + enumerable: true, + get: function () { + return pdfLoader_js_1.PdfLoader; + }, +}); diff --git a/JS/edgechains/document-loader/tsconfig.json b/JS/edgechains/document-loader/tsconfig.json index 6e01456b4..0827b39c8 100644 --- a/JS/edgechains/document-loader/tsconfig.json +++ b/JS/edgechains/document-loader/tsconfig.json @@ -13,7 +13,7 @@ "moduleResolution": "NodeNext", "declaration": true, "outDir": "./dist", - "rootDir": "./src", + "rootDir": "./src" }, "exclude": ["node_modules", "dist", "./**/*.test.ts"] } diff --git a/JS/edgechains/examples/Test01/package.json b/JS/edgechains/examples/Test01/package.json index f1bc51783..b1c31117e 100644 --- a/JS/edgechains/examples/Test01/package.json +++ b/JS/edgechains/examples/Test01/package.json @@ -52,4 +52,4 @@ "typeorm": "^0.3.17", "typescript": "^5.0.2" } -} \ No newline at end of file +} diff --git a/JS/edgechains/lib/.babelrc b/JS/edgechains/lib/.babelrc index b4d43ab3d..69fe71505 100644 --- a/JS/edgechains/lib/.babelrc +++ b/JS/edgechains/lib/.babelrc @@ -1,3 +1,3 @@ { - "presets": ["@babel/preset-env"] -} \ No newline at end of file + "presets": ["@babel/preset-env"] +} diff --git a/JS/edgechains/lib/create-edgechains/__common/htmljs.js b/JS/edgechains/lib/create-edgechains/__common/htmljs.js index d0e507a24..d52af79ca 100644 --- a/JS/edgechains/lib/create-edgechains/__common/htmljs.js +++ b/JS/edgechains/lib/create-edgechains/__common/htmljs.js @@ -22,9 +22,11 @@ export const rootLayout = (layoutToApply) => { export const layout = (layoutToApply) => { return async (c, next) => { await next(); - if ((c.req.header("HX-Request") === "true" && - (c.req.header("HX-Boosted") === "true" || !c.req.header("HX-Target"))) || - c.req.header("HX-Request") !== "true") { + if ( + (c.req.header("HX-Request") === "true" && + (c.req.header("HX-Boosted") === "true" || !c.req.header("HX-Target"))) || + c.req.header("HX-Request") !== "true" + ) { // Req is regular req or boosted link, so we apply layouts const curBody = await c.res.text(); c.res = undefined; // To overwrite res, set it to undefined before setting new value https://github.com/honojs/hono/pull/970 released in https://github.com/honojs/hono/releases/tag/v3.1.0 @@ -36,7 +38,7 @@ export const layout = (layoutToApply) => { }; export const Link = ({ to, "hx-target": hxTarget, class: className, children }) => { if (hxTarget) { - return html `${children}`; - } - else { - return html `${children}`; + } else { + return html`${children}`; } }; diff --git a/JS/edgechains/lib/create-edgechains/__common/src/layouts/ExampleLayout.js b/JS/edgechains/lib/create-edgechains/__common/src/layouts/ExampleLayout.js index a12b5ce8b..7f0c9cd3b 100644 --- a/JS/edgechains/lib/create-edgechains/__common/src/layouts/ExampleLayout.js +++ b/JS/edgechains/lib/create-edgechains/__common/src/layouts/ExampleLayout.js @@ -1,5 +1,5 @@ import { html } from "hono/html"; -const ExampleLayout = (props) => html ` +const ExampleLayout = (props) => html` { }, orderRRF: query.orderRRF, }; - const answer = await hydeSearchAdaEmbedding(arkRequest, process.env.OPENAI_API_KEY, process.env.OPENAI_ORG_ID); + const answer = await hydeSearchAdaEmbedding( + arkRequest, + process.env.OPENAI_API_KEY, + process.env.OPENAI_ORG_ID + ); const final_answer = answer.finalAnswer; const responses = answer.wordEmbeddings; const data = { responses, final_answer }; @@ -32,26 +36,36 @@ HydeSearchRouter.get("/search", async (c) => {
${data.final_answer}
`); diff --git a/JS/edgechains/lib/create-edgechains/__common/src/service/HydeSearchService.d.ts b/JS/edgechains/lib/create-edgechains/__common/src/service/HydeSearchService.d.ts index bf348124f..f1ab0c89f 100644 --- a/JS/edgechains/lib/create-edgechains/__common/src/service/HydeSearchService.d.ts +++ b/JS/edgechains/lib/create-edgechains/__common/src/service/HydeSearchService.d.ts @@ -1,5 +1,9 @@ import type { ArkRequest } from "@arakoodev/edgechains.js"; -declare function hydeSearchAdaEmbedding(arkRequest: ArkRequest, apiKey: string, orgId: string): Promise<{ +declare function hydeSearchAdaEmbedding( + arkRequest: ArkRequest, + apiKey: string, + orgId: string +): Promise<{ wordEmbeddings: any; finalAnswer: any; }>; diff --git a/JS/edgechains/lib/create-edgechains/__common/src/service/HydeSearchService.js b/JS/edgechains/lib/create-edgechains/__common/src/service/HydeSearchService.js index 925e6c196..801ccda37 100644 --- a/JS/edgechains/lib/create-edgechains/__common/src/service/HydeSearchService.js +++ b/JS/edgechains/lib/create-edgechains/__common/src/service/HydeSearchService.js @@ -10,7 +10,14 @@ var PostgresDistanceMetric; })(PostgresDistanceMetric || (PostgresDistanceMetric = {})); async function hydeSearchAdaEmbedding(arkRequest, apiKey, orgId) { try { - const gpt3endpoint = new OpenAiEndpoint("https://api.openai.com/v1/chat/completions", apiKey, orgId, "gpt-3.5-turbo", "user", parseInt("0.7")); + const gpt3endpoint = new OpenAiEndpoint( + "https://api.openai.com/v1/chat/completions", + apiKey, + orgId, + "gpt-3.5-turbo", + "user", + parseInt("0.7") + ); // Get required params from API... const table = "ada_hyde_prod"; const namespace = "360_docs"; @@ -37,17 +44,30 @@ async function hydeSearchAdaEmbedding(arkRequest, apiKey, orgId) { // Chain 1 ==> Get Gpt3Response & split const gpt3Responses = gptResponse.split("\n"); // Chain 2 ==> Get Embeddings from OpenAI using Each Response - const embeddingsListChain = Promise.all(gpt3Responses.map(async (resp) => { - const embedding = await gpt3endpoint.embeddings(resp); - return embedding; - })); + const embeddingsListChain = Promise.all( + gpt3Responses.map(async (resp) => { + const embedding = await gpt3endpoint.embeddings(resp); + return embedding; + }) + ); // Chain 5 ==> Query via EmbeddingChain - const dbClient = new PostgresClient(await embeddingsListChain, PostgresDistanceMetric.IP, topK, 20, table, namespace, arkRequest, 15); + const dbClient = new PostgresClient( + await embeddingsListChain, + PostgresDistanceMetric.IP, + topK, + 20, + table, + namespace, + arkRequest, + 15 + ); const queryResult = await dbClient.dbQuery(); // Chain 6 ==> Create Prompt using Embeddings const retrievedDocs = []; for (const embeddings of queryResult) { - retrievedDocs.push(`${embeddings.raw_text}\n score:${embeddings.score}\n filename:${embeddings.filename}\n`); + retrievedDocs.push( + `${embeddings.raw_text}\n score:${embeddings.score}\n filename:${embeddings.filename}\n` + ); } if (retrievedDocs.join("").length > 4096) { retrievedDocs.length = 4096; @@ -79,8 +99,7 @@ async function hydeSearchAdaEmbedding(arkRequest, apiKey, orgId) { finalAnswer: finalAnswer, }; return response; - } - catch (error) { + } catch (error) { // Handle errors here console.error(error); throw error; diff --git a/JS/edgechains/lib/create-edgechains/__common/src/service/HydeSearchService.test.js b/JS/edgechains/lib/create-edgechains/__common/src/service/HydeSearchService.test.js index f19efe0b6..8bf864409 100644 --- a/JS/edgechains/lib/create-edgechains/__common/src/service/HydeSearchService.test.js +++ b/JS/edgechains/lib/create-edgechains/__common/src/service/HydeSearchService.test.js @@ -21,6 +21,14 @@ describe("Hyde Search", () => { }, orderRRF: "default", }; - expect((await hydeSearchAdaEmbedding(arkRequest, process.env.OPENAI_API_KEY, process.env.OPENAI_ORG_ID)).finalAnswer).toContain("Java"); + expect( + ( + await hydeSearchAdaEmbedding( + arkRequest, + process.env.OPENAI_API_KEY, + process.env.OPENAI_ORG_ID + ) + ).finalAnswer + ).toContain("Java"); }, 30000); }); diff --git a/JS/edgechains/lib/create-edgechains/__common/src/testGeneration/TestGenerator.js b/JS/edgechains/lib/create-edgechains/__common/src/testGeneration/TestGenerator.js index 44f0b73c7..4fd8f94ca 100644 --- a/JS/edgechains/lib/create-edgechains/__common/src/testGeneration/TestGenerator.js +++ b/JS/edgechains/lib/create-edgechains/__common/src/testGeneration/TestGenerator.js @@ -4,8 +4,16 @@ import { OpenAiEndpoint } from "@arakoodev/edgechains.js"; const jsonnet = new Jsonnet(); const promptPath = path.join(process.cwd(), "./src/testGeneration/prompts.jsonnet"); const testGeneratorPath = path.join(process.cwd(), "./src/testGeneration/testGenerator.jsonnet"); -const gpt3endpoint = new OpenAiEndpoint("https://api.openai.com/v1/chat/completions", "", "", "gpt-3.5-turbo", "user", 0.7); -const classText = "public class ChatMessage {\n" + +const gpt3endpoint = new OpenAiEndpoint( + "https://api.openai.com/v1/chat/completions", + "", + "", + "gpt-3.5-turbo", + "user", + 0.7 +); +const classText = + "public class ChatMessage {\n" + " String role;\n" + " String content;\n\n" + " public ChatMessage(String role, String content) {\n" + @@ -42,8 +50,7 @@ export async function getContent() { finalResponse += await gpt3endpoint.gptFnTestGenerator(responce); console.log("Final Response.......\n\n"); return finalResponse; - } - catch (error) { + } catch (error) { console.log(error); } } diff --git a/JS/edgechains/lib/create-edgechains/index.js b/JS/edgechains/lib/create-edgechains/index.js index 672dac94f..5440565f1 100644 --- a/JS/edgechains/lib/create-edgechains/index.js +++ b/JS/edgechains/lib/create-edgechains/index.js @@ -25,8 +25,7 @@ const prompts = [ async function ask_questions() { try { return await inquirer.prompt(prompts); - } - catch (error) { + } catch (error) { console.error("\nError:", error); } } @@ -48,11 +47,12 @@ async function main() { console.log("\nWorking..."); try { const new_dir_path = path.join(process.cwd(), choices.new_dir_name); - const dir_already_exists = fs.existsSync(new_dir_path) && fs.statSync(new_dir_path).isDirectory(); + const dir_already_exists = + fs.existsSync(new_dir_path) && fs.statSync(new_dir_path).isDirectory(); if (dir_already_exists) { throw new Error(`Directory ${new_dir_path} already exists.`); } - function handle_file_copy({ code, destination_without_extension, extension, }) { + function handle_file_copy({ code, destination_without_extension, extension }) { return handle_file_copy_low_level({ code, destination_without_extension, @@ -70,13 +70,21 @@ async function main() { fs.mkdirSync(path.join(new_dir_path, "src/layouts"), { recursive: true }); fs.mkdirSync(path.join(new_dir_path, "src/testGeneration"), { recursive: true }); // tsconfig - fs.writeFileSync(path.join(new_dir_path, "tsconfig.json"), await format(get_ts_config(), { - parser: "json", - }), "utf8"); + fs.writeFileSync( + path.join(new_dir_path, "tsconfig.json"), + await format(get_ts_config(), { + parser: "json", + }), + "utf8" + ); // package.json - fs.writeFileSync(path.join(new_dir_path, "package.json"), await format(get_package_json(options), { - parser: "json", - }), "utf8"); + fs.writeFileSync( + path.join(new_dir_path, "package.json"), + await format(get_package_json(options), { + parser: "json", + }), + "utf8" + ); //gitignore fs.writeFileSync(path.join(new_dir_path, ".gitignore"), get_gitignore(), "utf8"); //.env @@ -84,78 +92,116 @@ async function main() { const root_dir_path = path.join(dirname_from_import_meta(import.meta.url), `../`); const layout_file = "ExampleLayout"; handle_file_copy({ - code: fs.readFileSync(path.join(root_dir_path, "__common/src/layouts/" + layout_file + ".ts"), "utf8"), + code: fs.readFileSync( + path.join(root_dir_path, "__common/src/layouts/" + layout_file + ".ts"), + "utf8" + ), destination_without_extension: "src/layouts/" + layout_file, extension: ".ts", }); const route_file = "hydeSearch.route"; handle_file_copy({ - code: fs.readFileSync(path.join(root_dir_path, "__common/src/routes/" + route_file + ".ts"), "utf8"), + code: fs.readFileSync( + path.join(root_dir_path, "__common/src/routes/" + route_file + ".ts"), + "utf8" + ), destination_without_extension: "src/routes/" + route_file, extension: ".ts", }); const service_files = ["HydeSearchService", "HydeSearchService.test"]; - await Promise.all(service_files.map(async (file) => { - return handle_file_copy({ - code: fs.readFileSync(path.join(root_dir_path, "__common/src/service/" + file + ".ts"), "utf8"), - destination_without_extension: "src/service/" + file, - extension: ".ts", - }); - })); + await Promise.all( + service_files.map(async (file) => { + return handle_file_copy({ + code: fs.readFileSync( + path.join(root_dir_path, "__common/src/service/" + file + ".ts"), + "utf8" + ), + destination_without_extension: "src/service/" + file, + extension: ".ts", + }); + }) + ); const type_file = "HydeFragmentData"; handle_file_copy({ - code: fs.readFileSync(path.join(root_dir_path, "__common/src/types/" + type_file + ".ts"), "utf8"), + code: fs.readFileSync( + path.join(root_dir_path, "__common/src/types/" + type_file + ".ts"), + "utf8" + ), destination_without_extension: "src/types/" + type_file, extension: ".ts", }); const test_file = "TestGenerator"; handle_file_copy({ - code: fs.readFileSync(path.join(root_dir_path, "__common/src/testGeneration/" + test_file + ".ts"), "utf8"), + code: fs.readFileSync( + path.join(root_dir_path, "__common/src/testGeneration/" + test_file + ".ts"), + "utf8" + ), destination_without_extension: "src/testGeneration/" + test_file, extension: ".ts", }); const index_file = "index"; handle_file_copy({ - code: fs.readFileSync(path.join(root_dir_path, "__common/src/" + index_file + ".ts"), "utf8"), + code: fs.readFileSync( + path.join(root_dir_path, "__common/src/" + index_file + ".ts"), + "utf8" + ), destination_without_extension: "src/" + index_file, extension: ".ts", }); const jsonnet_files = ["hyde", "prompts"]; - await Promise.all(jsonnet_files.map(async (file) => { - return handle_file_copy({ - code: fs.readFileSync(path.join(root_dir_path, "__common/src/jsonnet/" + file + ".jsonnet"), "utf8"), - destination_without_extension: "src/jsonnet/" + file, - extension: ".jsonnet", - }); - })); - await Promise.all(jsonnet_files.map(async (file) => { - return handle_file_copy({ - code: fs.readFileSync(path.join(root_dir_path, "__common/src/jsonnet/" + file + ".jsonnet"), "utf8"), - destination_without_extension: "src/testGeneration/" + file, - extension: ".jsonnet", - }); - })); + await Promise.all( + jsonnet_files.map(async (file) => { + return handle_file_copy({ + code: fs.readFileSync( + path.join(root_dir_path, "__common/src/jsonnet/" + file + ".jsonnet"), + "utf8" + ), + destination_without_extension: "src/jsonnet/" + file, + extension: ".jsonnet", + }); + }) + ); + await Promise.all( + jsonnet_files.map(async (file) => { + return handle_file_copy({ + code: fs.readFileSync( + path.join(root_dir_path, "__common/src/jsonnet/" + file + ".jsonnet"), + "utf8" + ), + destination_without_extension: "src/testGeneration/" + file, + extension: ".jsonnet", + }); + }) + ); const build_file = "esbuild.build"; handle_file_copy({ - code: fs.readFileSync(path.join(root_dir_path, "__common/" + build_file + ".js"), "utf8"), + code: fs.readFileSync( + path.join(root_dir_path, "__common/" + build_file + ".js"), + "utf8" + ), destination_without_extension: "/" + build_file, extension: ".js", }); const html_js_file = "htmljs"; handle_file_copy({ - code: fs.readFileSync(path.join(root_dir_path, "__common/" + html_js_file + ".ts"), "utf8"), + code: fs.readFileSync( + path.join(root_dir_path, "__common/" + html_js_file + ".ts"), + "utf8" + ), destination_without_extension: "/" + html_js_file, extension: ".ts", }); const orm_file = "ormconfig"; handle_file_copy({ - code: fs.readFileSync(path.join(root_dir_path, "__common/" + orm_file + ".json"), "utf8"), + code: fs.readFileSync( + path.join(root_dir_path, "__common/" + orm_file + ".json"), + "utf8" + ), destination_without_extension: "/" + orm_file, extension: ".json", }); console.log("\nFinished..."); - } - catch (e) { + } catch (e) { return e; } } @@ -163,6 +209,15 @@ async function runMain() { await main(); } runMain(); -async function handle_file_copy_low_level({ code, destination_without_extension, new_dir_path, extension, }) { - fs.writeFileSync(path.join(new_dir_path, destination_without_extension + extension), code, "utf8"); +async function handle_file_copy_low_level({ + code, + destination_without_extension, + new_dir_path, + extension, +}) { + fs.writeFileSync( + path.join(new_dir_path, destination_without_extension + extension), + code, + "utf8" + ); } diff --git a/JS/edgechains/lib/create-edgechains/src/get-package-json.js b/JS/edgechains/lib/create-edgechains/src/get-package-json.js index 09a8cbe81..a0085424b 100644 --- a/JS/edgechains/lib/create-edgechains/src/get-package-json.js +++ b/JS/edgechains/lib/create-edgechains/src/get-package-json.js @@ -1,52 +1,58 @@ function get_package_json(options) { - return (JSON.stringify({ - name: "example", - version: "1.0.0", - description: "", - main: "dist/index.js", - scripts: { - build: "rm -rf dist && node esbuild.build.js", - start: "node dist/index.js", - lint: "eslint --ignore-path .eslintignore --ext .js,.ts", - format: 'prettier --ignore-path .gitignore --write "**/*.+(js|ts|json)"', - test: "npx jest", - }, - keywords: [], - author: "", - license: "ISC", - dependencies: { - "@hono/node-server": "^1.2.0", - "@types/dotenv": "^8.2.0", - hono: "^3.9.2", - pg: "^8.11.3", - "reflect-metadata": "^0.1.13", - tsc: "^2.0.4", - typescript: "^5.3.2", - }, - devDependencies: { - "@hanazuki/node-jsonnet": "^2.1.0", - "@types/jest": "^29.5.8", - "@types/node": "^20.9.4", - "@typescript-eslint/eslint-plugin": "^6.11.0", - "@typescript-eslint/parser": "^6.11.0", - axios: "^1.6.2", - dotenv: "^16.3.1", - "dts-bundle-generator": "^8.1.2", - eslint: "^8.54.0", - "eslint-config-prettier": "^9.0.0", - "eslint-config-standard-with-typescript": "^40.0.0", - "eslint-plugin-import": "^2.29.0", - "eslint-plugin-n": "^16.3.1", - "eslint-plugin-promise": "^6.1.1", - jest: "^29.7.0", - prettier: "^3.1.0", - react: "^18.2.0", - "@arakoodev/edgechains.js": "0.1.22", - "ts-jest": "^29.1.1", - tsx: "^3.12.2", - typeorm: "^0.3.17", - typescript: "^5.0.2", - }, - }, null, 2) + "\n"); + return ( + JSON.stringify( + { + name: "example", + version: "1.0.0", + description: "", + main: "dist/index.js", + scripts: { + build: "rm -rf dist && node esbuild.build.js", + start: "node dist/index.js", + lint: "eslint --ignore-path .eslintignore --ext .js,.ts", + format: 'prettier --ignore-path .gitignore --write "**/*.+(js|ts|json)"', + test: "npx jest", + }, + keywords: [], + author: "", + license: "ISC", + dependencies: { + "@hono/node-server": "^1.2.0", + "@types/dotenv": "^8.2.0", + hono: "^3.9.2", + pg: "^8.11.3", + "reflect-metadata": "^0.1.13", + tsc: "^2.0.4", + typescript: "^5.3.2", + }, + devDependencies: { + "@hanazuki/node-jsonnet": "^2.1.0", + "@types/jest": "^29.5.8", + "@types/node": "^20.9.4", + "@typescript-eslint/eslint-plugin": "^6.11.0", + "@typescript-eslint/parser": "^6.11.0", + axios: "^1.6.2", + dotenv: "^16.3.1", + "dts-bundle-generator": "^8.1.2", + eslint: "^8.54.0", + "eslint-config-prettier": "^9.0.0", + "eslint-config-standard-with-typescript": "^40.0.0", + "eslint-plugin-import": "^2.29.0", + "eslint-plugin-n": "^16.3.1", + "eslint-plugin-promise": "^6.1.1", + jest: "^29.7.0", + prettier: "^3.1.0", + react: "^18.2.0", + "@arakoodev/edgechains.js": "0.1.22", + "ts-jest": "^29.1.1", + tsx: "^3.12.2", + typeorm: "^0.3.17", + typescript: "^5.0.2", + }, + }, + null, + 2 + ) + "\n" + ); } export { get_package_json }; diff --git a/JS/edgechains/lib/create-edgechains/tsconfig.json b/JS/edgechains/lib/create-edgechains/tsconfig.json index bddeb8908..1fa64fa01 100644 --- a/JS/edgechains/lib/create-edgechains/tsconfig.json +++ b/JS/edgechains/lib/create-edgechains/tsconfig.json @@ -11,5 +11,5 @@ "jsx": "react-jsx", "jsxImportSource": "hono/jsx" }, - "exclude": ["node_modules", "dist",] + "exclude": ["node_modules", "dist"] } diff --git a/JS/edgechains/lib/setupTests.js b/JS/edgechains/lib/setupTests.js index ddebd9387..50dec607e 100644 --- a/JS/edgechains/lib/setupTests.js +++ b/JS/edgechains/lib/setupTests.js @@ -1 +1 @@ -import "regenerator-runtime/runtime"; \ No newline at end of file +import "regenerator-runtime/runtime"; diff --git a/JS/edgechains/lib/tsconfig.json b/JS/edgechains/lib/tsconfig.json index 0847f8679..f8cb68f8b 100644 --- a/JS/edgechains/lib/tsconfig.json +++ b/JS/edgechains/lib/tsconfig.json @@ -1,9 +1,6 @@ { "compilerOptions": { - "types": [ - "dotenv/config", - "node" - ], + "types": ["dotenv/config", "node"], "target": "ES2022", "module": "NodeNext", "esModuleInterop": true, @@ -16,9 +13,5 @@ "moduleResolution": "NodeNext", "declaration": true }, - "exclude": [ - "node_modules", - "dist", - "./**/*.test.ts" - ] -} \ No newline at end of file + "exclude": ["node_modules", "dist", "./**/*.test.ts"] +} diff --git a/JS/edgechains/middleware/dist/index.js b/JS/edgechains/middleware/dist/index.js index 7c96e27f4..fbfb43852 100644 --- a/JS/edgechains/middleware/dist/index.js +++ b/JS/edgechains/middleware/dist/index.js @@ -2,4 +2,9 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.Cors = void 0; var cors_js_1 = require("./lib/cores/cors.js"); -Object.defineProperty(exports, "Cors", { enumerable: true, get: function () { return cors_js_1.Cors; } }); +Object.defineProperty(exports, "Cors", { + enumerable: true, + get: function () { + return cors_js_1.Cors; + }, +}); diff --git a/JS/edgechains/middleware/tsconfig.json b/JS/edgechains/middleware/tsconfig.json index 6e01456b4..0827b39c8 100644 --- a/JS/edgechains/middleware/tsconfig.json +++ b/JS/edgechains/middleware/tsconfig.json @@ -13,7 +13,7 @@ "moduleResolution": "NodeNext", "declaration": true, "outDir": "./dist", - "rootDir": "./src", + "rootDir": "./src" }, "exclude": ["node_modules", "dist", "./**/*.test.ts"] } diff --git a/JS/edgechains/openai/.babelrc b/JS/edgechains/openai/.babelrc index b4d43ab3d..69fe71505 100644 --- a/JS/edgechains/openai/.babelrc +++ b/JS/edgechains/openai/.babelrc @@ -1,3 +1,3 @@ { - "presets": ["@babel/preset-env"] -} \ No newline at end of file + "presets": ["@babel/preset-env"] +} diff --git a/JS/edgechains/openai/setupTests.js b/JS/edgechains/openai/setupTests.js index ddebd9387..50dec607e 100644 --- a/JS/edgechains/openai/setupTests.js +++ b/JS/edgechains/openai/setupTests.js @@ -1 +1 @@ -import "regenerator-runtime/runtime"; \ No newline at end of file +import "regenerator-runtime/runtime"; diff --git a/JS/edgechains/openai/src/lib/endpoints/OpenAiEndpoint.ts b/JS/edgechains/openai/src/lib/endpoints/OpenAiEndpoint.ts index f5bc985de..5df49ad76 100644 --- a/JS/edgechains/openai/src/lib/endpoints/OpenAiEndpoint.ts +++ b/JS/edgechains/openai/src/lib/endpoints/OpenAiEndpoint.ts @@ -1,7 +1,7 @@ import axios from "axios"; import { config } from "dotenv"; config(); -const openAI_url = "https://api.openai.com/v1/chat/completions" +const openAI_url = "https://api.openai.com/v1/chat/completions"; interface ChatOpenAiOptions { url?: string; @@ -20,9 +20,7 @@ export class ChatOpenAi { role: string; temperature: number; - constructor( - options: ChatOpenAiOptions = {} - ) { + constructor(options: ChatOpenAiOptions = {}) { this.url = options.url || openAI_url; this.openAIApiKey = options.openAIApiKey || process.env.OPENAI_API_KEY!; // and check it's there this.orgId = options.orgId || ""; @@ -33,7 +31,8 @@ export class ChatOpenAi { async generateResponse(prompt: string): Promise { const responce = await axios - .post(openAI_url, + .post( + openAI_url, { model: this.model, messages: [ @@ -69,7 +68,8 @@ export class ChatOpenAi { async generateEmbeddings(resp): Promise { const response = await axios - .post("https://api.openai.com/v1/embeddings", + .post( + "https://api.openai.com/v1/embeddings", { model: "text-embedding-ada-002", input: resp, @@ -99,7 +99,8 @@ export class ChatOpenAi { async chatWithAI(chatMessages: any) { const response = await axios - .post(openAI_url, + .post( + openAI_url, { model: this.model, messages: chatMessages, @@ -113,11 +114,11 @@ export class ChatOpenAi { } ) .then((response) => { - console.log({ response }) + console.log({ response }); return response.data.choices; }) .catch((error) => { - console.log({ error }) + console.log({ error }); if (error.response) { console.log("Server responded with status code:", error.response.status); console.log("Response data:", error.response.data); @@ -128,12 +129,13 @@ export class ChatOpenAi { } }); - return response + return response; } async testResponseGeneration(prompt: string): Promise { const responce = await axios - .post(openAI_url, + .post( + openAI_url, { model: this.model, messages: [ diff --git a/JS/edgechains/openai/src/lib/streaming/OpenAiStreaming.ts b/JS/edgechains/openai/src/lib/streaming/OpenAiStreaming.ts index 7ba8812ad..8580e75d0 100644 --- a/JS/edgechains/openai/src/lib/streaming/OpenAiStreaming.ts +++ b/JS/edgechains/openai/src/lib/streaming/OpenAiStreaming.ts @@ -1,12 +1,8 @@ -import { - createParser, - ParsedEvent, - ReconnectInterval, -} from "eventsource-parser"; +import { createParser, ParsedEvent, ReconnectInterval } from "eventsource-parser"; export interface OpenAIStreamPayload { model?: string; - OpenApiKey?: string + OpenApiKey?: string; temperature?: number; top_p?: number; frequency_penalty?: number; @@ -39,7 +35,7 @@ export class Stream { } public encoder = new TextEncoder(); - public decoder = new TextDecoder() + public decoder = new TextDecoder(); async OpenAIStream(prompt: string): Promise { try { const res = await fetch("https://api.openai.com/v1/chat/completions", { @@ -49,17 +45,17 @@ export class Stream { "content-type": "application/json", }, body: JSON.stringify({ - "model": this.model, - "messages": [{ role: "user", content: prompt }], - "stream": this.stream, - "temperature": this.temperature, - "top_p": this.top_p, - "n": this.n, - "presence_penalty": this.presence_penalty, - "frequency_penalty": this.frequency_penalty, - "max_tokens": this.max_tokens + model: this.model, + messages: [{ role: "user", content: prompt }], + stream: this.stream, + temperature: this.temperature, + top_p: this.top_p, + n: this.n, + presence_penalty: this.presence_penalty, + frequency_penalty: this.frequency_penalty, + max_tokens: this.max_tokens, }), - }) + }); const readableStream = new ReadableStream({ start: async (controller) => { // callback @@ -68,7 +64,7 @@ export class Stream { const data = event.data; controller.enqueue(this.encoder.encode(data)); } - } + }; // optimistic error handling if (res.status !== 200) { @@ -76,10 +72,10 @@ export class Stream { status: res.status, statusText: res.statusText, body: await res.text(), - } + }; console.log(`Error: recieved non-200 status code, ${JSON.stringify(data)}`); controller.close(); - return + return; } // stream response (SSE) from OpenAI may be fragmented into multiple chunks @@ -118,7 +114,7 @@ export class Stream { counter++; } catch (e) { // maybe parse error - console.log(e) + console.log(e); controller.error(e); } }, @@ -126,7 +122,7 @@ export class Stream { return readableStream.pipeThrough(transformStream).getReader(); } catch (error) { - console.log(error) + console.log(error); } } -} \ No newline at end of file +} diff --git a/JS/edgechains/openai/src/tests/openAiEndpoints.test.ts b/JS/edgechains/openai/src/tests/openAiEndpoints.test.ts index ac9f8064c..2706deeb7 100644 --- a/JS/edgechains/openai/src/tests/openAiEndpoints.test.ts +++ b/JS/edgechains/openai/src/tests/openAiEndpoints.test.ts @@ -1,79 +1,79 @@ -import axios from 'axios'; -import { ChatOpenAi } from '../lib/endpoints/OpenAiEndpoint.js'; +import axios from "axios"; +import { ChatOpenAi } from "../lib/endpoints/OpenAiEndpoint.js"; -jest.mock('axios'); +jest.mock("axios"); -describe('ChatOpenAi', () => { - describe('generateResponse', () => { - test('should generate response from OpenAI', async () => { - const mockResponse = [ - { - message: { - content: 'Test response', - }, - }, - ]; - axios.post.mockResolvedValueOnce({ data: { choices: mockResponse } }); - const chatOpenAi = new ChatOpenAi({ openAIApiKey: 'test_api_key' }); - const response = await chatOpenAi.generateResponse('test prompt'); - expect(response).toEqual('Test response'); +describe("ChatOpenAi", () => { + describe("generateResponse", () => { + test("should generate response from OpenAI", async () => { + const mockResponse = [ + { + message: { + content: "Test response", + }, + }, + ]; + axios.post.mockResolvedValueOnce({ data: { choices: mockResponse } }); + const chatOpenAi = new ChatOpenAi({ openAIApiKey: "test_api_key" }); + const response = await chatOpenAi.generateResponse("test prompt"); + expect(response).toEqual("Test response"); + }); }); - }); - describe('generateEmbeddings', () => { - test('should generate embeddings from OpenAI', async () => { - const mockResponse = { embeddings: 'Test embeddings' }; - axios.post.mockResolvedValueOnce({ data: { data: mockResponse } }); - const chatOpenAi = new ChatOpenAi({ openAIApiKey: 'test_api_key' }); - const { embeddings } = await chatOpenAi.generateEmbeddings('test text'); - expect(embeddings).toEqual('Test embeddings'); + describe("generateEmbeddings", () => { + test("should generate embeddings from OpenAI", async () => { + const mockResponse = { embeddings: "Test embeddings" }; + axios.post.mockResolvedValueOnce({ data: { data: mockResponse } }); + const chatOpenAi = new ChatOpenAi({ openAIApiKey: "test_api_key" }); + const { embeddings } = await chatOpenAi.generateEmbeddings("test text"); + expect(embeddings).toEqual("Test embeddings"); + }); }); - }); - describe('chatWithAI', () => { - test('should chat with AI using multiple messages', async () => { - const mockResponse = [ - { - message: { - content: 'Test response 1', - }, - }, - { - message: { - content: 'Test response 2', - }, - }, - ]; - axios.post.mockResolvedValueOnce({ data: { choices: mockResponse } }); - const chatOpenAi = new ChatOpenAi({ openAIApiKey: 'test_api_key' }); - const chatMessages = [ - { - role: 'user', - content: 'message 1', - }, - { - role: 'agent', - content: 'message 2', - }, - ]; - const responses = await chatOpenAi.chatWithAI(chatMessages); - expect(responses).toEqual(mockResponse); + describe("chatWithAI", () => { + test("should chat with AI using multiple messages", async () => { + const mockResponse = [ + { + message: { + content: "Test response 1", + }, + }, + { + message: { + content: "Test response 2", + }, + }, + ]; + axios.post.mockResolvedValueOnce({ data: { choices: mockResponse } }); + const chatOpenAi = new ChatOpenAi({ openAIApiKey: "test_api_key" }); + const chatMessages = [ + { + role: "user", + content: "message 1", + }, + { + role: "agent", + content: "message 2", + }, + ]; + const responses = await chatOpenAi.chatWithAI(chatMessages); + expect(responses).toEqual(mockResponse); + }); }); - }); - describe('testResponseGeneration', () => { - test('should generate test response from OpenAI', async () => { - const mockResponse = [ - { - message: { - content: 'Test response', - }, - }, - ]; - axios.post.mockResolvedValueOnce({ data: { choices: mockResponse } }); - const chatOpenAi = new ChatOpenAi({ openAIApiKey: 'test_api_key' }); - const response = await chatOpenAi.testResponseGeneration('test prompt'); - expect(response).toEqual('Test response'); + describe("testResponseGeneration", () => { + test("should generate test response from OpenAI", async () => { + const mockResponse = [ + { + message: { + content: "Test response", + }, + }, + ]; + axios.post.mockResolvedValueOnce({ data: { choices: mockResponse } }); + const chatOpenAi = new ChatOpenAi({ openAIApiKey: "test_api_key" }); + const response = await chatOpenAi.testResponseGeneration("test prompt"); + expect(response).toEqual("Test response"); + }); }); - }); }); diff --git a/JS/edgechains/openai/src/tests/streaming.test.ts b/JS/edgechains/openai/src/tests/streaming.test.ts index 6274ff0ce..f4182d875 100644 --- a/JS/edgechains/openai/src/tests/streaming.test.ts +++ b/JS/edgechains/openai/src/tests/streaming.test.ts @@ -1,63 +1,79 @@ import { Stream } from "../lib/streaming/OpenAiStreaming.js"; -jest.mock('../lib/streaming/OpenAiStreaming.js', () => { - return { - Stream: jest.fn().mockImplementation(() => ({ - OpenAIStream: jest.fn().mockImplementation((prompt) => { - return new ReadableStream({ - start(controller) { - controller.enqueue(new TextEncoder().encode('[{"choices":[{"delta":{"content":"Hi! "}}]}]')); - controller.enqueue(new TextEncoder().encode('[{"choices":[{"delta":{"content":"How "}}]}]')); - controller.enqueue(new TextEncoder().encode('[{"choices":[{"delta":{"content":"can "}}]}]')); - controller.enqueue(new TextEncoder().encode('[{"choices":[{"delta":{"content":"I "}}]}]')); - controller.enqueue(new TextEncoder().encode('[{"choices":[{"delta":{"content":"help "}}]}]')); - controller.enqueue(new TextEncoder().encode('[{"choices":[{"delta":{"content":"you?."}}]}]')); - controller.enqueue(new TextEncoder().encode('[DONE]')); - controller.close(); - } - }) - }), - })), - }; +jest.mock("../lib/streaming/OpenAiStreaming.js", () => { + return { + Stream: jest.fn().mockImplementation(() => ({ + OpenAIStream: jest.fn().mockImplementation((prompt) => { + return new ReadableStream({ + start(controller) { + controller.enqueue( + new TextEncoder().encode('[{"choices":[{"delta":{"content":"Hi! "}}]}]') + ); + controller.enqueue( + new TextEncoder().encode('[{"choices":[{"delta":{"content":"How "}}]}]') + ); + controller.enqueue( + new TextEncoder().encode('[{"choices":[{"delta":{"content":"can "}}]}]') + ); + controller.enqueue( + new TextEncoder().encode('[{"choices":[{"delta":{"content":"I "}}]}]') + ); + controller.enqueue( + new TextEncoder().encode( + '[{"choices":[{"delta":{"content":"help "}}]}]' + ) + ); + controller.enqueue( + new TextEncoder().encode( + '[{"choices":[{"delta":{"content":"you?."}}]}]' + ) + ); + controller.enqueue(new TextEncoder().encode("[DONE]")); + controller.close(); + }, + }); + }), + })), + }; }); -describe('Streaming', () => { - afterEach(() => { - jest.clearAllMocks(); // Clear mock function calls after each test - }); +describe("Streaming", () => { + afterEach(() => { + jest.clearAllMocks(); // Clear mock function calls after each test + }); - test('OpenAIStream should return expected text', async () => { - const options = { - model: 'test_model', - OpenApiKey: 'test_api_key', - temperature: 0.7, - top_p: 1, - frequency_penalty: 0, - presence_penalty: 0, - max_tokens: 500, - stream: true, - n: 1, - }; + test("OpenAIStream should return expected text", async () => { + const options = { + model: "test_model", + OpenApiKey: "test_api_key", + temperature: 0.7, + top_p: 1, + frequency_penalty: 0, + presence_penalty: 0, + max_tokens: 500, + stream: true, + n: 1, + }; - const stream = new Stream(options); + const stream = new Stream(options); - //@ts-ignore - const streamReader = await stream.OpenAIStream('hi').getReader(); - const text = await readStreamToString(streamReader); + //@ts-ignore + const streamReader = await stream.OpenAIStream("hi").getReader(); + const text = await readStreamToString(streamReader); - expect(text).toBe('Hi! How can I help you?.'); - }); + expect(text).toBe("Hi! How can I help you?."); + }); }); async function readStreamToString(reader) { - const decoder = new TextDecoder(); - let text = ''; - while (true) { - const { value, done } = await reader.read(); - if (done) break; - const decodedValue = decoder.decode(value); - if (decodedValue.includes('DONE')) break; - text += JSON.parse(decodedValue)[0]['choices'][0]['delta']['content']; - } - return text; + const decoder = new TextDecoder(); + let text = ""; + while (true) { + const { value, done } = await reader.read(); + if (done) break; + const decodedValue = decoder.decode(value); + if (decodedValue.includes("DONE")) break; + text += JSON.parse(decodedValue)[0]["choices"][0]["delta"]["content"]; + } + return text; } diff --git a/JS/edgechains/openai/tsconfig.json b/JS/edgechains/openai/tsconfig.json index 35ab55ff5..d41b09479 100644 --- a/JS/edgechains/openai/tsconfig.json +++ b/JS/edgechains/openai/tsconfig.json @@ -13,7 +13,7 @@ "moduleResolution": "NodeNext", "declaration": true, "outDir": "./dist", - "rootDir": "./src", + "rootDir": "./src" }, "exclude": ["node_modules", "dist", "./**/*.test.ts", "./setupTests.js"] } diff --git a/JS/edgechains/package.json b/JS/edgechains/package.json index 2cac6ecfe..115b27e05 100644 --- a/JS/edgechains/package.json +++ b/JS/edgechains/package.json @@ -31,4 +31,4 @@ "keywords": [], "author": "", "license": "ISC" -} \ No newline at end of file +} diff --git a/JS/edgechains/splitter/dist/index.js b/JS/edgechains/splitter/dist/index.js index 4e7134469..25f65639e 100644 --- a/JS/edgechains/splitter/dist/index.js +++ b/JS/edgechains/splitter/dist/index.js @@ -2,4 +2,9 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.TextSplitter = void 0; var textSplitter_js_1 = require("./lib/text-splitter/textSplitter.js"); -Object.defineProperty(exports, "TextSplitter", { enumerable: true, get: function () { return textSplitter_js_1.TextSplitter; } }); +Object.defineProperty(exports, "TextSplitter", { + enumerable: true, + get: function () { + return textSplitter_js_1.TextSplitter; + }, +}); diff --git a/JS/edgechains/splitter/tsconfig.json b/JS/edgechains/splitter/tsconfig.json index 6e01456b4..0827b39c8 100644 --- a/JS/edgechains/splitter/tsconfig.json +++ b/JS/edgechains/splitter/tsconfig.json @@ -13,7 +13,7 @@ "moduleResolution": "NodeNext", "declaration": true, "outDir": "./dist", - "rootDir": "./src", + "rootDir": "./src" }, "exclude": ["node_modules", "dist", "./**/*.test.ts"] } diff --git a/JS/edgechains/vector-db/dist/index.js b/JS/edgechains/vector-db/dist/index.js index 4ae6c94ba..b528d75ec 100644 --- a/JS/edgechains/vector-db/dist/index.js +++ b/JS/edgechains/vector-db/dist/index.js @@ -2,4 +2,9 @@ Object.defineProperty(exports, "__esModule", { value: true }); exports.Supabase = void 0; var supabase_js_1 = require("./lib/supabase/supabase.js"); -Object.defineProperty(exports, "Supabase", { enumerable: true, get: function () { return supabase_js_1.Supabase; } }); +Object.defineProperty(exports, "Supabase", { + enumerable: true, + get: function () { + return supabase_js_1.Supabase; + }, +}); diff --git a/JS/edgechains/vector-db/tsconfig.json b/JS/edgechains/vector-db/tsconfig.json index 6e01456b4..0827b39c8 100644 --- a/JS/edgechains/vector-db/tsconfig.json +++ b/JS/edgechains/vector-db/tsconfig.json @@ -13,7 +13,7 @@ "moduleResolution": "NodeNext", "declaration": true, "outDir": "./dist", - "rootDir": "./src", + "rootDir": "./src" }, "exclude": ["node_modules", "dist", "./**/*.test.ts"] }