Skip to content

Commit

Permalink
fixing-exmaples (#372)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Raghuwanshi authored Jun 1, 2024
1 parent f25d7c3 commit fe1f91b
Show file tree
Hide file tree
Showing 23 changed files with 301 additions and 228 deletions.
3 changes: 2 additions & 1 deletion JS/edgechains/examples/chat-with-llm/dist/index.js
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ function openAICall() {
return openai.zodSchemaResponse({ prompt, schema: schema }).then((res) => {
return JSON.stringify(res);
});
} catch (error) {
}
catch (error) {
return error;
}
};
Expand Down
40 changes: 20 additions & 20 deletions JS/edgechains/examples/chat-with-llm/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
13 changes: 0 additions & 13 deletions JS/edgechains/examples/chat-with-pdf/jsonnet/intermediate.jsonnet

This file was deleted.

26 changes: 26 additions & 0 deletions JS/edgechains/examples/chat-with-pdf/jsonnet/main.jsonnet
Original file line number Diff line number Diff line change
@@ -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
9 changes: 0 additions & 9 deletions JS/edgechains/examples/chat-with-pdf/jsonnet/prompt.jsonnet

This file was deleted.

3 changes: 2 additions & 1 deletion JS/edgechains/examples/chat-with-pdf/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
64 changes: 64 additions & 0 deletions JS/edgechains/examples/chat-with-pdf/src/lib/InsertToSupabase.ts
Original file line number Diff line number Diff line change
@@ -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();
}
}
29 changes: 29 additions & 0 deletions JS/edgechains/examples/chat-with-pdf/src/lib/generateResponse.cts
Original file line number Diff line number Diff line change
@@ -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;
23 changes: 23 additions & 0 deletions JS/edgechains/examples/chat-with-pdf/src/lib/getEmbeddings.cts
Original file line number Diff line number Diff line change
@@ -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;
37 changes: 37 additions & 0 deletions JS/edgechains/examples/chat-with-pdf/src/lib/getQueryMatch.cts
Original file line number Diff line number Diff line change
@@ -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

Loading

0 comments on commit fe1f91b

Please sign in to comment.