-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f25d7c3
commit fe1f91b
Showing
23 changed files
with
301 additions
and
228 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
JS/edgechains/examples/chat-with-pdf/jsonnet/intermediate.jsonnet
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
JS/edgechains/examples/chat-with-pdf/src/lib/InsertToSupabase.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
JS/edgechains/examples/chat-with-pdf/src/lib/generateResponse.cts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
JS/edgechains/examples/chat-with-pdf/src/lib/getEmbeddings.cts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
37
JS/edgechains/examples/chat-with-pdf/src/lib/getQueryMatch.cts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
Oops, something went wrong.