mirror of
https://github.com/run-llama/LlamaIndexTS.git
synced 2026-07-16 07:14:29 -04:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0b2d43b532 | |||
| 02feb54070 | |||
| 9bf778eb36 | |||
| 746408b992 | |||
| ea64162b89 | |||
| 0094a2e420 | |||
| ef1e8b4121 | |||
| a20704bbf8 | |||
| 247a3d0b5f |
@@ -1,5 +0,0 @@
|
||||
---
|
||||
"llamaindex": patch
|
||||
---
|
||||
|
||||
feat: experimental package + json query engine
|
||||
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"create-llama": patch
|
||||
---
|
||||
|
||||
Add LlamaParse option when selecting a pdf file or a folder
|
||||
@@ -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,5 +0,0 @@
|
||||
---
|
||||
"llamaindex": minor
|
||||
---
|
||||
|
||||
Use parameter object for retrieve function of Retriever (to align usage with query function of QueryEngine)
|
||||
@@ -11,13 +11,5 @@ module.exports = {
|
||||
"max-params": ["error", 4],
|
||||
"prefer-const": "error",
|
||||
},
|
||||
overrides: [
|
||||
{
|
||||
files: ["examples/**/*.ts"],
|
||||
rules: {
|
||||
"turbo/no-undeclared-env-vars": "off",
|
||||
},
|
||||
},
|
||||
],
|
||||
ignorePatterns: ["dist/", "lib/"],
|
||||
};
|
||||
|
||||
@@ -1,28 +0,0 @@
|
||||
name: Publish
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
|
||||
jobs:
|
||||
publish:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
id-token: write
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
|
||||
- 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 }}
|
||||
@@ -45,7 +45,6 @@ playwright-report/
|
||||
blob-report/
|
||||
playwright/.cache/
|
||||
.tsbuildinfo
|
||||
packages/create-llama/e2e/cache
|
||||
|
||||
# intellij
|
||||
**/.idea
|
||||
|
||||
@@ -4,4 +4,3 @@ pnpm-lock.yaml
|
||||
lib/
|
||||
dist/
|
||||
.docusaurus/
|
||||
packages/create-llama/e2e/cache/
|
||||
@@ -23,15 +23,3 @@ const results = await queryEngine.query({
|
||||
query,
|
||||
});
|
||||
```
|
||||
|
||||
Per default, `HuggingFaceEmbedding` is using the `Xenova/all-MiniLM-L6-v2` model. You can change the model by passing the `modelType` parameter to the constructor.
|
||||
If you're not using a quantized model, set the `quantized` parameter to `false`.
|
||||
|
||||
For example, to use the not quantized `BAAI/bge-small-en-v1.5` model, you can use the following code:
|
||||
|
||||
```
|
||||
const embedModel = new HuggingFaceEmbedding({
|
||||
modelType: "BAAI/bge-small-en-v1.5",
|
||||
quantized: false,
|
||||
});
|
||||
```
|
||||
|
||||
@@ -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 Transformation 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 Transformatio class has both a `transform` definition responsible for transforming the nodes
|
||||
|
||||
Currently, the following components are Transformation objects:
|
||||
|
||||
|
||||
@@ -100,7 +100,7 @@ const response = await queryEngine.query("<user_query>");
|
||||
```ts
|
||||
import { SimilarityPostprocessor } from "llamaindex";
|
||||
|
||||
nodes = await index.asRetriever().retrieve({ query: "test query str" });
|
||||
nodes = await index.asRetriever().retrieve("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: "query string" });
|
||||
const nodesWithScore = await retriever.retrieve("query string");
|
||||
```
|
||||
|
||||
## API Reference
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
|
||||
retriever.similarityTopK = 3;
|
||||
|
||||
// جلب العقد!
|
||||
const nodesWithScore = await retriever.retrieve({ query: "سلسلة الاستعلام" });
|
||||
const nodesWithScore = await retriever.retrieve("سلسلة الاستعلام");
|
||||
```
|
||||
|
||||
## مرجع الواجهة البرمجية (API Reference)
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
|
||||
retriever.similarityTopK = 3;
|
||||
|
||||
// Извличане на върхове!
|
||||
const nodesWithScore = await retriever.retrieve({ query: "query string" });
|
||||
const nodesWithScore = await retriever.retrieve("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({ query: "cadena de consulta" });
|
||||
const nodesAmbPuntuació = await recuperador.retrieve("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({ query: "dotazovací řetězec" });
|
||||
const nodesWithScore = await retriever.retrieve("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({ query: "forespørgselsstreng" });
|
||||
const nodesWithScore = await retriever.retrieve("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({ query: "Abfragezeichenfolge" });
|
||||
const nodesWithScore = await retriever.retrieve("Abfragezeichenfolge");
|
||||
```
|
||||
|
||||
## API-Referenz
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
|
||||
retriever.similarityTopK = 3;
|
||||
|
||||
// Ανάκτηση κόμβων!
|
||||
const nodesWithScore = await retriever.retrieve({ query: "συμβολοσειρά ερωτήματος" });
|
||||
const nodesWithScore = await retriever.retrieve("συμβολοσειρά ερωτήματος");
|
||||
```
|
||||
|
||||
## Αναφορά API
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ const recuperador = vector_index.asRetriever();
|
||||
recuperador.similarityTopK = 3;
|
||||
|
||||
// ¡Obtener nodos!
|
||||
const nodosConPuntuación = await recuperador.retrieve({ query: "cadena de consulta" });
|
||||
const nodosConPuntuación = await recuperador.retrieve("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({ query: "päringu string" });
|
||||
const nodesWithScore = await retriever.retrieve("päringu string");
|
||||
```
|
||||
|
||||
## API viide
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
|
||||
retriever.similarityTopK = 3;
|
||||
|
||||
// بازیابی گره ها!
|
||||
const nodesWithScore = await retriever.retrieve({ query: "رشته پرس و جو" });
|
||||
const nodesWithScore = await retriever.retrieve("رشته پرس و جو");
|
||||
```
|
||||
|
||||
## مرجع API
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
|
||||
retriever.similarityTopK = 3;
|
||||
|
||||
// Hae solmut!
|
||||
const nodesWithScore = await retriever.retrieve({ query: "kyselymerkkijono" });
|
||||
const nodesWithScore = await retriever.retrieve("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({ query: "chaîne de requête" });
|
||||
const nodesWithScore = await retriever.retrieve("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({ query: "מחרוזת שאילתה" });
|
||||
const nodesWithScore = await retriever.retrieve("מחרוזת שאילתה");
|
||||
```
|
||||
|
||||
## מדריך לממשק API
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
|
||||
retriever.similarityTopK = 3;
|
||||
|
||||
// नोड्स प्राप्त करें!
|
||||
const nodesWithScore = await retriever.retrieve({ query: "क्वेरी स्ट्रिंग" });
|
||||
const nodesWithScore = await retriever.retrieve("क्वेरी स्ट्रिंग");
|
||||
```
|
||||
|
||||
## एपीआई संदर्भ (API Reference)
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ const dohvatnik = vector_index.asRetriever();
|
||||
dohvatnik.similarityTopK = 3;
|
||||
|
||||
// Dohvati čvorove!
|
||||
const čvoroviSaRezultatom = await dohvatnik.retrieve({ query: "upitni niz" });
|
||||
const čvoroviSaRezultatom = await dohvatnik.retrieve("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({ query: "lekérdezési karakterlánc" });
|
||||
const nodesWithScore = await retriever.retrieve("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({ query: "string query" });
|
||||
const nodesWithScore = await retriever.retrieve("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({ query: "stringa di query" });
|
||||
const nodesWithScore = await retriever.retrieve("stringa di query");
|
||||
```
|
||||
|
||||
## Riferimento API
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
|
||||
retriever.similarityTopK = 3;
|
||||
|
||||
// ノードを取得します!
|
||||
const nodesWithScore = await retriever.retrieve({ query: "クエリ文字列" });
|
||||
const nodesWithScore = await retriever.retrieve("クエリ文字列");
|
||||
```
|
||||
|
||||
## API リファレンス
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
|
||||
retriever.similarityTopK = 3;
|
||||
|
||||
// 노드를 가져옵니다!
|
||||
const nodesWithScore = await retriever.retrieve({ query: "쿼리 문자열" });
|
||||
const nodesWithScore = await retriever.retrieve("쿼리 문자열");
|
||||
```
|
||||
|
||||
## 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({ query: "užklausos eilutė" });
|
||||
const mazgaiSuRezultatu = await gavėjas.retrieve("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({ query: "zoekopdracht" });
|
||||
const nodesWithScore = await retriever.retrieve("zoekopdracht");
|
||||
```
|
||||
|
||||
## API Referentie
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
|
||||
retriever.similarityTopK = 3;
|
||||
|
||||
// Hent noder!
|
||||
const nodesWithScore = await retriever.retrieve({ query: "spørringsstreng" });
|
||||
const nodesWithScore = await retriever.retrieve("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({ query: "ciąg zapytania" });
|
||||
const nodesWithScore = await retriever.retrieve("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({ query: "string de consulta" });
|
||||
const nósComPontuação = await recuperador.retrieve("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({ query: "șir de interogare" });
|
||||
const noduriCuScor = await recuperator.retrieve("șir de interogare");
|
||||
```
|
||||
|
||||
## Referință API
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
|
||||
retriever.similarityTopK = 3;
|
||||
|
||||
// Получение узлов!
|
||||
const nodesWithScore = await retriever.retrieve({ query: "строка запроса" });
|
||||
const nodesWithScore = await retriever.retrieve("строка запроса");
|
||||
```
|
||||
|
||||
## Справочник по API
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
|
||||
retriever.similarityTopK = 3;
|
||||
|
||||
// Dohvati čvorove!
|
||||
const nodesWithScore = await retriever.retrieve({ query: "upitni niz" });
|
||||
const nodesWithScore = await retriever.retrieve("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({ query: "poizvedbeni niz" });
|
||||
const vozliščaZRezultatom = await pridobitelj.retrieve("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({ query: "reťazec dotazu" });
|
||||
const nodesWithScore = await retriever.retrieve("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({ query: "frågesträng" });
|
||||
const nodesWithScore = await retriever.retrieve("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: "query string" });
|
||||
const nodesWithScore = await retriever.retrieve("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({ query: "sorgu dizesi" });
|
||||
const nodesWithScore = await retriever.retrieve("sorgu dizesi");
|
||||
```
|
||||
|
||||
## API Referansı
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
|
||||
retriever.similarityTopK = 3;
|
||||
|
||||
// Отримати вузли!
|
||||
const nodesWithScore = await retriever.retrieve({ query: "рядок запиту" });
|
||||
const nodesWithScore = await retriever.retrieve("рядок запиту");
|
||||
```
|
||||
|
||||
## Довідник 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({ query: "chuỗi truy vấn" });
|
||||
const nodesWithScore = await retriever.retrieve("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({ query: "查询字符串" });
|
||||
const nodesWithScore = await retriever.retrieve("查询字符串");
|
||||
```
|
||||
|
||||
## API 参考
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
|
||||
retriever.similarityTopK = 3;
|
||||
|
||||
// 提取節點!
|
||||
const nodesWithScore = await retriever.retrieve({ query: "查詢字串" });
|
||||
const nodesWithScore = await retriever.retrieve("查詢字串");
|
||||
```
|
||||
|
||||
## API 參考
|
||||
|
||||
@@ -1,77 +0,0 @@
|
||||
import { FunctionTool, OpenAIAgent } from "llamaindex";
|
||||
|
||||
// Define a function to sum two numbers
|
||||
function sumNumbers({ a, b }: { a: number; b: number }): number {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
// Define a function to divide two numbers
|
||||
function divideNumbers({ a, b }: { a: number; b: number }): number {
|
||||
return a / b;
|
||||
}
|
||||
|
||||
// Define the parameters of the sum function as a JSON schema
|
||||
const sumJSON = {
|
||||
type: "object",
|
||||
properties: {
|
||||
a: {
|
||||
type: "number",
|
||||
description: "The first number",
|
||||
},
|
||||
b: {
|
||||
type: "number",
|
||||
description: "The second number",
|
||||
},
|
||||
},
|
||||
required: ["a", "b"],
|
||||
};
|
||||
|
||||
const divideJSON = {
|
||||
type: "object",
|
||||
properties: {
|
||||
a: {
|
||||
type: "number",
|
||||
description: "The dividend",
|
||||
},
|
||||
b: {
|
||||
type: "number",
|
||||
description: "The divisor",
|
||||
},
|
||||
},
|
||||
required: ["a", "b"],
|
||||
};
|
||||
|
||||
async function main() {
|
||||
// Create a function tool from the sum function
|
||||
const functionTool = new FunctionTool(sumNumbers, {
|
||||
name: "sumNumbers",
|
||||
description: "Use this function to sum two numbers",
|
||||
parameters: sumJSON,
|
||||
});
|
||||
|
||||
// Create a function tool from the divide function
|
||||
const functionTool2 = new FunctionTool(divideNumbers, {
|
||||
name: "divideNumbers",
|
||||
description: "Use this function to divide two numbers",
|
||||
parameters: divideJSON,
|
||||
});
|
||||
|
||||
// Create an OpenAIAgent with the function tools
|
||||
const agent = new OpenAIAgent({
|
||||
tools: [functionTool, functionTool2],
|
||||
verbose: false,
|
||||
});
|
||||
|
||||
const stream = await agent.chat({
|
||||
message: "Divide 16 by 2 then add 20",
|
||||
stream: true,
|
||||
});
|
||||
|
||||
for await (const chunk of stream.response) {
|
||||
process.stdout.write(chunk.response);
|
||||
}
|
||||
}
|
||||
|
||||
main().then(() => {
|
||||
console.log("\nDone");
|
||||
});
|
||||
@@ -3,7 +3,6 @@ import { Anthropic } from "llamaindex";
|
||||
(async () => {
|
||||
const anthropic = new Anthropic({
|
||||
apiKey: process.env.ANTHROPIC_API_KEY,
|
||||
model: "claude-3-opus",
|
||||
});
|
||||
const result = await anthropic.chat({
|
||||
messages: [
|
||||
@@ -1,34 +0,0 @@
|
||||
import { Anthropic, SimpleChatEngine, SimpleChatHistory } from "llamaindex";
|
||||
import { stdin as input, stdout as output } from "node:process";
|
||||
import readline from "node:readline/promises";
|
||||
|
||||
(async () => {
|
||||
const llm = new Anthropic({
|
||||
apiKey: process.env.ANTHROPIC_API_KEY,
|
||||
model: "claude-3-opus",
|
||||
});
|
||||
// chatHistory will store all the messages in the conversation
|
||||
const chatHistory = new SimpleChatHistory({
|
||||
messages: [
|
||||
{
|
||||
content: "You want to talk in rhymes.",
|
||||
role: "system",
|
||||
},
|
||||
],
|
||||
});
|
||||
const chatEngine = new SimpleChatEngine({
|
||||
llm,
|
||||
chatHistory,
|
||||
});
|
||||
const rl = readline.createInterface({ input, output });
|
||||
|
||||
while (true) {
|
||||
const query = await rl.question("User: ");
|
||||
process.stdout.write("Assistant: ");
|
||||
const stream = await chatEngine.chat({ message: query, stream: true });
|
||||
for await (const chunk of stream) {
|
||||
process.stdout.write(chunk.response);
|
||||
}
|
||||
process.stdout.write("\n");
|
||||
}
|
||||
})();
|
||||
@@ -1,23 +0,0 @@
|
||||
import { Anthropic } from "llamaindex";
|
||||
|
||||
(async () => {
|
||||
const anthropic = new Anthropic({
|
||||
apiKey: process.env.ANTHROPIC_API_KEY,
|
||||
model: "claude-instant-1.2",
|
||||
});
|
||||
const stream = 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",
|
||||
},
|
||||
],
|
||||
stream: true,
|
||||
});
|
||||
|
||||
for await (const chunk of stream) {
|
||||
process.stdout.write(chunk.delta);
|
||||
}
|
||||
})();
|
||||
@@ -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({
|
||||
query: "what are Vincent van Gogh's famous paintings",
|
||||
});
|
||||
const results = await retriever.retrieve(
|
||||
"what are Vincent van Gogh's famous paintings",
|
||||
);
|
||||
for (const result of results) {
|
||||
const node = result.node;
|
||||
if (!node) {
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
import knex from "knex";
|
||||
import {
|
||||
NLSQLQueryEngine,
|
||||
OpenAI,
|
||||
SQLDatabase,
|
||||
serviceContextFromDefaults,
|
||||
} from "llamaindex";
|
||||
|
||||
async function main() {
|
||||
const llm = new OpenAI({
|
||||
model: "gpt-4",
|
||||
});
|
||||
|
||||
const engine = knex({
|
||||
client: "sqlite3", // or 'better-sqlite3'
|
||||
connection: {
|
||||
filename: ":memory:",
|
||||
},
|
||||
});
|
||||
|
||||
const db = new SQLDatabase({
|
||||
engine,
|
||||
schema: undefined,
|
||||
metadata: {},
|
||||
ignoreTables: undefined,
|
||||
includeTables: ["test_table_1"],
|
||||
sampleRowsInTableInfo: 3,
|
||||
indexesInTableInfo: true,
|
||||
customTableInfo: undefined,
|
||||
maxStringLength: 100,
|
||||
});
|
||||
|
||||
const tableName = "test_table_1";
|
||||
|
||||
await engine.schema.createTable(tableName, async (table) => {
|
||||
table.increments("id");
|
||||
table.string("comment");
|
||||
table.string("author");
|
||||
|
||||
await db.insertIntoTable(tableName, {
|
||||
comment: "this is a test1",
|
||||
author: "emanuel",
|
||||
});
|
||||
await db.insertIntoTable(tableName, {
|
||||
comment: "this is a test2",
|
||||
author: "alex",
|
||||
});
|
||||
await db.insertIntoTable(tableName, {
|
||||
comment: "this is a test3",
|
||||
author: "yi",
|
||||
});
|
||||
await db.insertIntoTable(tableName, {
|
||||
comment: "this is a test4",
|
||||
author: "alex",
|
||||
});
|
||||
|
||||
const ctx = serviceContextFromDefaults({
|
||||
llm,
|
||||
});
|
||||
|
||||
const engine = new NLSQLQueryEngine({
|
||||
sqlDatabase: db,
|
||||
tables: ["test_table_1"],
|
||||
verbose: true,
|
||||
serviceContext: ctx,
|
||||
synthesizeResponse: true,
|
||||
});
|
||||
|
||||
const response = await engine.query({
|
||||
query: "What's the comment from author yi and emanuel?",
|
||||
});
|
||||
|
||||
console.log({ response });
|
||||
|
||||
process.exit(0);
|
||||
});
|
||||
}
|
||||
|
||||
main().then(() => [
|
||||
// process.exit(0)
|
||||
]);
|
||||
@@ -9,8 +9,10 @@
|
||||
"chromadb": "^1.8.1",
|
||||
"commander": "^11.1.0",
|
||||
"dotenv": "^16.4.1",
|
||||
"knex": "^3.1.0",
|
||||
"llamaindex": "latest",
|
||||
"mongodb": "^6.2.0"
|
||||
"mongodb": "^6.2.0",
|
||||
"sqlite3": "^5.1.7"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^18.19.10",
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
import knex from "knex";
|
||||
import { SQLDatabase } from "llamaindex";
|
||||
|
||||
async function main() {
|
||||
const engine = knex({
|
||||
client: "sqlite3", // or 'better-sqlite3'
|
||||
connection: {
|
||||
filename: ":memory:",
|
||||
},
|
||||
});
|
||||
|
||||
const db = new SQLDatabase({
|
||||
engine,
|
||||
schema: undefined,
|
||||
metadata: {},
|
||||
ignoreTables: undefined,
|
||||
includeTables: ["test_table"],
|
||||
sampleRowsInTableInfo: 3,
|
||||
indexesInTableInfo: true,
|
||||
customTableInfo: undefined,
|
||||
maxStringLength: 100,
|
||||
});
|
||||
|
||||
const tableName = "test_table";
|
||||
|
||||
await engine.schema.createTable(tableName, () => {});
|
||||
|
||||
await db.insertIntoTable(tableName, {
|
||||
name: "test1",
|
||||
comment: "this is a test1",
|
||||
});
|
||||
await db.insertIntoTable(tableName, {
|
||||
name: "test2",
|
||||
comment: "this is a test2",
|
||||
});
|
||||
await db.insertIntoTable(tableName, {
|
||||
name: "test3",
|
||||
comment: "this is a test3",
|
||||
});
|
||||
await db.insertIntoTable(tableName, {
|
||||
name: "test4",
|
||||
comment: "this is a test4",
|
||||
});
|
||||
}
|
||||
|
||||
main();
|
||||
+2
-2
@@ -13,8 +13,8 @@
|
||||
"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-experimental": "pnpm run build:release && changeset version --ignore create-llama"
|
||||
"new-create-llama": "pnpm run build:release && changeset version --ignore llamaindex",
|
||||
"new-snapshots": "pnpm run build:release && changeset version --snapshot"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@changesets/cli": "^2.27.1",
|
||||
|
||||
@@ -1,12 +1,5 @@
|
||||
# llamaindex
|
||||
|
||||
## 0.1.21
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 552a61a: Add quantized parameter to HuggingFaceEmbedding
|
||||
- d824876: Add support for Claude 3
|
||||
|
||||
## 0.1.20
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@llamaindex/core",
|
||||
"version": "0.1.21",
|
||||
"version": "0.1.20",
|
||||
"exports": "./src/index.ts",
|
||||
"imports": {
|
||||
"@llamaindex/env": "jsr:@llamaindex/env@0.0.5"
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
{
|
||||
"name": "llamaindex",
|
||||
"version": "0.1.21",
|
||||
"version": "0.1.20",
|
||||
"license": "MIT",
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"@anthropic-ai/sdk": "^0.15.0",
|
||||
"@anthropic-ai/sdk": "^0.13.0",
|
||||
"@aws-crypto/sha256-js": "^5.2.0",
|
||||
"@datastax/astra-db-ts": "^0.1.4",
|
||||
"@llamaindex/cloud": "0.0.4",
|
||||
@@ -23,6 +23,7 @@
|
||||
"cohere-ai": "^7.7.5",
|
||||
"file-type": "^18.7.0",
|
||||
"js-tiktoken": "^1.0.10",
|
||||
"knex": "^3.1.0",
|
||||
"lodash": "^4.17.21",
|
||||
"mammoth": "^1.6.0",
|
||||
"md-utils-ts": "^2.0.0",
|
||||
@@ -92,9 +93,9 @@
|
||||
"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:type": "tsc -p tsconfig.json",
|
||||
"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": "rm -f .tsbuildinfo && tsc -b --diagnostics",
|
||||
"copy": "cp -r ../../README.md ../../LICENSE .",
|
||||
"postbuild": "pnpm run copy && node -e \"require('fs').writeFileSync('./dist/cjs/package.json', JSON.stringify({ type: 'commonjs' }))\"",
|
||||
"circular-check": "madge -c ./src/index.ts",
|
||||
|
||||
@@ -2,16 +2,14 @@ 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(params: RetrieveParams): Promise<NodeWithScore[]>;
|
||||
retrieve(
|
||||
query: string,
|
||||
parentEvent?: Event,
|
||||
preFilters?: unknown,
|
||||
): Promise<NodeWithScore[]>;
|
||||
getServiceContext(): ServiceContext;
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
// 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";
|
||||
import {
|
||||
AgentChatResponse,
|
||||
ChatResponseMode,
|
||||
StreamingAgentChatResponse,
|
||||
} from "../../engines/chat/types.js";
|
||||
import type {
|
||||
ChatMessage,
|
||||
@@ -12,7 +12,6 @@ import type {
|
||||
ChatResponseChunk,
|
||||
} from "../../llm/index.js";
|
||||
import { OpenAI } from "../../llm/index.js";
|
||||
import { streamConverter, streamReducer } from "../../llm/utils.js";
|
||||
import { ChatMemoryBuffer } from "../../memory/ChatMemoryBuffer.js";
|
||||
import type { ObjectRetriever } from "../../objects/base.js";
|
||||
import type { ToolOutput } from "../../tools/types.js";
|
||||
@@ -193,40 +192,13 @@ export class OpenAIAgentWorker implements AgentWorker {
|
||||
private _processMessage(
|
||||
task: Task,
|
||||
chatResponse: ChatResponse,
|
||||
): AgentChatResponse {
|
||||
): AgentChatResponse | AsyncIterable<ChatResponseChunk> {
|
||||
const aiMessage = chatResponse.message;
|
||||
task.extraState.newMemory.put(aiMessage);
|
||||
|
||||
return new AgentChatResponse(aiMessage.content, task.extraState.sources);
|
||||
}
|
||||
|
||||
private async _getStreamAiResponse(
|
||||
task: Task,
|
||||
llmChatKwargs: any,
|
||||
): Promise<StreamingAgentChatResponse> {
|
||||
const stream = await this.llm.chat({
|
||||
stream: true,
|
||||
...llmChatKwargs,
|
||||
});
|
||||
|
||||
const iterator = streamConverter(
|
||||
streamReducer({
|
||||
stream,
|
||||
initialValue: "",
|
||||
reducer: (accumulator, part) => (accumulator += part.delta),
|
||||
finished: (accumulator) => {
|
||||
task.extraState.newMemory.put({
|
||||
content: accumulator,
|
||||
role: "assistant",
|
||||
});
|
||||
},
|
||||
}),
|
||||
(r: ChatResponseChunk) => new Response(r.delta),
|
||||
);
|
||||
|
||||
return new StreamingAgentChatResponse(iterator, task.extraState.sources);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get agent response.
|
||||
* @param task: task
|
||||
@@ -238,7 +210,7 @@ export class OpenAIAgentWorker implements AgentWorker {
|
||||
task: Task,
|
||||
mode: ChatResponseMode,
|
||||
llmChatKwargs: any,
|
||||
): Promise<AgentChatResponse | StreamingAgentChatResponse> {
|
||||
): Promise<AgentChatResponse> {
|
||||
if (mode === ChatResponseMode.WAIT) {
|
||||
const chatResponse = (await this.llm.chat({
|
||||
stream: false,
|
||||
@@ -246,11 +218,9 @@ export class OpenAIAgentWorker implements AgentWorker {
|
||||
})) as unknown as ChatResponse;
|
||||
|
||||
return this._processMessage(task, chatResponse) as AgentChatResponse;
|
||||
} else if (mode === ChatResponseMode.STREAM) {
|
||||
return this._getStreamAiResponse(task, llmChatKwargs);
|
||||
} else {
|
||||
throw new Error("Not implemented");
|
||||
}
|
||||
|
||||
throw new Error("Invalid mode");
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,7 +4,6 @@ import type { ChatEngineAgentParams } from "../../engines/chat/index.js";
|
||||
import {
|
||||
AgentChatResponse,
|
||||
ChatResponseMode,
|
||||
StreamingAgentChatResponse,
|
||||
} from "../../engines/chat/index.js";
|
||||
import type { ChatMessage, LLM } from "../../llm/index.js";
|
||||
import { ChatMemoryBuffer } from "../../memory/ChatMemoryBuffer.js";
|
||||
@@ -232,26 +231,23 @@ export class AgentRunner extends BaseAgentRunner {
|
||||
taskId: string,
|
||||
stepOutput: TaskStepOutput,
|
||||
kwargs?: any,
|
||||
): Promise<AgentChatResponse | StreamingAgentChatResponse> {
|
||||
): Promise<AgentChatResponse> {
|
||||
if (!stepOutput) {
|
||||
stepOutput =
|
||||
this.getCompletedSteps(taskId)[
|
||||
this.getCompletedSteps(taskId).length - 1
|
||||
];
|
||||
}
|
||||
|
||||
if (!stepOutput.isLast) {
|
||||
throw new Error(
|
||||
"finalizeResponse can only be called on the last step output",
|
||||
);
|
||||
}
|
||||
|
||||
if (!(stepOutput.output instanceof StreamingAgentChatResponse)) {
|
||||
if (!(stepOutput.output instanceof AgentChatResponse)) {
|
||||
throw new Error(
|
||||
`When \`isLast\` is True, cur_step_output.output must be AGENT_CHAT_RESPONSE_TYPE: ${stepOutput.output}`,
|
||||
);
|
||||
}
|
||||
if (!(stepOutput.output instanceof AgentChatResponse)) {
|
||||
throw new Error(
|
||||
`When \`isLast\` is True, cur_step_output.output must be AGENT_CHAT_RESPONSE_TYPE: ${stepOutput.output}`,
|
||||
);
|
||||
}
|
||||
|
||||
this.agentWorker.finalizeTask(this.getTask(taskId), kwargs);
|
||||
@@ -266,32 +262,20 @@ export class AgentRunner extends BaseAgentRunner {
|
||||
protected async _chat({
|
||||
message,
|
||||
toolChoice,
|
||||
stream,
|
||||
}: ChatEngineAgentParams): Promise<AgentChatResponse>;
|
||||
protected async _chat({
|
||||
message,
|
||||
toolChoice,
|
||||
stream,
|
||||
}: ChatEngineAgentParams & {
|
||||
stream: true;
|
||||
}): Promise<StreamingAgentChatResponse>;
|
||||
protected async _chat({
|
||||
message,
|
||||
toolChoice,
|
||||
stream,
|
||||
}: ChatEngineAgentParams): Promise<
|
||||
AgentChatResponse | StreamingAgentChatResponse
|
||||
> {
|
||||
}: ChatEngineAgentParams & { mode: ChatResponseMode }) {
|
||||
const task = this.createTask(message as string);
|
||||
|
||||
let resultOutput;
|
||||
|
||||
const mode = stream ? ChatResponseMode.STREAM : ChatResponseMode.WAIT;
|
||||
|
||||
while (true) {
|
||||
const curStepOutput = await this._runStep(task.taskId, undefined, mode, {
|
||||
toolChoice,
|
||||
});
|
||||
const curStepOutput = await this._runStep(
|
||||
task.taskId,
|
||||
undefined,
|
||||
ChatResponseMode.WAIT,
|
||||
{
|
||||
toolChoice,
|
||||
},
|
||||
);
|
||||
|
||||
if (curStepOutput.isLast) {
|
||||
resultOutput = curStepOutput;
|
||||
@@ -315,26 +299,7 @@ export class AgentRunner extends BaseAgentRunner {
|
||||
message,
|
||||
chatHistory,
|
||||
toolChoice,
|
||||
stream,
|
||||
}: ChatEngineAgentParams & {
|
||||
stream?: false;
|
||||
}): Promise<AgentChatResponse>;
|
||||
public async chat({
|
||||
message,
|
||||
chatHistory,
|
||||
toolChoice,
|
||||
stream,
|
||||
}: ChatEngineAgentParams & {
|
||||
stream: true;
|
||||
}): Promise<StreamingAgentChatResponse>;
|
||||
public async chat({
|
||||
message,
|
||||
chatHistory,
|
||||
toolChoice,
|
||||
stream,
|
||||
}: ChatEngineAgentParams): Promise<
|
||||
AgentChatResponse | StreamingAgentChatResponse
|
||||
> {
|
||||
}: ChatEngineAgentParams): Promise<AgentChatResponse> {
|
||||
if (!toolChoice) {
|
||||
toolChoice = this.defaultToolChoice;
|
||||
}
|
||||
@@ -343,7 +308,7 @@ export class AgentRunner extends BaseAgentRunner {
|
||||
message,
|
||||
chatHistory,
|
||||
toolChoice,
|
||||
stream,
|
||||
mode: ChatResponseMode.WAIT,
|
||||
});
|
||||
|
||||
return chatResponse;
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
import type {
|
||||
AgentChatResponse,
|
||||
StreamingAgentChatResponse,
|
||||
} from "../../engines/chat/index.js";
|
||||
import type { AgentChatResponse } from "../../engines/chat/index.js";
|
||||
import type { Task, TaskStep, TaskStepOutput } from "../types.js";
|
||||
import { BaseAgent } from "../types.js";
|
||||
|
||||
@@ -60,7 +57,7 @@ export abstract class BaseAgentRunner extends BaseAgent {
|
||||
taskId: string,
|
||||
stepOutput: TaskStepOutput,
|
||||
kwargs?: any,
|
||||
): Promise<AgentChatResponse | StreamingAgentChatResponse>;
|
||||
): Promise<AgentChatResponse>;
|
||||
|
||||
abstract undoStep(taskId: string): void;
|
||||
}
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
import type {
|
||||
AgentChatResponse,
|
||||
ChatEngineAgentParams,
|
||||
StreamingAgentChatResponse,
|
||||
} from "../engines/chat/index.js";
|
||||
|
||||
import type { QueryEngineParamsNonStreaming } from "../types.js";
|
||||
|
||||
export interface AgentWorker {
|
||||
@@ -14,15 +12,11 @@ export interface AgentWorker {
|
||||
}
|
||||
|
||||
interface BaseChatEngine {
|
||||
chat(
|
||||
params: ChatEngineAgentParams,
|
||||
): Promise<AgentChatResponse | StreamingAgentChatResponse>;
|
||||
chat(params: ChatEngineAgentParams): Promise<AgentChatResponse>;
|
||||
}
|
||||
|
||||
interface BaseQueryEngine {
|
||||
query(
|
||||
params: QueryEngineParamsNonStreaming,
|
||||
): Promise<AgentChatResponse | StreamingAgentChatResponse>;
|
||||
query(params: QueryEngineParamsNonStreaming): Promise<AgentChatResponse>;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -37,10 +31,7 @@ export abstract class BaseAgent implements BaseChatEngine, BaseQueryEngine {
|
||||
return [];
|
||||
}
|
||||
|
||||
abstract chat(
|
||||
params: ChatEngineAgentParams,
|
||||
): Promise<AgentChatResponse | StreamingAgentChatResponse>;
|
||||
|
||||
abstract chat(params: ChatEngineAgentParams): Promise<AgentChatResponse>;
|
||||
abstract reset(): void;
|
||||
|
||||
/**
|
||||
@@ -50,7 +41,7 @@ export abstract class BaseAgent implements BaseChatEngine, BaseQueryEngine {
|
||||
*/
|
||||
async query(
|
||||
params: QueryEngineParamsNonStreaming,
|
||||
): Promise<AgentChatResponse | StreamingAgentChatResponse> {
|
||||
): Promise<AgentChatResponse> {
|
||||
// Handle non-streaming query
|
||||
const agentResponse = await this.chat({
|
||||
message: params.query,
|
||||
|
||||
@@ -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 { CloudRetrieveParams } from "./LlamaCloudRetriever.js";
|
||||
import type { RetrieveParams } 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: CloudRetrieveParams = {}): BaseRetriever {
|
||||
asRetriever(params: RetrieveParams = {}): BaseRetriever {
|
||||
return new LlamaCloudRetriever({ ...this.params, ...params });
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ export class LlamaCloudIndex {
|
||||
responseSynthesizer?: BaseSynthesizer;
|
||||
preFilters?: unknown;
|
||||
nodePostprocessors?: BaseNodePostprocessor[];
|
||||
} & CloudRetrieveParams,
|
||||
} & RetrieveParams,
|
||||
): BaseQueryEngine {
|
||||
const retriever = new LlamaCloudRetriever({
|
||||
...this.params,
|
||||
|
||||
@@ -2,14 +2,15 @@ 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, RetrieveParams } from "../Retriever.js";
|
||||
import type { BaseRetriever } 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 CloudRetrieveParams = Omit<
|
||||
export type RetrieveParams = Omit<
|
||||
PlatformApi.RetrievalParams,
|
||||
"query" | "searchFilters" | "pipelineId" | "className"
|
||||
> & { similarityTopK?: number };
|
||||
@@ -17,7 +18,7 @@ export type CloudRetrieveParams = Omit<
|
||||
export class LlamaCloudRetriever implements BaseRetriever {
|
||||
client?: PlatformApiClient;
|
||||
clientParams: ClientParams;
|
||||
retrieveParams: CloudRetrieveParams;
|
||||
retrieveParams: RetrieveParams;
|
||||
projectName: string = DEFAULT_PROJECT_NAME;
|
||||
pipelineName: string;
|
||||
serviceContext: ServiceContext;
|
||||
@@ -34,7 +35,7 @@ export class LlamaCloudRetriever implements BaseRetriever {
|
||||
});
|
||||
}
|
||||
|
||||
constructor(params: CloudConstructorParams & CloudRetrieveParams) {
|
||||
constructor(params: CloudConstructorParams & RetrieveParams) {
|
||||
this.clientParams = { apiKey: params.apiKey, baseUrl: params.baseUrl };
|
||||
if (params.similarityTopK) {
|
||||
params.denseSimilarityTopK = params.similarityTopK;
|
||||
@@ -54,11 +55,11 @@ export class LlamaCloudRetriever implements BaseRetriever {
|
||||
return this.client;
|
||||
}
|
||||
|
||||
async retrieve({
|
||||
query,
|
||||
parentEvent,
|
||||
preFilters,
|
||||
}: RetrieveParams): Promise<NodeWithScore[]> {
|
||||
async retrieve(
|
||||
query: string,
|
||||
parentEvent?: Event | undefined,
|
||||
preFilters?: unknown,
|
||||
): Promise<NodeWithScore[]> {
|
||||
const pipelines = await (
|
||||
await this.getClient()
|
||||
).pipeline.searchPipelines({
|
||||
|
||||
@@ -20,7 +20,6 @@ export enum HuggingFaceEmbeddingModelType {
|
||||
*/
|
||||
export class HuggingFaceEmbedding extends BaseEmbedding {
|
||||
modelType: string = HuggingFaceEmbeddingModelType.XENOVA_ALL_MINILM_L6_V2;
|
||||
quantized: boolean = true;
|
||||
|
||||
private extractor: any;
|
||||
|
||||
@@ -32,9 +31,7 @@ export class HuggingFaceEmbedding extends BaseEmbedding {
|
||||
async getExtractor() {
|
||||
if (!this.extractor) {
|
||||
const { pipeline } = await import("@xenova/transformers");
|
||||
this.extractor = await pipeline("feature-extraction", this.modelType, {
|
||||
quantized: this.quantized,
|
||||
});
|
||||
this.extractor = await pipeline("feature-extraction", this.modelType);
|
||||
}
|
||||
return this.extractor;
|
||||
}
|
||||
|
||||
@@ -64,10 +64,10 @@ export class DefaultContextGenerator
|
||||
tags: ["final"],
|
||||
};
|
||||
}
|
||||
const sourceNodesWithScore = await this.retriever.retrieve({
|
||||
query: message,
|
||||
const sourceNodesWithScore = await this.retriever.retrieve(
|
||||
message,
|
||||
parentEvent,
|
||||
});
|
||||
);
|
||||
|
||||
const nodes = await this.applyNodePostprocessors(
|
||||
sourceNodesWithScore,
|
||||
|
||||
@@ -27,7 +27,6 @@ export interface ChatEngineParamsNonStreaming extends ChatEngineParamsBase {
|
||||
|
||||
export interface ChatEngineAgentParams extends ChatEngineParamsBase {
|
||||
toolChoice?: string | Record<string, any>;
|
||||
stream?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -87,20 +86,3 @@ export class AgentChatResponse {
|
||||
return this.response ?? "";
|
||||
}
|
||||
}
|
||||
|
||||
export class StreamingAgentChatResponse {
|
||||
response: AsyncIterable<Response>;
|
||||
|
||||
sources: ToolOutput[];
|
||||
sourceNodes?: BaseNode[];
|
||||
|
||||
constructor(
|
||||
response: AsyncIterable<Response>,
|
||||
sources?: ToolOutput[],
|
||||
sourceNodes?: BaseNode[],
|
||||
) {
|
||||
this.response = response;
|
||||
this.sources = sources ?? [];
|
||||
this.sourceNodes = sourceNodes ?? [];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
preFilters: this.preFilters,
|
||||
});
|
||||
this.preFilters,
|
||||
);
|
||||
|
||||
return await this.applyNodePostprocessors(nodes, query);
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from "./RetrieverQueryEngine.js";
|
||||
export * from "./RouterQueryEngine.js";
|
||||
export * from "./SubQuestionQueryEngine.js";
|
||||
export * from "./sql/index.js";
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
import {
|
||||
NLSQLRetriever,
|
||||
type SQLDatabase,
|
||||
type ServiceContext,
|
||||
} from "../../../index.js";
|
||||
import type { TextToSQLPrompt } from "../../../retriever/sql/prompts.js";
|
||||
import { BaseSQLTableQueryEngine } from "./types.js";
|
||||
|
||||
type NLSQLQueryEngineParams = {
|
||||
sqlDatabase: SQLDatabase;
|
||||
textToSQLPrompt?: TextToSQLPrompt;
|
||||
contextQueryKwargs?: any | null;
|
||||
synthesizeResponse?: boolean;
|
||||
responseSynthesisPrompt?: any | null;
|
||||
tables?: any[] | string[] | undefined;
|
||||
serviceContext?: ServiceContext | undefined;
|
||||
contextStrPrefix?: string | undefined;
|
||||
sqlOnly?: boolean;
|
||||
verbose?: boolean;
|
||||
};
|
||||
|
||||
export class NLSQLQueryEngine extends BaseSQLTableQueryEngine {
|
||||
_sqlRetriever: NLSQLRetriever;
|
||||
|
||||
constructor({
|
||||
sqlDatabase,
|
||||
textToSQLPrompt,
|
||||
contextQueryKwargs = null,
|
||||
synthesizeResponse = true,
|
||||
responseSynthesisPrompt = null,
|
||||
tables,
|
||||
serviceContext,
|
||||
contextStrPrefix,
|
||||
sqlOnly = false,
|
||||
verbose = false,
|
||||
}: NLSQLQueryEngineParams) {
|
||||
super({
|
||||
synthesizeResponse,
|
||||
responseSynthesisPrompt,
|
||||
serviceContext,
|
||||
verbose,
|
||||
});
|
||||
|
||||
this._sqlRetriever = new NLSQLRetriever({
|
||||
sqlDatabase,
|
||||
textToSQLPrompt,
|
||||
contextQueryKwargs,
|
||||
tables,
|
||||
contextStrPrefix,
|
||||
serviceContext,
|
||||
sqlOnly,
|
||||
verbose,
|
||||
});
|
||||
}
|
||||
|
||||
get sqlRetriever(): NLSQLRetriever {
|
||||
return this._sqlRetriever;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export * from "./NLSQLQueryEngine.js";
|
||||
@@ -0,0 +1,17 @@
|
||||
export const defaultResponseSynthesisPrompt = ({
|
||||
query,
|
||||
context,
|
||||
sqlQuery,
|
||||
}: {
|
||||
query?: string;
|
||||
context?: string;
|
||||
sqlQuery: string;
|
||||
}) => `
|
||||
Given an input question, synthesize a response from the query results.
|
||||
Query: ${query}
|
||||
SQL: ${sqlQuery}
|
||||
SQL Response: ${context}
|
||||
Response:
|
||||
`;
|
||||
|
||||
export type ResponseSynthesisPrompt = typeof defaultResponseSynthesisPrompt;
|
||||
@@ -0,0 +1,117 @@
|
||||
import { Response } from "../../../Response.js";
|
||||
import {
|
||||
serviceContextFromDefaults,
|
||||
type ServiceContext,
|
||||
} from "../../../ServiceContext.js";
|
||||
import {
|
||||
CompactAndRefine,
|
||||
MetadataMode,
|
||||
ResponseSynthesizer,
|
||||
} from "../../../index.js";
|
||||
import type { SQLRetriever } from "../../../retriever/sql/types.js";
|
||||
import type {
|
||||
BaseQueryEngine,
|
||||
QueryEngineParamsNonStreaming,
|
||||
QueryEngineParamsStreaming,
|
||||
} from "../../../types.js";
|
||||
import {
|
||||
defaultResponseSynthesisPrompt,
|
||||
type ResponseSynthesisPrompt,
|
||||
} from "./prompts.js";
|
||||
|
||||
export abstract class BaseSQLTableQueryEngine implements BaseQueryEngine {
|
||||
synthesizeResponse: boolean;
|
||||
responseSynthesisPrompt: ResponseSynthesisPrompt;
|
||||
serviceContext: ServiceContext;
|
||||
verbose: boolean;
|
||||
|
||||
constructor(init: {
|
||||
synthesizeResponse?: boolean;
|
||||
responseSynthesisPrompt?: ResponseSynthesisPrompt;
|
||||
serviceContext?: ServiceContext;
|
||||
verbose?: boolean;
|
||||
}) {
|
||||
this.synthesizeResponse = init.synthesizeResponse ?? true;
|
||||
this.responseSynthesisPrompt =
|
||||
init.responseSynthesisPrompt || defaultResponseSynthesisPrompt;
|
||||
this.serviceContext = init.serviceContext || serviceContextFromDefaults({});
|
||||
this.verbose = init.verbose || false;
|
||||
}
|
||||
|
||||
getPrompts(): {
|
||||
responseSynthesisPrompt: ResponseSynthesisPrompt;
|
||||
} {
|
||||
return { responseSynthesisPrompt: this.responseSynthesisPrompt };
|
||||
}
|
||||
|
||||
updatePrompts(prompts: {
|
||||
responseSynthesisPrompt: ResponseSynthesisPrompt;
|
||||
}): void {
|
||||
if ("responseSynthesisPrompt" in prompts) {
|
||||
this.responseSynthesisPrompt = prompts.responseSynthesisPrompt;
|
||||
}
|
||||
}
|
||||
|
||||
getPromptModules(): {
|
||||
sqlRetriever: SQLRetriever;
|
||||
} {
|
||||
return { sqlRetriever: this.sqlRetriever };
|
||||
}
|
||||
|
||||
abstract get sqlRetriever(): SQLRetriever;
|
||||
|
||||
query(params: QueryEngineParamsStreaming): Promise<AsyncIterable<Response>>;
|
||||
query(params: QueryEngineParamsNonStreaming): Promise<Response>;
|
||||
async query(
|
||||
params: QueryEngineParamsStreaming | QueryEngineParamsNonStreaming,
|
||||
): Promise<Response | AsyncIterable<Response>> {
|
||||
const { query, stream } = params;
|
||||
|
||||
if (stream) {
|
||||
throw new Error("Streaming is not supported");
|
||||
}
|
||||
|
||||
const [retrievedNodes, metadata] =
|
||||
await this.sqlRetriever.retrieveWithMetadata({
|
||||
queryStr: query,
|
||||
});
|
||||
|
||||
const sqlQueryStr = metadata.sqlQuery;
|
||||
|
||||
console.log(`> SQL query: ${sqlQueryStr}`); // TODO: Remove
|
||||
|
||||
console.log(`> Sythesize Response ${this.synthesizeResponse}`);
|
||||
|
||||
if (this.synthesizeResponse) {
|
||||
const responseBuilder = new CompactAndRefine(
|
||||
this.serviceContext,
|
||||
({ query, context }) =>
|
||||
this.responseSynthesisPrompt({
|
||||
query,
|
||||
context,
|
||||
sqlQuery: sqlQueryStr,
|
||||
}),
|
||||
);
|
||||
|
||||
const responseSynthesizer = new ResponseSynthesizer({
|
||||
serviceContext: this.serviceContext,
|
||||
responseBuilder,
|
||||
});
|
||||
|
||||
const response = await responseSynthesizer.synthesize({
|
||||
query,
|
||||
nodesWithScore: retrievedNodes,
|
||||
});
|
||||
|
||||
response.metadata.sqlQuery = sqlQueryStr;
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
const responseStr = retrievedNodes
|
||||
.map((node) => node.node.getContent(MetadataMode.ALL))
|
||||
.join("\n");
|
||||
|
||||
return new Response(responseStr, []);
|
||||
}
|
||||
}
|
||||
@@ -26,8 +26,9 @@ export * from "./objects/index.js";
|
||||
export * from "./postprocessors/index.js";
|
||||
export * from "./prompts/index.js";
|
||||
export * from "./readers/index.js";
|
||||
export * from "./retriever/index.js";
|
||||
export * from "./selectors/index.js";
|
||||
export * from "./storage/index.js";
|
||||
export * from "./synthesizers/index.js";
|
||||
export * from "./tools/index.js";
|
||||
export * from "./types.js";
|
||||
export * from "./utilities/index.js";
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
export * from "./BaseIndex.js";
|
||||
export * from "./IndexStruct.js";
|
||||
export * from "./json-to-index-struct.js";
|
||||
export * from "./keyword/index.js";
|
||||
export * from "./summary/index.js";
|
||||
export * from "./vectorStore/index.js";
|
||||
|
||||
@@ -24,15 +24,9 @@ export class IndexDict extends IndexStruct {
|
||||
}
|
||||
|
||||
toJson(): Record<string, unknown> {
|
||||
const nodesDict: Record<string, unknown> = {};
|
||||
|
||||
for (const [key, node] of Object.entries(this.nodesDict)) {
|
||||
nodesDict[key] = node.toJSON();
|
||||
}
|
||||
|
||||
return {
|
||||
...super.toJson(),
|
||||
nodesDict,
|
||||
nodesDict: this.nodesDict,
|
||||
type: this.type,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ import {
|
||||
defaultKeywordExtractPrompt,
|
||||
defaultQueryKeywordExtractPrompt,
|
||||
} from "../../Prompt.js";
|
||||
import type { BaseRetriever, RetrieveParams } from "../../Retriever.js";
|
||||
import type { BaseRetriever } from "../../Retriever.js";
|
||||
import type { ServiceContext } from "../../ServiceContext.js";
|
||||
import { serviceContextFromDefaults } from "../../ServiceContext.js";
|
||||
import { RetrieverQueryEngine } from "../../engines/query/index.js";
|
||||
@@ -79,7 +79,7 @@ abstract class BaseKeywordTableRetriever implements BaseRetriever {
|
||||
|
||||
abstract getKeywords(query: string): Promise<string[]>;
|
||||
|
||||
async retrieve({ query }: RetrieveParams): Promise<NodeWithScore[]> {
|
||||
async retrieve(query: string): Promise<NodeWithScore[]> {
|
||||
const keywords = await this.getKeywords(query);
|
||||
const chunkIndicesCount: { [key: string]: number } = {};
|
||||
const filteredKeywords = keywords.filter((keyword) =>
|
||||
|
||||
@@ -3,9 +3,10 @@ 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, RetrieveParams } from "../../Retriever.js";
|
||||
import type { BaseRetriever } 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 {
|
||||
@@ -280,10 +281,7 @@ export class SummaryIndexRetriever implements BaseRetriever {
|
||||
this.index = index;
|
||||
}
|
||||
|
||||
async retrieve({
|
||||
query,
|
||||
parentEvent,
|
||||
}: RetrieveParams): Promise<NodeWithScore[]> {
|
||||
async retrieve(query: string, parentEvent?: Event): Promise<NodeWithScore[]> {
|
||||
const nodeIds = this.index.indexStruct.nodes;
|
||||
const nodes = await this.index.docStore.getNodes(nodeIds);
|
||||
const result = nodes.map((node) => ({
|
||||
@@ -339,10 +337,7 @@ export class SummaryIndexLLMRetriever implements BaseRetriever {
|
||||
this.serviceContext = serviceContext || index.serviceContext;
|
||||
}
|
||||
|
||||
async retrieve({
|
||||
query,
|
||||
parentEvent,
|
||||
}: RetrieveParams): Promise<NodeWithScore[]> {
|
||||
async retrieve(query: string, parentEvent?: Event): Promise<NodeWithScore[]> {
|
||||
const nodeIds = this.index.indexStruct.nodes;
|
||||
const results: NodeWithScore[] = [];
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ import {
|
||||
ObjectType,
|
||||
splitNodesByType,
|
||||
} from "../../Node.js";
|
||||
import type { BaseRetriever, RetrieveParams } from "../../Retriever.js";
|
||||
import type { BaseRetriever } from "../../Retriever.js";
|
||||
import type { ServiceContext } from "../../ServiceContext.js";
|
||||
import { serviceContextFromDefaults } from "../../ServiceContext.js";
|
||||
import type { Event } from "../../callbacks/CallbackManager.js";
|
||||
@@ -426,17 +426,14 @@ export class VectorIndexRetriever implements BaseRetriever {
|
||||
this.imageSimilarityTopK = imageSimilarityTopK ?? DEFAULT_SIMILARITY_TOP_K;
|
||||
}
|
||||
|
||||
async retrieve({
|
||||
query,
|
||||
parentEvent,
|
||||
preFilters,
|
||||
}: RetrieveParams): Promise<NodeWithScore[]> {
|
||||
let nodesWithScores = await this.textRetrieve(
|
||||
query,
|
||||
preFilters as MetadataFilters,
|
||||
);
|
||||
async retrieve(
|
||||
query: string,
|
||||
parentEvent?: Event,
|
||||
preFilters?: MetadataFilters,
|
||||
): Promise<NodeWithScore[]> {
|
||||
let nodesWithScores = await this.textRetrieve(query, preFilters);
|
||||
nodesWithScores = nodesWithScores.concat(
|
||||
await this.textToImageRetrieve(query, preFilters as MetadataFilters),
|
||||
await this.textToImageRetrieve(query, preFilters),
|
||||
);
|
||||
this.sendEvent(query, nodesWithScores, parentEvent);
|
||||
return nodesWithScores;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import type OpenAILLM from "openai";
|
||||
import type { ClientOptions as OpenAIClientOptions } from "openai";
|
||||
import type {
|
||||
AnthropicStreamToken,
|
||||
CallbackManager,
|
||||
Event,
|
||||
EventType,
|
||||
@@ -12,7 +13,11 @@ import type { ChatCompletionMessageParam } from "openai/resources/index.js";
|
||||
import type { LLMOptions } from "portkey-ai";
|
||||
import { Tokenizers, globalsHelper } from "../GlobalsHelper.js";
|
||||
import type { AnthropicSession } from "./anthropic.js";
|
||||
import { getAnthropicSession } from "./anthropic.js";
|
||||
import {
|
||||
ANTHROPIC_AI_PROMPT,
|
||||
ANTHROPIC_HUMAN_PROMPT,
|
||||
getAnthropicSession,
|
||||
} from "./anthropic.js";
|
||||
import type { AzureOpenAIConfig } from "./azure.js";
|
||||
import {
|
||||
getAzureBaseUrl,
|
||||
@@ -608,30 +613,12 @@ If a question does not make any sense, or is not factually coherent, explain why
|
||||
}
|
||||
}
|
||||
|
||||
export const ALL_AVAILABLE_ANTHROPIC_LEGACY_MODELS = {
|
||||
"claude-2.1": {
|
||||
contextWindow: 200000,
|
||||
},
|
||||
"claude-instant-1.2": {
|
||||
contextWindow: 100000,
|
||||
},
|
||||
};
|
||||
|
||||
export const ALL_AVAILABLE_V3_MODELS = {
|
||||
"claude-3-opus": { contextWindow: 200000 },
|
||||
"claude-3-sonnet": { contextWindow: 200000 },
|
||||
};
|
||||
|
||||
export const ALL_AVAILABLE_ANTHROPIC_MODELS = {
|
||||
...ALL_AVAILABLE_ANTHROPIC_LEGACY_MODELS,
|
||||
...ALL_AVAILABLE_V3_MODELS,
|
||||
// both models have 100k context window, see https://docs.anthropic.com/claude/reference/selecting-a-model
|
||||
"claude-2": { contextWindow: 200000 },
|
||||
"claude-instant-1": { contextWindow: 100000 },
|
||||
};
|
||||
|
||||
const AVAILABLE_ANTHROPIC_MODELS_WITHOUT_DATE: { [key: string]: string } = {
|
||||
"claude-3-opus": "claude-3-opus-20240229",
|
||||
"claude-3-sonnet": "claude-3-sonnet-20240229",
|
||||
} as { [key in keyof typeof ALL_AVAILABLE_ANTHROPIC_MODELS]: string };
|
||||
|
||||
/**
|
||||
* Anthropic LLM implementation
|
||||
*/
|
||||
@@ -653,7 +640,7 @@ export class Anthropic extends BaseLLM {
|
||||
|
||||
constructor(init?: Partial<Anthropic>) {
|
||||
super();
|
||||
this.model = init?.model ?? "claude-3-opus";
|
||||
this.model = init?.model ?? "claude-2";
|
||||
this.temperature = init?.temperature ?? 0.1;
|
||||
this.topP = init?.topP ?? 0.999; // Per Ben Mann
|
||||
this.maxTokens = init?.maxTokens ?? undefined;
|
||||
@@ -687,24 +674,21 @@ export class Anthropic extends BaseLLM {
|
||||
};
|
||||
}
|
||||
|
||||
getModelName = (model: string): string => {
|
||||
if (Object.keys(AVAILABLE_ANTHROPIC_MODELS_WITHOUT_DATE).includes(model)) {
|
||||
return AVAILABLE_ANTHROPIC_MODELS_WITHOUT_DATE[model];
|
||||
}
|
||||
return model;
|
||||
};
|
||||
|
||||
formatMessages(messages: ChatMessage[]) {
|
||||
return messages.map((message) => {
|
||||
if (message.role !== "user" && message.role !== "assistant") {
|
||||
throw new Error("Unsupported Anthropic role");
|
||||
}
|
||||
|
||||
return {
|
||||
content: message.content,
|
||||
role: message.role,
|
||||
};
|
||||
});
|
||||
mapMessagesToPrompt(messages: ChatMessage[]) {
|
||||
return (
|
||||
messages.reduce((acc, message) => {
|
||||
return (
|
||||
acc +
|
||||
`${
|
||||
message.role === "system"
|
||||
? ""
|
||||
: message.role === "assistant"
|
||||
? ANTHROPIC_AI_PROMPT + " "
|
||||
: ANTHROPIC_HUMAN_PROMPT + " "
|
||||
}${message.content.trim()}`
|
||||
);
|
||||
}, "") + ANTHROPIC_AI_PROMPT
|
||||
);
|
||||
}
|
||||
|
||||
chat(
|
||||
@@ -714,67 +698,49 @@ export class Anthropic extends BaseLLM {
|
||||
async chat(
|
||||
params: LLMChatParamsNonStreaming | LLMChatParamsStreaming,
|
||||
): Promise<ChatResponse | AsyncIterable<ChatResponseChunk>> {
|
||||
let { messages } = params;
|
||||
|
||||
const { parentEvent, stream } = params;
|
||||
|
||||
let systemPrompt: string | null = null;
|
||||
|
||||
const systemMessages = messages.filter(
|
||||
(message) => message.role === "system",
|
||||
);
|
||||
|
||||
if (systemMessages.length > 0) {
|
||||
systemPrompt = systemMessages
|
||||
.map((message) => message.content)
|
||||
.join("\n");
|
||||
messages = messages.filter((message) => message.role !== "system");
|
||||
}
|
||||
|
||||
const { messages, parentEvent, stream } = params;
|
||||
//Streaming
|
||||
if (stream) {
|
||||
return this.streamChat(messages, parentEvent, systemPrompt);
|
||||
return this.streamChat(messages, parentEvent);
|
||||
}
|
||||
|
||||
//Non-streaming
|
||||
const response = await this.session.anthropic.messages.create({
|
||||
model: this.getModelName(this.model),
|
||||
messages: this.formatMessages(messages),
|
||||
max_tokens: this.maxTokens ?? 4096,
|
||||
const response = await this.session.anthropic.completions.create({
|
||||
model: this.model,
|
||||
prompt: this.mapMessagesToPrompt(messages),
|
||||
max_tokens_to_sample: this.maxTokens ?? 100000,
|
||||
temperature: this.temperature,
|
||||
top_p: this.topP,
|
||||
...(systemPrompt && { system: systemPrompt }),
|
||||
});
|
||||
|
||||
return {
|
||||
message: { content: response.content[0].text, role: "assistant" },
|
||||
message: { content: response.completion.trimStart(), role: "assistant" },
|
||||
//^ We're trimming the start because Anthropic often starts with a space in the response
|
||||
// That space will be re-added when we generate the next prompt.
|
||||
};
|
||||
}
|
||||
|
||||
protected async *streamChat(
|
||||
messages: ChatMessage[],
|
||||
parentEvent?: Event | undefined,
|
||||
systemPrompt?: string | null,
|
||||
): AsyncIterable<ChatResponseChunk> {
|
||||
const stream = await this.session.anthropic.messages.create({
|
||||
model: this.getModelName(this.model),
|
||||
messages: this.formatMessages(messages),
|
||||
max_tokens: this.maxTokens ?? 4096,
|
||||
temperature: this.temperature,
|
||||
top_p: this.topP,
|
||||
stream: true,
|
||||
...(systemPrompt && { system: systemPrompt }),
|
||||
});
|
||||
// AsyncIterable<AnthropicStreamToken>
|
||||
const stream: AsyncIterable<AnthropicStreamToken> =
|
||||
await this.session.anthropic.completions.create({
|
||||
model: this.model,
|
||||
prompt: this.mapMessagesToPrompt(messages),
|
||||
max_tokens_to_sample: this.maxTokens ?? 100000,
|
||||
temperature: this.temperature,
|
||||
top_p: this.topP,
|
||||
stream: true,
|
||||
});
|
||||
|
||||
let idx_counter: number = 0;
|
||||
for await (const part of stream) {
|
||||
const content =
|
||||
part.type === "content_block_delta" ? part.delta.text : null;
|
||||
|
||||
if (typeof content !== "string") continue;
|
||||
//TODO: LLM Stream Callback, pending re-work.
|
||||
|
||||
idx_counter++;
|
||||
yield { delta: content };
|
||||
yield { delta: part.completion };
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ export class ObjectRetriever {
|
||||
|
||||
// Translating the retrieve method
|
||||
async retrieve(strOrQueryBundle: QueryType): Promise<any> {
|
||||
const nodes = await this.retriever.retrieve({ query: strOrQueryBundle });
|
||||
const nodes = await this.retriever.retrieve(strOrQueryBundle);
|
||||
const objs = nodes.map((n) => this._objectNodeMapping.fromNode(n.node));
|
||||
return objs;
|
||||
}
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
export * from "./sql/index.js";
|
||||
@@ -0,0 +1,259 @@
|
||||
import { serviceContextFromDefaults } from "../../ServiceContext.js";
|
||||
import {
|
||||
TextNode,
|
||||
type BaseRetriever,
|
||||
type CallbackManager,
|
||||
type LLM,
|
||||
type NodeWithScore,
|
||||
type ObjectRetriever,
|
||||
type SQLDatabase,
|
||||
type ServiceContext,
|
||||
} from "../../index.js";
|
||||
import { QueryBundle } from "../../types.js";
|
||||
import { defaultTextToSQLPrompt, type TextToSQLPrompt } from "./prompts.js";
|
||||
import {
|
||||
DefaultSQLParser,
|
||||
SQLParserMode,
|
||||
SQLRetriever,
|
||||
type SQLTableSchema,
|
||||
} from "./types.js";
|
||||
|
||||
export class NLSQLRetriever extends SQLRetriever implements BaseRetriever {
|
||||
sqlDatabase: SQLDatabase;
|
||||
sqlRetriever: SQLRetriever;
|
||||
sqlParser: DefaultSQLParser;
|
||||
textToSQLPrompt: TextToSQLPrompt;
|
||||
contextQueryKwargs: Record<string, any> | undefined;
|
||||
tables: any[] | string[] | undefined;
|
||||
tableRetriever: ObjectRetriever | undefined;
|
||||
contextStrPrefix: string | undefined;
|
||||
sqlParserMode: SQLParserMode;
|
||||
llm: LLM;
|
||||
serviceContext: ServiceContext;
|
||||
returnRaw: boolean;
|
||||
handleSQLErrors: boolean;
|
||||
sqlOnly: boolean;
|
||||
callbackManager: CallbackManager | undefined;
|
||||
verbose: boolean;
|
||||
getTables: any;
|
||||
|
||||
constructor({
|
||||
sqlDatabase,
|
||||
textToSQLPrompt,
|
||||
contextQueryKwargs,
|
||||
tables,
|
||||
tableRetriever,
|
||||
contextStrPrefix,
|
||||
sqlParserMode,
|
||||
llm,
|
||||
serviceContext,
|
||||
returnRaw,
|
||||
handleSQLErrors,
|
||||
sqlOnly,
|
||||
callbackManager,
|
||||
verbose,
|
||||
}: {
|
||||
sqlDatabase: SQLDatabase;
|
||||
textToSQLPrompt?: TextToSQLPrompt;
|
||||
contextQueryKwargs?: Record<string, any>;
|
||||
tables?: any[] | string[];
|
||||
tableRetriever?: ObjectRetriever;
|
||||
contextStrPrefix?: string;
|
||||
sqlParserMode?: SQLParserMode;
|
||||
llm?: LLM;
|
||||
serviceContext?: ServiceContext;
|
||||
returnRaw?: boolean;
|
||||
handleSQLErrors?: boolean;
|
||||
sqlOnly?: boolean;
|
||||
callbackManager?: CallbackManager;
|
||||
verbose?: boolean;
|
||||
}) {
|
||||
super(sqlDatabase, returnRaw, callbackManager);
|
||||
|
||||
this.sqlRetriever = new SQLRetriever(sqlDatabase, returnRaw);
|
||||
this.sqlDatabase = sqlDatabase;
|
||||
this.getTables = this.loadGetTablesFn(
|
||||
sqlDatabase,
|
||||
tables,
|
||||
contextQueryKwargs,
|
||||
tableRetriever,
|
||||
);
|
||||
this.contextStrPrefix = contextStrPrefix;
|
||||
this.serviceContext = serviceContext ?? serviceContextFromDefaults();
|
||||
this.textToSQLPrompt = textToSQLPrompt ?? defaultTextToSQLPrompt;
|
||||
this.sqlParserMode = sqlParserMode ?? SQLParserMode.DEFAULT;
|
||||
this.sqlParser = this.loadSQLParser(
|
||||
this.sqlParserMode,
|
||||
this.serviceContext,
|
||||
);
|
||||
this.handleSQLErrors = handleSQLErrors ?? true;
|
||||
this.sqlOnly = sqlOnly ?? false;
|
||||
this.verbose = verbose ?? false;
|
||||
this.returnRaw = returnRaw ?? false;
|
||||
this.llm = llm ?? this.serviceContext.llm;
|
||||
}
|
||||
|
||||
_getPrompts() {
|
||||
return {
|
||||
textToSQLPrompt: this.textToSQLPrompt,
|
||||
};
|
||||
}
|
||||
|
||||
_updatePrompts(prompts: Record<string, any>) {
|
||||
if ("textToSQLPrompt" in prompts) {
|
||||
this.textToSQLPrompt = prompts.textToSQLPrompt;
|
||||
}
|
||||
}
|
||||
|
||||
_getPromptModules() {
|
||||
return {};
|
||||
}
|
||||
|
||||
getServiceContext(): ServiceContext {
|
||||
return this.serviceContext;
|
||||
}
|
||||
|
||||
loadSQLParser(sqlParserMode: SQLParserMode, serviceContext: ServiceContext) {
|
||||
if (sqlParserMode === SQLParserMode.DEFAULT) {
|
||||
return new DefaultSQLParser();
|
||||
} else {
|
||||
throw new Error(`Unknown SQL parser mode: ${sqlParserMode}`);
|
||||
}
|
||||
}
|
||||
|
||||
loadGetTablesFn(
|
||||
sqlDatabase: SQLDatabase,
|
||||
tables: any[] | string[] | undefined,
|
||||
contextQueryKwargs: Record<string, any> | undefined,
|
||||
tableRetriever: ObjectRetriever | undefined,
|
||||
) {
|
||||
contextQueryKwargs = contextQueryKwargs || {};
|
||||
|
||||
if (tableRetriever) {
|
||||
return async (queryStr: string) =>
|
||||
await tableRetriever.retrieve(queryStr);
|
||||
} else {
|
||||
let tableNames: SQLTableSchema[] | string[];
|
||||
|
||||
if (tables) {
|
||||
tableNames = tables.map((t) => t);
|
||||
} else {
|
||||
tableNames = Array.from(sqlDatabase.usableTableNames);
|
||||
}
|
||||
|
||||
const contextStrs: string[] = [];
|
||||
|
||||
const tableSchemas = tableNames.map((t, i) => {
|
||||
if (typeof t === "string") {
|
||||
return {
|
||||
tableName: t,
|
||||
...(contextQueryKwargs
|
||||
? { contextStr: contextQueryKwargs[t] }
|
||||
: {}),
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
tableName: t.tableName,
|
||||
...(contextQueryKwargs
|
||||
? { contextStr: contextQueryKwargs[t.tableName] }
|
||||
: {}),
|
||||
};
|
||||
});
|
||||
|
||||
return () => tableSchemas;
|
||||
}
|
||||
}
|
||||
|
||||
async retrieveWithMetadata(strOrQueryBundle: string | QueryBundle): Promise<
|
||||
[
|
||||
NodeWithScore[],
|
||||
{
|
||||
sqlQuery: string;
|
||||
},
|
||||
]
|
||||
> {
|
||||
const queryBundle =
|
||||
typeof strOrQueryBundle === "string"
|
||||
? { queryStr: strOrQueryBundle }
|
||||
: strOrQueryBundle;
|
||||
|
||||
const tableDescStr = await this.getTableContext(queryBundle);
|
||||
|
||||
if (this.verbose) {
|
||||
console.log(`> Table desc str: ${tableDescStr}`);
|
||||
}
|
||||
|
||||
const response = await this.serviceContext?.llm?.complete({
|
||||
prompt: this.textToSQLPrompt({
|
||||
dialect: "sql",
|
||||
schema: tableDescStr,
|
||||
queryStr: queryBundle.queryStr,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response) {
|
||||
throw new Error("No response from LLM");
|
||||
}
|
||||
|
||||
const sqlQueryStr = this.sqlParser.parseResponseToSQL(
|
||||
response?.text,
|
||||
queryBundle,
|
||||
);
|
||||
|
||||
if (this.verbose) {
|
||||
console.log(`> Predicted SQL query: ${sqlQueryStr}`);
|
||||
}
|
||||
|
||||
let retrievedNodes: NodeWithScore[];
|
||||
let metadata: Record<string, unknown> = {};
|
||||
|
||||
if (this.sqlOnly) {
|
||||
const sqlOnlyNode = new TextNode({ text: sqlQueryStr });
|
||||
retrievedNodes = [{ node: sqlOnlyNode }];
|
||||
metadata = {};
|
||||
} else {
|
||||
try {
|
||||
const retrieverResponse = await this.sqlRetriever.retrieveWithMetadata({
|
||||
queryStr: sqlQueryStr,
|
||||
});
|
||||
|
||||
retrievedNodes = retrieverResponse[0];
|
||||
metadata = retrieverResponse[1];
|
||||
} catch (e) {
|
||||
if (this.handleSQLErrors) {
|
||||
const errNode = new TextNode({ text: `Error: ${e}` });
|
||||
retrievedNodes = [{ node: errNode }];
|
||||
metadata = {};
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
return [retrievedNodes, { sqlQuery: sqlQueryStr, ...metadata }];
|
||||
}
|
||||
|
||||
async retrieve(query: string): Promise<NodeWithScore[]> {
|
||||
const [retrievedNodes] = await this.retrieveWithMetadata(query);
|
||||
return retrievedNodes;
|
||||
}
|
||||
|
||||
async getTableContext(queryBundle: QueryBundle) {
|
||||
const tableSchemaObjs = this.getTables(queryBundle.queryStr);
|
||||
const contextStrs = [];
|
||||
if (this.contextStrPrefix) {
|
||||
contextStrs.push(this.contextStrPrefix);
|
||||
}
|
||||
for (const tableSchemaObj of tableSchemaObjs) {
|
||||
let tableInfo = await this.sqlDatabase.getSingleTableInfo(
|
||||
tableSchemaObj.tableName,
|
||||
);
|
||||
if (tableSchemaObj.contextStr) {
|
||||
const tableOptContext = `The table description is: ${tableSchemaObj.contextStr}`;
|
||||
tableInfo += tableOptContext;
|
||||
}
|
||||
contextStrs.push(tableInfo);
|
||||
}
|
||||
return contextStrs.join("\n\n");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export * from "./NLSQLRetriever.js";
|
||||
@@ -0,0 +1,31 @@
|
||||
export const defaultTextToSQLPrompt = ({
|
||||
dialect,
|
||||
schema,
|
||||
queryStr,
|
||||
}: {
|
||||
dialect: string;
|
||||
schema: string;
|
||||
queryStr: string;
|
||||
}) => `Given an input question, first create a syntactically correct ${dialect}
|
||||
query to run, then look at the results of the query and return the answer.
|
||||
You can order the results by a relevant column to return the most
|
||||
interesting examples in the database.
|
||||
Never query for all the columns from a specific table, only ask for a
|
||||
few relevant columns given the question.
|
||||
Pay attention to use only the column names that you can see in the schema
|
||||
description.
|
||||
Be careful to not query for columns that do not exist.
|
||||
Pay attention to which column is in which table.
|
||||
Also, qualify column names with the table name when needed.
|
||||
You are required to use the following format, each taking one line:
|
||||
Question: Question here
|
||||
SQLQuery: SQL Query to run
|
||||
SQLResult: Result of the SQLQuery
|
||||
Answer: Final answer here
|
||||
Only use tables listed below.
|
||||
${schema}
|
||||
Question: ${queryStr}
|
||||
SQLQuery:
|
||||
`;
|
||||
|
||||
export type TextToSQLPrompt = typeof defaultTextToSQLPrompt;
|
||||
@@ -0,0 +1,105 @@
|
||||
import type { BaseRetriever } from "../../Retriever.js";
|
||||
import {
|
||||
TextNode,
|
||||
type CallbackManager,
|
||||
type Event,
|
||||
type NodeWithScore,
|
||||
type SQLDatabase,
|
||||
type ServiceContext,
|
||||
} from "../../index.js";
|
||||
import type { QueryBundle } from "../../types.js";
|
||||
|
||||
export interface SQLTableSchema {
|
||||
tableName: string;
|
||||
contextStr: string;
|
||||
}
|
||||
|
||||
export enum SQLParserMode {
|
||||
DEFAULT = "default",
|
||||
PGVECTOR = "pgvector",
|
||||
}
|
||||
|
||||
// export type SQLParserMode = "default" | "pgvector";
|
||||
|
||||
export interface BaseSQLParser {
|
||||
parseResponseToSQL(response: string, queryBundle: QueryBundle): string;
|
||||
}
|
||||
|
||||
export class DefaultSQLParser implements BaseSQLParser {
|
||||
parseResponseToSQL(response: string, queryBundle: QueryBundle): string {
|
||||
const sqlQueryStart = response.indexOf("SQLQuery:");
|
||||
if (sqlQueryStart !== -1) {
|
||||
response = response.slice(sqlQueryStart);
|
||||
if (response.startsWith("SQLQuery:")) {
|
||||
response = response.slice("SQLQuery:".length);
|
||||
}
|
||||
}
|
||||
const sqlResultStart = response.indexOf("SQLResult:");
|
||||
if (sqlResultStart !== -1) {
|
||||
response = response.slice(0, sqlResultStart);
|
||||
}
|
||||
return response.trim().replace("```", "").trim();
|
||||
}
|
||||
}
|
||||
|
||||
export class SQLRetriever implements BaseRetriever {
|
||||
sqlDatabase: SQLDatabase;
|
||||
returnRaw: boolean;
|
||||
|
||||
constructor(
|
||||
sqlDatabase: SQLDatabase,
|
||||
returnRaw: boolean = true,
|
||||
callbackManager: CallbackManager | null = null,
|
||||
kwargs: any = {},
|
||||
) {
|
||||
this.sqlDatabase = sqlDatabase;
|
||||
this.returnRaw = returnRaw;
|
||||
}
|
||||
|
||||
getServiceContext(): ServiceContext {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
|
||||
_formatNodeResults(results: any[][], colKeys: string[]): NodeWithScore[] {
|
||||
const nodes: NodeWithScore[] = [];
|
||||
for (const result of results) {
|
||||
const metadata = Object.fromEntries(
|
||||
colKeys.map((key, i) => [key, result[i]]),
|
||||
);
|
||||
const textNode = new TextNode({
|
||||
text: "",
|
||||
metadata,
|
||||
});
|
||||
nodes.push({ node: textNode });
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
|
||||
async retrieveWithMetadata(
|
||||
strOrQueryBundle: QueryBundle,
|
||||
): Promise<[NodeWithScore[], any]> {
|
||||
const [rawResponseStr, metadata] = await this.sqlDatabase.runSQL(
|
||||
strOrQueryBundle.queryStr,
|
||||
);
|
||||
|
||||
if (this.returnRaw) {
|
||||
return [[{ node: new TextNode({ text: rawResponseStr }) }], metadata];
|
||||
} else {
|
||||
const results = metadata.result;
|
||||
const colKeys = metadata.colKeys;
|
||||
return [this._formatNodeResults(results, colKeys), metadata];
|
||||
}
|
||||
}
|
||||
|
||||
async retrieve(
|
||||
query: string,
|
||||
parentEvent: Event | undefined,
|
||||
preFilters: unknown,
|
||||
): Promise<NodeWithScore[]> {
|
||||
const retrievedNodes = await this.retrieveWithMetadata({
|
||||
queryStr: query,
|
||||
});
|
||||
|
||||
return retrievedNodes;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
import knex from "knex";
|
||||
|
||||
type SQLDatabaseParams = {
|
||||
engine: knex.Knex;
|
||||
schema: string | undefined;
|
||||
metadata: any;
|
||||
ignoreTables: string[] | undefined;
|
||||
includeTables: string[] | undefined;
|
||||
sampleRowsInTableInfo: number;
|
||||
indexesInTableInfo: boolean;
|
||||
customTableInfo: Record<string, any> | undefined;
|
||||
maxStringLength: number;
|
||||
};
|
||||
|
||||
export class SQLDatabase {
|
||||
engine: knex.Knex;
|
||||
schema: string | undefined;
|
||||
metadata: any;
|
||||
inspector: knex.Knex;
|
||||
allTables: Set<string>;
|
||||
includeTables: Set<string>;
|
||||
ignoreTables: Set<string>;
|
||||
usableTables: Set<string>;
|
||||
sampleRowsInTableInfo: number;
|
||||
indexesInTableInfo: boolean;
|
||||
customTableInfo: Record<string, any> | undefined;
|
||||
maxStringLength: number;
|
||||
|
||||
constructor({
|
||||
engine,
|
||||
schema,
|
||||
metadata,
|
||||
ignoreTables,
|
||||
includeTables,
|
||||
sampleRowsInTableInfo,
|
||||
indexesInTableInfo,
|
||||
customTableInfo,
|
||||
maxStringLength,
|
||||
}: SQLDatabaseParams) {
|
||||
this.engine = engine;
|
||||
this.schema = schema;
|
||||
this.metadata = metadata;
|
||||
this.inspector = engine;
|
||||
this.allTables = new Set(["test_table_1"]);
|
||||
this.includeTables = new Set(includeTables || []);
|
||||
this.ignoreTables = new Set(ignoreTables || []);
|
||||
this.usableTables = new Set();
|
||||
this.sampleRowsInTableInfo = sampleRowsInTableInfo;
|
||||
this.indexesInTableInfo = indexesInTableInfo;
|
||||
this.customTableInfo = customTableInfo;
|
||||
this.maxStringLength = maxStringLength;
|
||||
}
|
||||
|
||||
get usableTableNames(): string[] {
|
||||
if (this.includeTables.size > 0) {
|
||||
return Array.from(this.includeTables);
|
||||
}
|
||||
return Array.from(this.allTables);
|
||||
}
|
||||
|
||||
async getTableColumns(tableName: string) {
|
||||
return await this.inspector(tableName).columnInfo();
|
||||
}
|
||||
|
||||
async getSingleTableInfo(tableName: string) {
|
||||
const columns = await this.getTableColumns(tableName);
|
||||
|
||||
const columnStr = Object.keys(columns)
|
||||
.map((column) => {
|
||||
return `${column} (${columns[column].type})`;
|
||||
})
|
||||
.join(", ");
|
||||
|
||||
return `Table '${tableName}' has columns: ${columnStr}.`;
|
||||
}
|
||||
|
||||
insertIntoTable(tableName: string, data: Record<string, any>): Promise<void> {
|
||||
return this.engine(tableName).insert(data);
|
||||
}
|
||||
|
||||
truncateWord(content: any, length: number, suffix = "..."): string {
|
||||
if (typeof content !== "string" || length <= 0) {
|
||||
return content;
|
||||
}
|
||||
|
||||
if (content.length <= length) {
|
||||
return content;
|
||||
}
|
||||
|
||||
return (
|
||||
content
|
||||
.slice(0, length - suffix.length - 1)
|
||||
.split(" ")
|
||||
.slice(0, -1)
|
||||
.join(" ") + suffix
|
||||
);
|
||||
}
|
||||
|
||||
async runSQL(
|
||||
command: string,
|
||||
): Promise<[string, { result: any[]; colKeys: string[] }]> {
|
||||
return this.engine.raw(command).then((result: any) => {
|
||||
if (result.length > 0) {
|
||||
const truncatedResults = result.map((row: any) =>
|
||||
this.truncateWord(row, this.maxStringLength),
|
||||
);
|
||||
return [
|
||||
JSON.stringify(truncatedResults),
|
||||
{ result: truncatedResults, colKeys: Object.keys(result[0]) },
|
||||
];
|
||||
}
|
||||
return ["", { result: [], colKeys: [] }];
|
||||
});
|
||||
}
|
||||
|
||||
async getTableInfo(tableName: string): Promise<string> {
|
||||
const columns = await this.getTableColumns(tableName);
|
||||
const columnStr = Object.keys(columns)
|
||||
.map((column: any) => {
|
||||
const comment = column.COMMENT ? `'${column.COMMENT}'` : "";
|
||||
return `${column.COLUMN_NAME} (${column.DATA_TYPE}): ${comment}`;
|
||||
})
|
||||
.join(", ");
|
||||
return `Table '${tableName}' has columns: ${columnStr}.`;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export * from "./SQLWrapper.js";
|
||||
@@ -1,71 +0,0 @@
|
||||
import {
|
||||
IndexDict,
|
||||
IndexList,
|
||||
IndexStruct,
|
||||
IndexStructType,
|
||||
MetadataMode,
|
||||
TextNode,
|
||||
jsonToIndexStruct,
|
||||
} from "llamaindex";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
describe("jsonToIndexStruct", () => {
|
||||
it("transforms json to IndexDict", () => {
|
||||
function isIndexDict(some: IndexStruct): some is IndexDict {
|
||||
return "type" in some && some.type === IndexStructType.SIMPLE_DICT;
|
||||
}
|
||||
|
||||
const node = new TextNode({ text: "text", id_: "nodeId" });
|
||||
const expected = new IndexDict();
|
||||
expected.addNode(node);
|
||||
|
||||
console.log("expected.toJson()", expected.toJson());
|
||||
const actual = jsonToIndexStruct(expected.toJson());
|
||||
|
||||
expect(isIndexDict(actual)).toBe(true);
|
||||
expect(
|
||||
(actual as IndexDict).nodesDict.nodeId.getContent(MetadataMode.NONE),
|
||||
).toEqual("text");
|
||||
});
|
||||
it("transforms json to IndexList", () => {
|
||||
function isIndexList(some: IndexStruct): some is IndexList {
|
||||
return "type" in some && some.type === IndexStructType.LIST;
|
||||
}
|
||||
|
||||
const node = new TextNode({ text: "text", id_: "nodeId" });
|
||||
const expected = new IndexList();
|
||||
expected.addNode(node);
|
||||
|
||||
const actual = jsonToIndexStruct(expected.toJson());
|
||||
|
||||
expect(isIndexList(actual)).toBe(true);
|
||||
expect((actual as IndexList).nodes[0]).toEqual("nodeId");
|
||||
});
|
||||
it("fails for unknown index type", () => {
|
||||
expect(() => {
|
||||
const json = {
|
||||
indexId: "dd120b16-8dce-4ce3-9bb6-15ca87fe4a1d",
|
||||
summary: undefined,
|
||||
nodesDict: {},
|
||||
type: "FOO",
|
||||
};
|
||||
return jsonToIndexStruct(json);
|
||||
}).toThrowError("Unknown index struct type: FOO");
|
||||
});
|
||||
it("fails for unknown node type", () => {
|
||||
expect(() => {
|
||||
const json = {
|
||||
indexId: "dd120b16-8dce-4ce3-9bb6-15ca87fe4a1d",
|
||||
summary: undefined,
|
||||
nodesDict: {
|
||||
nodeId: {
|
||||
...new TextNode({ text: "text", id_: "nodeId" }).toJSON(),
|
||||
type: "BAR",
|
||||
},
|
||||
},
|
||||
type: IndexStructType.SIMPLE_DICT,
|
||||
};
|
||||
return jsonToIndexStruct(json);
|
||||
}).toThrowError("Invalid node type: BAR");
|
||||
});
|
||||
});
|
||||
@@ -3,7 +3,7 @@
|
||||
"compilerOptions": {
|
||||
"rootDir": "./src",
|
||||
"outDir": "./dist/type",
|
||||
"tsBuildInfoFile": "./dist/.tsbuildinfo",
|
||||
"tsBuildInfoFile": ".tsbuildinfo",
|
||||
"emitDeclarationOnly": true,
|
||||
"module": "node16",
|
||||
"moduleResolution": "node16",
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
{
|
||||
"root": false,
|
||||
"rules": {
|
||||
"turbo/no-undeclared-env-vars": [
|
||||
"error",
|
||||
{
|
||||
"allowList": [
|
||||
"OPENAI_API_KEY",
|
||||
"LLAMA_CLOUD_API_KEY",
|
||||
"npm_config_user_agent",
|
||||
"http_proxy",
|
||||
"https_proxy",
|
||||
"MODEL",
|
||||
"NEXT_PUBLIC_CHAT_API",
|
||||
"NEXT_PUBLIC_MODEL"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -1,20 +1,5 @@
|
||||
# create-llama
|
||||
|
||||
## 0.0.28
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 89a49f4: Add more config variables to .env file
|
||||
- fdf48dd: Add "Start in VSCode" option to postInstallAction
|
||||
- fdf48dd: Add devcontainers to generated code
|
||||
|
||||
## 0.0.27
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 2d29350: Add LlamaParse option when selecting a pdf file or a folder (FastAPI only)
|
||||
- b354f23: Add embedding model option to create-llama (FastAPI only)
|
||||
|
||||
## 0.0.26
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -11,7 +11,6 @@ import fs from "fs";
|
||||
import terminalLink from "terminal-link";
|
||||
import type { InstallTemplateArgs } from "./helpers";
|
||||
import { installTemplate } from "./helpers";
|
||||
import { writeDevcontainer } from "./helpers/devcontainer";
|
||||
import { templatesDir } from "./helpers/dir";
|
||||
import { toolsRequireConfig } from "./helpers/tools";
|
||||
|
||||
@@ -35,7 +34,6 @@ export async function createApp({
|
||||
openAiKey,
|
||||
llamaCloudKey,
|
||||
model,
|
||||
embeddingModel,
|
||||
communityProjectPath,
|
||||
llamapack,
|
||||
vectorDb,
|
||||
@@ -82,7 +80,6 @@ export async function createApp({
|
||||
openAiKey,
|
||||
llamaCloudKey,
|
||||
model,
|
||||
embeddingModel,
|
||||
communityProjectPath,
|
||||
llamapack,
|
||||
vectorDb,
|
||||
@@ -113,7 +110,7 @@ export async function createApp({
|
||||
path.join(root, "README.md"),
|
||||
);
|
||||
} else {
|
||||
await installTemplate({ ...args, backend: true });
|
||||
await installTemplate({ ...args, backend: true, forBackend: framework });
|
||||
}
|
||||
|
||||
process.chdir(root);
|
||||
@@ -122,8 +119,6 @@ export async function createApp({
|
||||
console.log();
|
||||
}
|
||||
|
||||
await writeDevcontainer(root, templatesDir, framework, frontend);
|
||||
|
||||
if (toolsRequireConfig(tools)) {
|
||||
console.log(
|
||||
yellow(
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user