Skip to content

Instantly share code, notes, and snippets.

@ochafik

ochafik/test.mjs Secret

Created June 4, 2024 13:27
Show Gist options
  • Select an option

  • Save ochafik/af31db96799f82c3a9320ba409f98e18 to your computer and use it in GitHub Desktop.

Select an option

Save ochafik/af31db96799f82c3a9320ba409f98e18 to your computer and use it in GitHub Desktop.
LlamaIndexTS Tutorial
/*
brew install ochafik/llama.cpp/llama-cpp
pip install concurrently
mkdir models
concurrently \
"llama-cpp serve --port 8088 -fa -c 0 --metrics \
--embeddings \
-hfr nomic-ai/nomic-embed-text-v1.5-GGUF -hff nomic-embed-text-v1.5.Q4_K_M.gguf \
--rope-freq-scale 0.75" \
"llama-cpp serve --port 8080 -fa -c 0 --metrics \
-ctk q4_0 \
https://huggingface.co/bartowski/Phi-3-medium-128k-instruct-GGUF/resolve/main/Phi-3-medium-128k-instruct-Q5_K_M.gguf"
for x in env core ; do ( cd packages/$x && npm install ) ; done⠹
// -mu https://huggingface.co/NousResearch/Hermes-2-Pro-Llama-3-8B-GGUF/resolve/main/Hermes-2-Pro-Llama-3-8B-Q8_0.gguf
*/
import fs from "node:fs/promises";
// import { Ollama, Settings } from "llamaindex";
import {
Document,
MetadataMode,
NodeWithScore,
VectorStoreIndex,
OpenAI,
Settings,
} from "llamaindex";
Settings.llm = new OpenAI({
baseURL: "http://localhost:8080",
apiKey: "..."
});
Settings.embedModel = new OpenAI({
baseURL: "http://localhost:8088",
apiKey: "..."
});
// export function f {}
async function main() {
// Load essay from abramov.txt in Node
const path = "node_modules/llamaindex/examples/abramov.txt";
const essay = await fs.readFile(path, "utf-8");
// Create Document object with essay
const document = new Document({ text: essay, id_: path });
// Split text and create embeddings. Store them in a VectorStoreIndex
const index = await VectorStoreIndex.fromDocuments([document]);
// Query the index
const queryEngine = index.asQueryEngine();
const { response, sourceNodes } = await queryEngine.query({
query: "What did the author do in college?",
});
// Output response with sources
console.log(response);
if (sourceNodes) {
sourceNodes.forEach((source, index) => {
console.log(
`\n${index}: Score: ${source.score} - ${source.node.getContent(MetadataMode.NONE).substring(0, 50)}...\n`,
);
});
}
}
main().catch(console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment