mirror of
https://github.com/run-llama/LlamaIndexTS.git
synced 2026-07-18 16:44:33 -04:00
Compare commits
40 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e658cfe0fb | |||
| 8bad594b05 | |||
| 8ed8581f5b | |||
| ee2a3b83c9 | |||
| 8c3a2d9062 | |||
| 6cb75b54a0 | |||
| 53edfe93cf | |||
| b856deae43 | |||
| 259c842259 | |||
| ffb195ea7a | |||
| b4677534d1 | |||
| f967b82467 | |||
| c81946930e | |||
| 1008b775a4 | |||
| 41210dfc51 | |||
| 67b7272249 | |||
| 964e045903 | |||
| 137cf67f40 | |||
| 309a526e3c | |||
| e1ac089d39 | |||
| dd95927498 | |||
| 4f72feae91 | |||
| 3cd8f9f597 | |||
| d2e8d0c62a | |||
| fafbd8c9c7 | |||
| a40c91b054 | |||
| 98894055c6 | |||
| 4589a84643 | |||
| e6b7f52d3e | |||
| b169db617a | |||
| 89a49f4f4f | |||
| 58490715fe | |||
| 4c2283c4e5 | |||
| a059070dec | |||
| 20dfeb4cfa | |||
| aefc3266c1 | |||
| fdf48dd459 | |||
| 66525346a2 | |||
| c9b2ec4a2b | |||
| bf583a7266 |
@@ -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",
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
---
|
||||
"llamaindex": patch
|
||||
"@llamaindex/core-test": patch
|
||||
---
|
||||
|
||||
- Add missing exports:
|
||||
- `IndexStructType`,
|
||||
- `IndexDict`,
|
||||
- `jsonToIndexStruct`,
|
||||
- `IndexList`,
|
||||
- `IndexStruct`
|
||||
- Fix `IndexDict.toJson()` method
|
||||
@@ -1,5 +0,0 @@
|
||||
---
|
||||
"llamaindex": patch
|
||||
---
|
||||
|
||||
Add streaming to agents
|
||||
@@ -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
|
||||
@@ -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 }}
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
+7
-3
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
+1
-1
@@ -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)
|
||||
|
||||
+1
-1
@@ -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 справка)
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+1
-1
@@ -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í)
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
|
||||
retriever.similarityTopK = 3;
|
||||
|
||||
// Ανάκτηση κόμβων!
|
||||
const nodesWithScore = await retriever.retrieve("συμβολοσειρά ερωτήματος");
|
||||
const nodesWithScore = await retriever.retrieve({ query: "συμβολοσειρά ερωτήματος" });
|
||||
```
|
||||
|
||||
## Αναφορά API
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
|
||||
retriever.similarityTopK = 3;
|
||||
|
||||
// بازیابی گره ها!
|
||||
const nodesWithScore = await retriever.retrieve("رشته پرس و جو");
|
||||
const nodesWithScore = await retriever.retrieve({ query: "رشته پرس و جو" });
|
||||
```
|
||||
|
||||
## مرجع API
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
|
||||
retriever.similarityTopK = 3;
|
||||
|
||||
// אחזור צמתים!
|
||||
const nodesWithScore = await retriever.retrieve("מחרוזת שאילתה");
|
||||
const nodesWithScore = await retriever.retrieve({ query: "מחרוזת שאילתה" });
|
||||
```
|
||||
|
||||
## מדריך לממשק API
|
||||
|
||||
+1
-1
@@ -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)
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
|
||||
retriever.similarityTopK = 3;
|
||||
|
||||
// ノードを取得します!
|
||||
const nodesWithScore = await retriever.retrieve("クエリ文字列");
|
||||
const nodesWithScore = await retriever.retrieve({ query: "クエリ文字列" });
|
||||
```
|
||||
|
||||
## API リファレンス
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
|
||||
retriever.similarityTopK = 3;
|
||||
|
||||
// 노드를 가져옵니다!
|
||||
const nodesWithScore = await retriever.retrieve("쿼리 문자열");
|
||||
const nodesWithScore = await retriever.retrieve({ query: "쿼리 문자열" });
|
||||
```
|
||||
|
||||
## API 참조
|
||||
|
||||
+1
-1
@@ -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)
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
|
||||
retriever.similarityTopK = 3;
|
||||
|
||||
// Получение узлов!
|
||||
const nodesWithScore = await retriever.retrieve("строка запроса");
|
||||
const nodesWithScore = await retriever.retrieve({ query: "строка запроса" });
|
||||
```
|
||||
|
||||
## Справочник по API
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+1
-1
@@ -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)
|
||||
|
||||
+1
-1
@@ -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ı
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
|
||||
retriever.similarityTopK = 3;
|
||||
|
||||
// Отримати вузли!
|
||||
const nodesWithScore = await retriever.retrieve("рядок запиту");
|
||||
const nodesWithScore = await retriever.retrieve({ query: "рядок запиту" });
|
||||
```
|
||||
|
||||
## Довідник API
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+1
-1
@@ -11,7 +11,7 @@ const retriever = vector_index.asRetriever();
|
||||
retriever.similarityTopK = 3;
|
||||
|
||||
// 获取节点!
|
||||
const nodesWithScore = await retriever.retrieve("查询字符串");
|
||||
const nodesWithScore = await retriever.retrieve({ query: "查询字符串" });
|
||||
```
|
||||
|
||||
## API 参考
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
|
||||
retriever.similarityTopK = 3;
|
||||
|
||||
// 提取節點!
|
||||
const nodesWithScore = await retriever.retrieve("查詢字串");
|
||||
const nodesWithScore = await retriever.retrieve({ query: "查詢字串" });
|
||||
```
|
||||
|
||||
## API 參考
|
||||
|
||||
@@ -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
|
||||
@@ -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);
|
||||
})();
|
||||
@@ -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`
|
||||
|
||||
@@ -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`
|
||||
@@ -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();
|
||||
@@ -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();
|
||||
@@ -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) {
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -37,10 +37,6 @@ async function main() {
|
||||
responseSynthesizer,
|
||||
});
|
||||
|
||||
console.log({
|
||||
promptsToUse: queryEngine.getPrompts(),
|
||||
});
|
||||
|
||||
queryEngine.updatePrompts({
|
||||
"responseSynthesizer:summaryTemplate": treeSummarizePrompt,
|
||||
});
|
||||
|
||||
@@ -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`.
|
||||
@@ -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();
|
||||
@@ -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
@@ -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",
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
@@ -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;
|
||||
|
||||
/*
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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,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,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,
|
||||
|
||||
@@ -3,6 +3,7 @@ import type {
|
||||
ChatEngineAgentParams,
|
||||
StreamingAgentChatResponse,
|
||||
} from "../engines/chat/index.js";
|
||||
|
||||
import type { QueryEngineParamsNonStreaming } from "../types.js";
|
||||
|
||||
export interface AgentWorker {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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({
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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,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";
|
||||
|
||||
@@ -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) =>
|
||||
|
||||
@@ -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[] = [];
|
||||
|
||||
|
||||
@@ -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";
|
||||
|
||||
/**
|
||||
|
||||
@@ -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 };
|
||||
|
||||
/**
|
||||
|
||||
@@ -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>>;
|
||||
|
||||
@@ -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>>;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -1 +1,3 @@
|
||||
export * from "./Mixin.js";
|
||||
export * from "./types.js";
|
||||
export * from "./utils.js";
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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 })];
|
||||
}
|
||||
}
|
||||
@@ -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";
|
||||
|
||||
@@ -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,
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
Reference in New Issue
Block a user