Compare commits

...

24 Commits

Author SHA1 Message Date
Marcus Schiesser 4a36de7397 docs: updated docs for retriever using param obj 2024-03-06 14:30:20 +07:00
Marcus Schiesser 1f1ee1eb8e refactor: Use parameter object for retrieve function of Retriever 2024-03-06 14:19:42 +07:00
Wojciech Grzebieniowski 484a7105a9 fix: restore missing exports (#610) 2024-03-05 14:56:25 -06:00
Alex Yang 8d18ea167b fix: publish.yml 2024-03-05 14:37:54 -06:00
Alex Yang a2ca89bfe0 fix: config (#611) 2024-03-05 14:20:58 -06:00
Alex Yang edeea40898 ci: add publish.yml 2024-03-05 13:49:49 -06:00
Alex Yang 2a7080b094 build: fix version 2024-03-05 12:26:47 -06:00
Huu Le (Lee) b354f2386b feat: add embedding model option to create-llama (#608) 2024-03-05 16:59:51 +07:00
Emanuel Ferreira d766bd03d2 feat: OpenAI Agent Stream (#597) 2024-03-05 15:46:44 +07:00
Huu Le (Lee) 6a69148356 fix: add --no-llama-parse and improve e2e test (#607) 2024-03-05 14:57:38 +07:00
Marcus Schiesser e1e1b0b522 RELEASING: Releasing 1 package(s)
Releases:
  llamaindex@0.1.21

[skip ci]
2024-03-05 13:00:16 +07:00
Marcus Schiesser d824876653 docs(changeset): Add support for Claude 3 2024-03-05 12:59:51 +07:00
Marcus Schiesser 2048698f77 docs: add interactive chat for anthropic 2024-03-05 11:15:50 +07:00
Emanuel Ferreira 9942979aa7 feat: Claude 3 (#604) 2024-03-04 15:02:18 -08:00
Alex Yang 3c2655a1f9 fix: .tsbuildinfo 2024-03-04 16:05:45 -06:00
Marcus Schiesser 552a61a66f Add quantized parameter to HuggingFaceEmbedding (#601) 2024-03-04 12:10:40 +07:00
Alex Yang d13143e322 RELEASING: Releasing 3 package(s)
Releases:
  llamaindex@0.1.20
  @llamaindex/env@0.0.5
  docs@0.0.4

[skip ci]
2024-03-02 18:42:24 -06:00
Alex Yang 5116ad8d08 fix: compatibility issue with Deno (#598) 2024-03-02 18:40:01 -06:00
Emanuel Ferreira 64683a55f3 fix: prefix messages always true (#596) 2024-03-01 21:45:02 -03:00
Emanuel Ferreira 698cd9c631 fix: step wise agent + examples (#594) 2024-03-01 21:28:02 -03:00
Alex Yang c744a99102 chore: bump @llamaindex/cloud (#595) 2024-03-01 17:22:50 -06:00
Huu Le (Lee) 2d2935085e feat: Add use LlamaParse option to create-llama (#591) 2024-03-01 16:54:06 +07:00
Marcus Schiesser 1b31e2c8cd chore: update @llamaindex/cloud to 0.0.2 2024-03-01 15:59:10 +07:00
Thuc Pham 7257751993 fix: empty store bugs (#592)
Co-authored-by: Marcus Schiesser <mail@marcusschiesser.de>
2024-03-01 15:04:10 +07:00
129 changed files with 1427 additions and 409 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"create-llama": patch
---
Add LlamaParse option when selecting a pdf file or a folder
+12
View File
@@ -0,0 +1,12 @@
---
"llamaindex": patch
"@llamaindex/core-test": patch
---
- Add missing exports:
- `IndexStructType`,
- `IndexDict`,
- `jsonToIndexStruct`,
- `IndexList`,
- `IndexStruct`
- Fix `IndexDict.toJson()` method
+5
View File
@@ -0,0 +1,5 @@
---
"llamaindex": patch
---
Add streaming to agents
+5
View File
@@ -0,0 +1,5 @@
---
"create-llama": patch
---
Add embedding model option to create-llama
+5
View File
@@ -0,0 +1,5 @@
---
"llamaindex": minor
---
Use parameter object for retrieve function of Retriever (to align usage with query function of QueryEngine)
+8
View File
@@ -11,5 +11,13 @@ module.exports = {
"max-params": ["error", 4],
"prefer-const": "error",
},
overrides: [
{
files: ["examples/**/*.ts"],
rules: {
"turbo/no-undeclared-env-vars": "off",
},
},
],
ignorePatterns: ["dist/", "lib/"],
};
+24
View File
@@ -0,0 +1,24 @@
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
- name: Publish @llamaindex/core
run: npx jsr publish --allow-slow-types
working-directory: packages/core
+1
View File
@@ -44,6 +44,7 @@ test-results/
playwright-report/
blob-report/
playwright/.cache/
.tsbuildinfo
# intellij
**/.idea
+7
View File
@@ -1,5 +1,12 @@
# docs
## 0.0.4
### Patch Changes
- Updated dependencies [5116ad8]
- @llamaindex/env@0.0.5
## 0.0.3
### Patch Changes
@@ -23,3 +23,15 @@ 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,
});
```
@@ -100,7 +100,7 @@ const response = await queryEngine.query("<user_query>");
```ts
import { SimilarityPostprocessor } from "llamaindex";
nodes = await index.asRetriever().retrieve("test query str");
nodes = await index.asRetriever().retrieve({ query: "test query str" });
const processor = new SimilarityPostprocessor({
similarityCutoff: 0.7,
+1 -1
View File
@@ -11,7 +11,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Fetch nodes!
const nodesWithScore = await retriever.retrieve("query string");
const nodesWithScore = await retriever.retrieve({ query: "query string" });
```
## API Reference
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// جلب العقد!
const nodesWithScore = await retriever.retrieve("سلسلة الاستعلام");
const nodesWithScore = await retriever.retrieve({ query: "سلسلة الاستعلام" });
```
## مرجع الواجهة البرمجية (API Reference)
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Извличане на върхове!
const nodesWithScore = await retriever.retrieve("query string");
const nodesWithScore = await retriever.retrieve({ query: "query string" });
```
## API Reference (API справка)
@@ -13,7 +13,7 @@ const recuperador = vector_index.asRetriever();
recuperador.similarityTopK = 3;
// Obteniu els nodes!
const nodesAmbPuntuació = await recuperador.retrieve("cadena de consulta");
const nodesAmbPuntuació = await recuperador.retrieve({ query: "cadena de consulta" });
```
## Referència de l'API
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Získání uzlů!
const nodesWithScore = await retriever.retrieve("dotazovací řetězec");
const nodesWithScore = await retriever.retrieve({ query: "dotazovací řetězec" });
```
## API Reference (Odkazy na rozhraní)
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Hent noder!
const nodesWithScore = await retriever.retrieve("forespørgselsstreng");
const nodesWithScore = await retriever.retrieve({ query: "forespørgselsstreng" });
```
## API Reference
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Knoten abrufen!
const nodesWithScore = await retriever.retrieve("Abfragezeichenfolge");
const nodesWithScore = await retriever.retrieve({ query: "Abfragezeichenfolge" });
```
## API-Referenz
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Ανάκτηση κόμβων!
const nodesWithScore = await retriever.retrieve("συμβολοσειρά ερωτήματος");
const nodesWithScore = await retriever.retrieve({ query: "συμβολοσειρά ερωτήματος" });
```
## Αναφορά API
@@ -13,7 +13,7 @@ const recuperador = vector_index.asRetriever();
recuperador.similarityTopK = 3;
// ¡Obtener nodos!
const nodosConPuntuación = await recuperador.retrieve("cadena de consulta");
const nodosConPuntuación = await recuperador.retrieve({ query: "cadena de consulta" });
```
## Referencia de la API
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Too sõlmed!
const nodesWithScore = await retriever.retrieve("päringu string");
const nodesWithScore = await retriever.retrieve({ query: "päringu string" });
```
## API viide
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// بازیابی گره ها!
const nodesWithScore = await retriever.retrieve("رشته پرس و جو");
const nodesWithScore = await retriever.retrieve({ query: "رشته پرس و جو" });
```
## مرجع API
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Hae solmut!
const nodesWithScore = await retriever.retrieve("kyselymerkkijono");
const nodesWithScore = await retriever.retrieve({ query: "kyselymerkkijono" });
```
## API-viite
@@ -11,7 +11,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Récupérer les nœuds !
const nodesWithScore = await retriever.retrieve("chaîne de requête");
const nodesWithScore = await retriever.retrieve({ query: "chaîne de requête" });
```
## Référence de l'API
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// אחזור צמתים!
const nodesWithScore = await retriever.retrieve("מחרוזת שאילתה");
const nodesWithScore = await retriever.retrieve({ query: "מחרוזת שאילתה" });
```
## מדריך לממשק API
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// नोड्स प्राप्त करें!
const nodesWithScore = await retriever.retrieve("क्वेरी स्ट्रिंग");
const nodesWithScore = await retriever.retrieve({ query: "क्वेरी स्ट्रिंग" });
```
## एपीआई संदर्भ (API Reference)
@@ -13,7 +13,7 @@ const dohvatnik = vector_index.asRetriever();
dohvatnik.similarityTopK = 3;
// Dohvati čvorove!
const čvoroviSaRezultatom = await dohvatnik.retrieve("upitni niz");
const čvoroviSaRezultatom = await dohvatnik.retrieve({ query: "upitni niz" });
```
## API Referenca
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Node-ok lekérése!
const nodesWithScore = await retriever.retrieve("lekérdezési karakterlánc");
const nodesWithScore = await retriever.retrieve({ query: "lekérdezési karakterlánc" });
```
## API Referencia
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Mengambil node!
const nodesWithScore = await retriever.retrieve("string query");
const nodesWithScore = await retriever.retrieve({ query: "string query" });
```
## Referensi API
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Recupera i nodi!
const nodesWithScore = await retriever.retrieve("stringa di query");
const nodesWithScore = await retriever.retrieve({ query: "stringa di query" });
```
## Riferimento API
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// ノードを取得します!
const nodesWithScore = await retriever.retrieve("クエリ文字列");
const nodesWithScore = await retriever.retrieve({ query: "クエリ文字列" });
```
## API リファレンス
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// 노드를 가져옵니다!
const nodesWithScore = await retriever.retrieve("쿼리 문자열");
const nodesWithScore = await retriever.retrieve({ query: "쿼리 문자열" });
```
## API 참조
@@ -13,7 +13,7 @@ const gavėjas = vector_index.asRetriever();
gavėjas.similarityTopK = 3;
// Išgaunami mazgai!
const mazgaiSuRezultatu = await gavėjas.retrieve("užklausos eilutė");
const mazgaiSuRezultatu = await gavėjas.retrieve({ query: "užklausos eilutė" });
```
## API nuorodos (API Reference)
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Haal knooppunten op!
const nodesWithScore = await retriever.retrieve("zoekopdracht");
const nodesWithScore = await retriever.retrieve({ query: "zoekopdracht" });
```
## API Referentie
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Hent noder!
const nodesWithScore = await retriever.retrieve("spørringsstreng");
const nodesWithScore = await retriever.retrieve({ query: "spørringsstreng" });
```
## API-referanse
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Pobierz węzły!
const nodesWithScore = await retriever.retrieve("ciąg zapytania");
const nodesWithScore = await retriever.retrieve({ query: "ciąg zapytania" });
```
## Dokumentacja interfejsu API
@@ -13,7 +13,7 @@ const recuperador = vector_index.asRetriever();
recuperador.similarityTopK = 3;
// Buscar nós!
const nósComPontuação = await recuperador.retrieve("string de consulta");
const nósComPontuação = await recuperador.retrieve({ query: "string de consulta" });
```
## Referência da API
@@ -13,7 +13,7 @@ const recuperator = vector_index.asRetriever();
recuperator.similarityTopK = 3;
// Preia nodurile!
const noduriCuScor = await recuperator.retrieve("șir de interogare");
const noduriCuScor = await recuperator.retrieve({ query: "șir de interogare" });
```
## Referință API
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Получение узлов!
const nodesWithScore = await retriever.retrieve("строка запроса");
const nodesWithScore = await retriever.retrieve({ query: "строка запроса" });
```
## Справочник по API
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Dohvati čvorove!
const nodesWithScore = await retriever.retrieve("upitni niz");
const nodesWithScore = await retriever.retrieve({ query: "upitni niz" });
```
## API Referenca
@@ -13,7 +13,7 @@ const pridobitelj = vector_index.asRetriever();
pridobitelj.similarityTopK = 3;
// Pridobivanje vozlišč!
const vozliščaZRezultatom = await pridobitelj.retrieve("poizvedbeni niz");
const vozliščaZRezultatom = await pridobitelj.retrieve({ query: "poizvedbeni niz" });
```
## API Sklic
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Získajte uzly!
const nodesWithScore = await retriever.retrieve("reťazec dotazu");
const nodesWithScore = await retriever.retrieve({ query: "reťazec dotazu" });
```
## API Referencia
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Hämta noder!
const nodesWithScore = await retriever.retrieve("frågesträng");
const nodesWithScore = await retriever.retrieve({ query: "frågesträng" });
```
## API-referens
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// เรียกคืนโหนด!
const nodesWithScore = await retriever.retrieve("query string");
const nodesWithScore = await retriever.retrieve({ query: "query string" });
```
## API Reference (การอ้างอิง API)
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Düğümleri getir!
const nodesWithScore = await retriever.retrieve("sorgu dizesi");
const nodesWithScore = await retriever.retrieve({ query: "sorgu dizesi" });
```
## API Referansı
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Отримати вузли!
const nodesWithScore = await retriever.retrieve("рядок запиту");
const nodesWithScore = await retriever.retrieve({ query: "рядок запиту" });
```
## Довідник API
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// Lấy các node!
const nodesWithScore = await retriever.retrieve("chuỗi truy vấn");
const nodesWithScore = await retriever.retrieve({ query: "chuỗi truy vấn" });
```
## Tài liệu tham khảo API
@@ -11,7 +11,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// 获取节点!
const nodesWithScore = await retriever.retrieve("查询字符串");
const nodesWithScore = await retriever.retrieve({ query: "查询字符串" });
```
## API 参考
@@ -13,7 +13,7 @@ const retriever = vector_index.asRetriever();
retriever.similarityTopK = 3;
// 提取節點!
const nodesWithScore = await retriever.retrieve("查詢字串");
const nodesWithScore = await retriever.retrieve({ query: "查詢字串" });
```
## API 參考
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "docs",
"version": "0.0.3",
"version": "0.0.4",
"private": true,
"scripts": {
"docusaurus": "docusaurus",
+95
View File
@@ -0,0 +1,95 @@
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 a to divide",
},
b: {
type: "number",
description: "The divisor b to divide by",
},
},
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: true,
});
// Create a task to sum and divide numbers
const task = agent.createTask("How much is 5 + 5? then divide by 2");
let count = 0;
while (true) {
const stepOutput = await agent.runStep(task.taskId);
console.log(`Runnning step ${count++}`);
console.log(`======== OUTPUT ==========`);
if (stepOutput.output.response) {
console.log(stepOutput.output.response);
} else {
console.log(stepOutput.output.sources);
}
console.log(`==========================`);
if (stepOutput.isLast) {
const finalResponse = await agent.finalizeResponse(
task.taskId,
stepOutput,
);
console.log({ finalResponse });
break;
}
}
}
main().then(() => {
console.log("Done");
});
+64
View File
@@ -0,0 +1,64 @@
import {
OpenAIAgent,
QueryEngineTool,
SimpleDirectoryReader,
VectorStoreIndex,
} from "llamaindex";
async function main() {
// Load the documents
const documents = await new SimpleDirectoryReader().loadData({
directoryPath: "node_modules/llamaindex/examples",
});
// Create a vector index from the documents
const vectorIndex = await VectorStoreIndex.fromDocuments(documents);
// Create a query engine from the vector index
const abramovQueryEngine = vectorIndex.asQueryEngine();
// Create a QueryEngineTool with the query engine
const queryEngineTool = new QueryEngineTool({
queryEngine: abramovQueryEngine,
metadata: {
name: "abramov_query_engine",
description: "A query engine for the Abramov documents",
},
});
// Create an OpenAIAgent with the function tools
const agent = new OpenAIAgent({
tools: [queryEngineTool],
verbose: true,
});
const task = agent.createTask("What was his salary?");
let count = 0;
while (true) {
const stepOutput = await agent.runStep(task.taskId);
console.log(`Runnning step ${count++}`);
console.log(`======== OUTPUT ==========`);
if (stepOutput.output.response) {
console.log(stepOutput.output.response);
} else {
console.log(stepOutput.output.sources);
}
console.log(`==========================`);
if (stepOutput.isLast) {
const finalResponse = await agent.finalizeResponse(
task.taskId,
stepOutput,
);
console.log({ finalResponse });
break;
}
}
}
main().then(() => {
console.log("Done");
});
+90
View File
@@ -0,0 +1,90 @@
import { FunctionTool, ReActAgent } 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 ReActAgent({
tools: [functionTool, functionTool2],
verbose: true,
});
const task = agent.createTask("Divide 16 by 2 then add 20");
let count = 0;
while (true) {
const stepOutput = await agent.runStep(task.taskId);
console.log(`Runnning step ${count++}`);
console.log(`======== OUTPUT ==========`);
console.log(stepOutput.output);
console.log(`==========================`);
if (stepOutput.isLast) {
const finalResponse = await agent.finalizeResponse(
task.taskId,
stepOutput,
);
console.log({ finalResponse });
break;
}
}
}
main().then(() => {
console.log("Done");
});
+77
View File
@@ -0,0 +1,77 @@
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,6 +3,7 @@ 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: [
+34
View File
@@ -0,0 +1,34 @@
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");
}
})();
+23
View File
@@ -0,0 +1,23 @@
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);
}
})();
+3 -3
View File
@@ -27,9 +27,9 @@ async function main() {
// retrieve documents using the index
const index = await createIndex();
const retriever = index.asRetriever({ similarityTopK: 3 });
const results = await retriever.retrieve(
"what are Vincent van Gogh's famous paintings",
);
const results = await retriever.retrieve({
query: "what are Vincent van Gogh's famous paintings",
});
for (const result of results) {
const node = result.node;
if (!node) {
+18
View File
@@ -1,5 +1,23 @@
# llamaindex
## 0.1.21
### Patch Changes
- 552a61a: Add quantized parameter to HuggingFaceEmbedding
- d824876: Add support for Claude 3
## 0.1.20
### Patch Changes
- 64683a5: fix: prefix messages always true
- 698cd9c: fix: step wise agent + examples
- 7257751: fixed removeRefDocNode and persist store on delete
- 5116ad8: fix: compatibility issue with Deno
- Updated dependencies [5116ad8]
- @llamaindex/env@0.0.5
## 0.1.19
### Patch Changes
+8
View File
@@ -0,0 +1,8 @@
{
"name": "@llamaindex/core",
"version": "0.1.21",
"exports": "./src/index.ts",
"imports": {
"@llamaindex/env": "jsr:@llamaindex/env@0.0.5"
}
}
+7 -7
View File
@@ -1,22 +1,22 @@
{
"name": "llamaindex",
"version": "0.1.19",
"version": "0.1.21",
"license": "MIT",
"type": "module",
"dependencies": {
"@anthropic-ai/sdk": "^0.13.0",
"@anthropic-ai/sdk": "^0.15.0",
"@aws-crypto/sha256-js": "^5.2.0",
"@datastax/astra-db-ts": "^0.1.4",
"@types/lodash": "^4.14.202",
"@types/node": "^18.19.14",
"@types/papaparse": "^5.3.14",
"@types/pg": "^8.11.0",
"@llamaindex/cloud": "^0.0.1",
"@llamaindex/cloud": "0.0.4",
"@llamaindex/env": "workspace:*",
"@mistralai/mistralai": "^0.0.10",
"@notionhq/client": "^2.2.14",
"@pinecone-database/pinecone": "^2.0.1",
"@qdrant/js-client-rest": "^1.7.0",
"@types/lodash": "^4.14.202",
"@types/node": "^18.19.14",
"@types/papaparse": "^5.3.14",
"@types/pg": "^8.11.0",
"@xenova/transformers": "^2.15.0",
"assemblyai": "^4.2.2",
"chromadb": "~1.7.3",
+7 -5
View File
@@ -2,14 +2,16 @@ import type { Event } from "./callbacks/CallbackManager.js";
import type { NodeWithScore } from "./Node.js";
import type { ServiceContext } from "./ServiceContext.js";
export type RetrieveParams = {
query: string;
parentEvent?: Event;
preFilters?: unknown;
};
/**
* Retrievers retrieve the nodes that most closely match our query in similarity.
*/
export interface BaseRetriever {
retrieve(
query: string,
parentEvent?: Event,
preFilters?: unknown,
): Promise<NodeWithScore[]>;
retrieve(params: RetrieveParams): Promise<NodeWithScore[]>;
getServiceContext(): ServiceContext;
}
-2
View File
@@ -37,8 +37,6 @@ export class OpenAIAgent extends AgentRunner {
toolRetriever,
systemPrompt,
}: OpenAIAgentParams) {
prefixMessages = prefixMessages || [];
llm = llm ?? new OpenAI({ model: "gpt-3.5-turbo-0613" });
if (systemPrompt) {
+36 -4
View File
@@ -1,10 +1,12 @@
// 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,6 +14,7 @@ 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";
@@ -192,13 +195,40 @@ export class OpenAIAgentWorker implements AgentWorker {
private _processMessage(
task: Task,
chatResponse: ChatResponse,
): AgentChatResponse | AsyncIterable<ChatResponseChunk> {
): AgentChatResponse {
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
@@ -210,7 +240,7 @@ export class OpenAIAgentWorker implements AgentWorker {
task: Task,
mode: ChatResponseMode,
llmChatKwargs: any,
): Promise<AgentChatResponse> {
): Promise<AgentChatResponse | StreamingAgentChatResponse> {
if (mode === ChatResponseMode.WAIT) {
const chatResponse = (await this.llm.chat({
stream: false,
@@ -218,9 +248,11 @@ export class OpenAIAgentWorker implements AgentWorker {
})) as unknown as ChatResponse;
return this._processMessage(task, chatResponse) as AgentChatResponse;
} else {
throw new Error("Not implemented");
} else if (mode === ChatResponseMode.STREAM) {
return this._getStreamAiResponse(task, llmChatKwargs);
}
throw new Error("Invalid mode");
}
/**
+54 -18
View File
@@ -4,6 +4,7 @@ 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";
@@ -14,7 +15,7 @@ import { AgentState, BaseAgentRunner, TaskState } from "./types.js";
const validateStepFromArgs = (
taskId: string,
input: string,
input?: string | null,
step?: any,
kwargs?: any,
): TaskStep | undefined => {
@@ -24,6 +25,7 @@ const validateStepFromArgs = (
}
return step;
} else {
if (!input) return;
return new TaskStep(taskId, step, input, kwargs);
}
};
@@ -194,7 +196,7 @@ export class AgentRunner extends BaseAgentRunner {
*/
async runStep(
taskId: string,
input: string,
input?: string | null,
step?: TaskStep,
kwargs: any = {},
): Promise<TaskStepOutput> {
@@ -230,23 +232,26 @@ export class AgentRunner extends BaseAgentRunner {
taskId: string,
stepOutput: TaskStepOutput,
kwargs?: any,
): Promise<AgentChatResponse> {
): Promise<AgentChatResponse | StreamingAgentChatResponse> {
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 AgentChatResponse)) {
throw new Error(
`When \`isLast\` is True, cur_step_output.output must be AGENT_CHAT_RESPONSE_TYPE: ${stepOutput.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}`,
);
}
}
this.agentWorker.finalizeTask(this.getTask(taskId), kwargs);
@@ -261,20 +266,32 @@ export class AgentRunner extends BaseAgentRunner {
protected async _chat({
message,
toolChoice,
}: ChatEngineAgentParams & { mode: ChatResponseMode }) {
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
> {
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,
ChatResponseMode.WAIT,
{
toolChoice,
},
);
const curStepOutput = await this._runStep(task.taskId, undefined, mode, {
toolChoice,
});
if (curStepOutput.isLast) {
resultOutput = curStepOutput;
@@ -298,7 +315,26 @@ export class AgentRunner extends BaseAgentRunner {
message,
chatHistory,
toolChoice,
}: ChatEngineAgentParams): Promise<AgentChatResponse> {
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
> {
if (!toolChoice) {
toolChoice = this.defaultToolChoice;
}
@@ -307,7 +343,7 @@ export class AgentRunner extends BaseAgentRunner {
message,
chatHistory,
toolChoice,
mode: ChatResponseMode.WAIT,
stream,
});
return chatResponse;
+5 -2
View File
@@ -1,4 +1,7 @@
import type { AgentChatResponse } from "../../engines/chat/index.js";
import type {
AgentChatResponse,
StreamingAgentChatResponse,
} from "../../engines/chat/index.js";
import type { Task, TaskStep, TaskStepOutput } from "../types.js";
import { BaseAgent } from "../types.js";
@@ -57,7 +60,7 @@ export abstract class BaseAgentRunner extends BaseAgent {
taskId: string,
stepOutput: TaskStepOutput,
kwargs?: any,
): Promise<AgentChatResponse>;
): Promise<AgentChatResponse | StreamingAgentChatResponse>;
abstract undoStep(taskId: string): void;
}
+14 -6
View File
@@ -1,6 +1,7 @@
import type {
AgentChatResponse,
ChatEngineAgentParams,
StreamingAgentChatResponse,
} from "../engines/chat/index.js";
import type { QueryEngineParamsNonStreaming } from "../types.js";
@@ -12,11 +13,15 @@ export interface AgentWorker {
}
interface BaseChatEngine {
chat(params: ChatEngineAgentParams): Promise<AgentChatResponse>;
chat(
params: ChatEngineAgentParams,
): Promise<AgentChatResponse | StreamingAgentChatResponse>;
}
interface BaseQueryEngine {
query(params: QueryEngineParamsNonStreaming): Promise<AgentChatResponse>;
query(
params: QueryEngineParamsNonStreaming,
): Promise<AgentChatResponse | StreamingAgentChatResponse>;
}
/**
@@ -31,7 +36,10 @@ export abstract class BaseAgent implements BaseChatEngine, BaseQueryEngine {
return [];
}
abstract chat(params: ChatEngineAgentParams): Promise<AgentChatResponse>;
abstract chat(
params: ChatEngineAgentParams,
): Promise<AgentChatResponse | StreamingAgentChatResponse>;
abstract reset(): void;
/**
@@ -41,7 +49,7 @@ export abstract class BaseAgent implements BaseChatEngine, BaseQueryEngine {
*/
async query(
params: QueryEngineParamsNonStreaming,
): Promise<AgentChatResponse> {
): Promise<AgentChatResponse | StreamingAgentChatResponse> {
// Handle non-streaming query
const agentResponse = await this.chat({
message: params.query,
@@ -161,13 +169,13 @@ export class TaskStep implements ITaskStep {
* @param isLast: isLast
*/
export class TaskStepOutput {
output: unknown;
output: any;
taskStep: TaskStep;
nextSteps: TaskStep[];
isLast: boolean;
constructor(
output: unknown,
output: any,
taskStep: TaskStep,
nextSteps: TaskStep[],
isLast: boolean = false,
+3 -3
View File
@@ -3,7 +3,7 @@ import { RetrieverQueryEngine } from "../engines/query/RetrieverQueryEngine.js";
import type { BaseNodePostprocessor } from "../postprocessors/types.js";
import type { BaseSynthesizer } from "../synthesizers/types.js";
import type { BaseQueryEngine } from "../types.js";
import type { RetrieveParams } from "./LlamaCloudRetriever.js";
import type { CloudRetrieveParams } from "./LlamaCloudRetriever.js";
import { LlamaCloudRetriever } from "./LlamaCloudRetriever.js";
import type { CloudConstructorParams } from "./types.js";
@@ -14,7 +14,7 @@ export class LlamaCloudIndex {
this.params = params;
}
asRetriever(params: RetrieveParams = {}): BaseRetriever {
asRetriever(params: CloudRetrieveParams = {}): BaseRetriever {
return new LlamaCloudRetriever({ ...this.params, ...params });
}
@@ -23,7 +23,7 @@ export class LlamaCloudIndex {
responseSynthesizer?: BaseSynthesizer;
preFilters?: unknown;
nodePostprocessors?: BaseNodePostprocessor[];
} & RetrieveParams,
} & CloudRetrieveParams,
): BaseQueryEngine {
const retriever = new LlamaCloudRetriever({
...this.params,
+9 -10
View File
@@ -2,15 +2,14 @@ import type { PlatformApi, PlatformApiClient } from "@llamaindex/cloud";
import { globalsHelper } from "../GlobalsHelper.js";
import type { NodeWithScore } from "../Node.js";
import { ObjectType, jsonToNode } from "../Node.js";
import type { BaseRetriever } from "../Retriever.js";
import type { BaseRetriever, RetrieveParams } from "../Retriever.js";
import type { ServiceContext } from "../ServiceContext.js";
import { serviceContextFromDefaults } from "../ServiceContext.js";
import type { Event } from "../callbacks/CallbackManager.js";
import type { ClientParams, CloudConstructorParams } from "./types.js";
import { DEFAULT_PROJECT_NAME } from "./types.js";
import { getClient } from "./utils.js";
export type RetrieveParams = Omit<
export type CloudRetrieveParams = Omit<
PlatformApi.RetrievalParams,
"query" | "searchFilters" | "pipelineId" | "className"
> & { similarityTopK?: number };
@@ -18,7 +17,7 @@ export type RetrieveParams = Omit<
export class LlamaCloudRetriever implements BaseRetriever {
client?: PlatformApiClient;
clientParams: ClientParams;
retrieveParams: RetrieveParams;
retrieveParams: CloudRetrieveParams;
projectName: string = DEFAULT_PROJECT_NAME;
pipelineName: string;
serviceContext: ServiceContext;
@@ -35,7 +34,7 @@ export class LlamaCloudRetriever implements BaseRetriever {
});
}
constructor(params: CloudConstructorParams & RetrieveParams) {
constructor(params: CloudConstructorParams & CloudRetrieveParams) {
this.clientParams = { apiKey: params.apiKey, baseUrl: params.baseUrl };
if (params.similarityTopK) {
params.denseSimilarityTopK = params.similarityTopK;
@@ -55,11 +54,11 @@ export class LlamaCloudRetriever implements BaseRetriever {
return this.client;
}
async retrieve(
query: string,
parentEvent?: Event | undefined,
preFilters?: unknown,
): Promise<NodeWithScore[]> {
async retrieve({
query,
parentEvent,
preFilters,
}: RetrieveParams): Promise<NodeWithScore[]> {
const pipelines = await (
await this.getClient()
).pipeline.searchPipelines({
+3 -2
View File
@@ -1,4 +1,5 @@
import type { PlatformApiClient } from "@llamaindex/cloud";
import { getEnv } from "@llamaindex/env";
import type { ClientParams } from "./types.js";
import { DEFAULT_BASE_URL } from "./types.js";
@@ -7,8 +8,8 @@ export async function getClient({
baseUrl,
}: ClientParams = {}): Promise<PlatformApiClient> {
// Get the environment variables or use defaults
baseUrl = baseUrl ?? process.env.LLAMA_CLOUD_BASE_URL ?? DEFAULT_BASE_URL;
apiKey = apiKey ?? process.env.LLAMA_CLOUD_API_KEY;
baseUrl = baseUrl ?? getEnv("LLAMA_CLOUD_BASE_URL") ?? DEFAULT_BASE_URL;
apiKey = apiKey ?? getEnv("LLAMA_CLOUD_API_KEY");
const { PlatformApiClient } = await import("@llamaindex/cloud");
@@ -20,6 +20,7 @@ export enum HuggingFaceEmbeddingModelType {
*/
export class HuggingFaceEmbedding extends BaseEmbedding {
modelType: string = HuggingFaceEmbeddingModelType.XENOVA_ALL_MINILM_L6_V2;
quantized: boolean = true;
private extractor: any;
@@ -31,7 +32,9 @@ export class HuggingFaceEmbedding extends BaseEmbedding {
async getExtractor() {
if (!this.extractor) {
const { pipeline } = await import("@xenova/transformers");
this.extractor = await pipeline("feature-extraction", this.modelType);
this.extractor = await pipeline("feature-extraction", this.modelType, {
quantized: this.quantized,
});
}
return this.extractor;
}
+2 -1
View File
@@ -1,9 +1,10 @@
import { getEnv } from "@llamaindex/env";
import { OpenAIEmbedding } from "./OpenAIEmbedding.js";
export class FireworksEmbedding extends OpenAIEmbedding {
constructor(init?: Partial<OpenAIEmbedding>) {
const {
apiKey = process.env.FIREWORKS_API_KEY,
apiKey = getEnv("FIREWORKS_API_KEY"),
additionalSessionOptions = {},
model = "nomic-ai/nomic-embed-text-v1.5",
...rest
+2 -1
View File
@@ -1,9 +1,10 @@
import { getEnv } from "@llamaindex/env";
import { OpenAIEmbedding } from "./OpenAIEmbedding.js";
export class TogetherEmbedding extends OpenAIEmbedding {
constructor(init?: Partial<OpenAIEmbedding>) {
const {
apiKey = process.env.TOGETHER_API_KEY,
apiKey = getEnv("TOGETHER_API_KEY"),
additionalSessionOptions = {},
model = "togethercomputer/m2-bert-80M-32k-retrieval",
...rest
@@ -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,
+18
View File
@@ -27,6 +27,7 @@ export interface ChatEngineParamsNonStreaming extends ChatEngineParamsBase {
export interface ChatEngineAgentParams extends ChatEngineParamsBase {
toolChoice?: string | Record<string, any>;
stream?: boolean;
}
/**
@@ -86,3 +87,20 @@ 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,
this.preFilters,
);
preFilters: this.preFilters,
});
return await this.applyNodePostprocessors(nodes, query);
}
+2
View File
@@ -1,4 +1,6 @@
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,9 +24,15 @@ 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: this.nodesDict,
nodesDict,
type: this.type,
};
}
+2 -2
View File
@@ -8,7 +8,7 @@ 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";
@@ -79,7 +79,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) =>
+9 -4
View File
@@ -3,10 +3,9 @@ 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 {
@@ -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 -8
View File
@@ -11,7 +11,7 @@ import {
ObjectType,
splitNodesByType,
} from "../../Node.js";
import type { BaseRetriever } from "../../Retriever.js";
import type { BaseRetriever, RetrieveParams } from "../../Retriever.js";
import type { ServiceContext } from "../../ServiceContext.js";
import { serviceContextFromDefaults } from "../../ServiceContext.js";
import type { Event } from "../../callbacks/CallbackManager.js";
@@ -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;
+82 -48
View File
@@ -1,7 +1,6 @@
import type OpenAILLM from "openai";
import type { ClientOptions as OpenAIClientOptions } from "openai";
import type {
AnthropicStreamToken,
CallbackManager,
Event,
EventType,
@@ -13,11 +12,7 @@ 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 {
ANTHROPIC_AI_PROMPT,
ANTHROPIC_HUMAN_PROMPT,
getAnthropicSession,
} from "./anthropic.js";
import { getAnthropicSession } from "./anthropic.js";
import type { AzureOpenAIConfig } from "./azure.js";
import {
getAzureBaseUrl,
@@ -260,7 +255,7 @@ export class OpenAI extends BaseLLM {
stream: false,
});
const content = response.choices[0].message?.content ?? "";
const content = response.choices[0].message?.content ?? null;
const kwargsOutput: Record<string, any> = {};
@@ -613,12 +608,30 @@ If a question does not make any sense, or is not factually coherent, explain why
}
}
export const ALL_AVAILABLE_ANTHROPIC_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 },
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,
};
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
*/
@@ -640,7 +653,7 @@ export class Anthropic extends BaseLLM {
constructor(init?: Partial<Anthropic>) {
super();
this.model = init?.model ?? "claude-2";
this.model = init?.model ?? "claude-3-opus";
this.temperature = init?.temperature ?? 0.1;
this.topP = init?.topP ?? 0.999; // Per Ben Mann
this.maxTokens = init?.maxTokens ?? undefined;
@@ -674,21 +687,24 @@ export class Anthropic extends BaseLLM {
};
}
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
);
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,
};
});
}
chat(
@@ -698,49 +714,67 @@ export class Anthropic extends BaseLLM {
async chat(
params: LLMChatParamsNonStreaming | LLMChatParamsStreaming,
): Promise<ChatResponse | AsyncIterable<ChatResponseChunk>> {
const { messages, parentEvent, stream } = params;
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");
}
//Streaming
if (stream) {
return this.streamChat(messages, parentEvent);
return this.streamChat(messages, parentEvent, systemPrompt);
}
//Non-streaming
const response = await this.session.anthropic.completions.create({
model: this.model,
prompt: this.mapMessagesToPrompt(messages),
max_tokens_to_sample: this.maxTokens ?? 100000,
const response = 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,
...(systemPrompt && { system: systemPrompt }),
});
return {
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.
message: { content: response.content[0].text, role: "assistant" },
};
}
protected async *streamChat(
messages: ChatMessage[],
parentEvent?: Event | undefined,
systemPrompt?: string | null,
): AsyncIterable<ChatResponseChunk> {
// 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,
});
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 }),
});
let idx_counter: number = 0;
for await (const part of stream) {
//TODO: LLM Stream Callback, pending re-work.
const content =
part.type === "content_block_delta" ? part.delta.text : null;
if (typeof content !== "string") continue;
idx_counter++;
yield { delta: part.completion };
yield { delta: content };
}
return;
}
+2 -3
View File
@@ -1,5 +1,6 @@
import type { ClientOptions } from "@anthropic-ai/sdk";
import Anthropic, { AI_PROMPT, HUMAN_PROMPT } from "@anthropic-ai/sdk";
import { getEnv } from "@llamaindex/env";
import _ from "lodash";
export class AnthropicSession {
@@ -7,9 +8,7 @@ export class AnthropicSession {
constructor(options: ClientOptions = {}) {
if (!options.apiKey) {
if (typeof process !== undefined) {
options.apiKey = process.env.ANTHROPIC_API_KEY;
}
options.apiKey = getEnv("ANTHROPIC_API_KEY");
}
if (!options.apiKey) {
+16 -14
View File
@@ -1,3 +1,5 @@
import { getEnv } from "@llamaindex/env";
export interface AzureOpenAIConfig {
apiKey?: string;
endpoint?: string;
@@ -67,24 +69,24 @@ export function getAzureConfigFromEnv(
return {
apiKey:
init?.apiKey ??
process.env.AZURE_OPENAI_KEY ?? // From Azure docs
process.env.OPENAI_API_KEY ?? // Python compatible
process.env.AZURE_OPENAI_API_KEY, // LCJS compatible
getEnv("AZURE_OPENAI_KEY") ?? // From Azure docs
getEnv("OPENAI_API_KEY") ?? // Python compatible
getEnv("AZURE_OPENAI_API_KEY"), // LCJS compatible
endpoint:
init?.endpoint ??
process.env.AZURE_OPENAI_ENDPOINT ?? // From Azure docs
process.env.OPENAI_API_BASE ?? // Python compatible
process.env.AZURE_OPENAI_API_INSTANCE_NAME, // LCJS compatible
getEnv("AZURE_OPENAI_ENDPOINT") ?? // From Azure docs
getEnv("OPENAI_API_BASE") ?? // Python compatible
getEnv("AZURE_OPENAI_API_INSTANCE_NAME"), // LCJS compatible
apiVersion:
init?.apiVersion ??
process.env.AZURE_OPENAI_API_VERSION ?? // From Azure docs
process.env.OPENAI_API_VERSION ?? // Python compatible
process.env.AZURE_OPENAI_API_VERSION ?? // LCJS compatible
getEnv("AZURE_OPENAI_API_VERSION") ?? // From Azure docs
getEnv("OPENAI_API_VERSION") ?? // Python compatible
getEnv("AZURE_OPENAI_API_VERSION") ?? // LCJS compatible
DEFAULT_API_VERSION,
deploymentName:
init?.deploymentName ??
process.env.AZURE_OPENAI_DEPLOYMENT ?? // From Azure docs
process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME ?? // LCJS compatible
getEnv("AZURE_OPENAI_DEPLOYMENT") ?? // From Azure docs
getEnv("AZURE_OPENAI_API_DEPLOYMENT_NAME") ?? // LCJS compatible
init?.model, // Fall back to model name, Python compatible
};
}
@@ -113,8 +115,8 @@ export function getAzureModel(openAIModel: string) {
export function shouldUseAzure() {
return (
process.env.AZURE_OPENAI_ENDPOINT ||
process.env.AZURE_OPENAI_API_INSTANCE_NAME ||
process.env.OPENAI_API_TYPE === "azure"
getEnv("AZURE_OPENAI_ENDPOINT") ||
getEnv("AZURE_OPENAI_API_INSTANCE_NAME") ||
getEnv("OPENAI_API_TYPE") === "azure"
);
}
+2 -1
View File
@@ -1,9 +1,10 @@
import { getEnv } from "@llamaindex/env";
import { OpenAI } from "./LLM.js";
export class FireworksLLM extends OpenAI {
constructor(init?: Partial<OpenAI>) {
const {
apiKey = process.env.FIREWORKS_API_KEY,
apiKey = getEnv("FIREWORKS_API_KEY"),
additionalSessionOptions = {},
model = "accounts/fireworks/models/mixtral-8x7b-instruct",
...rest
+2 -1
View File
@@ -1,9 +1,10 @@
import { getEnv } from "@llamaindex/env";
import { OpenAI } from "./LLM.js";
export class Groq extends OpenAI {
constructor(init?: Partial<OpenAI>) {
const {
apiKey = process.env.GROQ_API_KEY,
apiKey = getEnv("GROQ_API_KEY"),
additionalSessionOptions = {},
model = "mixtral-8x7b-32768",
...rest
+2 -3
View File
@@ -1,3 +1,4 @@
import { getEnv } from "@llamaindex/env";
import type {
CallbackManager,
Event,
@@ -27,9 +28,7 @@ export class MistralAISession {
if (init?.apiKey) {
this.apiKey = init?.apiKey;
} else {
if (typeof process !== undefined) {
this.apiKey = process.env.MISTRAL_API_KEY;
}
this.apiKey = getEnv("MISTRAL_API_KEY");
}
if (!this.apiKey) {
throw new Error("Set Mistral API key in MISTRAL_API_KEY env variable"); // Overriding MistralAI package's error message
+2 -3
View File
@@ -1,3 +1,4 @@
import { getEnv } from "@llamaindex/env";
import _ from "lodash";
import type { ClientOptions } from "openai";
import OpenAI from "openai";
@@ -13,9 +14,7 @@ export class OpenAISession {
constructor(options: ClientOptions & { azure?: boolean } = {}) {
if (!options.apiKey) {
if (typeof process !== undefined) {
options.apiKey = process.env.OPENAI_API_KEY;
}
options.apiKey = getEnv("OPENAI_API_KEY");
}
if (!options.apiKey) {
+3 -12
View File
@@ -1,17 +1,8 @@
import { getEnv } from "@llamaindex/env";
import _ from "lodash";
import type { LLMOptions } from "portkey-ai";
import { Portkey } from "portkey-ai";
export const readEnv = (
env: string,
default_val?: string,
): string | undefined => {
if (typeof process !== "undefined") {
return process.env?.[env] ?? default_val;
}
return default_val;
};
interface PortkeyOptions {
apiKey?: string;
baseURL?: string;
@@ -24,11 +15,11 @@ export class PortkeySession {
constructor(options: PortkeyOptions = {}) {
if (!options.apiKey) {
options.apiKey = readEnv("PORTKEY_API_KEY");
options.apiKey = getEnv("PORTKEY_API_KEY");
}
if (!options.baseURL) {
options.baseURL = readEnv("PORTKEY_BASE_URL", "https://api.portkey.ai");
options.baseURL = getEnv("PORTKEY_BASE_URL") ?? "https://api.portkey.ai";
}
this.portkey = new Portkey({});
+3 -2
View File
@@ -1,3 +1,4 @@
import { getEnv } from "@llamaindex/env";
import Replicate from "replicate";
export class ReplicateSession {
@@ -7,8 +8,8 @@ export class ReplicateSession {
constructor(replicateKey: string | null = null) {
if (replicateKey) {
this.replicateKey = replicateKey;
} else if (process.env.REPLICATE_API_TOKEN) {
this.replicateKey = process.env.REPLICATE_API_TOKEN;
} else if (getEnv("REPLICATE_API_TOKEN")) {
this.replicateKey = getEnv("REPLICATE_API_TOKEN") as string;
} else {
throw new Error(
"Set Replicate token in REPLICATE_API_TOKEN env variable",
+2 -1
View File
@@ -1,9 +1,10 @@
import { getEnv } from "@llamaindex/env";
import { OpenAI } from "./LLM.js";
export class TogetherLLM extends OpenAI {
constructor(init?: Partial<OpenAI>) {
const {
apiKey = process.env.TOGETHER_API_KEY,
apiKey = getEnv("TOGETHER_API_KEY"),
additionalSessionOptions = {},
model = "togethercomputer/llama-2-7b-chat",
...rest
+1 -1
View File
@@ -69,7 +69,7 @@ export class ObjectRetriever {
// Translating the retrieve method
async retrieve(strOrQueryBundle: QueryType): Promise<any> {
const nodes = await this.retriever.retrieve(strOrQueryBundle);
const nodes = await this.retriever.retrieve({ query: strOrQueryBundle });
const objs = nodes.map((n) => this._objectNodeMapping.fromNode(n.node));
return objs;
}
@@ -1,3 +1,4 @@
import { getEnv } from "@llamaindex/env";
import type {
BaseServiceParams,
SubtitleFormat,
@@ -28,7 +29,7 @@ abstract class AssemblyAIReader implements BaseReader {
options = {};
}
if (!options.apiKey) {
options.apiKey = process.env.ASSEMBLYAI_API_KEY;
options.apiKey = getEnv("ASSEMBLYAI_API_KEY");
}
if (!options.apiKey) {
throw new Error(
@@ -1,5 +1,4 @@
import type { GenericFileSystem } from "@llamaindex/env";
import { defaultFS } from "@llamaindex/env";
import { defaultFS, getEnv, type GenericFileSystem } from "@llamaindex/env";
import { Document } from "../Node.js";
import type { FileReader } from "./type.js";
@@ -24,7 +23,7 @@ export class LlamaParseReader implements FileReader {
constructor(params: Partial<LlamaParseReader> = {}) {
Object.assign(this, params);
params.apiKey = params.apiKey ?? process.env.LLAMA_CLOUD_API_KEY;
params.apiKey = params.apiKey ?? getEnv("LLAMA_CLOUD_API_KEY");
if (!params.apiKey) {
throw new Error(
"API Key is required for LlamaParseReader. Please pass the apiKey parameter or set the LLAMA_CLOUD_API_KEY environment variable.",
@@ -1,4 +1,4 @@
import _, * as lodash from "lodash";
import _ from "lodash";
import type { BaseNode } from "../../Node.js";
import { ObjectType } from "../../Node.js";
import { DEFAULT_NAMESPACE } from "../constants.js";
@@ -123,10 +123,10 @@ export class KVDocumentStore extends BaseDocumentStore {
const refDocInfo = await this.kvstore.get(refDocId, this.refDocCollection);
if (!_.isNil(refDocInfo)) {
lodash.pull(refDocInfo.docIds, docId);
!_.pull(refDocInfo.nodeIds, docId);
if (refDocInfo.docIds.length > 0) {
this.kvstore.put(refDocId, refDocInfo.toDict(), this.refDocCollection);
if (refDocInfo.nodeIds.length > 0) {
this.kvstore.put(refDocId, refDocInfo, this.refDocCollection);
}
this.kvstore.delete(refDocId, this.metadataCollection);
}
@@ -54,6 +54,9 @@ export class SimpleKVStore extends BaseKVStore {
): Promise<boolean> {
if (key in this.data[collection]) {
delete this.data[collection][key];
if (this.persistPath) {
await this.persist(this.persistPath, this.fs);
}
return true;
}
return false;
@@ -1,5 +1,6 @@
import { AstraDB } from "@datastax/astra-db-ts";
import type { Collection } from "@datastax/astra-db-ts/dist/collections";
import { getEnv } from "@llamaindex/env";
import type { BaseNode } from "../../Node.js";
import { MetadataMode } from "../../Node.js";
import type {
@@ -34,9 +35,8 @@ export class AstraDBVectorStore implements VectorStore {
if (init?.astraDBClient) {
this.astraDBClient = init.astraDBClient;
} else {
const token =
init?.params?.token ?? process.env.ASTRA_DB_APPLICATION_TOKEN;
const endpoint = init?.params?.endpoint ?? process.env.ASTRA_DB_ENDPOINT;
const token = init?.params?.token ?? getEnv("ASTRA_DB_APPLICATION_TOKEN");
const endpoint = init?.params?.endpoint ?? getEnv("ASTRA_DB_ENDPOINT");
if (!token) {
throw new Error(
@@ -48,7 +48,7 @@ export class AstraDBVectorStore implements VectorStore {
}
const namespace =
init?.params?.namespace ??
process.env.ASTRA_DB_NAMESPACE ??
getEnv("ASTRA_DB_NAMESPACE") ??
"default_keyspace";
this.astraDBClient = new AstraDB(token, endpoint, namespace);
}
@@ -1,3 +1,4 @@
import { getEnv } from "@llamaindex/env";
import type { BulkWriteOptions, Collection } from "mongodb";
import { MongoClient } from "mongodb";
import type { BaseNode } from "../../Node.js";
@@ -44,7 +45,7 @@ export class MongoDBAtlasVectorSearch implements VectorStore {
if (init.mongodbClient) {
this.mongodbClient = init.mongodbClient;
} else {
const mongoUri = process.env.MONGODB_URI;
const mongoUri = getEnv("MONGODB_URI");
if (!mongoUri) {
throw new Error(
"Must specify MONGODB_URI via env variable if not directly passing in client.",
@@ -6,7 +6,7 @@ import type {
VectorStoreQueryResult,
} from "./types.js";
import type { GenericFileSystem } from "@llamaindex/env";
import { getEnv, type GenericFileSystem } from "@llamaindex/env";
import type {
FetchResponse,
Index,
@@ -45,11 +45,11 @@ export class PineconeVectorStore implements VectorStore {
constructor(params?: PineconeParams) {
this.indexName =
params?.indexName ?? process.env.PINECONE_INDEX_NAME ?? "llama";
this.namespace = params?.namespace ?? process.env.PINECONE_NAMESPACE ?? "";
params?.indexName ?? getEnv("PINECONE_INDEX_NAME") ?? "llama";
this.namespace = params?.namespace ?? getEnv("PINECONE_NAMESPACE") ?? "";
this.chunkSize =
params?.chunkSize ??
Number.parseInt(process.env.PINECONE_CHUNK_SIZE ?? "100");
Number.parseInt(getEnv("PINECONE_CHUNK_SIZE") ?? "100");
this.textKey = params?.textKey ?? "text";
}
@@ -82,6 +82,9 @@ export class SimpleVectorStore implements VectorStore {
delete this.data.embeddingDict[textId];
delete this.data.textIdToRefDocId[textId];
}
if (this.persistPath) {
await this.persist(this.persistPath, this.fs);
}
return Promise.resolve();
}

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