Compare commits

..

40 Commits

Author SHA1 Message Date
Emanuel Ferreira e658cfe0fb chore: fix llm 2024-03-18 11:29:59 -03:00
Emanuel Ferreira 8bad594b05 sync dependencies 2024-03-18 11:07:29 -03:00
Emanuel Ferreira 8ed8581f5b merge 2024-03-18 11:04:15 -03:00
Emanuel Ferreira ee2a3b83c9 minor changes (temp) 2024-03-18 11:00:00 -03:00
Emanuel Ferreira 8c3a2d9062 generics 2024-03-18 10:38:11 -03:00
Marcus Schiesser 6cb75b54a0 docs: update release process 2024-03-18 16:22:59 +07:00
Marcus Schiesser 53edfe93cf release llamaindex@0.2.1 2024-03-18 16:17:58 +07:00
Marcus Schiesser b856deae43 fix: fix syncing edge with core version 2024-03-18 15:53:31 +07:00
Marcus Schiesser 259c842259 Support NextJS edge runtime (#618) 2024-03-18 15:13:27 +07:00
shodevacc ffb195ea7a Fix: Metadata filters doesn't seem to work for Qdrant (#623) 2024-03-18 11:53:51 +07:00
Alex Yang b4677534d1 ci: install node_modules (#653) 2024-03-18 12:49:28 +08:00
Peli de Halleux f967b82467 [docs] missing await in sample (#650) 2024-03-15 16:23:27 -03:00
Marcus Schiesser c81946930e test: fix openai mock 2024-03-15 15:20:57 +07:00
Marcus Schiesser 1008b775a4 test: cleaned up tests and added test to ignore duplicates 2024-03-15 12:05:58 +07:00
Huu Le (Lee) 41210dfc51 feat: Add auto create collection and node metadata for Milvus vector store (#645) 2024-03-15 10:46:25 +07:00
Emanuel Ferreira 67b7272249 feat: expected minor version (#644) 2024-03-14 09:34:21 -03:00
Marcus Schiesser 964e045903 feat: add support for snapshots 2024-03-14 10:23:58 +07:00
Marcus Schiesser 137cf67f40 fix: Use Pinecone namespaces for all operations (#633) 2024-03-14 10:15:52 +07:00
Emanuel Ferreira 309a526e3c RELEASING: Releasing 5 package(s) (#643) 2024-03-13 22:17:27 -03:00
Emanuel Ferreira e1ac089d39 u 2024-03-13 20:03:22 -03:00
yisding dd95927498 Claude haiku (#642) 2024-03-13 19:57:45 -03:00
Thuc Pham 4f72feae91 Feat: add tools module (#621) 2024-03-13 16:41:36 +07:00
Marcus Schiesser 3cd8f9f597 refactor: move create-llama to own repo (#641) 2024-03-13 15:53:33 +07:00
Huu Le (Lee) d2e8d0c62a feat: Add Milvus vector store (#640)
Co-authored-by: Michael Schramm <michael@tucan.ai>
2024-03-13 13:55:48 +07:00
Huu Le (Lee) fafbd8c9c7 fix: add missing env value; improve docs and error message (#638) 2024-03-13 09:08:53 +07:00
Marcus Schiesser a40c91b054 docs: fixed path 2024-03-12 14:45:27 +07:00
Marcus Schiesser 98894055c6 fix: create-llama release 2024-03-12 13:42:38 +07:00
Marcus Schiesser 4589a84643 RELEASING: Releasing 1 package(s)
Releases:
  create-llama@0.0.28

[skip ci]
2024-03-12 13:41:36 +07:00
Huu Le (Lee) e6b7f52d3e fix: add missing check env logic (#636) 2024-03-12 12:29:00 +07:00
Marcus Schiesser b169db617a refactor: use a function for webpack config (#634) 2024-03-12 11:10:31 +07:00
Huu Le (Lee) 89a49f4f4f feat: Add more. env variables to config host, port, llm and embedding (#630) 2024-03-12 09:22:21 +07:00
Marcus Schiesser 58490715fe refactor: clean nextjs config generation (use JSON) (#631) 2024-03-11 14:16:15 +07:00
Huu Le (Lee) 4c2283c4e5 fix: Rename folder e2e/.cache to e2e/cache (#632) 2024-03-11 14:15:13 +07:00
Eka Prasetia a059070dec docs: Fix typo in transformations.md (#625) 2024-03-11 12:16:23 +07:00
Emanuel Ferreira 20dfeb4cfa chore: remove comment (#624) 2024-03-08 15:54:24 -03:00
Emanuel Ferreira aefc3266c1 feat: experimental package + json query engine (#613) 2024-03-07 14:34:55 -03:00
Huu Le (Lee) fdf48dd459 feat: Add start in VSCode option and support python for dev container (#619) 2024-03-07 17:19:08 +07:00
Alex Yang 66525346a2 build: use single swc config (#620) 2024-03-06 23:41:42 -06:00
Alex Yang c9b2ec4a2b fix: release 2024-03-06 23:32:02 -06:00
Marcus Schiesser bf583a7266 Use parameter object for retrieve function of Retriever (#616) 2024-03-06 21:15:22 -08:00
311 changed files with 3839 additions and 7841 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
{
"$schema": "https://unpkg.com/@changesets/config@2.3.1/schema.json",
"changelog": "@changesets/cli/changelog",
"commit": true,
"commit": false,
"fixed": [],
"linked": [],
"access": "public",
-12
View File
@@ -1,12 +0,0 @@
---
"llamaindex": patch
"@llamaindex/core-test": patch
---
- Add missing exports:
- `IndexStructType`,
- `IndexDict`,
- `jsonToIndexStruct`,
- `IndexList`,
- `IndexStruct`
- Fix `IndexDict.toJson()` method
-5
View File
@@ -1,5 +0,0 @@
---
"llamaindex": patch
---
Add streaming to agents
-68
View File
@@ -1,68 +0,0 @@
name: E2E Tests
on:
push:
branches: [main]
pull_request:
paths:
- "packages/create-llama/**"
- ".github/workflows/e2e.yml"
branches: [main]
env:
POETRY_VERSION: "1.6.1"
jobs:
e2e:
name: create-llama
timeout-minutes: 60
strategy:
fail-fast: true
matrix:
node-version: [18, 20]
python-version: ["3.11"]
os: [macos-latest, windows-latest]
defaults:
run:
shell: bash
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- name: Set up python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: ${{ env.POETRY_VERSION }}
- uses: pnpm/action-setup@v2
- name: Setup Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: "pnpm"
- name: Install dependencies
run: pnpm install
- name: Install Playwright Browsers
run: pnpm exec playwright install --with-deps
working-directory: ./packages/create-llama
- name: Build create-llama
run: pnpm run build
working-directory: ./packages/create-llama
- name: Pack
run: pnpm pack --pack-destination ./output
working-directory: ./packages/create-llama
- name: Extract Pack
run: tar -xvzf ./output/*.tgz -C ./output
working-directory: ./packages/create-llama
- name: Run Playwright tests
run: pnpm exec playwright test
env:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
working-directory: ./packages/create-llama
- uses: actions/upload-artifact@v3
if: always()
with:
name: playwright-report
path: ./packages/create-llama/playwright-report/
retention-days: 30
+12
View File
@@ -14,11 +14,23 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v2
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version-file: ".nvmrc"
cache: "pnpm"
- name: Install dependencies
run: pnpm install
- name: Publish @llamaindex/env
run: npx jsr publish
working-directory: packages/env
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Publish @llamaindex/core
run: npx jsr publish --allow-slow-types
working-directory: packages/core
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+18
View File
@@ -44,6 +44,24 @@ jobs:
name: typecheck-build-dist
path: ./packages/core/dist
if-no-files-found: error
core-edge-runtime:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v2
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version-file: ".nvmrc"
cache: "pnpm"
- name: Install dependencies
run: pnpm install
- name: Build
run: pnpm run build --filter @llamaindex/edge
- name: Build Edge Runtime
run: pnpm run build
working-directory: ./packages/edge/e2e/test-edge-runtime
typecheck-examples:
runs-on: ubuntu-latest
View File
+7 -3
View File
@@ -81,11 +81,15 @@ Any changes you make should be reflected in the browser. If you need to regenera
## Publishing
To publish a new version of the library, run
To publish a new version of the library, first create a new version:
```shell
pnpm new-version
```
If everything looks good, commit the generated files and release the new version:
```shell
pnpm new-llamaindex
pnpm new-create-llama
pnpm release
git push # push to the main branch
git push --tags
+36
View File
@@ -121,6 +121,42 @@ const nextConfig = {
module.exports = nextConfig;
```
### NextJS with Milvus:
As proto files are not loaded per default in NextJS, you'll need to add the following to your next.config.js to have it load the proto files.
```js
const path = require("path");
const CopyWebpackPlugin = require("copy-webpack-plugin");
// next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
webpack: (config, { isServer }) => {
if (isServer) {
// Copy the proto files to the server build directory
config.plugins.push(
new CopyWebpackPlugin({
patterns: [
{
from: path.join(
__dirname,
"node_modules/@zilliz/milvus2-sdk-node/dist",
),
to: path.join(__dirname, ".next"),
},
],
}),
);
}
// Important: return the modified config
return config;
},
};
module.exports = nextConfig;
```
## Supported LLMs:
- OpenAI GPT-3.5-turbo and GPT-4
@@ -1,6 +1,6 @@
# Transformations
A transformation is something that takes a list of nodes as an input, and returns a list of nodes. Each component that implements the Transformatio class has both a `transform` definition responsible for transforming the nodes
A transformation is something that takes a list of nodes as an input, and returns a list of nodes. Each component that implements the Transformation class has both a `transform` definition responsible for transforming the nodes.
Currently, the following components are Transformation objects:
@@ -36,7 +36,7 @@ const processor = new SimilarityPostprocessor({
similarityCutoff: 0.7,
});
const filteredNodes = processor.postprocessNodes(nodes);
const filteredNodes = await processor.postprocessNodes(nodes);
// cohere rerank: rerank nodes given query using trained model
const reranker = new CohereRerank({
@@ -100,7 +100,7 @@ const response = await queryEngine.query("<user_query>");
```ts
import { SimilarityPostprocessor } from "llamaindex";
nodes = await index.asRetriever().retrieve("test query str");
nodes = await index.asRetriever().retrieve({ query: "test query str" });
const processor = new SimilarityPostprocessor({
similarityCutoff: 0.7,
+1 -1
View File
@@ -11,7 +11,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Fetch nodes!
const nodesWithScore = await retriever.retrieve("query string");
const nodesWithScore = await retriever.retrieve({ query: "query string" });
```
## API Reference
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// جلب العقد!
const nodesWithScore = await retriever.retrieve("سلسلة الاستعلام");
const nodesWithScore = await retriever.retrieve({ query: "سلسلة الاستعلام" });
```
## مرجع الواجهة البرمجية (API Reference)
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Извличане на върхове!
const nodesWithScore = await retriever.retrieve("query string");
const nodesWithScore = await retriever.retrieve({ query: "query string" });
```
## API Reference (API справка)
@@ -13,7 +13,7 @@ const recuperador = vector_index.asRetriever();
recuperador.similarityTopK = 3;
// Obteniu els nodes!
const nodesAmbPuntuació = await recuperador.retrieve("cadena de consulta");
const nodesAmbPuntuació = await recuperador.retrieve({ query: "cadena de consulta" });
```
## Referència de l'API
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Získání uzlů!
const nodesWithScore = await retriever.retrieve("dotazovací řetězec");
const nodesWithScore = await retriever.retrieve({ query: "dotazovací řetězec" });
```
## API Reference (Odkazy na rozhraní)
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Hent noder!
const nodesWithScore = await retriever.retrieve("forespørgselsstreng");
const nodesWithScore = await retriever.retrieve({ query: "forespørgselsstreng" });
```
## API Reference
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Knoten abrufen!
const nodesWithScore = await retriever.retrieve("Abfragezeichenfolge");
const nodesWithScore = await retriever.retrieve({ query: "Abfragezeichenfolge" });
```
## API-Referenz
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Ανάκτηση κόμβων!
const nodesWithScore = await retriever.retrieve("συμβολοσειρά ερωτήματος");
const nodesWithScore = await retriever.retrieve({ query: "συμβολοσειρά ερωτήματος" });
```
## Αναφορά API
@@ -13,7 +13,7 @@ const recuperador = vector_index.asRetriever();
recuperador.similarityTopK = 3;
// ¡Obtener nodos!
const nodosConPuntuación = await recuperador.retrieve("cadena de consulta");
const nodosConPuntuación = await recuperador.retrieve({ query: "cadena de consulta" });
```
## Referencia de la API
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Too sõlmed!
const nodesWithScore = await retriever.retrieve("päringu string");
const nodesWithScore = await retriever.retrieve({ query: "päringu string" });
```
## API viide
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// بازیابی گره ها!
const nodesWithScore = await retriever.retrieve("رشته پرس و جو");
const nodesWithScore = await retriever.retrieve({ query: "رشته پرس و جو" });
```
## مرجع API
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Hae solmut!
const nodesWithScore = await retriever.retrieve("kyselymerkkijono");
const nodesWithScore = await retriever.retrieve({ query: "kyselymerkkijono" });
```
## API-viite
@@ -11,7 +11,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Récupérer les nœuds !
const nodesWithScore = await retriever.retrieve("chaîne de requête");
const nodesWithScore = await retriever.retrieve({ query: "chaîne de requête" });
```
## Référence de l'API
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// אחזור צמתים!
const nodesWithScore = await retriever.retrieve("מחרוזת שאילתה");
const nodesWithScore = await retriever.retrieve({ query: "מחרוזת שאילתה" });
```
## מדריך לממשק API
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// नोड्स प्राप्त करें!
const nodesWithScore = await retriever.retrieve("क्वेरी स्ट्रिंग");
const nodesWithScore = await retriever.retrieve({ query: "क्वेरी स्ट्रिंग" });
```
## एपीआई संदर्भ (API Reference)
@@ -13,7 +13,7 @@ const dohvatnik = vector_index.asRetriever();
dohvatnik.similarityTopK = 3;
// Dohvati čvorove!
const čvoroviSaRezultatom = await dohvatnik.retrieve("upitni niz");
const čvoroviSaRezultatom = await dohvatnik.retrieve({ query: "upitni niz" });
```
## API Referenca
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Node-ok lekérése!
const nodesWithScore = await retriever.retrieve("lekérdezési karakterlánc");
const nodesWithScore = await retriever.retrieve({ query: "lekérdezési karakterlánc" });
```
## API Referencia
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Mengambil node!
const nodesWithScore = await retriever.retrieve("string query");
const nodesWithScore = await retriever.retrieve({ query: "string query" });
```
## Referensi API
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Recupera i nodi!
const nodesWithScore = await retriever.retrieve("stringa di query");
const nodesWithScore = await retriever.retrieve({ query: "stringa di query" });
```
## Riferimento API
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// ノードを取得します!
const nodesWithScore = await retriever.retrieve("クエリ文字列");
const nodesWithScore = await retriever.retrieve({ query: "クエリ文字列" });
```
## API リファレンス
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// 노드를 가져옵니다!
const nodesWithScore = await retriever.retrieve("쿼리 문자열");
const nodesWithScore = await retriever.retrieve({ query: "쿼리 문자열" });
```
## API 참조
@@ -13,7 +13,7 @@ const gavėjas = vector_index.asRetriever();
gavėjas.similarityTopK = 3;
// Išgaunami mazgai!
const mazgaiSuRezultatu = await gavėjas.retrieve("užklausos eilutė");
const mazgaiSuRezultatu = await gavėjas.retrieve({ query: "užklausos eilutė" });
```
## API nuorodos (API Reference)
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Haal knooppunten op!
const nodesWithScore = await retriever.retrieve("zoekopdracht");
const nodesWithScore = await retriever.retrieve({ query: "zoekopdracht" });
```
## API Referentie
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Hent noder!
const nodesWithScore = await retriever.retrieve("spørringsstreng");
const nodesWithScore = await retriever.retrieve({ query: "spørringsstreng" });
```
## API-referanse
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Pobierz węzły!
const nodesWithScore = await retriever.retrieve("ciąg zapytania");
const nodesWithScore = await retriever.retrieve({ query: "ciąg zapytania" });
```
## Dokumentacja interfejsu API
@@ -13,7 +13,7 @@ const recuperador = vector_index.asRetriever();
recuperador.similarityTopK = 3;
// Buscar nós!
const nósComPontuação = await recuperador.retrieve("string de consulta");
const nósComPontuação = await recuperador.retrieve({ query: "string de consulta" });
```
## Referência da API
@@ -13,7 +13,7 @@ const recuperator = vector_index.asRetriever();
recuperator.similarityTopK = 3;
// Preia nodurile!
const noduriCuScor = await recuperator.retrieve("șir de interogare");
const noduriCuScor = await recuperator.retrieve({ query: "șir de interogare" });
```
## Referință API
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Получение узлов!
const nodesWithScore = await retriever.retrieve("строка запроса");
const nodesWithScore = await retriever.retrieve({ query: "строка запроса" });
```
## Справочник по API
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Dohvati čvorove!
const nodesWithScore = await retriever.retrieve("upitni niz");
const nodesWithScore = await retriever.retrieve({ query: "upitni niz" });
```
## API Referenca
@@ -13,7 +13,7 @@ const pridobitelj = vector_index.asRetriever();
pridobitelj.similarityTopK = 3;
// Pridobivanje vozlišč!
const vozliščaZRezultatom = await pridobitelj.retrieve("poizvedbeni niz");
const vozliščaZRezultatom = await pridobitelj.retrieve({ query: "poizvedbeni niz" });
```
## API Sklic
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Získajte uzly!
const nodesWithScore = await retriever.retrieve("reťazec dotazu");
const nodesWithScore = await retriever.retrieve({ query: "reťazec dotazu" });
```
## API Referencia
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Hämta noder!
const nodesWithScore = await retriever.retrieve("frågesträng");
const nodesWithScore = await retriever.retrieve({ query: "frågesträng" });
```
## API-referens
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// เรียกคืนโหนด!
const nodesWithScore = await retriever.retrieve("query string");
const nodesWithScore = await retriever.retrieve({ query: "query string" });
```
## API Reference (การอ้างอิง API)
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Düğümleri getir!
const nodesWithScore = await retriever.retrieve("sorgu dizesi");
const nodesWithScore = await retriever.retrieve({ query: "sorgu dizesi" });
```
## API Referansı
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Отримати вузли!
const nodesWithScore = await retriever.retrieve("рядок запиту");
const nodesWithScore = await retriever.retrieve({ query: "рядок запиту" });
```
## Довідник API
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Lấy các node!
const nodesWithScore = await retriever.retrieve("chuỗi truy vấn");
const nodesWithScore = await retriever.retrieve({ query: "chuỗi truy vấn" });
```
## Tài liệu tham khảo API
@@ -11,7 +11,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// 获取节点!
const nodesWithScore = await retriever.retrieve("查询字符串");
const nodesWithScore = await retriever.retrieve({ query: "查询字符串" });
```
## API 参考
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// 提取節點!
const nodesWithScore = await retriever.retrieve("查詢字串");
const nodesWithScore = await retriever.retrieve({ query: "查詢字串" });
```
## API 參考
+14
View File
@@ -0,0 +1,14 @@
# examples
## 0.0.4
### Patch Changes
- d2e8d0c: add support for Milvus vector store
- Updated dependencies [d2e8d0c]
- Updated dependencies [aefc326]
- Updated dependencies [484a710]
- Updated dependencies [d766bd0]
- Updated dependencies [dd95927]
- Updated dependencies [bf583a7]
- llamaindex@0.2.0
+19
View File
@@ -0,0 +1,19 @@
import { Anthropic } from "llamaindex";
(async () => {
const anthropic = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
model: "claude-3-haiku",
});
const result = await anthropic.chat({
messages: [
{ content: "You want to talk in rhymes.", role: "system" },
{
content:
"How much wood would a woodchuck chuck if a woodchuck could chuck wood?",
role: "user",
},
],
});
console.log(result);
})();
+2 -2
View File
@@ -32,10 +32,10 @@ run `ts-node astradb/example`
This sample loads the same dataset of movie reviews as the Astra Portal sample dataset. (Feel free to load the data in your the Astra Data Explorer to compare)
run `ts-node astradb/load`
run `npx ts-node astradb/load`
### Use RAG to Query the data
Check out your data in the Astra Data Explorer and change the sample query as you see fit.
run `ts-node astradb/query`
run `npx ts-node astradb/query`
+34
View File
@@ -0,0 +1,34 @@
# Milvus Vector Store
Here are two sample scripts which work with loading and querying data from a Milvus Vector Store.
## Prerequisites
- An Milvus Vector Database
- Hosted https://milvus.io/
- Self Hosted https://milvus.io/docs/install_standalone-docker.md
- An OpenAI API Key
## Setup
1. Set your env variables:
- `MILVUS_ADDRESS`: Address of your Milvus Vector Store (like localhost:19530)
- `MILVUS_USERNAME`: empty or username for your Milvus Vector Store
- `MILVUS_PASSWORD`: empty or password for your Milvus Vector Store
- `OPENAI_API_KEY`: Your OpenAI key
2. `cd` Into the `examples` directory
3. run `npm i`
## Load the data
This sample loads the same dataset of movie reviews as sample dataset. You can install https://github.com/zilliztech/attu to inspect the loaded data.
run `npx ts-node milvus/load`
## Use RAG to Query the data
Check out your data in Attu and change the sample query as you see fit.
run `npx ts-node milvus/query`
+26
View File
@@ -0,0 +1,26 @@
import {
MilvusVectorStore,
PapaCSVReader,
storageContextFromDefaults,
VectorStoreIndex,
} from "llamaindex";
const collectionName = "movie_reviews";
async function main() {
try {
const reader = new PapaCSVReader(false);
const docs = await reader.loadData("./data/movie_reviews.csv");
const vectorStore = new MilvusVectorStore({ collection: collectionName });
const ctx = await storageContextFromDefaults({ vectorStore });
const index = await VectorStoreIndex.fromDocuments(docs, {
storageContext: ctx,
});
} catch (e) {
console.error(e);
}
}
main();
+30
View File
@@ -0,0 +1,30 @@
import {
MilvusVectorStore,
serviceContextFromDefaults,
VectorStoreIndex,
} from "llamaindex";
const collectionName = "movie_reviews";
async function main() {
try {
const milvus = new MilvusVectorStore({ collection: collectionName });
const ctx = serviceContextFromDefaults();
const index = await VectorStoreIndex.fromVectorStore(milvus, ctx);
const retriever = await index.asRetriever({ similarityTopK: 20 });
const queryEngine = await index.asQueryEngine({ retriever });
const results = await queryEngine.query({
query: "What is the best reviewed movie?",
});
console.log(results.response);
} catch (e) {
console.error(e);
}
}
main();
+3 -3
View File
@@ -27,9 +27,9 @@ async function main() {
// retrieve documents using the index
const index = await createIndex();
const retriever = index.asRetriever({ similarityTopK: 3 });
const results = await retriever.retrieve(
"what are Vincent van Gogh's famous paintings",
);
const results = await retriever.retrieve({
query: "what are Vincent van Gogh's famous paintings",
});
for (const result of results) {
const node = result.node;
if (!node) {
+5 -2
View File
@@ -1,16 +1,19 @@
{
"name": "examples",
"private": true,
"version": "0.0.3",
"version": "0.0.4",
"dependencies": {
"@aws-crypto/sha256-js": "^5.2.0",
"@datastax/astra-db-ts": "^0.1.4",
"@notionhq/client": "^2.2.14",
"@pinecone-database/pinecone": "^1.1.3",
"@zilliz/milvus2-sdk-node": "^2.3.5",
"chromadb": "^1.8.1",
"commander": "^11.1.0",
"dotenv": "^16.4.1",
"llamaindex": "latest",
"mongodb": "^6.2.0"
"mongodb": "^6.2.0",
"pathe": "^1.1.2"
},
"devDependencies": {
"@types/node": "^18.19.10",
-4
View File
@@ -37,10 +37,6 @@ async function main() {
responseSynthesizer,
});
console.log({
promptsToUse: queryEngine.getPrompts(),
});
queryEngine.updatePrompts({
"responseSynthesizer:summaryTemplate": treeSummarizePrompt,
});
+11
View File
@@ -0,0 +1,11 @@
# Qdrant Vector Store Example
How to run `examples/qdrantdb/preFilters.ts`:
Add your OpenAI API Key into a file called `.env` in the parent folder of this directory. It should look like this:
```
OPEN_API_KEY=sk-you-key
```
Now, open a new terminal window and inside `examples`, run `npx ts-node qdrantdb/preFilters.ts`.
+82
View File
@@ -0,0 +1,82 @@
import * as dotenv from "dotenv";
import {
CallbackManager,
Document,
MetadataMode,
QdrantVectorStore,
VectorStoreIndex,
serviceContextFromDefaults,
storageContextFromDefaults,
} from "llamaindex";
// Load environment variables from local .env file
dotenv.config();
const collectionName = "dog_colors";
const qdrantUrl = "http://127.0.0.1:6333";
async function main() {
try {
const docs = [
new Document({
text: "The dog is brown",
metadata: {
dogId: "1",
},
}),
new Document({
text: "The dog is red",
metadata: {
dogId: "2",
},
}),
];
console.log("Creating QdrantDB vector store");
const qdrantVs = new QdrantVectorStore({ url: qdrantUrl, collectionName });
const ctx = await storageContextFromDefaults({ vectorStore: qdrantVs });
console.log("Embedding documents and adding to index");
const index = await VectorStoreIndex.fromDocuments(docs, {
storageContext: ctx,
serviceContext: serviceContextFromDefaults({
callbackManager: new CallbackManager({
onRetrieve: (data) => {
console.log(
"The retrieved nodes are:",
data.nodes.map((node) => node.node.getContent(MetadataMode.NONE)),
);
},
}),
}),
});
console.log(
"Querying index with no filters: Expected output: Brown probably",
);
const queryEngineNoFilters = index.asQueryEngine();
const noFilterResponse = await queryEngineNoFilters.query({
query: "What is the color of the dog?",
});
console.log("No filter response:", noFilterResponse.toString());
console.log("Querying index with dogId 2: Expected output: Red");
const queryEngineDogId2 = index.asQueryEngine({
preFilters: {
filters: [
{
key: "dogId",
value: "2",
filterType: "ExactMatch",
},
],
},
});
const response = await queryEngineDogId2.query({
query: "What is the color of the dog?",
});
console.log("Filter with dogId 2 response:", response.toString());
} catch (e) {
console.error(e);
}
}
main();
+11
View File
@@ -0,0 +1,11 @@
# llamaindex-loader-example
### Patch Changes
- Updated dependencies [d2e8d0c]
- Updated dependencies [aefc326]
- Updated dependencies [484a710]
- Updated dependencies [d766bd0]
- Updated dependencies [dd95927]
- Updated dependencies [bf583a7]
- llamaindex@0.2.0
@@ -2,8 +2,8 @@ import type { BaseReader, Document, Metadata } from "llamaindex";
import {
FILE_EXT_TO_READER,
SimpleDirectoryReader,
TextFileReader,
} from "llamaindex/readers/SimpleDirectoryReader";
import { TextFileReader } from "llamaindex/readers/TextFileReader";
class ZipReader implements BaseReader {
loadData(...args: any[]): Promise<Document<Metadata>[]> {
+6 -4
View File
@@ -11,10 +11,12 @@
"prepare": "husky",
"test": "turbo run test",
"type-check": "tsc -b --diagnostics",
"release": "pnpm run build:release && changeset publish",
"new-llamaindex": "pnpm run build:release && changeset version --ignore create-llama",
"new-create-llama": "pnpm run build:release && changeset version --ignore llamaindex --ignore @llamaindex/core-test",
"new-snapshots": "pnpm run build:release && changeset version --snapshot"
"release": "pnpm run check-minor-version && pnpm run build:release && changeset publish",
"release-snapshot": "pnpm run check-minor-version && pnpm run build:release && changeset publish --tag snapshot",
"check-minor-version": "node ./scripts/check-minor-version",
"update-version": "node ./scripts/update-version",
"new-version": "pnpm run build:release && changeset version && pnpm run check-minor-version && pnpm run update-version",
"new-snapshot": "pnpm run build:release && changeset version --snapshot && pnpm run update-version"
},
"devDependencies": {
"@changesets/cli": "^2.27.1",
+28
View File
@@ -1,5 +1,33 @@
# llamaindex
## 0.2.1
### Patch Changes
- 41210df: Add auto create milvus collection and add milvus node metadata
- 137cf67: Use Pinecone namespaces for all operations
- 259c842: Add support for edge runtime by using @llamaindex/edge
## 0.2.0
### Minor Changes
- bf583a7: Use parameter object for retrieve function of Retriever (to align usage with query function of QueryEngine)
### Patch Changes
- d2e8d0c: add support for Milvus vector store
- aefc326: feat: experimental package + json query engine
- 484a710: - Add missing exports:
- `IndexStructType`,
- `IndexDict`,
- `jsonToIndexStruct`,
- `IndexList`,
- `IndexStruct`
- Fix `IndexDict.toJson()` method
- d766bd0: Add streaming to agents
- dd95927: add Claude Haiku support and update anthropic SDK
## 0.1.21
### Patch Changes
+10 -9
View File
@@ -1,12 +1,14 @@
{
"name": "llamaindex",
"version": "0.1.21",
"version": "0.2.1",
"expectedMinorVersion": "2",
"license": "MIT",
"type": "module",
"dependencies": {
"@anthropic-ai/sdk": "^0.15.0",
"@anthropic-ai/sdk": "^0.18.0",
"@aws-crypto/sha256-js": "^5.2.0",
"@datastax/astra-db-ts": "^0.1.4",
"@grpc/grpc-js": "^1.10.2",
"@llamaindex/cloud": "0.0.4",
"@llamaindex/env": "workspace:*",
"@mistralai/mistralai": "^0.0.10",
@@ -18,15 +20,17 @@
"@types/papaparse": "^5.3.14",
"@types/pg": "^8.11.0",
"@xenova/transformers": "^2.15.0",
"@zilliz/milvus2-sdk-node": "^2.3.5",
"assemblyai": "^4.2.2",
"chromadb": "~1.7.3",
"cohere-ai": "^7.7.5",
"file-type": "^18.7.0",
"js-tiktoken": "^1.0.10",
"lodash": "^4.17.21",
"magic-bytes.js": "^1.10.0",
"mammoth": "^1.6.0",
"md-utils-ts": "^2.0.0",
"mongodb": "^6.3.0",
"mustache": "^4.2.0",
"notion-md-crawler": "^0.0.2",
"openai": "^4.26.1",
"papaparse": "^5.4.1",
@@ -43,6 +47,7 @@
"devDependencies": {
"@swc/cli": "^0.3.9",
"@swc/core": "^1.4.2",
"@types/mustache": "^4.2.5",
"concurrently": "^8.2.2",
"glob": "^10.3.10",
"madge": "^6.1.0",
@@ -59,10 +64,6 @@
"types": "./dist/type/index.d.ts",
"default": "./dist/index.js"
},
"edge-light": {
"types": "./dist/type/index.d.ts",
"default": "./dist/index.edge-light.js"
},
"require": {
"types": "./dist/type/index.d.ts",
"default": "./dist/cjs/index.js"
@@ -92,8 +93,8 @@
"scripts": {
"lint": "eslint .",
"build": "rm -rf ./dist && pnpm run build:esm && pnpm run build:cjs && pnpm run build:type",
"build:esm": "swc src -d dist --strip-leading-paths --config-file .swcrc",
"build:cjs": "swc src -d dist/cjs --strip-leading-paths --config-file .cjs.swcrc",
"build:esm": "swc src -d dist --strip-leading-paths --config-file ../../.swcrc",
"build:cjs": "swc src -d dist/cjs --strip-leading-paths --config-file ../../.cjs.swcrc",
"build:type": "tsc -p tsconfig.json",
"copy": "cp -r ../../README.md ../../LICENSE .",
"postbuild": "pnpm run copy && node -e \"require('fs').writeFileSync('./dist/cjs/package.json', JSON.stringify({ type: 'commonjs' }))\"",
+26 -19
View File
@@ -1,5 +1,6 @@
import type { SubQuestion } from "./engines/query/types.js";
import type { ChatMessage } from "./llm/types.js";
import { PromptTemplate } from "./prompts/types.js";
import type { ToolMetadata } from "./types.js";
/**
@@ -24,28 +25,30 @@ DEFAULT_TEXT_QA_PROMPT_TMPL = (
)
*/
export const defaultTextQaPrompt = ({ context = "", query = "" }) => {
return `Context information is below.
export const defaultTextQaPrompt = () => `Context information is below.
---------------------
${context}
{{context}}
---------------------
Given the context information and not prior knowledge, answer the query.
Query: ${query}
Query: {{query}}
Answer:`;
};
export const defaultTextQaTemplate = new PromptTemplate<{
context: string;
query: string;
}>(defaultTextQaPrompt);
export type TextQaPromptTemplate = typeof defaultTextQaTemplate;
export type TextQaPrompt = typeof defaultTextQaPrompt;
export const anthropicTextQaPrompt: TextQaPrompt = ({
context = "",
query = "",
}) => {
export const anthropicTextQaPrompt = () => {
return `Context information:
<context>
${context}
{{context}}
</context>
Given the context information and not prior knowledge, answer the query.
Query: ${query}`;
Query: {{query}}`;
};
/*
@@ -101,21 +104,25 @@ DEFAULT_REFINE_PROMPT_TMPL = (
)
*/
export const defaultRefinePrompt = ({
query = "",
existingAnswer = "",
context = "",
}) => {
return `The original query is as follows: ${query}
We have provided an existing answer: ${existingAnswer}
export const defaultRefinePrompt = () => {
return `The original query is as follows: {{query}}
We have provided an existing answer: {{existingAnswer}}
We have the opportunity to refine the existing answer (only if needed) with some more context below.
------------
${context}
{{context}}
------------
Given the new context, refine the original answer to better answer the query. If the context isn't useful, return the original answer.
Refined Answer:`;
};
export const defaultRefinePromptTemplate = new PromptTemplate<{
query: string;
existingAnswer: string;
context: string;
}>(defaultRefinePrompt);
export type RefinePromptTemplate = typeof defaultRefinePromptTemplate;
export type RefinePrompt = typeof defaultRefinePrompt;
/*
+7 -5
View File
@@ -2,14 +2,16 @@ import type { Event } from "./callbacks/CallbackManager.js";
import type { NodeWithScore } from "./Node.js";
import type { ServiceContext } from "./ServiceContext.js";
export type RetrieveParams = {
query: string;
parentEvent?: Event;
preFilters?: unknown;
};
/**
* Retrievers retrieve the nodes that most closely match our query in similarity.
*/
export interface BaseRetriever {
retrieve(
query: string,
parentEvent?: Event,
preFilters?: unknown,
): Promise<NodeWithScore[]>;
retrieve(params: RetrieveParams): Promise<NodeWithScore[]>;
getServiceContext(): ServiceContext;
}
-2
View File
@@ -1,5 +1,3 @@
// Assuming that the necessary interfaces and classes (like BaseTool, OpenAI, ChatMessage, CallbackManager, etc.) are defined elsewhere
import { randomUUID } from "@llamaindex/env";
import { Response } from "../../Response.js";
import type { CallbackManager } from "../../callbacks/CallbackManager.js";
+1 -2
View File
@@ -1,4 +1,4 @@
import { randomUUID } from "crypto";
import { randomUUID } from "@llamaindex/env";
import { CallbackManager } from "../../callbacks/CallbackManager.js";
import { AgentChatResponse } from "../../engines/chat/index.js";
import type { ChatResponse, LLM } from "../../llm/index.js";
@@ -17,7 +17,6 @@ import {
ObservationReasoningStep,
ResponseReasoningStep,
} from "./types.js";
type ReActAgentWorkerParams = {
tools: BaseTool[];
llm?: LLM;
+1 -2
View File
@@ -1,4 +1,4 @@
import { randomUUID } from "crypto";
import { randomUUID } from "@llamaindex/env";
import { CallbackManager } from "../../callbacks/CallbackManager.js";
import type { ChatEngineAgentParams } from "../../engines/chat/index.js";
import {
@@ -12,7 +12,6 @@ import type { BaseMemory } from "../../memory/types.js";
import type { AgentWorker, TaskStepOutput } from "../types.js";
import { Task, TaskStep } from "../types.js";
import { AgentState, BaseAgentRunner, TaskState } from "./types.js";
const validateStepFromArgs = (
taskId: string,
input?: string | null,
+1
View File
@@ -3,6 +3,7 @@ import type {
ChatEngineAgentParams,
StreamingAgentChatResponse,
} from "../engines/chat/index.js";
import type { QueryEngineParamsNonStreaming } from "../types.js";
export interface AgentWorker {
+3 -3
View File
@@ -3,7 +3,7 @@ import { RetrieverQueryEngine } from "../engines/query/RetrieverQueryEngine.js";
import type { BaseNodePostprocessor } from "../postprocessors/types.js";
import type { BaseSynthesizer } from "../synthesizers/types.js";
import type { BaseQueryEngine } from "../types.js";
import type { RetrieveParams } from "./LlamaCloudRetriever.js";
import type { CloudRetrieveParams } from "./LlamaCloudRetriever.js";
import { LlamaCloudRetriever } from "./LlamaCloudRetriever.js";
import type { CloudConstructorParams } from "./types.js";
@@ -14,7 +14,7 @@ export class LlamaCloudIndex {
this.params = params;
}
asRetriever(params: RetrieveParams = {}): BaseRetriever {
asRetriever(params: CloudRetrieveParams = {}): BaseRetriever {
return new LlamaCloudRetriever({ ...this.params, ...params });
}
@@ -23,7 +23,7 @@ export class LlamaCloudIndex {
responseSynthesizer?: BaseSynthesizer;
preFilters?: unknown;
nodePostprocessors?: BaseNodePostprocessor[];
} & RetrieveParams,
} & CloudRetrieveParams,
): BaseQueryEngine {
const retriever = new LlamaCloudRetriever({
...this.params,
+9 -10
View File
@@ -2,15 +2,14 @@ import type { PlatformApi, PlatformApiClient } from "@llamaindex/cloud";
import { globalsHelper } from "../GlobalsHelper.js";
import type { NodeWithScore } from "../Node.js";
import { ObjectType, jsonToNode } from "../Node.js";
import type { BaseRetriever } from "../Retriever.js";
import type { BaseRetriever, RetrieveParams } from "../Retriever.js";
import type { ServiceContext } from "../ServiceContext.js";
import { serviceContextFromDefaults } from "../ServiceContext.js";
import type { Event } from "../callbacks/CallbackManager.js";
import type { ClientParams, CloudConstructorParams } from "./types.js";
import { DEFAULT_PROJECT_NAME } from "./types.js";
import { getClient } from "./utils.js";
export type RetrieveParams = Omit<
export type CloudRetrieveParams = Omit<
PlatformApi.RetrievalParams,
"query" | "searchFilters" | "pipelineId" | "className"
> & { similarityTopK?: number };
@@ -18,7 +17,7 @@ export type RetrieveParams = Omit<
export class LlamaCloudRetriever implements BaseRetriever {
client?: PlatformApiClient;
clientParams: ClientParams;
retrieveParams: RetrieveParams;
retrieveParams: CloudRetrieveParams;
projectName: string = DEFAULT_PROJECT_NAME;
pipelineName: string;
serviceContext: ServiceContext;
@@ -35,7 +34,7 @@ export class LlamaCloudRetriever implements BaseRetriever {
});
}
constructor(params: CloudConstructorParams & RetrieveParams) {
constructor(params: CloudConstructorParams & CloudRetrieveParams) {
this.clientParams = { apiKey: params.apiKey, baseUrl: params.baseUrl };
if (params.similarityTopK) {
params.denseSimilarityTopK = params.similarityTopK;
@@ -55,11 +54,11 @@ export class LlamaCloudRetriever implements BaseRetriever {
return this.client;
}
async retrieve(
query: string,
parentEvent?: Event | undefined,
preFilters?: unknown,
): Promise<NodeWithScore[]> {
async retrieve({
query,
parentEvent,
preFilters,
}: RetrieveParams): Promise<NodeWithScore[]> {
const pipelines = await (
await this.getClient()
).pipeline.searchPipelines({
+4 -4
View File
@@ -1,5 +1,6 @@
import { defaultFS } from "@llamaindex/env";
import _ from "lodash";
import { filetypemime } from "magic-bytes.js";
import type { ImageType } from "../Node.js";
import { DEFAULT_SIMILARITY_TOP_K } from "../constants.js";
import { VectorStoreQueryMode } from "../storage/vectorStore/types.js";
@@ -199,13 +200,12 @@ export function getTopKMMREmbeddings(
}
async function blobToDataUrl(input: Blob) {
const { fileTypeFromBuffer } = await import("file-type");
const buffer = Buffer.from(await input.arrayBuffer());
const type = await fileTypeFromBuffer(buffer);
if (!type) {
const mimes = filetypemime(buffer);
if (mimes.length < 1) {
throw new Error("Unsupported image type");
}
return "data:" + type.mime + ";base64," + buffer.toString("base64");
return "data:" + mimes[0] + ";base64," + buffer.toString("base64");
}
export async function readImage(input: ImageType) {
@@ -64,10 +64,10 @@ export class DefaultContextGenerator
tags: ["final"],
};
}
const sourceNodesWithScore = await this.retriever.retrieve(
message,
const sourceNodesWithScore = await this.retriever.retrieve({
query: message,
parentEvent,
);
});
const nodes = await this.applyNodePostprocessors(
sourceNodesWithScore,
@@ -63,11 +63,11 @@ export class RetrieverQueryEngine
}
private async retrieve(query: string, parentEvent: Event) {
const nodes = await this.retriever.retrieve(
const nodes = await this.retriever.retrieve({
query,
parentEvent,
this.preFilters,
);
preFilters: this.preFilters,
});
return await this.applyNodePostprocessors(nodes, query);
}
+31
View File
@@ -0,0 +1,31 @@
export * from "./ChatHistory.js";
export * from "./GlobalsHelper.js";
export * from "./Node.js";
export * from "./OutputParser.js";
export * from "./Prompt.js";
export * from "./PromptHelper.js";
export * from "./QuestionGenerator.js";
export * from "./Response.js";
export * from "./Retriever.js";
export * from "./ServiceContext.js";
export * from "./TextSplitter.js";
export * from "./agent/index.js";
export * from "./callbacks/CallbackManager.js";
export * from "./cloud/index.js";
export * from "./constants.js";
export * from "./embeddings/index.js";
export * from "./engines/chat/index.js";
export * from "./engines/query/index.js";
export * from "./evaluation/index.js";
export * from "./extractors/index.js";
export * from "./indices/index.js";
export * from "./ingestion/index.js";
export * from "./llm/index.js";
export * from "./nodeParsers/index.js";
export * from "./objects/index.js";
export * from "./postprocessors/index.js";
export * from "./prompts/index.js";
export * from "./selectors/index.js";
export * from "./synthesizers/index.js";
export * from "./tools/index.js";
export * from "./types.js";
+1 -30
View File
@@ -1,32 +1,3 @@
export * from "./ChatHistory.js";
export * from "./GlobalsHelper.js";
export * from "./Node.js";
export * from "./OutputParser.js";
export * from "./Prompt.js";
export * from "./PromptHelper.js";
export * from "./QuestionGenerator.js";
export * from "./Response.js";
export * from "./Retriever.js";
export * from "./ServiceContext.js";
export * from "./TextSplitter.js";
export * from "./agent/index.js";
export * from "./callbacks/CallbackManager.js";
export * from "./cloud/index.js";
export * from "./constants.js";
export * from "./embeddings/index.js";
export * from "./engines/chat/index.js";
export * from "./engines/query/index.js";
export * from "./evaluation/index.js";
export * from "./extractors/index.js";
export * from "./indices/index.js";
export * from "./ingestion/index.js";
export * from "./llm/index.js";
export * from "./nodeParsers/index.js";
export * from "./objects/index.js";
export * from "./postprocessors/index.js";
export * from "./prompts/index.js";
export * from "./index.edge.js";
export * from "./readers/index.js";
export * from "./selectors/index.js";
export * from "./storage/index.js";
export * from "./synthesizers/index.js";
export * from "./tools/index.js";
+5 -4
View File
@@ -8,13 +8,14 @@ import {
defaultKeywordExtractPrompt,
defaultQueryKeywordExtractPrompt,
} from "../../Prompt.js";
import type { BaseRetriever } from "../../Retriever.js";
import type { BaseRetriever, RetrieveParams } from "../../Retriever.js";
import type { ServiceContext } from "../../ServiceContext.js";
import { serviceContextFromDefaults } from "../../ServiceContext.js";
import { RetrieverQueryEngine } from "../../engines/query/index.js";
import type { BaseNodePostprocessor } from "../../postprocessors/index.js";
import type { BaseDocumentStore, StorageContext } from "../../storage/index.js";
import { storageContextFromDefaults } from "../../storage/index.js";
import type { StorageContext } from "../../storage/StorageContext.js";
import { storageContextFromDefaults } from "../../storage/StorageContext.js";
import type { BaseDocumentStore } from "../../storage/docStore/types.js";
import type { BaseSynthesizer } from "../../synthesizers/index.js";
import type { BaseQueryEngine } from "../../types.js";
import type { BaseIndexInit } from "../BaseIndex.js";
@@ -79,7 +80,7 @@ abstract class BaseKeywordTableRetriever implements BaseRetriever {
abstract getKeywords(query: string): Promise<string[]>;
async retrieve(query: string): Promise<NodeWithScore[]> {
async retrieve({ query }: RetrieveParams): Promise<NodeWithScore[]> {
const keywords = await this.getKeywords(query);
const chunkIndicesCount: { [key: string]: number } = {};
const filteredKeywords = keywords.filter((keyword) =>
+12 -7
View File
@@ -3,18 +3,17 @@ import { globalsHelper } from "../../GlobalsHelper.js";
import type { BaseNode, Document, NodeWithScore } from "../../Node.js";
import type { ChoiceSelectPrompt } from "../../Prompt.js";
import { defaultChoiceSelectPrompt } from "../../Prompt.js";
import type { BaseRetriever } from "../../Retriever.js";
import type { BaseRetriever, RetrieveParams } from "../../Retriever.js";
import type { ServiceContext } from "../../ServiceContext.js";
import { serviceContextFromDefaults } from "../../ServiceContext.js";
import type { Event } from "../../callbacks/CallbackManager.js";
import { RetrieverQueryEngine } from "../../engines/query/index.js";
import type { BaseNodePostprocessor } from "../../postprocessors/index.js";
import type { StorageContext } from "../../storage/StorageContext.js";
import { storageContextFromDefaults } from "../../storage/StorageContext.js";
import type {
BaseDocumentStore,
RefDocInfo,
StorageContext,
} from "../../storage/index.js";
import { storageContextFromDefaults } from "../../storage/index.js";
} from "../../storage/docStore/types.js";
import type { BaseSynthesizer } from "../../synthesizers/index.js";
import {
CompactAndRefine,
@@ -281,7 +280,10 @@ export class SummaryIndexRetriever implements BaseRetriever {
this.index = index;
}
async retrieve(query: string, parentEvent?: Event): Promise<NodeWithScore[]> {
async retrieve({
query,
parentEvent,
}: RetrieveParams): Promise<NodeWithScore[]> {
const nodeIds = this.index.indexStruct.nodes;
const nodes = await this.index.docStore.getNodes(nodeIds);
const result = nodes.map((node) => ({
@@ -337,7 +339,10 @@ export class SummaryIndexLLMRetriever implements BaseRetriever {
this.serviceContext = serviceContext || index.serviceContext;
}
async retrieve(query: string, parentEvent?: Event): Promise<NodeWithScore[]> {
async retrieve({
query,
parentEvent,
}: RetrieveParams): Promise<NodeWithScore[]> {
const nodeIds = this.index.indexStruct.nodes;
const results: NodeWithScore[] = [];
+14 -11
View File
@@ -11,7 +11,7 @@ import {
ObjectType,
splitNodesByType,
} from "../../Node.js";
import type { BaseRetriever } from "../../Retriever.js";
import type { BaseRetriever, RetrieveParams } from "../../Retriever.js";
import type { ServiceContext } from "../../ServiceContext.js";
import { serviceContextFromDefaults } from "../../ServiceContext.js";
import type { Event } from "../../callbacks/CallbackManager.js";
@@ -24,15 +24,15 @@ import { ClipEmbedding } from "../../embeddings/index.js";
import { RetrieverQueryEngine } from "../../engines/query/RetrieverQueryEngine.js";
import { runTransformations } from "../../ingestion/index.js";
import type { BaseNodePostprocessor } from "../../postprocessors/types.js";
import type { StorageContext } from "../../storage/StorageContext.js";
import { storageContextFromDefaults } from "../../storage/StorageContext.js";
import type { BaseIndexStore } from "../../storage/indexStore/types.js";
import type {
BaseIndexStore,
MetadataFilters,
StorageContext,
VectorStore,
VectorStoreQuery,
VectorStoreQueryResult,
} from "../../storage/index.js";
} from "../../storage/vectorStore/types.js";
import { VectorStoreQueryMode } from "../../storage/vectorStore/types.js";
import type { BaseSynthesizer } from "../../synthesizers/types.js";
import type { BaseQueryEngine } from "../../types.js";
@@ -426,14 +426,17 @@ export class VectorIndexRetriever implements BaseRetriever {
this.imageSimilarityTopK = imageSimilarityTopK ?? DEFAULT_SIMILARITY_TOP_K;
}
async retrieve(
query: string,
parentEvent?: Event,
preFilters?: MetadataFilters,
): Promise<NodeWithScore[]> {
let nodesWithScores = await this.textRetrieve(query, preFilters);
async retrieve({
query,
parentEvent,
preFilters,
}: RetrieveParams): Promise<NodeWithScore[]> {
let nodesWithScores = await this.textRetrieve(
query,
preFilters as MetadataFilters,
);
nodesWithScores = nodesWithScores.concat(
await this.textToImageRetrieve(query, preFilters),
await this.textToImageRetrieve(query, preFilters as MetadataFilters),
);
this.sendEvent(query, nodesWithScores, parentEvent);
return nodesWithScores;
@@ -1,5 +1,6 @@
import type { BaseNode } from "../../Node.js";
import type { BaseDocumentStore, VectorStore } from "../../storage/index.js";
import type { BaseDocumentStore } from "../../storage/docStore/types.js";
import type { VectorStore } from "../../storage/vectorStore/types.js";
import { classify } from "./classify.js";
/**
+2
View File
@@ -620,6 +620,7 @@ export const ALL_AVAILABLE_ANTHROPIC_LEGACY_MODELS = {
export const ALL_AVAILABLE_V3_MODELS = {
"claude-3-opus": { contextWindow: 200000 },
"claude-3-sonnet": { contextWindow: 200000 },
"claude-3-haiku": { contextWindow: 200000 },
};
export const ALL_AVAILABLE_ANTHROPIC_MODELS = {
@@ -630,6 +631,7 @@ export const ALL_AVAILABLE_ANTHROPIC_MODELS = {
const AVAILABLE_ANTHROPIC_MODELS_WITHOUT_DATE: { [key: string]: string } = {
"claude-3-opus": "claude-3-opus-20240229",
"claude-3-sonnet": "claude-3-sonnet-20240229",
"claude-3-haiku": "claude-3-haiku-20240307",
} as { [key in keyof typeof ALL_AVAILABLE_ANTHROPIC_MODELS]: string };
/**
+64
View File
@@ -1,3 +1,4 @@
import { ChatPromptTemplate, PromptTemplate } from "llamaindex";
import type {
ChatMessage,
ChatResponse,
@@ -15,6 +16,69 @@ import { streamConverter } from "./utils.js";
export abstract class BaseLLM implements LLM {
abstract metadata: LLMMetadata;
predict(
params: LLMCompletionParamsStreaming,
): Promise<AsyncIterable<CompletionResponse>>;
predict(params: LLMCompletionParamsNonStreaming): Promise<CompletionResponse>;
async predict(
params: LLMCompletionParamsStreaming | LLMCompletionParamsNonStreaming,
): Promise<CompletionResponse | AsyncIterable<CompletionResponse>> {
const { prompt, parentEvent, stream } = params;
if (prompt instanceof ChatPromptTemplate) {
if (stream) {
const stream = await this.chat({
messages: prompt.formatMessages(),
parentEvent,
stream: true,
});
return streamConverter(stream, (chunk) => {
return {
text: chunk.delta,
};
});
}
const chatResponse = await this.chat({
messages: prompt.formatMessages(),
parentEvent,
});
return { text: chatResponse.message.content as string };
}
if (prompt instanceof PromptTemplate) {
if (stream) {
const stream = await this.complete({
prompt: prompt.format(),
parentEvent,
stream: true,
});
return stream;
}
return this.complete({
prompt: prompt.format(),
parentEvent,
});
}
if (stream) {
const stream = await this.complete({
prompt,
parentEvent,
stream: true,
});
return stream;
}
return this.complete({
prompt,
parentEvent,
});
}
complete(
params: LLMCompletionParamsStreaming,
): Promise<AsyncIterable<CompletionResponse>>;
+10
View File
@@ -64,6 +64,16 @@ export class Ollama extends BaseEmbedding implements LLM {
};
}
predict(
params: LLMCompletionParamsStreaming,
): Promise<AsyncIterable<CompletionResponse>>;
predict(params: LLMCompletionParamsNonStreaming): Promise<CompletionResponse>;
predict(
params: unknown,
): Promise<AsyncIterable<CompletionResponse>> | Promise<CompletionResponse> {
throw new Error("TODO: method not implemented for OLLAMA.");
}
chat(
params: LLMChatParamsStreaming,
): Promise<AsyncIterable<ChatResponseChunk>>;
+13 -1
View File
@@ -1,11 +1,23 @@
import type { Tokenizers } from "../GlobalsHelper.js";
import type { Event } from "../callbacks/CallbackManager.js";
import type { BasePromptTemplate } from "../prompts/types.js";
/**
* Unified language model interface
*/
export interface LLM {
metadata: LLMMetadata;
/**
* Predict the next completion from the LLM
* *
* @param params
*/
predict(
params: LLMCompletionParamsStreaming,
): Promise<AsyncIterable<CompletionResponse>>;
predict(params: LLMCompletionParamsNonStreaming): Promise<CompletionResponse>;
/**
* Get a chat response from the LLM
*
@@ -91,7 +103,7 @@ export interface LLMChatParamsNonStreaming extends LLMChatParamsBase {
}
export interface LLMCompletionParamsBase {
prompt: any;
prompt: BasePromptTemplate | string;
parentEvent?: Event;
}
+1 -1
View File
@@ -69,7 +69,7 @@ export class ObjectRetriever {
// Translating the retrieve method
async retrieve(strOrQueryBundle: QueryType): Promise<any> {
const nodes = await this.retriever.retrieve(strOrQueryBundle);
const nodes = await this.retriever.retrieve({ query: strOrQueryBundle });
const objs = nodes.map((n) => this._objectNodeMapping.fromNode(n.node));
return objs;
}
+2
View File
@@ -1 +1,3 @@
export * from "./Mixin.js";
export * from "./types.js";
export * from "./utils.js";
+120
View File
@@ -0,0 +1,120 @@
import type { ChatMessage } from "../llm/types.js";
import mustache from "mustache";
import { mapTemplateVars, messagesToPrompt } from "./utils.js";
export interface BasePromptTemplate<T extends object = {}> {
templateVars: Partial<T>;
partialFormat: (vars: Partial<T>) => PromptTemplate | ChatPromptTemplate;
format: (extraVars?: T) => string;
formatMessages: (extraVars?: T) => ChatMessage[];
}
export class PromptTemplate<T extends object = {}>
implements BasePromptTemplate<T>
{
template: (...args: any) => string;
templateVars: Partial<T> = {} as T;
constructor(template: (...args: any) => string, templateVars?: T) {
this.template = template;
this.templateVars = templateVars ?? ({} as T);
}
mapTemplateVars(): Array<string> {
return mapTemplateVars(this.template());
}
partialFormat(vars: Partial<T>): PromptTemplate {
const templateVars = {
...vars,
};
this.templateVars = templateVars;
return new PromptTemplate(this.template, templateVars);
}
format(extraVars?: Partial<T>): string {
const prompt = mustache.render(this.template(), {
...this.templateVars,
...extraVars,
});
return prompt;
}
formatMessages(extraVars?: Partial<T>): ChatMessage[] {
const prompt = this.format(extraVars);
return [
{
content: prompt,
role: "user",
},
];
}
}
export class ChatPromptTemplate<T extends object = {}>
implements BasePromptTemplate<T>
{
messageTemplates: (...args: any) => ChatMessage[];
templateVars: Partial<T> = {} as T;
constructor(
messageTemplates: (...args: any) => ChatMessage[],
templateVars?: T,
) {
this.messageTemplates = messageTemplates;
this.templateVars = templateVars ?? ({} as T);
}
mapTemplateVars(): Array<string> {
const allVars: Array<string> = [];
const messages = this.messageTemplates();
for (const message of messages) {
allVars.push(...mapTemplateVars(message.content));
}
return allVars;
}
partialFormat(vars: Partial<T>): ChatPromptTemplate {
const templateVars = {
...vars,
};
this.templateVars = templateVars;
return new ChatPromptTemplate(this.messageTemplates, templateVars);
}
format(extraVars?: T): string {
const messages = this.formatMessages(extraVars);
return messagesToPrompt(messages);
}
formatMessages(extraVars?: Partial<T>): ChatMessage[] {
const messages: ChatMessage[] = [];
for (const message of this.messageTemplates()) {
const formattedMessage = mustache.render(message.content, {
...this.templateVars,
...extraVars,
});
messages.push({
content: formattedMessage,
role: message.role,
});
}
return messages;
}
}
+44
View File
@@ -0,0 +1,44 @@
import type { ChatMessage } from "../llm/types.js";
export const messagesToPrompt = (messages: ChatMessage[]): string => {
const stringMessages = [];
for (const message of messages) {
const role = message.role;
const content = message.content;
let stringMessage = `${role}: ${content}`;
const additionalKwargs = message.additionalKwargs;
if (additionalKwargs) {
stringMessage += `\n${additionalKwargs}`;
}
stringMessages.push(stringMessage);
}
stringMessages.push(`assistant: `);
return stringMessages.join("\n");
};
export const mapTemplateVars = (template: string): Array<string> => {
const placeholders: Array<string> = [];
let index = 0;
while (index < template.length) {
const startIndex = template.indexOf("{{", index);
const endIndex = template.indexOf("}}", startIndex) + 2; // +2 to include the '}}'
if (startIndex === -1 || endIndex === 1) {
// No more placeholders
break;
}
const placeholderName = template
.substring(startIndex + 2, endIndex - 2)
.trim(); // Extract name
placeholders.push(placeholderName);
index = endIndex; // Move index to end of current placeholder
}
return placeholders;
};
@@ -1,4 +1,5 @@
import { defaultFS, getEnv, type GenericFileSystem } from "@llamaindex/env";
import { filetypemime } from "magic-bytes.js";
import { Document } from "../Node.js";
import type { FileReader } from "./type.js";
@@ -109,11 +110,10 @@ export class LlamaParseReader implements FileReader {
}
private async getMimeType(data: Buffer): Promise<string> {
const { fileTypeFromBuffer } = await import("file-type");
const type = await fileTypeFromBuffer(data);
if (type?.mime !== "application/pdf") {
const mimes = filetypemime(data);
if (!mimes.includes("application/pdf")) {
throw new Error("Currently, only PDF files are supported.");
}
return type.mime;
return "application/pdf";
}
}
@@ -0,0 +1,126 @@
import type { CompleteFileSystem } from "@llamaindex/env";
import { defaultFS, path } from "@llamaindex/env";
import { Document } from "../Node.js";
import { walk } from "../storage/FileSystem.js";
import { TextFileReader } from "./TextFileReader.js";
import type { BaseReader } from "./type.js";
type ReaderCallback = (
category: "file" | "directory",
name: string,
status: ReaderStatus,
message?: string,
) => boolean;
enum ReaderStatus {
STARTED = 0,
COMPLETE,
ERROR,
}
export type SimpleDirectoryReaderLoadDataParams = {
directoryPath: string;
fs?: CompleteFileSystem;
defaultReader?: BaseReader | null;
fileExtToReader?: Record<string, BaseReader>;
};
/**
* Read all the documents in a directory.
* By default, supports the list of file types
* in the FILE_EXT_TO_READER map.
*/
export class SimpleDirectoryReader implements BaseReader {
constructor(private observer?: ReaderCallback) {}
async loadData(
params: SimpleDirectoryReaderLoadDataParams,
): Promise<Document[]>;
async loadData(directoryPath: string): Promise<Document[]>;
async loadData(
params: SimpleDirectoryReaderLoadDataParams | string,
): Promise<Document[]> {
if (typeof params === "string") {
params = { directoryPath: params };
}
const {
directoryPath,
fs = defaultFS,
defaultReader = new TextFileReader(),
fileExtToReader,
} = params;
// Observer can decide to skip the directory
if (
!this.doObserverCheck("directory", directoryPath, ReaderStatus.STARTED)
) {
return [];
}
const docs: Document[] = [];
for await (const filePath of walk(fs, directoryPath)) {
try {
const fileExt = path.extname(filePath).slice(1).toLowerCase();
// Observer can decide to skip each file
if (!this.doObserverCheck("file", filePath, ReaderStatus.STARTED)) {
// Skip this file
continue;
}
let reader: BaseReader;
if (fileExtToReader && fileExt in fileExtToReader) {
reader = fileExtToReader[fileExt];
} else if (defaultReader != null) {
reader = defaultReader;
} else {
const msg = `No reader for file extension of ${filePath}`;
console.warn(msg);
// In an error condition, observer's false cancels the whole process.
if (
!this.doObserverCheck("file", filePath, ReaderStatus.ERROR, msg)
) {
return [];
}
continue;
}
const fileDocs = await reader.loadData(filePath, fs);
// Observer can still cancel addition of the resulting docs from this file
if (this.doObserverCheck("file", filePath, ReaderStatus.COMPLETE)) {
docs.push(...fileDocs);
}
} catch (e) {
const msg = `Error reading file ${filePath}: ${e}`;
console.error(msg);
// In an error condition, observer's false cancels the whole process.
if (!this.doObserverCheck("file", filePath, ReaderStatus.ERROR, msg)) {
return [];
}
}
}
// After successful import of all files, directory completion
// is only a notification for observer, cannot be cancelled.
this.doObserverCheck("directory", directoryPath, ReaderStatus.COMPLETE);
return docs;
}
private doObserverCheck(
category: "file" | "directory",
name: string,
status: ReaderStatus,
message?: string,
): boolean {
if (this.observer) {
return this.observer(category, name, status, message);
}
return true;
}
}
@@ -1,40 +1,17 @@
import type { CompleteFileSystem } from "@llamaindex/env";
import { defaultFS, path } from "@llamaindex/env";
import { Document } from "../Node.js";
import { walk } from "../storage/FileSystem.js";
import { PapaCSVReader } from "./CSVReader.js";
import { DocxReader } from "./DocxReader.js";
import { HTMLReader } from "./HTMLReader.js";
import { ImageReader } from "./ImageReader.js";
import { MarkdownReader } from "./MarkdownReader.js";
import { PDFReader } from "./PDFReader.js";
import {
SimpleDirectoryReader as EdgeSimpleDirectoryReader,
type SimpleDirectoryReaderLoadDataParams,
} from "./SimpleDirectoryReader.edge.js";
import { TextFileReader } from "./TextFileReader.js";
import type { BaseReader } from "./type.js";
type ReaderCallback = (
category: "file" | "directory",
name: string,
status: ReaderStatus,
message?: string,
) => boolean;
enum ReaderStatus {
STARTED = 0,
COMPLETE,
ERROR,
}
/**
* Read a .txt file
*/
export class TextFileReader implements BaseReader {
async loadData(
file: string,
fs: CompleteFileSystem = defaultFS,
): Promise<Document[]> {
const dataBuffer = await fs.readFile(file);
return [new Document({ text: dataBuffer, id_: file })];
}
}
export const FILE_EXT_TO_READER: Record<string, BaseReader> = {
txt: new TextFileReader(),
pdf: new PDFReader(),
@@ -49,21 +26,12 @@ export const FILE_EXT_TO_READER: Record<string, BaseReader> = {
gif: new ImageReader(),
};
export type SimpleDirectoryReaderLoadDataParams = {
directoryPath: string;
fs?: CompleteFileSystem;
defaultReader?: BaseReader | null;
fileExtToReader?: Record<string, BaseReader>;
};
/**
* Read all the documents in a directory.
* By default, supports the list of file types
* in the FILE_EXT_TO_READER map.
*/
export class SimpleDirectoryReader implements BaseReader {
constructor(private observer?: ReaderCallback) {}
export class SimpleDirectoryReader extends EdgeSimpleDirectoryReader {
async loadData(
params: SimpleDirectoryReaderLoadDataParams,
): Promise<Document[]>;
@@ -74,85 +42,7 @@ export class SimpleDirectoryReader implements BaseReader {
if (typeof params === "string") {
params = { directoryPath: params };
}
const {
directoryPath,
fs = defaultFS,
defaultReader = new TextFileReader(),
fileExtToReader = FILE_EXT_TO_READER,
} = params;
// Observer can decide to skip the directory
if (
!this.doObserverCheck("directory", directoryPath, ReaderStatus.STARTED)
) {
return [];
}
const docs: Document[] = [];
for await (const filePath of walk(fs, directoryPath)) {
try {
const fileExt = path.extname(filePath).slice(1).toLowerCase();
// Observer can decide to skip each file
if (!this.doObserverCheck("file", filePath, ReaderStatus.STARTED)) {
// Skip this file
continue;
}
let reader: BaseReader;
if (fileExt in fileExtToReader) {
reader = fileExtToReader[fileExt];
} else if (defaultReader != null) {
reader = defaultReader;
} else {
const msg = `No reader for file extension of ${filePath}`;
console.warn(msg);
// In an error condition, observer's false cancels the whole process.
if (
!this.doObserverCheck("file", filePath, ReaderStatus.ERROR, msg)
) {
return [];
}
continue;
}
const fileDocs = await reader.loadData(filePath, fs);
// Observer can still cancel addition of the resulting docs from this file
if (this.doObserverCheck("file", filePath, ReaderStatus.COMPLETE)) {
docs.push(...fileDocs);
}
} catch (e) {
const msg = `Error reading file ${filePath}: ${e}`;
console.error(msg);
// In an error condition, observer's false cancels the whole process.
if (!this.doObserverCheck("file", filePath, ReaderStatus.ERROR, msg)) {
return [];
}
}
}
// After successful import of all files, directory completion
// is only a notification for observer, cannot be cancelled.
this.doObserverCheck("directory", directoryPath, ReaderStatus.COMPLETE);
return docs;
}
private doObserverCheck(
category: "file" | "directory",
name: string,
status: ReaderStatus,
message?: string,
): boolean {
if (this.observer) {
return this.observer(category, name, status, message);
}
return true;
params.fileExtToReader = params.fileExtToReader ?? FILE_EXT_TO_READER;
return super.loadData(params);
}
}
@@ -0,0 +1,18 @@
import type { CompleteFileSystem } from "@llamaindex/env";
import { defaultFS } from "@llamaindex/env";
import { Document } from "../Node.js";
import type { BaseReader } from "./type.js";
/**
* Read a .txt file
*/
export class TextFileReader implements BaseReader {
async loadData(
file: string,
fs: CompleteFileSystem = defaultFS,
): Promise<Document[]> {
const dataBuffer = await fs.readFile(file);
return [new Document({ text: dataBuffer, id_: file })];
}
}
+1
View File
@@ -9,4 +9,5 @@ export * from "./NotionReader.js";
export * from "./PDFReader.js";
export * from "./SimpleDirectoryReader.js";
export * from "./SimpleMongoReader.js";
export * from "./TextFileReader.js";
export * from "./type.js";
+35 -20
View File
@@ -9,10 +9,13 @@ import type {
} from "../types.js";
import type { SelectorResult } from "./base.js";
import { BaseSelector } from "./base.js";
import type { MultiSelectPrompt, SingleSelectPrompt } from "./prompts.js";
import type {
MultiSelectPromptTemplate,
SingleSelectPromptTemplate,
} from "./prompts.js";
import {
defaultMultiSelectPrompt,
defaultSingleSelectPrompt,
defaultMultiSelectPromptTemplate,
defaultSingleSelectPromptTemplate,
} from "./prompts.js";
function buildChoicesText(choices: ToolMetadataOnlyDescription[]): string {
@@ -46,29 +49,29 @@ type LLMPredictorType = LLM;
*/
export class LLMMultiSelector extends BaseSelector {
llm: LLMPredictorType;
prompt: MultiSelectPrompt;
prompt: MultiSelectPromptTemplate;
maxOutputs: number;
outputParser: BaseOutputParser<StructuredOutput<Answer[]>> | null;
constructor(init: {
llm: LLMPredictorType;
prompt?: MultiSelectPrompt;
prompt?: MultiSelectPromptTemplate;
maxOutputs?: number;
outputParser?: BaseOutputParser<StructuredOutput<Answer[]>>;
}) {
super();
this.llm = init.llm;
this.prompt = init.prompt ?? defaultMultiSelectPrompt;
this.prompt = init.prompt ?? defaultMultiSelectPromptTemplate;
this.maxOutputs = init.maxOutputs ?? 10;
this.outputParser = init.outputParser ?? new SelectionOutputParser();
}
_getPrompts(): Record<string, MultiSelectPrompt> {
_getPrompts(): Record<string, MultiSelectPromptTemplate> {
return { prompt: this.prompt };
}
_updatePrompts(prompts: Record<string, MultiSelectPrompt>): void {
_updatePrompts(prompts: Record<string, MultiSelectPromptTemplate>): void {
if ("prompt" in prompts) {
this.prompt = prompts.prompt;
}
@@ -85,15 +88,19 @@ export class LLMMultiSelector extends BaseSelector {
): Promise<SelectorResult> {
const choicesText = buildChoicesText(choices);
const prompt = this.prompt(
choicesText.length,
choicesText,
query.queryStr,
this.maxOutputs,
);
const prompt = this.prompt.format({
numChoices: choicesText.length,
contextList: choicesText,
queryStr: query.queryStr,
maxOutputs: this.maxOutputs,
});
const formattedPrompt = this.outputParser?.format(prompt);
if (!formattedPrompt) {
throw new Error("Formatted prompt is undefined");
}
const prediction = await this.llm.complete({
prompt: formattedPrompt,
});
@@ -117,25 +124,25 @@ export class LLMMultiSelector extends BaseSelector {
*/
export class LLMSingleSelector extends BaseSelector {
llm: LLMPredictorType;
prompt: SingleSelectPrompt;
prompt: SingleSelectPromptTemplate;
outputParser: BaseOutputParser<StructuredOutput<Answer[]>> | null;
constructor(init: {
llm: LLMPredictorType;
prompt?: SingleSelectPrompt;
prompt?: SingleSelectPromptTemplate;
outputParser?: BaseOutputParser<StructuredOutput<Answer[]>>;
}) {
super();
this.llm = init.llm;
this.prompt = init.prompt ?? defaultSingleSelectPrompt;
this.prompt = init.prompt ?? defaultSingleSelectPromptTemplate;
this.outputParser = init.outputParser ?? new SelectionOutputParser();
}
_getPrompts(): Record<string, SingleSelectPrompt> {
_getPrompts(): Record<string, SingleSelectPromptTemplate> {
return { prompt: this.prompt };
}
_updatePrompts(prompts: Record<string, SingleSelectPrompt>): void {
_updatePrompts(prompts: Record<string, SingleSelectPromptTemplate>): void {
if ("prompt" in prompts) {
this.prompt = prompts.prompt;
}
@@ -152,10 +159,18 @@ export class LLMSingleSelector extends BaseSelector {
): Promise<SelectorResult> {
const choicesText = buildChoicesText(choices);
const prompt = this.prompt(choicesText.length, choicesText, query.queryStr);
const prompt = this.prompt.format({
numChoices: choicesText.length,
contextList: choicesText,
queryStr: query.queryStr,
});
const formattedPrompt = this.outputParser?.format(prompt);
if (!formattedPrompt) {
throw new Error("Formatted prompt is undefined");
}
const prediction = await this.llm.complete({
prompt: formattedPrompt,
});
+24 -8
View File
@@ -1,16 +1,23 @@
export const defaultSingleSelectPrompt = (
numChoices: number,
contextList: string,
queryStr: string,
): string => {
return `Some choices are given below. It is provided in a numbered list (1 to ${numChoices}), where each item in the list corresponds to a summary.
import { PromptTemplate } from "../index.js";
export const defaultSingleSelectPrompt = (): string => {
return `Some choices are given below. It is provided in a numbered list (1 to {{numChoices}), where each item in the list corresponds to a summary.
---------------------
${contextList}
{{contextList}}
---------------------
Using only the choices above and not prior knowledge, return the choice that is most relevant to the question: '${queryStr}'
Using only the choices above and not prior knowledge, return the choice that is most relevant to the question: '{{queryStr}}'
`;
};
export const defaultSingleSelectPromptTemplate = new PromptTemplate<{
numChoices: number;
contextList: string;
queryStr: string;
}>(defaultSingleSelectPrompt);
export type SingleSelectPromptTemplate =
typeof defaultSingleSelectPromptTemplate;
export type SingleSelectPrompt = typeof defaultSingleSelectPrompt;
export const defaultMultiSelectPrompt = (
@@ -27,4 +34,13 @@ Using only the choices above and not prior knowledge, return the top choices (no
`;
};
export const defaultMultiSelectPromptTemplate = new PromptTemplate<{
numChoices: number;
contextList: string;
queryStr: string;
maxOutputs: number;
}>(defaultMultiSelectPrompt);
export type MultiSelectPromptTemplate = typeof defaultMultiSelectPromptTemplate;
export type MultiSelectPrompt = typeof defaultMultiSelectPrompt;
+1
View File
@@ -11,6 +11,7 @@ export { SimpleKVStore } from "./kvStore/SimpleKVStore.js";
export * from "./kvStore/types.js";
export { AstraDBVectorStore } from "./vectorStore/AstraDBVectorStore.js";
export { ChromaVectorStore } from "./vectorStore/ChromaVectorStore.js";
export { MilvusVectorStore } from "./vectorStore/MilvusVectorStore.js";
export { MongoDBAtlasVectorSearch } from "./vectorStore/MongoDBAtlasVectorStore.js";
export { PGVectorStore } from "./vectorStore/PGVectorStore.js";
export { PineconeVectorStore } from "./vectorStore/PineconeVectorStore.js";
@@ -0,0 +1,214 @@
/* eslint-disable turbo/no-undeclared-env-vars */
import type { ChannelOptions } from "@grpc/grpc-js";
import {
DataType,
MilvusClient,
type ClientConfig,
type DeleteReq,
type RowData,
} from "@zilliz/milvus2-sdk-node";
import { BaseNode, MetadataMode, type Metadata } from "../../Node.js";
import type {
VectorStore,
VectorStoreQuery,
VectorStoreQueryResult,
} from "./types.js";
import { metadataDictToNode, nodeToMetadata } from "./utils.js";
export class MilvusVectorStore implements VectorStore {
public storesText: boolean = true;
public isEmbeddingQuery?: boolean;
private flatMetadata: boolean = true;
private milvusClient: MilvusClient;
private collectionInitialized = false;
private collectionName: string;
private idKey: string;
private contentKey: string;
private metadataKey: string;
private embeddingKey: string;
constructor(
init?: Partial<{ milvusClient: MilvusClient }> & {
params?: {
configOrAddress: ClientConfig | string;
ssl?: boolean;
username?: string;
password?: string;
channelOptions?: ChannelOptions;
};
collection?: string;
idKey?: string;
contentKey?: string;
metadataKey?: string;
embeddingKey?: string;
},
) {
if (init?.milvusClient) {
this.milvusClient = init.milvusClient;
} else {
const configOrAddress =
init?.params?.configOrAddress ?? process.env.MILVUS_ADDRESS;
const ssl = init?.params?.ssl ?? process.env.MILVUS_SSL === "true";
const username = init?.params?.username ?? process.env.MILVUS_USERNAME;
const password = init?.params?.password ?? process.env.MILVUS_PASSWORD;
if (!configOrAddress) {
throw new Error("Must specify MILVUS_ADDRESS via env variable.");
}
this.milvusClient = new MilvusClient(
configOrAddress,
ssl,
username,
password,
init?.params?.channelOptions,
);
}
this.collectionName = init?.collection ?? "llamacollection";
this.idKey = init?.idKey ?? "id";
this.contentKey = init?.contentKey ?? "content";
this.metadataKey = init?.metadataKey ?? "metadata";
this.embeddingKey = init?.embeddingKey ?? "embedding";
}
public client(): MilvusClient {
return this.milvusClient;
}
private async createCollection() {
await this.milvusClient.createCollection({
collection_name: this.collectionName,
fields: [
{
name: this.idKey,
data_type: DataType.VarChar,
is_primary_key: true,
max_length: 200,
},
{
name: this.embeddingKey,
data_type: DataType.FloatVector,
dim: 1536,
},
{
name: this.contentKey,
data_type: DataType.VarChar,
max_length: 9000,
},
{
name: this.metadataKey,
data_type: DataType.JSON,
},
],
});
await this.milvusClient.createIndex({
collection_name: this.collectionName,
field_name: this.embeddingKey,
});
}
private async ensureCollection(): Promise<void> {
if (!this.collectionInitialized) {
await this.milvusClient.connectPromise;
// Check collection exists
const isCollectionExist = await this.milvusClient.hasCollection({
collection_name: this.collectionName,
});
if (!isCollectionExist.value) {
await this.createCollection();
}
await this.milvusClient.loadCollectionSync({
collection_name: this.collectionName,
});
this.collectionInitialized = true;
}
}
public async add(nodes: BaseNode<Metadata>[]): Promise<string[]> {
await this.ensureCollection();
const result = await this.milvusClient.insert({
collection_name: this.collectionName,
data: nodes.map((node) => {
const metadata = nodeToMetadata(
node,
true,
this.contentKey,
this.flatMetadata,
);
const entry: RowData = {
[this.idKey]: node.id_,
[this.embeddingKey]: node.getEmbedding(),
[this.contentKey]: node.getContent(MetadataMode.NONE),
[this.metadataKey]: metadata,
};
return entry;
}),
});
if (!result.IDs) {
return [];
}
if ("int_id" in result.IDs) {
return result.IDs.int_id.data.map((i) => String(i));
}
return result.IDs.str_id.data.map((s) => String(s));
}
public async delete(
refDocId: string,
deleteOptions?: Omit<DeleteReq, "ids">,
): Promise<void> {
this.ensureCollection();
await this.milvusClient.delete({
ids: [refDocId],
collection_name: this.collectionName,
...deleteOptions,
});
}
public async query(
query: VectorStoreQuery,
_options?: any,
): Promise<VectorStoreQueryResult> {
await this.ensureCollection();
const found = await this.milvusClient.search({
collection_name: this.collectionName,
limit: query.similarityTopK,
vector: query.queryEmbedding,
});
const nodes: BaseNode<Metadata>[] = [];
const similarities: number[] = [];
const ids: string[] = [];
found.results.forEach((result) => {
const node = metadataDictToNode(result.metadata);
node.setContent(result.content);
nodes.push(node);
similarities.push(result.score);
ids.push(String(result.id));
});
return {
nodes,
similarities,
ids,
};
}
public async persist() {
// no need to do anything
}
}

Some files were not shown because too many files have changed in this diff Show More