diff --git a/JS/edgechains/examples/chat-with-llm/dist/index.js b/JS/edgechains/examples/chat-with-llm/dist/index.js index 37f8f19df..694a2300e 100644 --- a/JS/edgechains/examples/chat-with-llm/dist/index.js +++ b/JS/edgechains/examples/chat-with-llm/dist/index.js @@ -1,7 +1,8 @@ +//@ts-ignore import { ArakooServer } from "@arakoodev/edgechains.js/arakooserver"; import Jsonnet from "@arakoodev/jsonnet"; //@ts-ignore -import createClient from "sync-rpc"; +import createClient from 'sync-rpc'; import { fileURLToPath } from "url"; import path from "path"; const server = new ArakooServer(); diff --git a/JS/edgechains/examples/chat-with-llm/dist/lib/generateResponse.cjs b/JS/edgechains/examples/chat-with-llm/dist/lib/generateResponse.cjs index 23fca4770..54d5d462d 100644 --- a/JS/edgechains/examples/chat-with-llm/dist/lib/generateResponse.cjs +++ b/JS/edgechains/examples/chat-with-llm/dist/lib/generateResponse.cjs @@ -38,7 +38,8 @@ function openAICall() { return openai.zodSchemaResponse({ prompt, schema: schema }).then((res) => { return JSON.stringify(res); }); - } catch (error) { + } + catch (error) { return error; } }; diff --git a/JS/edgechains/examples/chat-with-llm/package.json b/JS/edgechains/examples/chat-with-llm/package.json index 458710e54..80e58a0b4 100644 --- a/JS/edgechains/examples/chat-with-llm/package.json +++ b/JS/edgechains/examples/chat-with-llm/package.json @@ -1,22 +1,22 @@ { - "name": "ownChatGpt", - "version": "1.0.0", - "description": "", - "main": "index.js", - "type": "module", - "keywords": [], - "author": "", - "scripts": { - "start": "tsc && node --experimental-wasm-modules ./dist/index.js" - }, - "license": "ISC", - "dependencies": { - "@arakoodev/edgechains.js": "^0.1.23", - "@arakoodev/jsonnet": "^0.2.1", - "sync-rpc": "^1.3.6", - "zod": "^3.23.8" - }, - "devDependencies": { - "@types/node": "^20.12.12" - } + "name": "ownchatgpt", + "version": "1.0.0", + "description": "", + "main": "index.js", + "type": "module", + "keywords": [], + "author": "", + "scripts": { + "start": "tsc && node --experimental-wasm-modules ./dist/index.js" + }, + "license": "ISC", + "dependencies": { + "@arakoodev/edgechains.js": "^0.1.23", + "@arakoodev/jsonnet": "^0.3.1", + "sync-rpc": "^1.3.6", + "zod": "^3.23.8" + }, + "devDependencies": { + "@types/node": "^20.12.12" + } } diff --git a/JS/edgechains/examples/chat-with-pdf/jsonnet/intermediate.jsonnet b/JS/edgechains/examples/chat-with-pdf/jsonnet/intermediate.jsonnet deleted file mode 100644 index 268359589..000000000 --- a/JS/edgechains/examples/chat-with-pdf/jsonnet/intermediate.jsonnet +++ /dev/null @@ -1,13 +0,0 @@ -local updateQueryPrompt(promptTemplate, query, content) = - local updatedPrompt = std.strReplace(promptTemplate, '{}', query + "\n"); - local updatedContent = std.strReplace(updatedPrompt, '{content}', content + "\n"); - updatedContent; - -local promptTemplate = std.extVar("promptTemplate"); -local query = std.extVar("query"); -local content = std.extVar("content"); - -local updatedQueryPrompt = updateQueryPrompt(promptTemplate, query, content); -{ - "prompt": updatedQueryPrompt -} diff --git a/JS/edgechains/examples/chat-with-pdf/jsonnet/main.jsonnet b/JS/edgechains/examples/chat-with-pdf/jsonnet/main.jsonnet new file mode 100644 index 000000000..b8e678697 --- /dev/null +++ b/JS/edgechains/examples/chat-with-pdf/jsonnet/main.jsonnet @@ -0,0 +1,26 @@ +local CUSTOM_TEMPLATE = ||| + You are a senior software developer seeking accurate answers based on provided content. Your task is to find answers exclusively from the given content and the answer should be verbose and detailedfull. If the answer is not present within the provided content, respond with "Sorry! I don't know the answer. + Content:{content} + Question:{} + |||; + + +local updateQueryPrompt(promptTemplate, query, content) = + local updatedPrompt = std.strReplace(promptTemplate, '{}', query + "\n"); + local updatedContent = std.strReplace(updatedPrompt, '{content}', content + "\n"); + updatedContent; + + +local query = std.extVar("query"); +local content = std.extVar("content"); + +local getQueryMatch(query)= + local queryEmbeddings = arakoo.native("getEmbeddings")(query); + local content = arakoo.native("getQueryMatch")(queryEmbeddings); + content; + +local updatedQueryPrompt = updateQueryPrompt(CUSTOM_TEMPLATE, query, getQueryMatch(query)); + +local getOpenAiResponse = arakoo.native("openAICall")(updatedQueryPrompt); + +getOpenAiResponse \ No newline at end of file diff --git a/JS/edgechains/examples/chat-with-pdf/jsonnet/prompt.jsonnet b/JS/edgechains/examples/chat-with-pdf/jsonnet/prompt.jsonnet deleted file mode 100644 index 00ffb5b6b..000000000 --- a/JS/edgechains/examples/chat-with-pdf/jsonnet/prompt.jsonnet +++ /dev/null @@ -1,9 +0,0 @@ -local CUSTOM_TEMPLATE = ||| - You are a senior software developer seeking accurate answers based on provided content. Your task is to find answers exclusively from the given content. If the answer is not present within the provided content, respond with "Sorry! I don't know the answer. - Content:{content} - Question:{} - |||; - -{ - "custom_template": CUSTOM_TEMPLATE, -} \ No newline at end of file diff --git a/JS/edgechains/examples/chat-with-pdf/package.json b/JS/edgechains/examples/chat-with-pdf/package.json index 404d85dbd..fdffae838 100644 --- a/JS/edgechains/examples/chat-with-pdf/package.json +++ b/JS/edgechains/examples/chat-with-pdf/package.json @@ -14,10 +14,11 @@ "license": "ISC", "dependencies": { "@arakoodev/edgechains.js": "^0.1.23", - "@arakoodev/jsonnet": "^0.2.0", + "@arakoodev/jsonnet": "^0.3.1", "@babel/preset-env": "^7.24.4", "cli-spinner": "^0.2.10", "regenerator-runtime": "^0.14.1", + "sync-rpc": "^1.3.6", "zod": "^3.23.8" }, "devDependencies": { diff --git a/JS/edgechains/examples/chat-with-pdf/src/lib/InsertToSupabase.ts b/JS/edgechains/examples/chat-with-pdf/src/lib/InsertToSupabase.ts new file mode 100644 index 000000000..80c946617 --- /dev/null +++ b/JS/edgechains/examples/chat-with-pdf/src/lib/InsertToSupabase.ts @@ -0,0 +1,64 @@ +import { fileURLToPath } from "url"; +import { Spinner } from "cli-spinner" +import { PdfLoader } from "@arakoodev/edgechains.js/document-loader"; +import { TextSplitter } from "@arakoodev/edgechains.js/splitter"; +import { createRequire } from 'module'; +import { Supabase } from "@arakoodev/edgechains.js/vector-db"; +import Jsonnet from "@arakoodev/jsonnet"; +import path from "path" +import fs from "fs"; + +const require = createRequire(import.meta.url) + +const getEmbeddings = require("./getEmbeddings.cjs") + +const __dirname = fileURLToPath(import.meta.url); + +const pdfPath = path.join(__dirname, "../../../example.pdf"); +const pdfData = fs.readFileSync(pdfPath); +const bufferPdf = Buffer.from(pdfData); +const loader = new PdfLoader(bufferPdf); +const docs = await loader.loadPdf(); +const splitter = new TextSplitter(); +export const splitedDocs = await splitter.splitTextIntoChunks(docs, 500); + +const secretsPath = path.join(__dirname, "../../../jsonnet/secrets.jsonnet"); + +const jsonnet = new Jsonnet(); + +const secretsLoader = jsonnet.evaluateFile(secretsPath); +const supabaseApiKey = await JSON.parse(secretsLoader).supabase_api_key; +const supabaseUrl = await JSON.parse(secretsLoader).supabase_url; +const supabase = new Supabase(supabaseUrl, supabaseApiKey); +const client = supabase.createClient(); + +export async function InsertToSupabase(content:any) { + var spinner = new Spinner("Inserting to Supabase.. %s"); + try { + spinner.setSpinnerString("|/-\\"); + spinner.start(); + + const response = await getEmbeddings()(content); + for (let i = 0; i < response?.length; i++) { + if (content[i].length <= 1) { + continue; + } + + const element = response[i].embedding; + await supabase.insertVectorData({ + client, + tableName: "documents", + content: content[i].toLowerCase(), + embedding: element, + }); + } + if (!response) { + return console.log("Error inserting to Supabase"); + } + console.log("Inserted to Supabase"); + } catch (error) { + console.log("Error inserting to Supabase", error); + } finally { + spinner.stop(); + } +} \ No newline at end of file diff --git a/JS/edgechains/examples/chat-with-pdf/src/lib/generateResponse.cts b/JS/edgechains/examples/chat-with-pdf/src/lib/generateResponse.cts new file mode 100644 index 000000000..4ad8dd5f6 --- /dev/null +++ b/JS/edgechains/examples/chat-with-pdf/src/lib/generateResponse.cts @@ -0,0 +1,29 @@ +const path = require("path"); +const { OpenAI } = require("@arakoodev/edgechains.js/openai"); +const z = require("zod"); +const Jsonnet = require("@arakoodev/jsonnet"); +const jsonnet = new Jsonnet(); + + +const secretsPath = path.join(__dirname, "../../jsonnet/secrets.jsonnet"); +const openAIApiKey = JSON.parse(jsonnet.evaluateFile(secretsPath)).openai_api_key; + +const openai = new OpenAI({ apiKey: openAIApiKey }); + +const schema = z.object({ + answer: z.string().describe("The answer to the question"), +}); + +function openAICall() { + return function (prompt: string) { + try { + return openai.zodSchemaResponse({ prompt, schema }).then((res: any) => { + return JSON.stringify(res); + }); + } catch (error) { + return error; + } + }; +} + +module.exports = openAICall; diff --git a/JS/edgechains/examples/chat-with-pdf/src/lib/getEmbeddings.cts b/JS/edgechains/examples/chat-with-pdf/src/lib/getEmbeddings.cts new file mode 100644 index 000000000..cd999dd65 --- /dev/null +++ b/JS/edgechains/examples/chat-with-pdf/src/lib/getEmbeddings.cts @@ -0,0 +1,23 @@ +const { OpenAI } = require("@arakoodev/edgechains.js/openai") +const path = require("path"); +const Jsonnet = require("@arakoodev/jsonnet"); + +const jsonnet = new Jsonnet(); + +const secretsPath = path.join(__dirname, "../../jsonnet/secrets.jsonnet"); +const openAIApiKey = JSON.parse(jsonnet.evaluateFile(secretsPath)).openai_api_key; + +const llm = new OpenAI({ + apiKey: openAIApiKey +}); + +function getEmbeddings() { + return ((content: any) => { + const embeddings = llm.generateEmbeddings(content).then((res: any) => { + return JSON.stringify(res) + }) + return embeddings; + }) +} + +module.exports = getEmbeddings; diff --git a/JS/edgechains/examples/chat-with-pdf/src/lib/getQueryMatch.cts b/JS/edgechains/examples/chat-with-pdf/src/lib/getQueryMatch.cts new file mode 100644 index 000000000..18d1f0a15 --- /dev/null +++ b/JS/edgechains/examples/chat-with-pdf/src/lib/getQueryMatch.cts @@ -0,0 +1,37 @@ +import path from "path" +const Jsonnet = require("@arakoodev/jsonnet"); +const { Supabase } = require("@arakoodev/edgechains.js/vector-db"); + +const secretsPath = path.join(__dirname, "../../jsonnet/secrets.jsonnet"); + +const jsonnet = new Jsonnet(); + +const secretsLoader = jsonnet.evaluateFile(secretsPath); +const supabaseApiKey = JSON.parse(secretsLoader).supabase_api_key; +const supabaseUrl = JSON.parse(secretsLoader).supabase_url; + +const supabase = new Supabase(supabaseUrl, supabaseApiKey); +const client = supabase.createClient(); + +function getQueryMatch() { + return function (embeddings: any) { + const response = supabase.getDataFromQuery({ + client, + functionNameToCall: "match_documents", + query_embedding: JSON.parse(embeddings)[0].embedding, + similarity_threshold: 0.5, + match_count: 1, + }).then((response:any) => { + const contentArr: string[] = []; + for (let i = 0; i < response?.length; i++) { + const element = response[i]; + contentArr.push(element.content); + } + return contentArr.join(" "); + }) + return response; + } +} + +module.exports = getQueryMatch + diff --git a/JS/edgechains/examples/chat-with-pdf/src/routes/chat.ts b/JS/edgechains/examples/chat-with-pdf/src/routes/chat.ts index ce390f9e2..2b29f9e25 100644 --- a/JS/edgechains/examples/chat-with-pdf/src/routes/chat.ts +++ b/JS/edgechains/examples/chat-with-pdf/src/routes/chat.ts @@ -1,127 +1,34 @@ import path from "path"; -import fs from "fs"; +//@ts-ignore +import createClient from "sync-rpc" import { fileURLToPath } from "url"; -import { OpenAI } from "@arakoodev/edgechains.js/openai"; -import { Supabase } from "@arakoodev/edgechains.js/vector-db"; -import { PdfLoader } from "@arakoodev/edgechains.js/document-loader"; -import { TextSplitter } from "@arakoodev/edgechains.js/splitter"; import { ArakooServer } from "@arakoodev/edgechains.js/arakooserver"; -import { Spinner } from "cli-spinner"; +import { InsertToSupabase, splitedDocs } from "../lib/InsertToSupabase.js"; -import { z } from "zod"; +import Jsonnet from "@arakoodev/jsonnet"; const server = new ArakooServer(); - const __dirname = fileURLToPath(import.meta.url); -const pdfPath = path.join(__dirname, "../../../example.pdf"); -const pdfData = fs.readFileSync(pdfPath); -const bufferPdf = Buffer.from(pdfData); -const loader = new PdfLoader(bufferPdf); -const docs = await loader.loadPdf(); -const splitter = new TextSplitter(); -const splitedDocs = await splitter.splitTextIntoChunks(docs, 500); - -export const ChatRouter: any = server.createApp(); - -const getJsonnet = async () => { - let jsonnet = await import("@arakoodev/jsonnet"); - return jsonnet.default; -}; - -const promptPath = path.join(__dirname, "../../../jsonnet/prompt.jsonnet"); -const InterPath = path.join(__dirname, "../../../jsonnet/intermediate.jsonnet"); -const secretsPath = path.join(__dirname, "../../../jsonnet/secrets.jsonnet"); - -const Jsonnet = await getJsonnet(); const jsonnet = new Jsonnet(); -const secretsLoader = jsonnet.evaluateFile(secretsPath); - -const openAIApiKey = await JSON.parse(secretsLoader).openai_api_key; -const supabaseApiKey = await JSON.parse(secretsLoader).supabase_api_key; -const supabaseUrl = await JSON.parse(secretsLoader).supabase_url; - -const supabase = new Supabase(supabaseUrl, supabaseApiKey); - -const client = supabase.createClient(); - -const llm = new OpenAI({ - apiKey: openAIApiKey, -}); - -async function getEmbeddings(content) { - const embeddings = await llm.generateEmbeddings(content); - - return embeddings; -} - -async function InsertToSupabase(content) { - var spinner = new Spinner("Inserting to Supabase.. %s"); - try { - spinner.setSpinnerString("|/-\\"); - spinner.start(); +const openAICall = createClient(path.join(__dirname, "../../lib/generateResponse.cjs")); +const getQueryMatch = createClient(path.join(__dirname, "../../lib/getQueryMatch.cjs")); +const getEmbeddings = createClient(path.join(__dirname, "../../lib/getEmbeddings.cjs")); - const response = await getEmbeddings(content); - for (let i = 0; i < response?.length; i++) { - if (content[i].length <= 1) { - continue; - } - - const element = response[i].embedding; - const data = await supabase.insertVectorData({ - client, - tableName: "documents", - content: content[i].toLowerCase(), - embedding: element, - }); - } - if (!response) { - return console.log("Error inserting to Supabase"); - } - console.log("Inserted to Supabase"); - } catch (error) { - console.log("Error inserting to Supabase", error); - } finally { - spinner.stop(); - } -} // this should run only once for uploding pdf data to supabase then you can continue with the chatbot functionality await InsertToSupabase(splitedDocs); -const chatSchema = z.object({ - answer: z.string().describe("The answer to the question"), -}); +export const ChatRouter: any = server.createApp(); -ChatRouter.get("/", async (c) => { +ChatRouter.get("/", async (c: any) => { + console.time("ExecutionTime") const searchStr = c.req.query("question").toLowerCase(); - const promptLoader = jsonnet.evaluateFile(promptPath); - - const promptTemplate = await JSON.parse(promptLoader).custom_template; - - const embeddingsArr = await getEmbeddings(searchStr); - const response = await supabase.getDataFromQuery({ - client, - functionNameToCall: "match_documents", - query_embedding: embeddingsArr[0].embedding, - similarity_threshold: 0.5, - match_count: 1, - }); - const contentArr: string[] = []; - for (let i = 0; i < response?.length; i++) { - const element = response[i]; - contentArr.push(element.content); - } - - const content = contentArr.join(" "); - - let InterLoader = await jsonnet - .extString("promptTemplate", promptTemplate) - .extString("query", searchStr || "") - .extString("content", content) - .evaluateFile(InterPath); - - const prompt = JSON.parse(InterLoader).prompt; - const res = await llm.zodSchemaResponse({ schema: chatSchema, prompt: prompt }); - return c.json(res); + jsonnet.extString("query", searchStr); + jsonnet.javascriptCallback("getEmbeddings", getEmbeddings); + jsonnet.javascriptCallback("getQueryMatch", getQueryMatch); + jsonnet.javascriptCallback("openAICall", openAICall); + const response = JSON.parse(jsonnet.evaluateFile(path.join(__dirname, "../../../jsonnet/main.jsonnet"))); + console.timeEnd("ExecutionTime"); + return c.json(response); }); diff --git a/JS/edgechains/examples/chat-with-pdf/tsconfig.json b/JS/edgechains/examples/chat-with-pdf/tsconfig.json index 30a867b9a..22f20e090 100644 --- a/JS/edgechains/examples/chat-with-pdf/tsconfig.json +++ b/JS/edgechains/examples/chat-with-pdf/tsconfig.json @@ -1,19 +1,14 @@ { "compilerOptions": { "target": "ES2022", + "moduleResolution": "NodeNext", "module": "NodeNext", + "rootDir": "./src", + "outDir": "./dist", "esModuleInterop": true, "forceConsistentCasingInFileNames": true, "strict": true, - "skipLibCheck": true, - "jsx": "react-jsx", - "jsxImportSource": "hono/jsx", - "noImplicitAny": false, - "moduleResolution": "NodeNext", - "declaration": true, - "outDir": "./dist", - "rootDir": "./src" + "skipLibCheck": true }, - "include": ["src/**/*.ts"], - "exclude": ["node_modules", "dist", "./src/tests/**/*"] + "exclude": ["./**/*.test.ts", "vitest.config.ts", "./jsonnet/**/*"] } diff --git a/JS/edgechains/examples/react-chain/jsonnet/react-chain.jsonnet b/JS/edgechains/examples/react-chain/jsonnet/main.jsonnet similarity index 90% rename from JS/edgechains/examples/react-chain/jsonnet/react-chain.jsonnet rename to JS/edgechains/examples/react-chain/jsonnet/main.jsonnet index db39de2fd..030bd5034 100644 --- a/JS/edgechains/examples/react-chain/jsonnet/react-chain.jsonnet +++ b/JS/edgechains/examples/react-chain/jsonnet/main.jsonnet @@ -139,11 +139,30 @@ local extractAction(text, actionNumber) = local get_search_string = arakoo.regexMatch(text, searchPattern)[0]; get_search_string; +local findMatch(text, searchString) = + local match = arakoo.includes(searchString, text) || arakoo.includes(text, searchString); + match; local getObservation(searchTitle) = - local pageId = arakoo.native("callWikipediaApi")(searchTitle); - local extractedSummary = arakoo.native("getExtractedSummary")(pageId); - extractedSummary; + local url = "https://en.wikipedia.org/w/api.php"; + local apiUrl = url + "?action=query&format=json&list=search&formatversion=2&srsearch=" + searchTitle; + local wikiConfig = { + url: apiUrl, + }; + local wikipediaResponse = std.parseJson(arakoo.native("apiCall")(wikiConfig)).query.search; + local data = [ + if findMatch(searchTitle, wikipediaResponse[x].title) then {title: wikipediaResponse[x].title, pageId: wikipediaResponse[x].pageid} for x in std.range(0, std.length(wikipediaResponse) - 1) + ]; + + local pageId = std.toString(data[0].pageId); + local summaryUrl = url + "?action=query&format=json&exintro&explaintext&prop=extracts&redirects=1&pageids=" + pageId; + + local summaryConfig = { + url: summaryUrl, + }; + local extractedSummary = std.parseJson(arakoo.native("apiCall")(summaryConfig)).query.pages[pageId].extract; + + extractedSummary; local extractAnswer(text, actionNumber) = diff --git a/JS/edgechains/examples/react-chain/package.json b/JS/edgechains/examples/react-chain/package.json index f786273af..ce601d1a4 100644 --- a/JS/edgechains/examples/react-chain/package.json +++ b/JS/edgechains/examples/react-chain/package.json @@ -13,7 +13,7 @@ "license": "ISC", "dependencies": { "@arakoodev/edgechains.js": "^0.1.23", - "@arakoodev/jsonnet": "^0.2.1", + "@arakoodev/jsonnet": "^0.3.1", "sync-rpc": "^1.3.6" }, "devDependencies": { diff --git a/JS/edgechains/examples/react-chain/src/lib/apiCall.cts b/JS/edgechains/examples/react-chain/src/lib/apiCall.cts new file mode 100644 index 000000000..397e61bef --- /dev/null +++ b/JS/edgechains/examples/react-chain/src/lib/apiCall.cts @@ -0,0 +1,15 @@ + +import axios from "axios"; + +function apiCall() { + return (config: any) => { + const response = axios.request(config).then((response: any) => { + return JSON.stringify(response.data); + }).catch((error: any) => { + console.error(error); + }) + return response; + } +} + +module.exports = apiCall diff --git a/JS/edgechains/examples/react-chain/src/lib/getExtractedSummary.cts b/JS/edgechains/examples/react-chain/src/lib/getExtractedSummary.cts deleted file mode 100644 index e54987a1e..000000000 --- a/JS/edgechains/examples/react-chain/src/lib/getExtractedSummary.cts +++ /dev/null @@ -1,26 +0,0 @@ -const axios = require("axios"); - -function getExtractedSummary() { - return function (pageId: string) { - const url = "https://en.wikipedia.org/w/api.php"; - const queryParams = new URLSearchParams({ - action: "query", - format: "json", - exintro: "", - explaintext: "", - prop: "extracts", - redirects: "1", - pageids: pageId, - }); - const apiUrl = `${url}?${queryParams.toString()}`; - try { - return axios(apiUrl).then((res: any) => { - return res.data.query.pages[pageId].extract; - }); - } catch (err) { - console.error(err); - } - }; -} - -module.exports = getExtractedSummary; diff --git a/JS/edgechains/examples/react-chain/src/routes/react-chain.ts b/JS/edgechains/examples/react-chain/src/routes/react-chain.ts index 42ac9a8f4..a02a89b17 100644 --- a/JS/edgechains/examples/react-chain/src/routes/react-chain.ts +++ b/JS/edgechains/examples/react-chain/src/routes/react-chain.ts @@ -1,5 +1,4 @@ import { ArakooServer } from "@arakoodev/edgechains.js/arakooserver"; -//@ts-ignore import Jsonnet from "@arakoodev/jsonnet"; import { fileURLToPath } from "url"; import path from "path"; @@ -10,9 +9,10 @@ const server = new ArakooServer(); const jsonnet = new Jsonnet(); const __dirname = fileURLToPath(import.meta.url); -const getExtractedSummary = createClient(path.join(__dirname, "../../lib/getExtractedSummary.cjs")); -const callWikipediaApi = createClient(path.join(__dirname, "../../lib/callWikipediaApi.cjs")); +// const getExtractedSummary = createClient(path.join(__dirname, "../../lib/getExtractedSummary.cjs")); +// const callWikipediaApi = createClient(path.join(__dirname, "../../lib/callWikipediaApi.cjs")); const openAICall = createClient(path.join(__dirname, "../../lib/generateResponse.cjs")); +const apiCall = createClient(path.join(__dirname, "../../lib/apiCall.cjs")); export const ReactChainRouter = server.createApp(); @@ -21,12 +21,9 @@ ReactChainRouter.get("/", async (c: any) => { const question = c.req.query("question"); jsonnet.extString("question", question || ""); - jsonnet.javascriptCallback("getExtractedSummary", getExtractedSummary); jsonnet.javascriptCallback("openAICall", openAICall); - let response = jsonnet - .javascriptCallback("callWikipediaApi", callWikipediaApi) - .evaluateFile(path.join(__dirname, "../../../jsonnet/react-chain.jsonnet")); - console.log({ response }); + jsonnet.javascriptCallback("apiCall", apiCall); + let response = jsonnet.evaluateFile(path.join(__dirname, "../../../jsonnet/main.jsonnet")); return c.json(response); } catch (error) { return c.json({ diff --git a/JS/edgechains/examples/summarize-page/.gitignore b/JS/edgechains/examples/summarize-page/.gitignore new file mode 100644 index 000000000..1521c8b76 --- /dev/null +++ b/JS/edgechains/examples/summarize-page/.gitignore @@ -0,0 +1 @@ +dist diff --git a/JS/edgechains/examples/summarize-page/package.json b/JS/edgechains/examples/summarize-page/package.json index c77c5770c..838417b13 100644 --- a/JS/edgechains/examples/summarize-page/package.json +++ b/JS/edgechains/examples/summarize-page/package.json @@ -1,22 +1,22 @@ { - "name": "summarizer-app", - "version": "1.0.0", - "description": "", - "main": "index.js", - "type": "module", - "keywords": [], - "author": "", - "scripts": { - "start": "tsc && node --experimental-wasm-modules ./dist/index.js" - }, - "license": "ISC", - "dependencies": { - "@arakoodev/edgechains.js": "^0.1.23", - "@arakoodev/jsonnet": "^0.2.1", - "sync-rpc": "^1.3.6", - "zod": "^3.23.8" - }, - "devDependencies": { - "@types/request": "^2.48.12" - } + "name": "summarizer-app", + "version": "1.0.0", + "description": "", + "main": "index.js", + "type": "module", + "keywords": [], + "author": "", + "scripts": { + "start": "tsc && node --experimental-wasm-modules ./dist/index.js" + }, + "license": "ISC", + "dependencies": { + "@arakoodev/edgechains.js": "^0.1.23", + "@arakoodev/jsonnet": "^0.3.1", + "sync-rpc": "^1.3.6", + "zod": "^3.23.8" + }, + "devDependencies": { + "@types/request": "^2.48.12" + } } diff --git a/JS/edgechains/examples/summarize-page/src/index.ts b/JS/edgechains/examples/summarize-page/src/index.ts index e231fd120..38474c079 100644 --- a/JS/edgechains/examples/summarize-page/src/index.ts +++ b/JS/edgechains/examples/summarize-page/src/index.ts @@ -15,12 +15,14 @@ const __dirname = fileURLToPath(import.meta.url); const openAICall = createClient(path.join(__dirname, "../lib/generateResponse.cjs")); const getPageContent = createClient(path.join(__dirname, "../lib/getDataFromUrl.cjs")); + app.get("/", async (c: any) => { const pageUrl = c.req.query("pageUrl"); jsonnet.extString("pageUrl", pageUrl || ""); jsonnet.javascriptCallback("openAICall", openAICall); jsonnet.javascriptCallback("getPageContent", getPageContent); let response = jsonnet.evaluateFile(path.join(__dirname, "../../jsonnet/main.jsonnet")); + console.log(response) return c.json(response); }); diff --git a/JS/edgechains/examples/summarize-page/src/lib/generateResponse.cts b/JS/edgechains/examples/summarize-page/src/lib/generateResponse.cts index a7b3d2750..2b2872b61 100644 --- a/JS/edgechains/examples/summarize-page/src/lib/generateResponse.cts +++ b/JS/edgechains/examples/summarize-page/src/lib/generateResponse.cts @@ -19,8 +19,7 @@ const openai = new OpenAI({ function openAICall() { return function (prompt: string) { try { - return openai.zodSchemaResponse({ prompt, schema: schema }).then((res: any) => { - console.log({ res }); + return openai.zodSchemaResponse({ prompt, schema }).then((res: any) => { return JSON.stringify(res); }); } catch (error) { diff --git a/JS/edgechains/examples/summarize-page/src/lib/getDataFromUrl.cts b/JS/edgechains/examples/summarize-page/src/lib/getDataFromUrl.cts index 4252ca4d7..3acfb18f1 100644 --- a/JS/edgechains/examples/summarize-page/src/lib/getDataFromUrl.cts +++ b/JS/edgechains/examples/summarize-page/src/lib/getDataFromUrl.cts @@ -4,9 +4,13 @@ const scraper = new WebScraper(); function getPageContent() { return (url: string) => { - return scraper.getContent(url).then((res: any) => { - return res; - }); + try { + return scraper.getContent(url).then((res: any) => { + return res; + }); + } catch (error) { + console.log(error) + } }; }