mirror of
https://github.com/run-llama/LlamaIndexTS.git
synced 2026-07-01 22:14:03 -04:00
Compare commits
50 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 040160c360 | |||
| 981811efd1 | |||
| d563b45a27 | |||
| 2774e80234 | |||
| 449274ca5a | |||
| 78037a664c | |||
| 1d9e3b1000 | |||
| df83e32107 | |||
| f7b4e94231 | |||
| 4c07a2655d | |||
| 5c0c8b2ec4 | |||
| e5e18688a6 | |||
| b6fb10eba8 | |||
| df441e28f4 | |||
| a4e05ec7ab | |||
| 96f72ad86e | |||
| f3556c011c | |||
| ebc510582b | |||
| f3bfdc29e3 | |||
| 6cce3b12ea | |||
| 0273e9739a | |||
| 7c8b883448 | |||
| c2bb418542 | |||
| ed6acbead0 | |||
| 976cce40d7 | |||
| e4fd4158bb | |||
| 31d5dffcef | |||
| d12edee802 | |||
| ac41ed3aae | |||
| d8c1159032 | |||
| c856c5becb | |||
| 50e6b57be0 | |||
| 8b7fdba544 | |||
| 22ae8d0166 | |||
| 23bcc379a8 | |||
| bdc4bfe7b0 | |||
| 025ffe6b50 | |||
| a6595747fa | |||
| d902cc3e7e | |||
| 726eb41359 | |||
| e9714dbfcd | |||
| a3618e761e | |||
| 24eabe7f35 | |||
| ecfa939ea6 | |||
| b48bcc3add | |||
| fa01fa2051 | |||
| fb36eff5e1 | |||
| d24d3d1e8c | |||
| 5c4badbcca | |||
| 2cd1383dc8 |
@@ -13,8 +13,10 @@ concurrency:
|
||||
cancel-in-progress: true
|
||||
|
||||
env:
|
||||
POSTGRES_USER: runneradmin
|
||||
POSTGRES_HOST_AUTH_METHOD: trust
|
||||
TURBO_TOKEN: ${{ secrets.TURBO_TOKEN }}
|
||||
TURBO_TEAM: ${{ vars.TURBO_TEAM }}
|
||||
TURBO_REMOTE_ONLY: true
|
||||
|
||||
jobs:
|
||||
e2e:
|
||||
@@ -87,13 +89,7 @@ jobs:
|
||||
- name: Run Type Check
|
||||
run: pnpm run type-check
|
||||
- name: Run Circular Dependency Check
|
||||
run: pnpm dlx turbo run circular-check
|
||||
- uses: actions/upload-artifact@v3
|
||||
if: failure()
|
||||
with:
|
||||
name: typecheck-build-dist
|
||||
path: ./packages/llamaindex/dist
|
||||
if-no-files-found: error
|
||||
run: pnpm run circular-check
|
||||
e2e-llamaindex-examples:
|
||||
strategy:
|
||||
fail-fast: false
|
||||
@@ -149,6 +145,9 @@ jobs:
|
||||
- name: Pack @llamaindex/groq
|
||||
run: pnpm pack --pack-destination ${{ runner.temp }}
|
||||
working-directory: packages/llm/groq
|
||||
- name: Pack @llamaindex/ollama
|
||||
run: pnpm pack --pack-destination ${{ runner.temp }}
|
||||
working-directory: packages/llm/ollama
|
||||
- name: Pack @llamaindex/core
|
||||
run: pnpm pack --pack-destination ${{ runner.temp }}
|
||||
working-directory: packages/core
|
||||
@@ -164,3 +163,10 @@ jobs:
|
||||
- name: Run Type Check
|
||||
run: npx tsc --project ./tsconfig.json
|
||||
working-directory: ${{ runner.temp }}/examples
|
||||
- uses: actions/upload-artifact@v4
|
||||
if: failure()
|
||||
with:
|
||||
name: build-dist
|
||||
path: |
|
||||
${{ runner.temp }}/*.tgz
|
||||
if-no-files-found: error
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
LlamaIndex is a data framework for your LLM application.
|
||||
|
||||
Use your own data with large language models (LLMs, OpenAI ChatGPT and others) in Typescript and Javascript.
|
||||
Use your own data with large language models (LLMs, OpenAI ChatGPT and others) in JS runtime environments with TypeScript support.
|
||||
|
||||
Documentation: https://ts.llamaindex.ai/
|
||||
|
||||
@@ -19,17 +19,36 @@ Try examples online:
|
||||
|
||||
LlamaIndex.TS aims to be a lightweight, easy to use set of libraries to help you integrate large language models into your applications with your own data.
|
||||
|
||||
## Multiple JS Environment Support
|
||||
## Compatibility
|
||||
|
||||
### Multiple JS Environment Support
|
||||
|
||||
LlamaIndex.TS supports multiple JS environments, including:
|
||||
|
||||
- Node.js (18, 20, 22) ✅
|
||||
- Deno ✅
|
||||
- Bun ✅
|
||||
- React Server Components (Next.js) ✅
|
||||
- Nitro ✅
|
||||
- Vercel Edge Runtime ✅ (with some limitations)
|
||||
- Cloudflare Workers ✅ (with some limitations)
|
||||
|
||||
For now, browser support is limited due to the lack of support for [AsyncLocalStorage-like APIs](https://github.com/tc39/proposal-async-context)
|
||||
|
||||
### Supported LLMs:
|
||||
|
||||
- OpenAI LLms
|
||||
- Anthropic LLms
|
||||
- Groq LLMs
|
||||
- Llama2, Llama3, Llama3.1 LLMs
|
||||
- MistralAI LLMs
|
||||
- Fireworks LLMs
|
||||
- DeepSeek LLMs
|
||||
- ReplicateAI LLMs
|
||||
- TogetherAI LLMs
|
||||
- HuggingFace LLms
|
||||
- DeepInfra LLMs
|
||||
- Gemini LLMs
|
||||
|
||||
## Getting started
|
||||
|
||||
```shell
|
||||
@@ -77,7 +96,7 @@ See more about [moduleResolution](https://www.typescriptlang.org/docs/handbook/m
|
||||
### Node.js
|
||||
|
||||
```ts
|
||||
import fs from "fs/promises";
|
||||
import fs from "node:fs/promises";
|
||||
import { Document, VectorStoreIndex } from "llamaindex";
|
||||
|
||||
async function main() {
|
||||
@@ -111,9 +130,9 @@ main();
|
||||
node --import tsx ./main.ts
|
||||
```
|
||||
|
||||
### React Server Component (Next.js, Waku, Redwood.JS...)
|
||||
### Next.js
|
||||
|
||||
First, you will need to add a llamaindex plugin to your Next.js project.
|
||||
You will need to add a llamaindex plugin to your Next.js project.
|
||||
|
||||
```js
|
||||
// next.config.js
|
||||
@@ -124,20 +143,18 @@ module.exports = withLlamaIndex({
|
||||
});
|
||||
```
|
||||
|
||||
You can combine `ai` with `llamaindex` in Next.js with RSC (React Server Components).
|
||||
### React Server Actions
|
||||
|
||||
You can combine `ai` with `llamaindex` in Next.js, Waku or Redwood.js with RSC (React Server Components).
|
||||
|
||||
```tsx
|
||||
// src/apps/page.tsx
|
||||
"use client";
|
||||
import { chatWithAgent } from "@/actions";
|
||||
import type { JSX } from "react";
|
||||
import { useFormState } from "react-dom";
|
||||
|
||||
// You can use the Edge runtime in Next.js by adding this line:
|
||||
// export const runtime = "edge";
|
||||
import { useActionState } from "react";
|
||||
|
||||
export default function Home() {
|
||||
const [ui, action] = useFormState<JSX.Element | null>(async () => {
|
||||
const [ui, action] = useActionState<JSX.Element | null>(async () => {
|
||||
return chatWithAgent("hello!", []);
|
||||
}, null);
|
||||
return (
|
||||
@@ -167,11 +184,13 @@ export async function chatWithAgent(
|
||||
// ... adding your tools here
|
||||
],
|
||||
});
|
||||
const responseStream = await agent.chat({
|
||||
stream: true,
|
||||
message: question,
|
||||
chatHistory: prevMessages,
|
||||
});
|
||||
const responseStream = await agent.chat(
|
||||
{
|
||||
message: question,
|
||||
chatHistory: prevMessages,
|
||||
},
|
||||
true,
|
||||
);
|
||||
const uiStream = createStreamableUI(<div>loading...</div>);
|
||||
responseStream
|
||||
.pipeTo(
|
||||
@@ -189,6 +208,48 @@ export async function chatWithAgent(
|
||||
}
|
||||
```
|
||||
|
||||
### Cloudflare Workers
|
||||
|
||||
> [!TIP]
|
||||
> Some modules are not supported in Cloudflare Workers which require Node.js APIs.
|
||||
|
||||
```ts
|
||||
// add `OPENAI_API_KEY` to the `.dev.vars` file
|
||||
interface Env {
|
||||
OPENAI_API_KEY: string;
|
||||
}
|
||||
|
||||
export default {
|
||||
async fetch(
|
||||
request: Request,
|
||||
env: Env,
|
||||
ctx: ExecutionContext,
|
||||
): Promise<Response> {
|
||||
const { OpenAIAgent, OpenAI } = await import("@llamaindex/openai");
|
||||
const text = await request.text();
|
||||
const agent = new OpenAIAgent({
|
||||
llm: new OpenAI({
|
||||
apiKey: env.OPENAI_API_KEY,
|
||||
}),
|
||||
tools: [],
|
||||
});
|
||||
const responseStream = await agent.chat({
|
||||
stream: true,
|
||||
message: text,
|
||||
});
|
||||
const textEncoder = new TextEncoder();
|
||||
const response = responseStream.pipeThrough<Uint8Array>(
|
||||
new TransformStream({
|
||||
transform: (chunk, controller) => {
|
||||
controller.enqueue(textEncoder.encode(chunk.delta));
|
||||
},
|
||||
}),
|
||||
);
|
||||
return new Response(response);
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
### Vite
|
||||
|
||||
We have some wasm dependencies for better performance. You can use `vite-plugin-wasm` to load them.
|
||||
@@ -204,29 +265,9 @@ export default {
|
||||
};
|
||||
```
|
||||
|
||||
## Playground
|
||||
### Tips when using in non-Node.js environments
|
||||
|
||||
Check out our NextJS playground at https://llama-playground.vercel.app/. The source is available at https://github.com/run-llama/ts-playground
|
||||
|
||||
## Core concepts for getting started:
|
||||
|
||||
- [Document](/packages/llamaindex/src/Node.ts): A document represents a text file, PDF file or other contiguous piece of data.
|
||||
|
||||
- [Node](/packages/llamaindex/src/Node.ts): The basic data building block. Most commonly, these are parts of the document split into manageable pieces that are small enough to be fed into an embedding model and LLM.
|
||||
|
||||
- [Embedding](/packages/llamaindex/src/embeddings/OpenAIEmbedding.ts): Embeddings are sets of floating point numbers which represent the data in a Node. By comparing the similarity of embeddings, we can derive an understanding of the similarity of two pieces of data. One use case is to compare the embedding of a question with the embeddings of our Nodes to see which Nodes may contain the data needed to answer that question. Because the default service context is OpenAI, the default embedding is `OpenAIEmbedding`. If using different models, say through Ollama, use this [Embedding](/packages/llamaindex/src/embeddings/OllamaEmbedding.ts) (see all [here](/packages/llamaindex/src/embeddings)).
|
||||
|
||||
- [Indices](/packages/llamaindex/src/indices/): Indices store the Nodes and the embeddings of those nodes. QueryEngines retrieve Nodes from these Indices using embedding similarity.
|
||||
|
||||
- [QueryEngine](/packages/llamaindex/src/engines/query/RetrieverQueryEngine.ts): Query engines are what generate the query you put in and give you back the result. Query engines generally combine a pre-built prompt with selected Nodes from your Index to give the LLM the context it needs to answer your query. To build a query engine from your Index (recommended), use the [`asQueryEngine`](/packages/llamaindex/src/indices/BaseIndex.ts) method on your Index. See all query engines [here](/packages/llamaindex/src/engines/query).
|
||||
|
||||
- [ChatEngine](/packages/llamaindex/src/engines/chat/SimpleChatEngine.ts): A ChatEngine helps you build a chatbot that will interact with your Indices. See all chat engines [here](/packages/llamaindex/src/engines/chat).
|
||||
|
||||
- [SimplePrompt](/packages/llamaindex/src/Prompt.ts): A simple standardized function call definition that takes in inputs and formats them in a template literal. SimplePrompts can be specialized using currying and combined using other SimplePrompt functions.
|
||||
|
||||
## Tips when using in non-Node.js environments
|
||||
|
||||
When you are importing `llamaindex` in a non-Node.js environment(such as React Server Components, Cloudflare Workers, etc.)
|
||||
When you are importing `llamaindex` in a non-Node.js environment(such as Vercel Edge, Cloudflare Workers, etc.)
|
||||
Some classes are not exported from top-level entry file.
|
||||
|
||||
The reason is that some classes are only compatible with Node.js runtime,(e.g. `PDFReader`) which uses Node.js specific APIs(like `fs`, `child_process`, `crypto`).
|
||||
@@ -262,19 +303,31 @@ export async function getDocuments() {
|
||||
|
||||
You'll find a complete example with LlamaIndexTS here: https://github.com/run-llama/create_llama_projects/tree/main/nextjs-edge-llamaparse
|
||||
|
||||
## Supported LLMs:
|
||||
## Playground
|
||||
|
||||
- OpenAI GPT-3.5-turbo and GPT-4
|
||||
- Anthropic Claude 3 (Opus, Sonnet, and Haiku) and the legacy models (Claude 2 and Instant)
|
||||
- Groq LLMs
|
||||
- Llama2/3 Chat LLMs (70B, 13B, and 7B parameters)
|
||||
- MistralAI Chat LLMs
|
||||
- Fireworks Chat LLMs
|
||||
Check out our NextJS playground at https://llama-playground.vercel.app/. The source is available at https://github.com/run-llama/ts-playground
|
||||
|
||||
## Core concepts for getting started:
|
||||
|
||||
- [Document](/packages/llamaindex/src/Node.ts): A document represents a text file, PDF file or other contiguous piece of data.
|
||||
|
||||
- [Node](/packages/llamaindex/src/Node.ts): The basic data building block. Most commonly, these are parts of the document split into manageable pieces that are small enough to be fed into an embedding model and LLM.
|
||||
|
||||
- [Embedding](/packages/llamaindex/src/embeddings/OpenAIEmbedding.ts): Embeddings are sets of floating point numbers which represent the data in a Node. By comparing the similarity of embeddings, we can derive an understanding of the similarity of two pieces of data. One use case is to compare the embedding of a question with the embeddings of our Nodes to see which Nodes may contain the data needed to answer that question. Because the default service context is OpenAI, the default embedding is `OpenAIEmbedding`. If using different models, say through Ollama, use this [Embedding](/packages/llamaindex/src/embeddings/OllamaEmbedding.ts) (see all [here](/packages/llamaindex/src/embeddings)).
|
||||
|
||||
- [Indices](/packages/llamaindex/src/indices/): Indices store the Nodes and the embeddings of those nodes. QueryEngines retrieve Nodes from these Indices using embedding similarity.
|
||||
|
||||
- [QueryEngine](/packages/llamaindex/src/engines/query/RetrieverQueryEngine.ts): Query engines are what generate the query you put in and give you back the result. Query engines generally combine a pre-built prompt with selected Nodes from your Index to give the LLM the context it needs to answer your query. To build a query engine from your Index (recommended), use the [`asQueryEngine`](/packages/llamaindex/src/indices/BaseIndex.ts) method on your Index. See all query engines [here](/packages/llamaindex/src/engines/query).
|
||||
|
||||
- [ChatEngine](/packages/llamaindex/src/engines/chat/SimpleChatEngine.ts): A ChatEngine helps you build a chatbot that will interact with your Indices. See all chat engines [here](/packages/llamaindex/src/engines/chat).
|
||||
|
||||
- [SimplePrompt](/packages/llamaindex/src/Prompt.ts): A simple standardized function call definition that takes in inputs and formats them in a template literal. SimplePrompts can be specialized using currying and combined using other SimplePrompt functions.
|
||||
|
||||
## Contributing:
|
||||
|
||||
We are in the very early days of LlamaIndex.TS. If you’re interested in hacking on it with us check out our [contributing guide](/CONTRIBUTING.md)
|
||||
Please see our [contributing guide](CONTRIBUTING.md) for more information.
|
||||
You are highly encouraged to contribute to LlamaIndex.TS!
|
||||
|
||||
## Bugs? Questions?
|
||||
## Community
|
||||
|
||||
Please join our Discord! https://discord.com/invite/eN6D2HQ4aX
|
||||
|
||||
@@ -1,5 +1,86 @@
|
||||
# docs
|
||||
|
||||
## 0.0.82
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- llamaindex@0.6.13
|
||||
|
||||
## 0.0.81
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [f7b4e94]
|
||||
- Updated dependencies [78037a6]
|
||||
- Updated dependencies [1d9e3b1]
|
||||
- llamaindex@0.6.12
|
||||
|
||||
## 0.0.80
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [df441e2]
|
||||
- llamaindex@0.6.11
|
||||
|
||||
## 0.0.79
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [ebc5105]
|
||||
- Updated dependencies [6cce3b1]
|
||||
- llamaindex@0.6.10
|
||||
|
||||
## 0.0.78
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- llamaindex@0.6.9
|
||||
|
||||
## 0.0.77
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [8b7fdba]
|
||||
- llamaindex@0.6.8
|
||||
|
||||
## 0.0.76
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [23bcc37]
|
||||
- llamaindex@0.6.7
|
||||
|
||||
## 0.0.75
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [d902cc3]
|
||||
- Updated dependencies [025ffe6]
|
||||
- Updated dependencies [a659574]
|
||||
- llamaindex@0.6.6
|
||||
|
||||
## 0.0.74
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [e9714db]
|
||||
- llamaindex@0.6.5
|
||||
|
||||
## 0.0.73
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [b48bcc3]
|
||||
- llamaindex@0.6.4
|
||||
|
||||
## 0.0.72
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [2cd1383]
|
||||
- Updated dependencies [5c4badb]
|
||||
- llamaindex@0.6.3
|
||||
|
||||
## 0.0.71
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -13,7 +13,7 @@ Official documentation for LlamaParse can be found [here](https://docs.cloud.lla
|
||||
## Usage
|
||||
|
||||
You can then use the `LlamaParseReader` class to load local files and convert them into a parsed document that can be used by LlamaIndex.
|
||||
See [LlamaParseReader.ts](https://github.com/run-llama/LlamaIndexTS/blob/main/packages/llamaindex/src/readers/LlamaParseReader.ts) for a list of supported file types:
|
||||
See [reader.ts](https://github.com/run-llama/LlamaIndexTS/blob/main/packages/cloud/src/reader.ts) for a list of supported file types:
|
||||
|
||||
<CodeBlock language="ts">{CodeSource}</CodeBlock>
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "docs",
|
||||
"version": "0.0.71",
|
||||
"version": "0.0.82",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"docusaurus": "docusaurus",
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"extends": ["//"],
|
||||
"tasks": {
|
||||
"build": {
|
||||
"outputs": ["build/**", ".docusaurus/**"]
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,7 @@ import readline from "node:readline/promises";
|
||||
});
|
||||
const chatEngine = new SimpleChatEngine({
|
||||
llm,
|
||||
chatHistory,
|
||||
memory: chatHistory,
|
||||
});
|
||||
const rl = readline.createInterface({ input, output });
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import {
|
||||
Document,
|
||||
getResponseSynthesizer,
|
||||
NodeWithScore,
|
||||
ResponseSynthesizer,
|
||||
SentenceSplitter,
|
||||
TextNode,
|
||||
} from "llamaindex";
|
||||
@@ -14,7 +14,7 @@ import {
|
||||
|
||||
console.log(nodes);
|
||||
|
||||
const responseSynthesizer = new ResponseSynthesizer();
|
||||
const responseSynthesizer = getResponseSynthesizer("compact");
|
||||
|
||||
const nodesWithScore: NodeWithScore[] = [
|
||||
{
|
||||
@@ -30,7 +30,7 @@ import {
|
||||
const stream = await responseSynthesizer.synthesize(
|
||||
{
|
||||
query: "What age am I?",
|
||||
nodesWithScore,
|
||||
nodes: nodesWithScore,
|
||||
},
|
||||
true,
|
||||
);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
// call pnpm tsx multimodal/load.ts first to init the storage
|
||||
import { extractText } from "@llamaindex/core/utils";
|
||||
import {
|
||||
ContextChatEngine,
|
||||
NodeWithScore,
|
||||
@@ -25,8 +26,9 @@ Settings.callbackManager.on("retrieve-end", (event) => {
|
||||
const textNodes = nodes.filter(
|
||||
(node: NodeWithScore) => node.node.type === ObjectType.TEXT,
|
||||
);
|
||||
const text = extractText(query);
|
||||
console.log(
|
||||
`Retrieved ${textNodes.length} text nodes and ${imageNodes.length} image nodes for query: ${query}`,
|
||||
`Retrieved ${textNodes.length} text nodes and ${imageNodes.length} image nodes for query: ${text}`,
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { extractText } from "@llamaindex/core/utils";
|
||||
import {
|
||||
MultiModalResponseSynthesizer,
|
||||
getResponseSynthesizer,
|
||||
OpenAI,
|
||||
Settings,
|
||||
VectorStoreIndex,
|
||||
@@ -16,7 +17,8 @@ Settings.llm = new OpenAI({ model: "gpt-4-turbo", maxTokens: 512 });
|
||||
// Update callbackManager
|
||||
Settings.callbackManager.on("retrieve-end", (event) => {
|
||||
const { nodes, query } = event.detail;
|
||||
console.log(`Retrieved ${nodes.length} nodes for query: ${query}`);
|
||||
const text = extractText(query);
|
||||
console.log(`Retrieved ${nodes.length} nodes for query: ${text}`);
|
||||
});
|
||||
|
||||
async function main() {
|
||||
@@ -27,7 +29,7 @@ async function main() {
|
||||
});
|
||||
|
||||
const queryEngine = index.asQueryEngine({
|
||||
responseSynthesizer: new MultiModalResponseSynthesizer(),
|
||||
responseSynthesizer: getResponseSynthesizer("multi_modal"),
|
||||
retriever: index.asRetriever({ topK: { TEXT: 3, IMAGE: 1 } }),
|
||||
});
|
||||
const stream = await queryEngine.query({
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
"@llamaindex/core": "^0.2.0",
|
||||
"@notionhq/client": "^2.2.15",
|
||||
"@pinecone-database/pinecone": "^3.0.2",
|
||||
"@vercel/postgres": "^0.10.0",
|
||||
"@zilliz/milvus2-sdk-node": "^2.4.6",
|
||||
"chromadb": "^1.8.1",
|
||||
"commander": "^12.1.0",
|
||||
@@ -16,7 +17,8 @@
|
||||
"js-tiktoken": "^1.0.14",
|
||||
"llamaindex": "^0.6.0",
|
||||
"mongodb": "^6.7.0",
|
||||
"pathe": "^1.1.2"
|
||||
"pathe": "^1.1.2",
|
||||
"postgres": "^3.4.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^22.5.1",
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import {
|
||||
Document,
|
||||
getResponseSynthesizer,
|
||||
PromptTemplate,
|
||||
ResponseSynthesizer,
|
||||
TreeSummarize,
|
||||
TreeSummarizePrompt,
|
||||
VectorStoreIndex,
|
||||
} from "llamaindex";
|
||||
@@ -27,9 +26,7 @@ async function main() {
|
||||
|
||||
const query = "The quick brown fox jumps over the lazy dog";
|
||||
|
||||
const responseSynthesizer = new ResponseSynthesizer({
|
||||
responseBuilder: new TreeSummarize(),
|
||||
});
|
||||
const responseSynthesizer = getResponseSynthesizer("tree_summarize");
|
||||
|
||||
const queryEngine = index.asQueryEngine({
|
||||
responseSynthesizer,
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import {
|
||||
CompactAndRefine,
|
||||
getResponseSynthesizer,
|
||||
OpenAI,
|
||||
PromptTemplate,
|
||||
ResponseSynthesizer,
|
||||
Settings,
|
||||
VectorStoreIndex,
|
||||
} from "llamaindex";
|
||||
@@ -29,8 +28,8 @@ Given the CSV file, generate me Typescript code to answer the question: {query}.
|
||||
`,
|
||||
});
|
||||
|
||||
const responseSynthesizer = new ResponseSynthesizer({
|
||||
responseBuilder: new CompactAndRefine(undefined, csvPrompt),
|
||||
const responseSynthesizer = getResponseSynthesizer("compact", {
|
||||
textQATemplate: csvPrompt,
|
||||
});
|
||||
|
||||
const queryEngine = index.asQueryEngine({ responseSynthesizer });
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { createMessageContent } from "@llamaindex/core/response-synthesizers";
|
||||
import {
|
||||
Document,
|
||||
ImageNode,
|
||||
@@ -6,7 +7,6 @@ import {
|
||||
PromptTemplate,
|
||||
VectorStoreIndex,
|
||||
} from "llamaindex";
|
||||
import { createMessageContent } from "llamaindex/synthesizers/utils";
|
||||
|
||||
const reader = new LlamaParseReader();
|
||||
async function main() {
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
# neon template
|
||||
PGHOST=
|
||||
PGDATABASE=
|
||||
PGUSER=
|
||||
PGPASSWORD=
|
||||
ENDPOINT_ID=
|
||||
|
||||
# vercel template
|
||||
POSTGRES_URL=
|
||||
@@ -1,11 +1,11 @@
|
||||
// load-docs.ts
|
||||
import fs from "fs/promises";
|
||||
import {
|
||||
PGVectorStore,
|
||||
SimpleDirectoryReader,
|
||||
storageContextFromDefaults,
|
||||
VectorStoreIndex,
|
||||
} from "llamaindex";
|
||||
import fs from "node:fs/promises";
|
||||
|
||||
async function getSourceFilenames(sourceDir: string) {
|
||||
return await fs
|
||||
@@ -40,7 +40,11 @@ async function main(args: any) {
|
||||
const rdr = new SimpleDirectoryReader(callback);
|
||||
const docs = await rdr.loadData({ directoryPath: sourceDir });
|
||||
|
||||
const pgvs = new PGVectorStore();
|
||||
const pgvs = new PGVectorStore({
|
||||
clientConfig: {
|
||||
connectionString: process.env.PG_CONNECTION_STRING,
|
||||
},
|
||||
});
|
||||
pgvs.setCollection(sourceDir);
|
||||
await pgvs.clearCollection();
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
/* eslint-disable turbo/no-undeclared-env-vars */
|
||||
import dotenv from "dotenv";
|
||||
import { Document, PGVectorStore, VectorStoreQueryMode } from "llamaindex";
|
||||
import postgres from "postgres";
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const { PGHOST, PGDATABASE, PGUSER, ENDPOINT_ID } = process.env;
|
||||
const PGPASSWORD = decodeURIComponent(process.env.PGPASSWORD!);
|
||||
|
||||
const sql = postgres({
|
||||
host: PGHOST,
|
||||
database: PGDATABASE,
|
||||
username: PGUSER,
|
||||
password: PGPASSWORD,
|
||||
port: 5432,
|
||||
ssl: "require",
|
||||
connection: {
|
||||
options: `project=${ENDPOINT_ID}`,
|
||||
},
|
||||
});
|
||||
|
||||
await sql`CREATE EXTENSION IF NOT EXISTS vector`;
|
||||
|
||||
const vectorStore = new PGVectorStore({
|
||||
dimensions: 3,
|
||||
client: sql,
|
||||
});
|
||||
|
||||
await vectorStore.add([
|
||||
new Document({
|
||||
text: "hello, world",
|
||||
embedding: [1, 2, 3],
|
||||
}),
|
||||
]);
|
||||
|
||||
const results = await vectorStore.query({
|
||||
mode: VectorStoreQueryMode.DEFAULT,
|
||||
similarityTopK: 1,
|
||||
queryEmbedding: [1, 2, 3],
|
||||
});
|
||||
|
||||
console.log("result", results);
|
||||
|
||||
await sql.end();
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"name": "pg-vector-store",
|
||||
"type": "module",
|
||||
"private": true
|
||||
}
|
||||
@@ -7,7 +7,11 @@ async function main() {
|
||||
});
|
||||
|
||||
try {
|
||||
const pgvs = new PGVectorStore();
|
||||
const pgvs = new PGVectorStore({
|
||||
clientConfig: {
|
||||
connectionString: process.env.PG_CONNECTION_STRING,
|
||||
},
|
||||
});
|
||||
// Optional - set your collection name, default is no filter on this field.
|
||||
// pgvs.setCollection();
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "./dist",
|
||||
"types": ["node"],
|
||||
"skipLibCheck": true
|
||||
},
|
||||
"include": ["./**/*.ts"]
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
// https://vercel.com/docs/storage/vercel-postgres/sdk
|
||||
import { sql } from "@vercel/postgres";
|
||||
import dotenv from "dotenv";
|
||||
import { Document, PGVectorStore, VectorStoreQueryMode } from "llamaindex";
|
||||
|
||||
dotenv.config();
|
||||
|
||||
await sql`CREATE EXTENSION IF NOT EXISTS vector`;
|
||||
|
||||
const vectorStore = new PGVectorStore({
|
||||
dimensions: 3,
|
||||
client: sql,
|
||||
});
|
||||
|
||||
await vectorStore.add([
|
||||
new Document({
|
||||
text: "hello, world",
|
||||
embedding: [1, 2, 3],
|
||||
}),
|
||||
]);
|
||||
|
||||
const results = await vectorStore.query({
|
||||
mode: VectorStoreQueryMode.DEFAULT,
|
||||
similarityTopK: 1,
|
||||
queryEmbedding: [1, 2, 3],
|
||||
});
|
||||
|
||||
console.log("result", results);
|
||||
|
||||
await sql.end();
|
||||
@@ -2,12 +2,10 @@ import fs from "node:fs/promises";
|
||||
|
||||
import {
|
||||
Anthropic,
|
||||
CompactAndRefine,
|
||||
Document,
|
||||
ResponseSynthesizer,
|
||||
Settings,
|
||||
VectorStoreIndex,
|
||||
anthropicTextQaPrompt,
|
||||
getResponseSynthesizer,
|
||||
} from "llamaindex";
|
||||
|
||||
// Update llm to use Anthropic
|
||||
@@ -23,9 +21,7 @@ async function main() {
|
||||
const document = new Document({ text: essay, id_: path });
|
||||
|
||||
// Split text and create embeddings. Store them in a VectorStoreIndex
|
||||
const responseSynthesizer = new ResponseSynthesizer({
|
||||
responseBuilder: new CompactAndRefine(undefined, anthropicTextQaPrompt),
|
||||
});
|
||||
const responseSynthesizer = getResponseSynthesizer("compact");
|
||||
|
||||
const index = await VectorStoreIndex.fromDocuments([document]);
|
||||
|
||||
|
||||
@@ -1,11 +1,10 @@
|
||||
import {
|
||||
getResponseSynthesizer,
|
||||
OpenAI,
|
||||
OpenAIEmbedding,
|
||||
ResponseSynthesizer,
|
||||
RetrieverQueryEngine,
|
||||
Settings,
|
||||
TextNode,
|
||||
TreeSummarize,
|
||||
VectorIndexRetriever,
|
||||
VectorStore,
|
||||
VectorStoreIndex,
|
||||
@@ -165,10 +164,7 @@ async function main() {
|
||||
similarityTopK: 500,
|
||||
});
|
||||
|
||||
const responseSynthesizer = new ResponseSynthesizer({
|
||||
responseBuilder: new TreeSummarize(),
|
||||
});
|
||||
|
||||
const responseSynthesizer = getResponseSynthesizer("tree_summarize");
|
||||
return new RetrieverQueryEngine(retriever, responseSynthesizer, {
|
||||
filter,
|
||||
});
|
||||
|
||||
+2
-1
@@ -12,6 +12,7 @@
|
||||
"e2e": "turbo run e2e",
|
||||
"test": "turbo run test",
|
||||
"type-check": "tsc -b --diagnostics",
|
||||
"circular-check": "madge --circular ./packages/**/**/dist/index.js",
|
||||
"release": "pnpm run build:release && changeset publish",
|
||||
"release-snapshot": "pnpm run build:release && changeset publish --tag snapshot",
|
||||
"new-version": "changeset version && pnpm format:write && pnpm run build:release",
|
||||
@@ -30,7 +31,7 @@
|
||||
"madge": "^8.0.0",
|
||||
"prettier": "^3.3.3",
|
||||
"prettier-plugin-organize-imports": "^4.0.0",
|
||||
"turbo": "^2.1.0",
|
||||
"turbo": "^2.1.2",
|
||||
"typescript": "^5.6.2"
|
||||
},
|
||||
"packageManager": "pnpm@9.5.0",
|
||||
|
||||
@@ -1,5 +1,87 @@
|
||||
# @llamaindex/autotool
|
||||
|
||||
## 3.0.13
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- llamaindex@0.6.13
|
||||
|
||||
## 3.0.12
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [f7b4e94]
|
||||
- Updated dependencies [78037a6]
|
||||
- Updated dependencies [1d9e3b1]
|
||||
- llamaindex@0.6.12
|
||||
|
||||
## 3.0.11
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- df441e2: fix: consoleLogger is missing from `@llamaindex/env`
|
||||
- Updated dependencies [df441e2]
|
||||
- llamaindex@0.6.11
|
||||
|
||||
## 3.0.10
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [ebc5105]
|
||||
- Updated dependencies [6cce3b1]
|
||||
- llamaindex@0.6.10
|
||||
|
||||
## 3.0.9
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- llamaindex@0.6.9
|
||||
|
||||
## 3.0.8
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [8b7fdba]
|
||||
- llamaindex@0.6.8
|
||||
|
||||
## 3.0.7
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [23bcc37]
|
||||
- llamaindex@0.6.7
|
||||
|
||||
## 3.0.6
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [d902cc3]
|
||||
- Updated dependencies [025ffe6]
|
||||
- Updated dependencies [a659574]
|
||||
- llamaindex@0.6.6
|
||||
|
||||
## 3.0.5
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [e9714db]
|
||||
- llamaindex@0.6.5
|
||||
|
||||
## 3.0.4
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [b48bcc3]
|
||||
- llamaindex@0.6.4
|
||||
|
||||
## 3.0.3
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [2cd1383]
|
||||
- Updated dependencies [5c4badb]
|
||||
- llamaindex@0.6.3
|
||||
|
||||
## 3.0.2
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,5 +1,97 @@
|
||||
# @llamaindex/autotool-01-node-example
|
||||
|
||||
## 0.0.22
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- llamaindex@0.6.13
|
||||
- @llamaindex/autotool@3.0.13
|
||||
|
||||
## 0.0.21
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [f7b4e94]
|
||||
- Updated dependencies [78037a6]
|
||||
- Updated dependencies [1d9e3b1]
|
||||
- llamaindex@0.6.12
|
||||
- @llamaindex/autotool@3.0.12
|
||||
|
||||
## 0.0.20
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [df441e2]
|
||||
- @llamaindex/autotool@3.0.11
|
||||
- llamaindex@0.6.11
|
||||
|
||||
## 0.0.19
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [ebc5105]
|
||||
- Updated dependencies [6cce3b1]
|
||||
- llamaindex@0.6.10
|
||||
- @llamaindex/autotool@3.0.10
|
||||
|
||||
## 0.0.18
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- llamaindex@0.6.9
|
||||
- @llamaindex/autotool@3.0.9
|
||||
|
||||
## 0.0.17
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [8b7fdba]
|
||||
- llamaindex@0.6.8
|
||||
- @llamaindex/autotool@3.0.8
|
||||
|
||||
## 0.0.16
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [23bcc37]
|
||||
- llamaindex@0.6.7
|
||||
- @llamaindex/autotool@3.0.7
|
||||
|
||||
## 0.0.15
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [d902cc3]
|
||||
- Updated dependencies [025ffe6]
|
||||
- Updated dependencies [a659574]
|
||||
- llamaindex@0.6.6
|
||||
- @llamaindex/autotool@3.0.6
|
||||
|
||||
## 0.0.14
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [e9714db]
|
||||
- llamaindex@0.6.5
|
||||
- @llamaindex/autotool@3.0.5
|
||||
|
||||
## 0.0.13
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [b48bcc3]
|
||||
- llamaindex@0.6.4
|
||||
- @llamaindex/autotool@3.0.4
|
||||
|
||||
## 0.0.12
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [2cd1383]
|
||||
- Updated dependencies [5c4badb]
|
||||
- llamaindex@0.6.3
|
||||
- @llamaindex/autotool@3.0.3
|
||||
|
||||
## 0.0.11
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -13,5 +13,5 @@
|
||||
"scripts": {
|
||||
"start": "node --import tsx --import @llamaindex/autotool/node ./src/index.ts"
|
||||
},
|
||||
"version": "0.0.11"
|
||||
"version": "0.0.22"
|
||||
}
|
||||
|
||||
@@ -1,5 +1,97 @@
|
||||
# @llamaindex/autotool-02-next-example
|
||||
|
||||
## 0.1.66
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- llamaindex@0.6.13
|
||||
- @llamaindex/autotool@3.0.13
|
||||
|
||||
## 0.1.65
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [f7b4e94]
|
||||
- Updated dependencies [78037a6]
|
||||
- Updated dependencies [1d9e3b1]
|
||||
- llamaindex@0.6.12
|
||||
- @llamaindex/autotool@3.0.12
|
||||
|
||||
## 0.1.64
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [df441e2]
|
||||
- @llamaindex/autotool@3.0.11
|
||||
- llamaindex@0.6.11
|
||||
|
||||
## 0.1.63
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [ebc5105]
|
||||
- Updated dependencies [6cce3b1]
|
||||
- llamaindex@0.6.10
|
||||
- @llamaindex/autotool@3.0.10
|
||||
|
||||
## 0.1.62
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- llamaindex@0.6.9
|
||||
- @llamaindex/autotool@3.0.9
|
||||
|
||||
## 0.1.61
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [8b7fdba]
|
||||
- llamaindex@0.6.8
|
||||
- @llamaindex/autotool@3.0.8
|
||||
|
||||
## 0.1.60
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [23bcc37]
|
||||
- llamaindex@0.6.7
|
||||
- @llamaindex/autotool@3.0.7
|
||||
|
||||
## 0.1.59
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [d902cc3]
|
||||
- Updated dependencies [025ffe6]
|
||||
- Updated dependencies [a659574]
|
||||
- llamaindex@0.6.6
|
||||
- @llamaindex/autotool@3.0.6
|
||||
|
||||
## 0.1.58
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [e9714db]
|
||||
- llamaindex@0.6.5
|
||||
- @llamaindex/autotool@3.0.5
|
||||
|
||||
## 0.1.57
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [b48bcc3]
|
||||
- llamaindex@0.6.4
|
||||
- @llamaindex/autotool@3.0.4
|
||||
|
||||
## 0.1.56
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [2cd1383]
|
||||
- Updated dependencies [5c4badb]
|
||||
- llamaindex@0.6.3
|
||||
- @llamaindex/autotool@3.0.3
|
||||
|
||||
## 0.1.55
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@llamaindex/autotool-02-next-example",
|
||||
"private": true,
|
||||
"version": "0.1.55",
|
||||
"version": "0.1.66",
|
||||
"scripts": {
|
||||
"dev": "next dev",
|
||||
"build": "next build",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@llamaindex/autotool",
|
||||
"type": "module",
|
||||
"version": "3.0.2",
|
||||
"version": "3.0.13",
|
||||
"description": "auto transpile your JS function to LLM Agent compatible",
|
||||
"files": [
|
||||
"dist",
|
||||
|
||||
@@ -1,5 +1,38 @@
|
||||
# @llamaindex/cloud
|
||||
|
||||
## 0.2.10
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 981811e: fix(cloud): llama parse reader save image incorrectly
|
||||
|
||||
## 0.2.9
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- df441e2: fix: consoleLogger is missing from `@llamaindex/env`
|
||||
- Updated dependencies [df441e2]
|
||||
- @llamaindex/core@0.2.8
|
||||
- @llamaindex/env@0.1.13
|
||||
|
||||
## 0.2.8
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- ac41ed3: feat: bump cloud sdk version
|
||||
|
||||
## 0.2.7
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- fb36eff: fix: backport for node.js 18
|
||||
|
||||
There could have one missing API in the node.js 18, so we need to backport it to make it work.
|
||||
|
||||
- d24d3d1: fix: print warning when llama parse reader has error
|
||||
- Updated dependencies [2cd1383]
|
||||
- @llamaindex/core@0.2.3
|
||||
|
||||
## 0.2.6
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"type": "module",
|
||||
"main": "./dist/index.cjs",
|
||||
"module": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": "./dist/index.js",
|
||||
"private": true
|
||||
}
|
||||
+7890
-4744
File diff suppressed because it is too large
Load Diff
+20
-19
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@llamaindex/cloud",
|
||||
"version": "0.2.6",
|
||||
"version": "0.2.10",
|
||||
"type": "module",
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
@@ -9,54 +9,55 @@
|
||||
},
|
||||
"files": [
|
||||
"openapi.json",
|
||||
"dist"
|
||||
"./api",
|
||||
"./reader"
|
||||
],
|
||||
"exports": {
|
||||
"./openapi.json": "./openapi.json",
|
||||
"./api": {
|
||||
"require": {
|
||||
"types": "./dist/api.d.cts",
|
||||
"default": "./dist/api.cjs"
|
||||
"types": "./api/dist/index.d.cts",
|
||||
"default": "./api/dist/index.cjs"
|
||||
},
|
||||
"import": {
|
||||
"types": "./dist/api.d.ts",
|
||||
"default": "./dist/api.js"
|
||||
"types": "./api/dist/index.d.ts",
|
||||
"default": "./api/dist/index.js"
|
||||
},
|
||||
"default": {
|
||||
"types": "./dist/api.d.ts",
|
||||
"default": "./dist/api.js"
|
||||
"types": "./api/dist/index.d.ts",
|
||||
"default": "./api/dist/index.js"
|
||||
}
|
||||
},
|
||||
"./reader": {
|
||||
"require": {
|
||||
"types": "./dist/reader.d.cts",
|
||||
"default": "./dist/reader.cjs"
|
||||
"types": "./reader/dist/index.d.cts",
|
||||
"default": "./reader/dist/index.cjs"
|
||||
},
|
||||
"import": {
|
||||
"types": "./dist/reader.d.ts",
|
||||
"default": "./dist/reader.js"
|
||||
"types": "./reader/dist/index.d.ts",
|
||||
"default": "./reader/dist/index.js"
|
||||
},
|
||||
"default": {
|
||||
"types": "./dist/reader.d.ts",
|
||||
"default": "./dist/reader.js"
|
||||
"types": "./reader/dist/index.d.ts",
|
||||
"default": "./reader/dist/index.js"
|
||||
}
|
||||
}
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/himself65/LlamaIndexTS.git",
|
||||
"url": "https://github.com/run-llama/LlamaIndexTS.git",
|
||||
"directory": "packages/cloud"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@hey-api/client-fetch": "^0.2.4",
|
||||
"@hey-api/openapi-ts": "^0.53.0",
|
||||
"@llamaindex/core": "workspace:^0.2.2",
|
||||
"@llamaindex/env": "workspace:^0.1.11",
|
||||
"@llamaindex/core": "workspace:^0.2.8",
|
||||
"@llamaindex/env": "workspace:^0.1.13",
|
||||
"bunchee": "5.3.2"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@llamaindex/core": "workspace:^0.2.2",
|
||||
"@llamaindex/env": "workspace:^0.1.11"
|
||||
"@llamaindex/core": "workspace:^0.2.8",
|
||||
"@llamaindex/env": "workspace:^0.1.13"
|
||||
},
|
||||
"dependencies": {
|
||||
"magic-bytes.js": "^1.10.0"
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"type": "module",
|
||||
"main": "./dist/index.cjs",
|
||||
"module": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": "./dist/index.js",
|
||||
"private": true
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
import { createClient, createConfig, type Client } from "@hey-api/client-fetch";
|
||||
import { type Client, createClient, createConfig } from "@hey-api/client-fetch";
|
||||
import { Document, FileReader } from "@llamaindex/core/schema";
|
||||
import { fs, getEnv } from "@llamaindex/env";
|
||||
import { fs, getEnv, path } from "@llamaindex/env";
|
||||
import { filetypeinfo } from "magic-bytes.js";
|
||||
import {
|
||||
ParsingService,
|
||||
type Body_upload_file_api_v1_parsing_upload_post,
|
||||
type ParserLanguages,
|
||||
ParsingService,
|
||||
} from "./api";
|
||||
import { sleep } from "./utils";
|
||||
|
||||
@@ -170,6 +170,15 @@ export class LlamaParseReader extends FileReader {
|
||||
vendorMultimodalModelName?: string | undefined;
|
||||
// The API key for the multimodal API. Can also be set as an env variable: LLAMA_CLOUD_VENDOR_MULTIMODAL_API_KEY
|
||||
vendorMultimodalApiKey?: string | undefined;
|
||||
|
||||
webhookUrl?: string | undefined;
|
||||
premiumMode?: boolean | undefined;
|
||||
takeScreenshot?: boolean | undefined;
|
||||
disableOcr?: boolean | undefined;
|
||||
disableReconstruction?: boolean | undefined;
|
||||
inputS3Path?: string | undefined;
|
||||
outputS3PathPrefix?: string | undefined;
|
||||
|
||||
// numWorkers is implemented in SimpleDirectoryReader
|
||||
stdout?: WriteStream | undefined;
|
||||
|
||||
@@ -229,20 +238,18 @@ export class LlamaParseReader extends FileReader {
|
||||
}
|
||||
|
||||
// Create a job for the LlamaParse API
|
||||
private async createJob(
|
||||
data: Uint8Array,
|
||||
fileName: string = "unknown",
|
||||
): Promise<string> {
|
||||
private async createJob(data: Uint8Array): Promise<string> {
|
||||
// Load data, set the mime type
|
||||
const { mime, extension } = await LlamaParseReader.getMimeType(data);
|
||||
const { mime } = await LlamaParseReader.getMimeType(data);
|
||||
|
||||
if (this.verbose) {
|
||||
const name = fileName ? fileName : extension;
|
||||
console.log(`Starting load for ${name} file`);
|
||||
console.log("Started uploading the file");
|
||||
}
|
||||
|
||||
const body = {
|
||||
file: new File([data], fileName, { type: mime }),
|
||||
file: new Blob([data], {
|
||||
type: mime,
|
||||
}),
|
||||
language: this.language,
|
||||
parsing_instruction: this.parsingInstruction,
|
||||
skip_diagonal_text: this.skipDiagonalText,
|
||||
@@ -260,13 +267,13 @@ export class LlamaParseReader extends FileReader {
|
||||
use_vendor_multimodal_model: this.useVendorMultimodalModel,
|
||||
vendor_multimodal_model_name: this.vendorMultimodalModelName,
|
||||
vendor_multimodal_api_key: this.vendorMultimodalApiKey,
|
||||
// fixme: does these fields need to be set?
|
||||
webhook_url: undefined,
|
||||
take_screenshot: undefined,
|
||||
disable_ocr: undefined,
|
||||
disable_reconstruction: undefined,
|
||||
input_s3_path: undefined,
|
||||
output_s3_path_prefix: undefined,
|
||||
premium_mode: this.premiumMode,
|
||||
webhook_url: this.webhookUrl,
|
||||
take_screenshot: this.takeScreenshot,
|
||||
disable_ocr: this.disableOcr,
|
||||
disable_reconstruction: this.disableReconstruction,
|
||||
input_s3_path: this.inputS3Path,
|
||||
output_s3_path_prefix: this.outputS3PathPrefix,
|
||||
} satisfies {
|
||||
[Key in keyof Body_upload_file_api_v1_parsing_upload_post]-?:
|
||||
| Body_upload_file_api_v1_parsing_upload_post[Key]
|
||||
@@ -373,14 +380,10 @@ export class LlamaParseReader extends FileReader {
|
||||
* To be used with resultType = "text" and "markdown"
|
||||
*
|
||||
* @param {Uint8Array} fileContent - The content of the file to be loaded.
|
||||
* @param {string} [fileName] - The optional name of the file to be loaded.
|
||||
* @return {Promise<Document[]>} A Promise object that resolves to an array of Document objects.
|
||||
*/
|
||||
async loadDataAsContent(
|
||||
fileContent: Uint8Array,
|
||||
fileName?: string,
|
||||
): Promise<Document[]> {
|
||||
return this.createJob(fileContent, fileName)
|
||||
async loadDataAsContent(fileContent: Uint8Array): Promise<Document[]> {
|
||||
return this.createJob(fileContent)
|
||||
.then(async (jobId) => {
|
||||
if (this.verbose) {
|
||||
console.log(`Started parsing the file under job id ${jobId}`);
|
||||
@@ -403,6 +406,7 @@ export class LlamaParseReader extends FileReader {
|
||||
})
|
||||
.catch((error) => {
|
||||
if (this.ignoreErrors) {
|
||||
console.warn(`Error while parsing the file: ${error.message}`);
|
||||
return [];
|
||||
} else {
|
||||
throw error;
|
||||
@@ -437,8 +441,8 @@ export class LlamaParseReader extends FileReader {
|
||||
resultJson.file_path = isFilePath ? filePathOrContent : undefined;
|
||||
return [resultJson];
|
||||
} catch (e) {
|
||||
console.error(`Error while parsing the file under job id ${jobId}`, e);
|
||||
if (this.ignoreErrors) {
|
||||
console.error(`Error while parsing the file under job id ${jobId}`, e);
|
||||
return [];
|
||||
} else {
|
||||
throw e;
|
||||
@@ -506,14 +510,7 @@ export class LlamaParseReader extends FileReader {
|
||||
jobId: string,
|
||||
imageName: string,
|
||||
): Promise<string> {
|
||||
// Get the full path
|
||||
let imagePath = `${downloadPath}/${jobId}-${imageName}`;
|
||||
// Get a valid image path
|
||||
if (!imagePath.endsWith(".png") && !imagePath.endsWith(".jpg")) {
|
||||
imagePath += ".png";
|
||||
}
|
||||
|
||||
return imagePath;
|
||||
return path.join(downloadPath, `${jobId}-${imageName}`);
|
||||
}
|
||||
|
||||
private async fetchAndSaveImage(
|
||||
@@ -534,10 +531,9 @@ export class LlamaParseReader extends FileReader {
|
||||
if (response.error) {
|
||||
throw new Error(`Failed to download image: ${response.error.detail}`);
|
||||
}
|
||||
const arrayBuffer = (await response.data) as ArrayBuffer;
|
||||
const buffer = new Uint8Array(arrayBuffer);
|
||||
const blob = (await response.data) as Blob;
|
||||
// Write the image buffer to the specified imagePath
|
||||
await fs.writeFile(imagePath, buffer);
|
||||
await fs.writeFile(imagePath, new Uint8Array(await blob.arrayBuffer()));
|
||||
}
|
||||
|
||||
// Filters out invalid values (null, undefined, empty string) of specific params.
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"extends": ["//"],
|
||||
"tasks": {
|
||||
"build": {
|
||||
"outputs": ["**/dist/**", "src/client/**"]
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,57 @@
|
||||
# @llamaindex/community
|
||||
|
||||
## 0.0.43
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 2774e80: feat: added meta3.2 support via Bedrock including vision, tool call and inference region support
|
||||
|
||||
## 0.0.42
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- df441e2: fix: consoleLogger is missing from `@llamaindex/env`
|
||||
- Updated dependencies [df441e2]
|
||||
- @llamaindex/core@0.2.8
|
||||
- @llamaindex/env@0.1.13
|
||||
|
||||
## 0.0.41
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [6cce3b1]
|
||||
- @llamaindex/core@0.2.7
|
||||
|
||||
## 0.0.40
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 50e6b57: feat: add Amazon Bedrock Retriever
|
||||
- Updated dependencies [8b7fdba]
|
||||
- @llamaindex/core@0.2.6
|
||||
|
||||
## 0.0.39
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [d902cc3]
|
||||
- @llamaindex/core@0.2.5
|
||||
|
||||
## 0.0.38
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [b48bcc3]
|
||||
- @llamaindex/core@0.2.4
|
||||
- @llamaindex/env@0.1.12
|
||||
|
||||
## 0.0.37
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- Updated dependencies [2cd1383]
|
||||
- @llamaindex/core@0.2.3
|
||||
|
||||
## 0.0.36
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -5,8 +5,11 @@
|
||||
## Current Features:
|
||||
|
||||
- Bedrock support for the Anthropic Claude Models [usage](https://ts.llamaindex.ai/modules/llms/available_llms/bedrock)
|
||||
- Bedrock support for the Meta LLama 2, 3 and 3.1 Models [usage](https://ts.llamaindex.ai/modules/llms/available_llms/bedrock)
|
||||
- Meta LLama3.1 405b tool call support
|
||||
- Bedrock support for the Meta LLama 2, 3, 3.1 and 3.2 Models [usage](https://ts.llamaindex.ai/modules/llms/available_llms/bedrock)
|
||||
- Meta LLama3.1 405b and Llama3.2 tool call support
|
||||
- Meta 3.2 11B and 90B vision support
|
||||
- Bedrock support for querying Knowledge Base
|
||||
- Bedrock: [Supported Regions and models for cross-region inference](https://docs.aws.amazon.com/bedrock/latest/userguide/cross-region-inference-support.html)
|
||||
|
||||
## LICENSE
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@llamaindex/community",
|
||||
"description": "Community package for LlamaIndexTS",
|
||||
"version": "0.0.36",
|
||||
"version": "0.0.43",
|
||||
"type": "module",
|
||||
"types": "dist/type/index.d.ts",
|
||||
"main": "dist/cjs/index.js",
|
||||
@@ -47,6 +47,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@aws-sdk/client-bedrock-runtime": "^3.642.0",
|
||||
"@aws-sdk/client-bedrock-agent-runtime": "^3.642.0",
|
||||
"@llamaindex/core": "workspace:*",
|
||||
"@llamaindex/env": "workspace:*"
|
||||
}
|
||||
|
||||
@@ -2,4 +2,7 @@ export {
|
||||
BEDROCK_MODELS,
|
||||
BEDROCK_MODEL_MAX_TOKENS,
|
||||
Bedrock,
|
||||
INFERENCE_BEDROCK_MODELS,
|
||||
INFERENCE_TO_BEDROCK_MAP,
|
||||
} from "./llm/bedrock/index.js";
|
||||
export { AmazonKnowledgeBaseRetriever } from "./retrievers/bedrock.js";
|
||||
|
||||
@@ -6,7 +6,10 @@ import type {
|
||||
MessageContentDetail,
|
||||
ToolCallLLMMessageOptions,
|
||||
} from "@llamaindex/core/llms";
|
||||
import { mapMessageContentToMessageContentDetails } from "../utils";
|
||||
import {
|
||||
extractDataUrlComponents,
|
||||
mapMessageContentToMessageContentDetails,
|
||||
} from "../utils";
|
||||
import type {
|
||||
AnthropicContent,
|
||||
AnthropicImageContent,
|
||||
@@ -143,27 +146,6 @@ export const mapTextContent = (text: string): AnthropicTextContent => {
|
||||
return { type: "text", text };
|
||||
};
|
||||
|
||||
export const extractDataUrlComponents = (
|
||||
dataUrl: string,
|
||||
): {
|
||||
mimeType: string;
|
||||
base64: string;
|
||||
} => {
|
||||
const parts = dataUrl.split(";base64,");
|
||||
|
||||
if (parts.length !== 2 || !parts[0]!.startsWith("data:")) {
|
||||
throw new Error("Invalid data URL");
|
||||
}
|
||||
|
||||
const mimeType = parts[0]!.slice(5);
|
||||
const base64 = parts[1]!;
|
||||
|
||||
return {
|
||||
mimeType,
|
||||
base64,
|
||||
};
|
||||
};
|
||||
|
||||
export const mapImageContent = (imageUrl: string): AnthropicImageContent => {
|
||||
if (!imageUrl.startsWith("data:"))
|
||||
throw new Error(
|
||||
|
||||
@@ -16,7 +16,7 @@ import {
|
||||
ToolCallLLM,
|
||||
type ToolCallLLMMessageOptions,
|
||||
} from "@llamaindex/core/llms";
|
||||
import { streamConverter, wrapLLMEvent } from "@llamaindex/core/utils";
|
||||
import { streamConverter } from "@llamaindex/core/utils";
|
||||
import {
|
||||
type BedrockAdditionalChatOptions,
|
||||
type BedrockChatStreamResponse,
|
||||
@@ -24,6 +24,7 @@ import {
|
||||
} from "./provider";
|
||||
import { mapMessageContentToMessageContentDetails } from "./utils";
|
||||
|
||||
import { wrapLLMEvent } from "@llamaindex/core/decorator";
|
||||
import { AnthropicProvider } from "./anthropic/provider";
|
||||
import { MetaProvider } from "./meta/provider";
|
||||
|
||||
@@ -46,35 +47,96 @@ export type BedrockChatParamsNonStreaming = LLMChatParamsNonStreaming<
|
||||
export type BedrockChatNonStreamResponse =
|
||||
ChatResponse<ToolCallLLMMessageOptions>;
|
||||
|
||||
export enum BEDROCK_MODELS {
|
||||
AMAZON_TITAN_TG1_LARGE = "amazon.titan-tg1-large",
|
||||
AMAZON_TITAN_TEXT_EXPRESS_V1 = "amazon.titan-text-express-v1",
|
||||
AI21_J2_GRANDE_INSTRUCT = "ai21.j2-grande-instruct",
|
||||
AI21_J2_JUMBO_INSTRUCT = "ai21.j2-jumbo-instruct",
|
||||
AI21_J2_MID = "ai21.j2-mid",
|
||||
AI21_J2_MID_V1 = "ai21.j2-mid-v1",
|
||||
AI21_J2_ULTRA = "ai21.j2-ultra",
|
||||
AI21_J2_ULTRA_V1 = "ai21.j2-ultra-v1",
|
||||
COHERE_COMMAND_TEXT_V14 = "cohere.command-text-v14",
|
||||
ANTHROPIC_CLAUDE_INSTANT_1 = "anthropic.claude-instant-v1",
|
||||
ANTHROPIC_CLAUDE_1 = "anthropic.claude-v1", // EOF: No longer supported
|
||||
ANTHROPIC_CLAUDE_2 = "anthropic.claude-v2",
|
||||
ANTHROPIC_CLAUDE_2_1 = "anthropic.claude-v2:1",
|
||||
ANTHROPIC_CLAUDE_3_SONNET = "anthropic.claude-3-sonnet-20240229-v1:0",
|
||||
ANTHROPIC_CLAUDE_3_HAIKU = "anthropic.claude-3-haiku-20240307-v1:0",
|
||||
ANTHROPIC_CLAUDE_3_OPUS = "anthropic.claude-3-opus-20240229-v1:0",
|
||||
ANTHROPIC_CLAUDE_3_5_SONNET = "anthropic.claude-3-5-sonnet-20240620-v1:0",
|
||||
META_LLAMA2_13B_CHAT = "meta.llama2-13b-chat-v1",
|
||||
META_LLAMA2_70B_CHAT = "meta.llama2-70b-chat-v1",
|
||||
META_LLAMA3_8B_INSTRUCT = "meta.llama3-8b-instruct-v1:0",
|
||||
META_LLAMA3_70B_INSTRUCT = "meta.llama3-70b-instruct-v1:0",
|
||||
META_LLAMA3_1_8B_INSTRUCT = "meta.llama3-1-8b-instruct-v1:0",
|
||||
META_LLAMA3_1_70B_INSTRUCT = "meta.llama3-1-70b-instruct-v1:0",
|
||||
META_LLAMA3_1_405B_INSTRUCT = "meta.llama3-1-405b-instruct-v1:0",
|
||||
MISTRAL_7B_INSTRUCT = "mistral.mistral-7b-instruct-v0:2",
|
||||
MISTRAL_MIXTRAL_7B_INSTRUCT = "mistral.mixtral-8x7b-instruct-v0:1",
|
||||
MISTRAL_MIXTRAL_LARGE_2402 = "mistral.mistral-large-2402-v1:0",
|
||||
}
|
||||
export const BEDROCK_MODELS = {
|
||||
AMAZON_TITAN_TG1_LARGE: "amazon.titan-tg1-large",
|
||||
AMAZON_TITAN_TEXT_EXPRESS_V1: "amazon.titan-text-express-v1",
|
||||
AI21_J2_GRANDE_INSTRUCT: "ai21.j2-grande-instruct",
|
||||
AI21_J2_JUMBO_INSTRUCT: "ai21.j2-jumbo-instruct",
|
||||
AI21_J2_MID: "ai21.j2-mid",
|
||||
AI21_J2_MID_V1: "ai21.j2-mid-v1",
|
||||
AI21_J2_ULTRA: "ai21.j2-ultra",
|
||||
AI21_J2_ULTRA_V1: "ai21.j2-ultra-v1",
|
||||
COHERE_COMMAND_TEXT_V14: "cohere.command-text-v14",
|
||||
ANTHROPIC_CLAUDE_INSTANT_1: "anthropic.claude-instant-v1",
|
||||
ANTHROPIC_CLAUDE_1: "anthropic.claude-v1", // EOF: No longer supported
|
||||
ANTHROPIC_CLAUDE_2: "anthropic.claude-v2",
|
||||
ANTHROPIC_CLAUDE_2_1: "anthropic.claude-v2:1",
|
||||
ANTHROPIC_CLAUDE_3_SONNET: "anthropic.claude-3-sonnet-20240229-v1:0",
|
||||
ANTHROPIC_CLAUDE_3_HAIKU: "anthropic.claude-3-haiku-20240307-v1:0",
|
||||
ANTHROPIC_CLAUDE_3_OPUS: "anthropic.claude-3-opus-20240229-v1:0",
|
||||
ANTHROPIC_CLAUDE_3_5_SONNET: "anthropic.claude-3-5-sonnet-20240620-v1:0",
|
||||
META_LLAMA2_13B_CHAT: "meta.llama2-13b-chat-v1",
|
||||
META_LLAMA2_70B_CHAT: "meta.llama2-70b-chat-v1",
|
||||
META_LLAMA3_8B_INSTRUCT: "meta.llama3-8b-instruct-v1:0",
|
||||
META_LLAMA3_70B_INSTRUCT: "meta.llama3-70b-instruct-v1:0",
|
||||
META_LLAMA3_1_8B_INSTRUCT: "meta.llama3-1-8b-instruct-v1:0",
|
||||
META_LLAMA3_1_70B_INSTRUCT: "meta.llama3-1-70b-instruct-v1:0",
|
||||
META_LLAMA3_1_405B_INSTRUCT: "meta.llama3-1-405b-instruct-v1:0",
|
||||
META_LLAMA3_2_1B_INSTRUCT: "meta.llama3-2-1b-instruct-v1:0",
|
||||
META_LLAMA3_2_3B_INSTRUCT: "meta.llama3-2-3b-instruct-v1:0",
|
||||
META_LLAMA3_2_11B_INSTRUCT: "meta.llama3-2-11b-instruct-v1:0",
|
||||
META_LLAMA3_2_90B_INSTRUCT: "meta.llama3-2-90b-instruct-v1:0",
|
||||
MISTRAL_7B_INSTRUCT: "mistral.mistral-7b-instruct-v0:2",
|
||||
MISTRAL_MIXTRAL_7B_INSTRUCT: "mistral.mixtral-8x7b-instruct-v0:1",
|
||||
MISTRAL_MIXTRAL_LARGE_2402: "mistral.mistral-large-2402-v1:0",
|
||||
};
|
||||
export type BEDROCK_MODELS =
|
||||
(typeof BEDROCK_MODELS)[keyof typeof BEDROCK_MODELS];
|
||||
|
||||
export const INFERENCE_BEDROCK_MODELS = {
|
||||
US_ANTHROPIC_CLAUDE_3_HAIKU: "us.anthropic.claude-3-haiku-20240307-v1:0",
|
||||
US_ANTHROPIC_CLAUDE_3_OPUS: "us.anthropic.claude-3-opus-20240229-v1:0",
|
||||
US_ANTHROPIC_CLAUDE_3_SONNET: "us.anthropic.claude-3-sonnet-20240229-v1:0",
|
||||
US_ANTHROPIC_CLAUDE_3_5_SONNET:
|
||||
"us.anthropic.claude-3-5-sonnet-20240620-v1:0",
|
||||
US_META_LLAMA_3_2_1B_INSTRUCT: "us.meta.llama3-2-1b-instruct-v1:0",
|
||||
US_META_LLAMA_3_2_3B_INSTRUCT: "us.meta.llama3-2-3b-instruct-v1:0",
|
||||
US_META_LLAMA_3_2_11B_INSTRUCT: "us.meta.llama3-2-11b-instruct-v1:0",
|
||||
US_META_LLAMA_3_2_90B_INSTRUCT: "us.meta.llama3-2-90b-instruct-v1:0",
|
||||
|
||||
EU_ANTHROPIC_CLAUDE_3_HAIKU: "eu.anthropic.claude-3-haiku-20240307-v1:0",
|
||||
EU_ANTHROPIC_CLAUDE_3_SONNET: "eu.anthropic.claude-3-sonnet-20240229-v1:0",
|
||||
EU_ANTHROPIC_CLAUDE_3_5_SONNET:
|
||||
"eu.anthropic.claude-3-5-sonnet-20240620-v1:0",
|
||||
EU_META_LLAMA_3_2_1B_INSTRUCT: "eu.meta.llama3-2-1b-instruct-v1:0",
|
||||
EU_META_LLAMA_3_2_3B_INSTRUCT: "eu.meta.llama3-2-3b-instruct-v1:0",
|
||||
};
|
||||
|
||||
export type INFERENCE_BEDROCK_MODELS =
|
||||
(typeof INFERENCE_BEDROCK_MODELS)[keyof typeof INFERENCE_BEDROCK_MODELS];
|
||||
|
||||
export const INFERENCE_TO_BEDROCK_MAP: Record<
|
||||
INFERENCE_BEDROCK_MODELS,
|
||||
BEDROCK_MODELS
|
||||
> = {
|
||||
[INFERENCE_BEDROCK_MODELS.US_ANTHROPIC_CLAUDE_3_HAIKU]:
|
||||
BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_HAIKU,
|
||||
[INFERENCE_BEDROCK_MODELS.US_ANTHROPIC_CLAUDE_3_OPUS]:
|
||||
BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_OPUS,
|
||||
[INFERENCE_BEDROCK_MODELS.US_ANTHROPIC_CLAUDE_3_SONNET]:
|
||||
BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_SONNET,
|
||||
[INFERENCE_BEDROCK_MODELS.US_ANTHROPIC_CLAUDE_3_5_SONNET]:
|
||||
BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_5_SONNET,
|
||||
[INFERENCE_BEDROCK_MODELS.US_META_LLAMA_3_2_1B_INSTRUCT]:
|
||||
BEDROCK_MODELS.META_LLAMA3_2_1B_INSTRUCT,
|
||||
[INFERENCE_BEDROCK_MODELS.US_META_LLAMA_3_2_3B_INSTRUCT]:
|
||||
BEDROCK_MODELS.META_LLAMA3_2_3B_INSTRUCT,
|
||||
[INFERENCE_BEDROCK_MODELS.US_META_LLAMA_3_2_11B_INSTRUCT]:
|
||||
BEDROCK_MODELS.META_LLAMA3_2_11B_INSTRUCT,
|
||||
[INFERENCE_BEDROCK_MODELS.US_META_LLAMA_3_2_90B_INSTRUCT]:
|
||||
BEDROCK_MODELS.META_LLAMA3_2_90B_INSTRUCT,
|
||||
|
||||
[INFERENCE_BEDROCK_MODELS.EU_ANTHROPIC_CLAUDE_3_HAIKU]:
|
||||
BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_HAIKU,
|
||||
[INFERENCE_BEDROCK_MODELS.EU_ANTHROPIC_CLAUDE_3_SONNET]:
|
||||
BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_SONNET,
|
||||
[INFERENCE_BEDROCK_MODELS.EU_ANTHROPIC_CLAUDE_3_5_SONNET]:
|
||||
BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_5_SONNET,
|
||||
[INFERENCE_BEDROCK_MODELS.EU_META_LLAMA_3_2_1B_INSTRUCT]:
|
||||
BEDROCK_MODELS.META_LLAMA3_2_1B_INSTRUCT,
|
||||
[INFERENCE_BEDROCK_MODELS.EU_META_LLAMA_3_2_3B_INSTRUCT]:
|
||||
BEDROCK_MODELS.META_LLAMA3_2_3B_INSTRUCT,
|
||||
};
|
||||
|
||||
/*
|
||||
* Values taken from https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters.html#model-parameters-claude
|
||||
@@ -108,6 +170,10 @@ const CHAT_ONLY_MODELS = {
|
||||
[BEDROCK_MODELS.META_LLAMA3_1_8B_INSTRUCT]: 128000,
|
||||
[BEDROCK_MODELS.META_LLAMA3_1_70B_INSTRUCT]: 128000,
|
||||
[BEDROCK_MODELS.META_LLAMA3_1_405B_INSTRUCT]: 128000,
|
||||
[BEDROCK_MODELS.META_LLAMA3_2_1B_INSTRUCT]: 131000,
|
||||
[BEDROCK_MODELS.META_LLAMA3_2_3B_INSTRUCT]: 131000,
|
||||
[BEDROCK_MODELS.META_LLAMA3_2_11B_INSTRUCT]: 128000,
|
||||
[BEDROCK_MODELS.META_LLAMA3_2_90B_INSTRUCT]: 128000,
|
||||
[BEDROCK_MODELS.MISTRAL_7B_INSTRUCT]: 32000,
|
||||
[BEDROCK_MODELS.MISTRAL_MIXTRAL_7B_INSTRUCT]: 32000,
|
||||
[BEDROCK_MODELS.MISTRAL_MIXTRAL_LARGE_2402]: 32000,
|
||||
@@ -138,17 +204,25 @@ export const STREAMING_MODELS = new Set([
|
||||
BEDROCK_MODELS.META_LLAMA3_1_8B_INSTRUCT,
|
||||
BEDROCK_MODELS.META_LLAMA3_1_70B_INSTRUCT,
|
||||
BEDROCK_MODELS.META_LLAMA3_1_405B_INSTRUCT,
|
||||
BEDROCK_MODELS.META_LLAMA3_2_1B_INSTRUCT,
|
||||
BEDROCK_MODELS.META_LLAMA3_2_3B_INSTRUCT,
|
||||
BEDROCK_MODELS.META_LLAMA3_2_11B_INSTRUCT,
|
||||
BEDROCK_MODELS.META_LLAMA3_2_90B_INSTRUCT,
|
||||
BEDROCK_MODELS.MISTRAL_7B_INSTRUCT,
|
||||
BEDROCK_MODELS.MISTRAL_MIXTRAL_7B_INSTRUCT,
|
||||
BEDROCK_MODELS.MISTRAL_MIXTRAL_LARGE_2402,
|
||||
]);
|
||||
|
||||
export const TOOL_CALL_MODELS = [
|
||||
export const TOOL_CALL_MODELS: BEDROCK_MODELS[] = [
|
||||
BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_SONNET,
|
||||
BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_HAIKU,
|
||||
BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_OPUS,
|
||||
BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_5_SONNET,
|
||||
BEDROCK_MODELS.META_LLAMA3_1_405B_INSTRUCT,
|
||||
BEDROCK_MODELS.META_LLAMA3_2_1B_INSTRUCT,
|
||||
BEDROCK_MODELS.META_LLAMA3_2_3B_INSTRUCT,
|
||||
BEDROCK_MODELS.META_LLAMA3_2_11B_INSTRUCT,
|
||||
BEDROCK_MODELS.META_LLAMA3_2_90B_INSTRUCT,
|
||||
];
|
||||
|
||||
const getProvider = (model: string): Provider => {
|
||||
@@ -165,7 +239,7 @@ const getProvider = (model: string): Provider => {
|
||||
};
|
||||
|
||||
export type BedrockModelParams = {
|
||||
model: keyof typeof BEDROCK_FOUNDATION_LLMS;
|
||||
model: BEDROCK_MODELS | INFERENCE_BEDROCK_MODELS;
|
||||
temperature?: number;
|
||||
topP?: number;
|
||||
maxTokens?: number;
|
||||
@@ -184,6 +258,10 @@ export const BEDROCK_MODEL_MAX_TOKENS: Partial<Record<BEDROCK_MODELS, number>> =
|
||||
[BEDROCK_MODELS.META_LLAMA3_1_8B_INSTRUCT]: 2048,
|
||||
[BEDROCK_MODELS.META_LLAMA3_1_70B_INSTRUCT]: 2048,
|
||||
[BEDROCK_MODELS.META_LLAMA3_1_405B_INSTRUCT]: 2048,
|
||||
[BEDROCK_MODELS.META_LLAMA3_2_1B_INSTRUCT]: 2048,
|
||||
[BEDROCK_MODELS.META_LLAMA3_2_3B_INSTRUCT]: 2048,
|
||||
[BEDROCK_MODELS.META_LLAMA3_2_11B_INSTRUCT]: 2048,
|
||||
[BEDROCK_MODELS.META_LLAMA3_2_90B_INSTRUCT]: 2048,
|
||||
};
|
||||
|
||||
const DEFAULT_BEDROCK_PARAMS = {
|
||||
@@ -192,14 +270,15 @@ const DEFAULT_BEDROCK_PARAMS = {
|
||||
maxTokens: 1024, // required by anthropic
|
||||
};
|
||||
|
||||
export type BedrockParams = BedrockModelParams & BedrockRuntimeClientConfig;
|
||||
export type BedrockParams = BedrockRuntimeClientConfig & BedrockModelParams;
|
||||
|
||||
/**
|
||||
* ToolCallLLM for Bedrock
|
||||
*/
|
||||
export class Bedrock extends ToolCallLLM<BedrockAdditionalChatOptions> {
|
||||
private client: BedrockRuntimeClient;
|
||||
model: keyof typeof BEDROCK_FOUNDATION_LLMS;
|
||||
protected actualModel: BEDROCK_MODELS | INFERENCE_BEDROCK_MODELS;
|
||||
model: BEDROCK_MODELS;
|
||||
temperature: number;
|
||||
topP: number;
|
||||
maxTokens?: number;
|
||||
@@ -216,8 +295,8 @@ export class Bedrock extends ToolCallLLM<BedrockAdditionalChatOptions> {
|
||||
...params
|
||||
}: BedrockParams) {
|
||||
super();
|
||||
|
||||
this.model = model;
|
||||
this.actualModel = model;
|
||||
this.model = INFERENCE_TO_BEDROCK_MAP[model] ?? model;
|
||||
this.provider = getProvider(this.model);
|
||||
this.maxTokens = maxTokens ?? DEFAULT_BEDROCK_PARAMS.maxTokens;
|
||||
this.temperature = temperature ?? DEFAULT_BEDROCK_PARAMS.temperature;
|
||||
@@ -240,7 +319,7 @@ export class Bedrock extends ToolCallLLM<BedrockAdditionalChatOptions> {
|
||||
temperature: this.temperature,
|
||||
topP: this.topP,
|
||||
maxTokens: this.maxTokens,
|
||||
contextWindow: BEDROCK_FOUNDATION_LLMS[this.model],
|
||||
contextWindow: BEDROCK_FOUNDATION_LLMS[this.model] ?? 128000,
|
||||
tokenizer: undefined,
|
||||
};
|
||||
}
|
||||
@@ -255,6 +334,8 @@ export class Bedrock extends ToolCallLLM<BedrockAdditionalChatOptions> {
|
||||
params.additionalChatOptions,
|
||||
);
|
||||
const command = new InvokeModelCommand(input);
|
||||
command.input.modelId = this.actualModel;
|
||||
|
||||
const response = await this.client.send(command);
|
||||
let options: ToolCallLLMMessageOptions = {};
|
||||
if (this.supportToolCall) {
|
||||
@@ -286,6 +367,8 @@ export class Bedrock extends ToolCallLLM<BedrockAdditionalChatOptions> {
|
||||
params.additionalChatOptions,
|
||||
);
|
||||
const command = new InvokeModelWithResponseStreamCommand(input);
|
||||
command.input.modelId = this.actualModel;
|
||||
|
||||
const response = await this.client.send(command);
|
||||
|
||||
if (response.body) yield* this.provider.reduceStream(response.body);
|
||||
|
||||
@@ -67,21 +67,26 @@ export class MetaProvider extends Provider<MetaStreamEvent> {
|
||||
for await (const response of stream) {
|
||||
const event = this.getStreamingEventResponse(response);
|
||||
const delta = this.getTextFromStreamResponse(response);
|
||||
|
||||
// odd quirk of llama3.1, start token is \n\n
|
||||
if (
|
||||
!toolId &&
|
||||
!event?.generation.trim() &&
|
||||
event?.generation_token_count === 1 &&
|
||||
event.prompt_token_count !== null
|
||||
event?.prompt_token_count !== null
|
||||
)
|
||||
continue;
|
||||
|
||||
if (delta === TOKENS.TOOL_CALL) {
|
||||
if (delta.startsWith(TOKENS.TOOL_CALL)) {
|
||||
toolId = randomUUID();
|
||||
const parts = delta.split(TOKENS.TOOL_CALL).filter((part) => part);
|
||||
collecting.push(...parts);
|
||||
continue;
|
||||
}
|
||||
|
||||
let options: undefined | ToolCallLLMMessageOptions = undefined;
|
||||
if (toolId && event?.stop_reason === "stop") {
|
||||
if (delta) collecting.push(delta);
|
||||
const tool = JSON.parse(collecting.join(""));
|
||||
options = {
|
||||
toolCall: [
|
||||
@@ -110,11 +115,18 @@ export class MetaProvider extends Provider<MetaStreamEvent> {
|
||||
getRequestBody<T extends ChatMessage>(
|
||||
metadata: LLMMetadata,
|
||||
messages: T[],
|
||||
tools?: BaseTool[],
|
||||
tools: BaseTool[] = [],
|
||||
): InvokeModelCommandInput | InvokeModelWithResponseStreamCommandInput {
|
||||
let prompt: string = "";
|
||||
let images: string[] = [];
|
||||
if (metadata.model.startsWith("meta.llama3")) {
|
||||
prompt = mapChatMessagesToMetaLlama3Messages(messages, tools);
|
||||
const mapped = mapChatMessagesToMetaLlama3Messages({
|
||||
messages,
|
||||
tools,
|
||||
model: metadata.model,
|
||||
});
|
||||
prompt = mapped.prompt;
|
||||
images = mapped.images;
|
||||
} else if (metadata.model.startsWith("meta.llama2")) {
|
||||
prompt = mapChatMessagesToMetaLlama2Messages(messages);
|
||||
} else {
|
||||
@@ -127,6 +139,7 @@ export class MetaProvider extends Provider<MetaStreamEvent> {
|
||||
accept: "application/json",
|
||||
body: JSON.stringify({
|
||||
prompt,
|
||||
images: images.length ? images : undefined,
|
||||
max_gen_len: metadata.maxTokens,
|
||||
temperature: metadata.temperature,
|
||||
top_p: metadata.topP,
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
import type {
|
||||
BaseTool,
|
||||
ChatMessage,
|
||||
LLMMetadata,
|
||||
MessageContentTextDetail,
|
||||
ToolCallLLMMessageOptions,
|
||||
} from "@llamaindex/core/llms";
|
||||
import { extractDataUrlComponents } from "../utils";
|
||||
import { TOKENS } from "./constants";
|
||||
import type { MetaMessage } from "./types";
|
||||
|
||||
const getToolCallInstructionString = (tool: BaseTool): string => {
|
||||
@@ -24,7 +27,7 @@ const getToolCallParametersString = (tool: BaseTool): string => {
|
||||
|
||||
// ported from https://github.com/meta-llama/llama-agentic-system/blob/main/llama_agentic_system/system_prompt.py
|
||||
// NOTE: using json instead of the above xml style tool calling works more reliability
|
||||
export const getToolsPrompt = (tools?: BaseTool[]) => {
|
||||
export const getToolsPrompt_3_1 = (tools?: BaseTool[]) => {
|
||||
if (!tools?.length) return "";
|
||||
|
||||
const customToolParams = tools.map((tool) => {
|
||||
@@ -77,6 +80,46 @@ Reminder:
|
||||
`;
|
||||
};
|
||||
|
||||
export const getToolsPrompt_3_2 = (tools?: BaseTool[]) => {
|
||||
if (!tools?.length) return "";
|
||||
return `
|
||||
You are an expert in composing functions. You are given a question and a set of possible functions.
|
||||
Based on the question, you will need to make one or more function/tool calls to achieve the purpose.
|
||||
If none of the function can be used, point it out. If the given question lacks the parameters required by the function,
|
||||
also point it out. You should only return the function call in tools call sections.
|
||||
|
||||
If you decide to invoke any of the function(s), you MUST put it in the format of and start with the token: ${TOKENS.TOOL_CALL}:
|
||||
{
|
||||
"name": function_name,
|
||||
"parameters": parameters,
|
||||
}
|
||||
where
|
||||
|
||||
{
|
||||
"name": function_name,
|
||||
"parameters": parameters, => a JSON dict with the function argument name as key and function argument value as value.
|
||||
}
|
||||
|
||||
Here is an example,
|
||||
|
||||
{
|
||||
"name": "example_function_name",
|
||||
"parameters": {"example_name": "example_value"}
|
||||
}
|
||||
|
||||
Reminder:
|
||||
- Function calls MUST follow the specified format
|
||||
- Required parameters MUST be specified
|
||||
- Only call one function at a time
|
||||
- You SHOULD NOT include any other text in the response
|
||||
- Put the entire function call reply on one line
|
||||
|
||||
Here is a list of functions in JSON format that you can invoke.
|
||||
|
||||
${JSON.stringify(tools)}
|
||||
`;
|
||||
};
|
||||
|
||||
export const mapChatRoleToMetaRole = (
|
||||
role: ChatMessage["role"],
|
||||
): MetaMessage["role"] => {
|
||||
@@ -125,16 +168,46 @@ export const mapChatMessagesToMetaMessages = <
|
||||
/**
|
||||
* Documentation at https://llama.meta.com/docs/model-cards-and-prompt-formats/meta-llama-3
|
||||
*/
|
||||
export const mapChatMessagesToMetaLlama3Messages = <T extends ChatMessage>(
|
||||
messages: T[],
|
||||
tools?: BaseTool[],
|
||||
): string => {
|
||||
export const mapChatMessagesToMetaLlama3Messages = <T extends ChatMessage>({
|
||||
messages,
|
||||
model,
|
||||
tools,
|
||||
}: {
|
||||
messages: T[];
|
||||
model: LLMMetadata["model"];
|
||||
tools?: BaseTool[];
|
||||
}): { prompt: string; images: string[] } => {
|
||||
const images: string[] = [];
|
||||
const textMessages: T[] = [];
|
||||
|
||||
messages.forEach((message) => {
|
||||
if (Array.isArray(message.content)) {
|
||||
message.content.forEach((content) => {
|
||||
if (content.type === "image_url") {
|
||||
const { base64 } = extractDataUrlComponents(content.image_url.url);
|
||||
images.push(base64);
|
||||
} else {
|
||||
textMessages.push(message);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
textMessages.push(message);
|
||||
}
|
||||
});
|
||||
|
||||
const parts: string[] = [];
|
||||
if (tools?.length) {
|
||||
|
||||
let toolsPrompt = "";
|
||||
if (model.startsWith("meta.llama3-2")) {
|
||||
toolsPrompt = getToolsPrompt_3_2(tools);
|
||||
} else if (model.startsWith("meta.llama3-1")) {
|
||||
toolsPrompt = getToolsPrompt_3_1(tools);
|
||||
}
|
||||
if (toolsPrompt) {
|
||||
parts.push(
|
||||
"<|begin_of_text|>",
|
||||
"<|start_header_id|>system<|end_header_id|>",
|
||||
getToolsPrompt(tools),
|
||||
toolsPrompt,
|
||||
"<|eot_id|>",
|
||||
);
|
||||
}
|
||||
@@ -154,7 +227,9 @@ export const mapChatMessagesToMetaLlama3Messages = <T extends ChatMessage>(
|
||||
...mapped,
|
||||
"<|start_header_id|>assistant<|end_header_id|>",
|
||||
);
|
||||
return parts.join("\n");
|
||||
|
||||
const prompt = parts.join("\n");
|
||||
return { prompt, images };
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -11,3 +11,24 @@ export const mapMessageContentToMessageContentDetails = (
|
||||
|
||||
export const toUtf8 = (input: Uint8Array): string =>
|
||||
new TextDecoder("utf-8").decode(input);
|
||||
|
||||
export const extractDataUrlComponents = (
|
||||
dataUrl: string,
|
||||
): {
|
||||
mimeType: string;
|
||||
base64: string;
|
||||
} => {
|
||||
const parts = dataUrl.split(";base64,");
|
||||
|
||||
if (parts.length !== 2 || !parts[0]!.startsWith("data:")) {
|
||||
throw new Error("Invalid data URL");
|
||||
}
|
||||
|
||||
const mimeType = parts[0]!.slice(5);
|
||||
const base64 = parts[1]!;
|
||||
|
||||
return {
|
||||
mimeType,
|
||||
base64,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -0,0 +1,165 @@
|
||||
import type { KnowledgeBaseVectorSearchConfiguration } from "@aws-sdk/client-bedrock-agent-runtime";
|
||||
import {
|
||||
BedrockAgentRuntimeClient,
|
||||
type BedrockAgentRuntimeClientConfig,
|
||||
type RetrievalFilter,
|
||||
RetrieveCommand,
|
||||
type SearchType,
|
||||
} from "@aws-sdk/client-bedrock-agent-runtime";
|
||||
import type { QueryBundle } from "@llamaindex/core/query-engine";
|
||||
import { BaseRetriever } from "@llamaindex/core/retriever";
|
||||
import { Document, type NodeWithScore } from "@llamaindex/core/schema";
|
||||
import { extractText } from "@llamaindex/core/utils";
|
||||
|
||||
/**
|
||||
* Interface for the arguments required to initialize an
|
||||
* AmazonKnowledgeBaseRetriever instance.
|
||||
*/
|
||||
export interface AmazonKnowledgeBaseRetrieverArgs {
|
||||
knowledgeBaseId: string;
|
||||
topK: number;
|
||||
region: string;
|
||||
clientOptions?: BedrockAgentRuntimeClientConfig;
|
||||
filter?: RetrievalFilter;
|
||||
overrideSearchType?: SearchType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Class for interacting with Amazon Bedrock Knowledge Bases, a RAG workflow oriented service
|
||||
* Extends the BaseRetriever class.
|
||||
* @example
|
||||
* ```typescript
|
||||
* const retriever = new AmazonKnowledgeBaseRetriever({
|
||||
* topK: 10,
|
||||
* knowledgeBaseId: "YOUR_KNOWLEDGE_BASE_ID",
|
||||
* region: "us-east-2",
|
||||
* clientOptions: {
|
||||
* credentials: {
|
||||
* accessKeyId: "YOUR_ACCESS_KEY_ID",
|
||||
* secretAccessKey: "YOUR_SECRET_ACCESS_KEY",
|
||||
* },
|
||||
* },
|
||||
* });
|
||||
*
|
||||
* const docs = await retriever.retrieve({query: "How are clouds formed?"});
|
||||
* ```
|
||||
*/
|
||||
export class AmazonKnowledgeBaseRetriever extends BaseRetriever {
|
||||
static lc_name() {
|
||||
return "AmazonKnowledgeBaseRetriever";
|
||||
}
|
||||
|
||||
lc_namespace = ["llamaindex", "retrievers", "amazon_bedrock_knowledge_base"];
|
||||
|
||||
knowledgeBaseId: string;
|
||||
|
||||
topK: number;
|
||||
|
||||
bedrockAgentRuntimeClient: BedrockAgentRuntimeClient;
|
||||
|
||||
filter: RetrievalFilter | undefined;
|
||||
|
||||
overrideSearchType: SearchType | undefined;
|
||||
|
||||
constructor({
|
||||
knowledgeBaseId,
|
||||
topK = 10,
|
||||
clientOptions,
|
||||
region,
|
||||
filter,
|
||||
overrideSearchType,
|
||||
}: AmazonKnowledgeBaseRetrieverArgs) {
|
||||
super();
|
||||
|
||||
this.topK = topK;
|
||||
this.filter = filter;
|
||||
this.overrideSearchType = overrideSearchType;
|
||||
this.bedrockAgentRuntimeClient = new BedrockAgentRuntimeClient({
|
||||
region,
|
||||
...clientOptions,
|
||||
});
|
||||
this.knowledgeBaseId = knowledgeBaseId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleans the result text by replacing sequences of whitespace with a
|
||||
* single space and removing ellipses.
|
||||
* @param resText The result text to clean.
|
||||
* @returns The cleaned result text.
|
||||
*/
|
||||
cleanResult(resText: string) {
|
||||
const res = resText.replace(/\s+/g, " ").replace(/\.\.\./g, "");
|
||||
return res;
|
||||
}
|
||||
|
||||
async queryKnowledgeBase(
|
||||
query: QueryBundle,
|
||||
topK: number,
|
||||
filter?: RetrievalFilter,
|
||||
overrideSearchType?: SearchType,
|
||||
): Promise<NodeWithScore[]> {
|
||||
const retrieveCommand = new RetrieveCommand({
|
||||
knowledgeBaseId: this.knowledgeBaseId,
|
||||
retrievalQuery: {
|
||||
text: extractText(query),
|
||||
},
|
||||
retrievalConfiguration: {
|
||||
vectorSearchConfiguration: {
|
||||
numberOfResults: topK,
|
||||
overrideSearchType,
|
||||
filter,
|
||||
} as KnowledgeBaseVectorSearchConfiguration,
|
||||
},
|
||||
});
|
||||
|
||||
const retrieveResponse =
|
||||
await this.bedrockAgentRuntimeClient.send(retrieveCommand);
|
||||
|
||||
return (
|
||||
retrieveResponse.retrievalResults?.map((result) => {
|
||||
let source;
|
||||
switch (result.location?.type) {
|
||||
case "CONFLUENCE":
|
||||
source = result.location?.confluenceLocation?.url;
|
||||
break;
|
||||
case "S3":
|
||||
source = result.location?.s3Location?.uri;
|
||||
break;
|
||||
case "SALESFORCE":
|
||||
source = result.location?.salesforceLocation?.url;
|
||||
break;
|
||||
case "SHAREPOINT":
|
||||
source = result.location?.sharePointLocation?.url;
|
||||
break;
|
||||
case "WEB":
|
||||
source = result.location?.webLocation?.url;
|
||||
break;
|
||||
default:
|
||||
source = result.location?.s3Location?.uri;
|
||||
break;
|
||||
}
|
||||
|
||||
return {
|
||||
node: new Document({
|
||||
text: this.cleanResult(result.content?.text || ""),
|
||||
metadata: {
|
||||
source,
|
||||
score: result.score,
|
||||
...result.metadata,
|
||||
},
|
||||
}),
|
||||
score: result.score ?? 1.0,
|
||||
};
|
||||
}) ?? []
|
||||
);
|
||||
}
|
||||
|
||||
async _retrieve(query: QueryBundle): Promise<NodeWithScore[]> {
|
||||
return await this.queryKnowledgeBase(
|
||||
query,
|
||||
this.topK,
|
||||
this.filter,
|
||||
this.overrideSearchType,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,57 @@
|
||||
# @llamaindex/core
|
||||
|
||||
## 0.2.8
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- df441e2: fix: consoleLogger is missing from `@llamaindex/env`
|
||||
- Updated dependencies [df441e2]
|
||||
- @llamaindex/env@0.1.13
|
||||
|
||||
## 0.2.7
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 6cce3b1: feat: support `npm:postgres`
|
||||
|
||||
## 0.2.6
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 8b7fdba: refactor: move chat engine & retriever into core.
|
||||
|
||||
- `chatHistory` in BaseChatEngine now returns `ChatMessage[] | Promise<ChatMessage[]>`, instead of `BaseMemory`
|
||||
- update `retrieve-end` type
|
||||
|
||||
## 0.2.5
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- d902cc3: Fix context not being sent using ContextChatEngine
|
||||
|
||||
## 0.2.4
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- b48bcc3: feat: add `load-transformers` event type when loading `@xenova/transformers` module
|
||||
|
||||
This would benefit user who want to customize the transformer env.
|
||||
|
||||
- Updated dependencies [b48bcc3]
|
||||
- @llamaindex/env@0.1.12
|
||||
|
||||
## 0.2.3
|
||||
|
||||
### Patch Changes
|
||||
|
||||
- 2cd1383: refactor: align `response-synthesizers` & `chat-engine` module
|
||||
|
||||
- builtin event system
|
||||
- correct class extends
|
||||
- aligin APIs, naming with llama-index python
|
||||
- move stream out of first parameter to second parameter for the better tyep checking
|
||||
- remove JSONQueryEngine in `@llamaindex/experimental`, as the code quality is not satisify and we will bring it back later
|
||||
|
||||
## 0.2.2
|
||||
|
||||
### Patch Changes
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"type": "module",
|
||||
"main": "./dist/index.cjs",
|
||||
"module": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": "./dist/index.js",
|
||||
"private": true
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"type": "module",
|
||||
"main": "./dist/index.cjs",
|
||||
"module": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": "./dist/index.js",
|
||||
"private": true
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"type": "module",
|
||||
"main": "./dist/index.cjs",
|
||||
"module": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": "./dist/index.js",
|
||||
"private": true
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"type": "module",
|
||||
"main": "./dist/index.cjs",
|
||||
"module": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": "./dist/index.js",
|
||||
"private": true
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"type": "module",
|
||||
"main": "./dist/index.cjs",
|
||||
"module": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": "./dist/index.js",
|
||||
"private": true
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"type": "module",
|
||||
"main": "./dist/index.cjs",
|
||||
"module": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": "./dist/index.js",
|
||||
"private": true
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"type": "module",
|
||||
"main": "./dist/index.cjs",
|
||||
"module": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": "./dist/index.js",
|
||||
"private": true
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"type": "module",
|
||||
"main": "./dist/index.cjs",
|
||||
"module": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": "./dist/index.js",
|
||||
"private": true
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"type": "module",
|
||||
"main": "./dist/index.cjs",
|
||||
"module": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": "./dist/index.js",
|
||||
"private": true
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"type": "module",
|
||||
"main": "./dist/index.cjs",
|
||||
"module": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": "./dist/index.js",
|
||||
"private": true
|
||||
}
|
||||
+184
-82
@@ -1,194 +1,295 @@
|
||||
{
|
||||
"name": "@llamaindex/core",
|
||||
"type": "module",
|
||||
"version": "0.2.2",
|
||||
"version": "0.2.8",
|
||||
"description": "LlamaIndex Core Module",
|
||||
"exports": {
|
||||
"./node-parser": {
|
||||
"./agent": {
|
||||
"require": {
|
||||
"types": "./dist/node-parser/index.d.cts",
|
||||
"default": "./dist/node-parser/index.cjs"
|
||||
"types": "./agent/dist/index.d.cts",
|
||||
"default": "./agent/dist/index.cjs"
|
||||
},
|
||||
"import": {
|
||||
"types": "./dist/node-parser/index.d.ts",
|
||||
"default": "./dist/node-parser/index.js"
|
||||
"types": "./agent/dist/index.d.ts",
|
||||
"default": "./agent/dist/index.js"
|
||||
},
|
||||
"default": {
|
||||
"types": "./dist/node-parser/index.d.ts",
|
||||
"default": "./dist/node-parser/index.js"
|
||||
"types": "./agent/dist/index.d.ts",
|
||||
"default": "./agent/dist/index.js"
|
||||
}
|
||||
},
|
||||
"./objects": {
|
||||
"require": {
|
||||
"types": "./objects/dist/index.d.cts",
|
||||
"default": "./objects/dist/index.cjs"
|
||||
},
|
||||
"import": {
|
||||
"types": "./objects/dist/index.d.ts",
|
||||
"default": "./objects/dist/index.js"
|
||||
},
|
||||
"default": {
|
||||
"types": "./objects/dist/index.d.ts",
|
||||
"default": "./objects/dist/index.js"
|
||||
}
|
||||
},
|
||||
"./node-parser": {
|
||||
"require": {
|
||||
"types": "./node-parser/dist/index.d.cts",
|
||||
"default": "./node-parser/dist/index.cjs"
|
||||
},
|
||||
"import": {
|
||||
"types": "./node-parser/dist/index.d.ts",
|
||||
"default": "./node-parser/dist/index.js"
|
||||
},
|
||||
"default": {
|
||||
"types": "./node-parser/dist/index.d.ts",
|
||||
"default": "./node-parser/dist/index.js"
|
||||
}
|
||||
},
|
||||
"./query-engine": {
|
||||
"require": {
|
||||
"types": "./dist/query-engine/index.d.cts",
|
||||
"default": "./dist/query-engine/index.cjs"
|
||||
"types": "./query-engine/dist/index.d.cts",
|
||||
"default": "./query-engine/dist/index.cjs"
|
||||
},
|
||||
"import": {
|
||||
"types": "./dist/query-engine/index.d.ts",
|
||||
"default": "./dist/query-engine/index.js"
|
||||
"types": "./query-engine/dist/index.d.ts",
|
||||
"default": "./query-engine/dist/index.js"
|
||||
},
|
||||
"default": {
|
||||
"types": "./dist/query-engine/index.d.ts",
|
||||
"default": "./dist/query-engine/index.js"
|
||||
"types": "./query-engine/dist/index.d.ts",
|
||||
"default": "./query-engine/dist/index.js"
|
||||
}
|
||||
},
|
||||
"./llms": {
|
||||
"require": {
|
||||
"types": "./dist/llms/index.d.cts",
|
||||
"default": "./dist/llms/index.cjs"
|
||||
"types": "./llms/dist/index.d.cts",
|
||||
"default": "./llms/dist/index.cjs"
|
||||
},
|
||||
"import": {
|
||||
"types": "./dist/llms/index.d.ts",
|
||||
"default": "./dist/llms/index.js"
|
||||
"types": "./llms/dist/index.d.ts",
|
||||
"default": "./llms/dist/index.js"
|
||||
},
|
||||
"default": {
|
||||
"types": "./dist/llms/index.d.ts",
|
||||
"default": "./dist/llms/index.js"
|
||||
"types": "./llms/dist/index.d.ts",
|
||||
"default": "./llms/dist/index.js"
|
||||
}
|
||||
},
|
||||
"./decorator": {
|
||||
"require": {
|
||||
"types": "./dist/decorator/index.d.cts",
|
||||
"default": "./dist/decorator/index.cjs"
|
||||
"types": "./decorator/dist/index.d.cts",
|
||||
"default": "./decorator/dist/index.cjs"
|
||||
},
|
||||
"import": {
|
||||
"types": "./dist/decorator/index.d.ts",
|
||||
"default": "./dist/decorator/index.js"
|
||||
"types": "./decorator/dist/index.d.ts",
|
||||
"default": "./decorator/dist/index.js"
|
||||
},
|
||||
"default": {
|
||||
"types": "./dist/decorator/index.d.ts",
|
||||
"default": "./dist/decorator/index.js"
|
||||
"types": "./decorator/dist/index.d.ts",
|
||||
"default": "./decorator/dist/index.js"
|
||||
}
|
||||
},
|
||||
"./embeddings": {
|
||||
"require": {
|
||||
"types": "./dist/embeddings/index.d.cts",
|
||||
"default": "./dist/embeddings/index.cjs"
|
||||
"types": "./embeddings/dist/index.d.cts",
|
||||
"default": "./embeddings/dist/index.cjs"
|
||||
},
|
||||
"import": {
|
||||
"types": "./dist/embeddings/index.d.ts",
|
||||
"default": "./dist/embeddings/index.js"
|
||||
"types": "./embeddings/dist/index.d.ts",
|
||||
"default": "./embeddings/dist/index.js"
|
||||
},
|
||||
"default": {
|
||||
"types": "./dist/embeddings/index.d.ts",
|
||||
"default": "./dist/embeddings/index.js"
|
||||
"types": "./embeddings/dist/index.d.ts",
|
||||
"default": "./embeddings/dist/index.js"
|
||||
}
|
||||
},
|
||||
"./global": {
|
||||
"require": {
|
||||
"types": "./dist/global/index.d.cts",
|
||||
"default": "./dist/global/index.cjs"
|
||||
"types": "./global/dist/index.d.cts",
|
||||
"default": "./global/dist/index.cjs"
|
||||
},
|
||||
"import": {
|
||||
"types": "./dist/global/index.d.ts",
|
||||
"default": "./dist/global/index.js"
|
||||
"types": "./global/dist/index.d.ts",
|
||||
"default": "./global/dist/index.js"
|
||||
},
|
||||
"default": {
|
||||
"types": "./dist/global/index.d.ts",
|
||||
"default": "./dist/global/index.js"
|
||||
"types": "./global/dist/index.d.ts",
|
||||
"default": "./global/dist/index.js"
|
||||
}
|
||||
},
|
||||
"./schema": {
|
||||
"require": {
|
||||
"types": "./dist/schema/index.d.cts",
|
||||
"default": "./dist/schema/index.cjs"
|
||||
"types": "./schema/dist/index.d.cts",
|
||||
"default": "./schema/dist/index.cjs"
|
||||
},
|
||||
"import": {
|
||||
"types": "./dist/schema/index.d.ts",
|
||||
"default": "./dist/schema/index.js"
|
||||
"types": "./schema/dist/index.d.ts",
|
||||
"default": "./schema/dist/index.js"
|
||||
},
|
||||
"default": {
|
||||
"types": "./dist/schema/index.d.ts",
|
||||
"default": "./dist/schema/index.js"
|
||||
"types": "./schema/dist/index.d.ts",
|
||||
"default": "./schema/dist/index.js"
|
||||
}
|
||||
},
|
||||
"./utils": {
|
||||
"require": {
|
||||
"types": "./dist/utils/index.d.cts",
|
||||
"default": "./dist/utils/index.cjs"
|
||||
"types": "./utils/dist/index.d.cts",
|
||||
"default": "./utils/dist/index.cjs"
|
||||
},
|
||||
"import": {
|
||||
"types": "./dist/utils/index.d.ts",
|
||||
"default": "./dist/utils/index.js"
|
||||
"types": "./utils/dist/index.d.ts",
|
||||
"default": "./utils/dist/index.js"
|
||||
},
|
||||
"default": {
|
||||
"types": "./dist/utils/index.d.ts",
|
||||
"default": "./dist/utils/index.js"
|
||||
"types": "./utils/dist/index.d.ts",
|
||||
"default": "./utils/dist/index.js"
|
||||
}
|
||||
},
|
||||
"./prompts": {
|
||||
"require": {
|
||||
"types": "./dist/prompts/index.d.cts",
|
||||
"default": "./dist/prompts/index.cjs"
|
||||
"types": "./prompts/dist/index.d.cts",
|
||||
"default": "./prompts/dist/index.cjs"
|
||||
},
|
||||
"import": {
|
||||
"types": "./dist/prompts/index.d.ts",
|
||||
"default": "./dist/prompts/index.js"
|
||||
"types": "./prompts/dist/index.d.ts",
|
||||
"default": "./prompts/dist/index.js"
|
||||
},
|
||||
"default": {
|
||||
"types": "./dist/prompts/index.d.ts",
|
||||
"default": "./dist/prompts/index.js"
|
||||
"types": "./prompts/dist/index.d.ts",
|
||||
"default": "./prompts/dist/index.js"
|
||||
}
|
||||
},
|
||||
"./indices": {
|
||||
"require": {
|
||||
"types": "./dist/indices/index.d.cts",
|
||||
"default": "./dist/indices/index.cjs"
|
||||
"types": "./indices/dist/index.d.cts",
|
||||
"default": "./indices/dist/index.cjs"
|
||||
},
|
||||
"import": {
|
||||
"types": "./dist/indices/index.d.ts",
|
||||
"default": "./dist/indices/index.js"
|
||||
"types": "./indices/dist/index.d.ts",
|
||||
"default": "./indices/dist/index.js"
|
||||
},
|
||||
"default": {
|
||||
"types": "./dist/indices/index.d.ts",
|
||||
"default": "./dist/indices/index.js"
|
||||
"types": "./indices/dist/index.d.ts",
|
||||
"default": "./indices/dist/index.js"
|
||||
}
|
||||
},
|
||||
"./workflow": {
|
||||
"require": {
|
||||
"types": "./dist/workflow/index.d.cts",
|
||||
"default": "./dist/workflow/index.cjs"
|
||||
"types": "./workflow/dist/index.d.cts",
|
||||
"default": "./workflow/dist/index.cjs"
|
||||
},
|
||||
"import": {
|
||||
"types": "./dist/workflow/index.d.ts",
|
||||
"default": "./dist/workflow/index.js"
|
||||
"types": "./workflow/dist/index.d.ts",
|
||||
"default": "./workflow/dist/index.js"
|
||||
},
|
||||
"default": {
|
||||
"types": "./dist/workflow/index.d.ts",
|
||||
"default": "./dist/workflow/index.js"
|
||||
"types": "./workflow/dist/index.d.ts",
|
||||
"default": "./workflow/dist/index.js"
|
||||
}
|
||||
},
|
||||
"./memory": {
|
||||
"require": {
|
||||
"types": "./dist/memory/index.d.cts",
|
||||
"default": "./dist/memory/index.cjs"
|
||||
"types": "./memory/dist/index.d.cts",
|
||||
"default": "./memory/dist/index.cjs"
|
||||
},
|
||||
"import": {
|
||||
"types": "./dist/memory/index.d.ts",
|
||||
"default": "./dist/memory/index.js"
|
||||
"types": "./memory/dist/index.d.ts",
|
||||
"default": "./memory/dist/index.js"
|
||||
},
|
||||
"default": {
|
||||
"types": "./dist/memory/index.d.ts",
|
||||
"default": "./dist/memory/index.js"
|
||||
"types": "./memory/dist/index.d.ts",
|
||||
"default": "./memory/dist/index.js"
|
||||
}
|
||||
},
|
||||
"./storage/chat-store": {
|
||||
"require": {
|
||||
"types": "./dist/storage/chat-store/index.d.cts",
|
||||
"default": "./dist/storage/chat-store/index.cjs"
|
||||
"types": "./storage/chat-store/dist/index.d.cts",
|
||||
"default": "./storage/chat-store/dist/index.cjs"
|
||||
},
|
||||
"import": {
|
||||
"types": "./dist/storage/chat-store/index.d.ts",
|
||||
"default": "./dist/storage/chat-store/index.js"
|
||||
"types": "./storage/chat-store/dist/index.d.ts",
|
||||
"default": "./storage/chat-store/dist/index.js"
|
||||
},
|
||||
"default": {
|
||||
"types": "./dist/storage/chat-store/index.d.ts",
|
||||
"default": "./dist/storage/chat-store/index.js"
|
||||
"types": "./storage/chat-store/dist/index.d.ts",
|
||||
"default": "./storage/chat-store/dist/index.js"
|
||||
}
|
||||
},
|
||||
"./response-synthesizers": {
|
||||
"require": {
|
||||
"types": "./response-synthesizers/dist/index.d.cts",
|
||||
"default": "./response-synthesizers/dist/index.cjs"
|
||||
},
|
||||
"import": {
|
||||
"types": "./response-synthesizers/dist/index.d.ts",
|
||||
"default": "./response-synthesizers/dist/index.js"
|
||||
},
|
||||
"default": {
|
||||
"types": "./response-synthesizers/dist/index.d.ts",
|
||||
"default": "./response-synthesizers/dist/index.js"
|
||||
}
|
||||
},
|
||||
"./chat-engine": {
|
||||
"require": {
|
||||
"types": "./chat-engine/dist/index.d.cts",
|
||||
"default": "./chat-engine/dist/index.cjs"
|
||||
},
|
||||
"import": {
|
||||
"types": "./chat-engine/dist/index.d.ts",
|
||||
"default": "./chat-engine/dist/index.js"
|
||||
},
|
||||
"default": {
|
||||
"types": "./chat-engine/dist/index.d.ts",
|
||||
"default": "./chat-engine/dist/index.js"
|
||||
}
|
||||
},
|
||||
"./retriever": {
|
||||
"require": {
|
||||
"types": "./retriever/dist/index.d.cts",
|
||||
"default": "./retriever/dist/index.cjs"
|
||||
},
|
||||
"import": {
|
||||
"types": "./retriever/dist/index.d.ts",
|
||||
"default": "./retriever/dist/index.js"
|
||||
},
|
||||
"default": {
|
||||
"types": "./retriever/dist/index.d.ts",
|
||||
"default": "./retriever/dist/index.js"
|
||||
}
|
||||
},
|
||||
"./vector-store": {
|
||||
"require": {
|
||||
"types": "./dist/vector-store/index.d.cts",
|
||||
"default": "./dist/vector-store/index.cjs"
|
||||
},
|
||||
"import": {
|
||||
"types": "./dist/vector-store/index.d.ts",
|
||||
"default": "./dist/vector-store/index.js"
|
||||
},
|
||||
"default": {
|
||||
"types": "./dist/vector-store/index.d.ts",
|
||||
"default": "./dist/vector-store/index.js"
|
||||
}
|
||||
}
|
||||
},
|
||||
"files": [
|
||||
"dist"
|
||||
"./agent",
|
||||
"./objects",
|
||||
"./node-parser",
|
||||
"./query-engine",
|
||||
"./llms",
|
||||
"./decorator",
|
||||
"./embeddings",
|
||||
"./global",
|
||||
"./schema",
|
||||
"./utils",
|
||||
"./prompts",
|
||||
"./indices",
|
||||
"./workflow",
|
||||
"./memory",
|
||||
"./storage",
|
||||
"./response-synthesizers",
|
||||
"./chat-engine",
|
||||
"./retriever"
|
||||
],
|
||||
"scripts": {
|
||||
"dev": "bunchee --watch",
|
||||
@@ -197,7 +298,7 @@
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"directory": "packages/core",
|
||||
"url": "https://github.com/himself65/LlamaIndexTS.git"
|
||||
"url": "https://github.com/run-llama/LlamaIndexTS.git"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@edge-runtime/vm": "^4.0.3",
|
||||
@@ -210,6 +311,7 @@
|
||||
"dependencies": {
|
||||
"@llamaindex/env": "workspace:*",
|
||||
"@types/node": "^22.5.1",
|
||||
"magic-bytes.js": "^1.10.0",
|
||||
"zod": "^3.23.8"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"type": "module",
|
||||
"main": "./dist/index.cjs",
|
||||
"module": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": "./dist/index.js",
|
||||
"private": true
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"type": "module",
|
||||
"main": "./dist/index.cjs",
|
||||
"module": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": "./dist/index.js",
|
||||
"private": true
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"type": "module",
|
||||
"main": "./dist/index.cjs",
|
||||
"module": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": "./dist/index.js",
|
||||
"private": true
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"type": "module",
|
||||
"main": "./dist/index.cjs",
|
||||
"module": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": "./dist/index.js",
|
||||
"private": true
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"type": "module",
|
||||
"main": "./dist/index.cjs",
|
||||
"module": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": "./dist/index.js",
|
||||
"private": true
|
||||
}
|
||||
@@ -1,23 +1,21 @@
|
||||
import { consoleLogger, emptyLogger, randomUUID } from "@llamaindex/env";
|
||||
import {
|
||||
BaseChatEngine,
|
||||
type NonStreamingChatEngineParams,
|
||||
type StreamingChatEngineParams,
|
||||
} from "../chat-engine";
|
||||
import { wrapEventCaller } from "../decorator";
|
||||
import { Settings } from "../global";
|
||||
import type {
|
||||
BaseToolWithCall,
|
||||
ChatMessage,
|
||||
LLM,
|
||||
MessageContent,
|
||||
ToolOutput,
|
||||
} from "@llamaindex/core/llms";
|
||||
import { BaseMemory } from "@llamaindex/core/memory";
|
||||
import { EngineResponse } from "@llamaindex/core/schema";
|
||||
import { wrapEventCaller } from "@llamaindex/core/utils";
|
||||
import { randomUUID } from "@llamaindex/env";
|
||||
import { Settings } from "../Settings.js";
|
||||
import {
|
||||
type ChatEngine,
|
||||
type ChatEngineParamsNonStreaming,
|
||||
type ChatEngineParamsStreaming,
|
||||
} from "../engines/chat/index.js";
|
||||
import { consoleLogger, emptyLogger } from "../internal/logger.js";
|
||||
import { isReadableStream } from "../internal/utils.js";
|
||||
import { ObjectRetriever } from "../objects/index.js";
|
||||
} from "../llms";
|
||||
import { BaseMemory } from "../memory";
|
||||
import type { ObjectRetriever } from "../objects";
|
||||
import { EngineResponse } from "../schema";
|
||||
import type {
|
||||
AgentTaskContext,
|
||||
TaskHandler,
|
||||
@@ -207,8 +205,7 @@ export abstract class AgentRunner<
|
||||
>
|
||||
? AdditionalMessageOptions
|
||||
: never,
|
||||
> implements ChatEngine
|
||||
{
|
||||
> extends BaseChatEngine {
|
||||
readonly #llm: AI;
|
||||
readonly #tools:
|
||||
| BaseToolWithCall[]
|
||||
@@ -259,6 +256,7 @@ export abstract class AgentRunner<
|
||||
protected constructor(
|
||||
params: AgentRunnerParams<AI, Store, AdditionalMessageOptions>,
|
||||
) {
|
||||
super();
|
||||
const { llm, chatHistory, systemPrompt, runner, tools, verbose } = params;
|
||||
this.#llm = llm;
|
||||
this.#chatHistory = chatHistory;
|
||||
@@ -345,20 +343,19 @@ export abstract class AgentRunner<
|
||||
});
|
||||
}
|
||||
|
||||
async chat(params: ChatEngineParamsNonStreaming): Promise<EngineResponse>;
|
||||
async chat(params: NonStreamingChatEngineParams): Promise<EngineResponse>;
|
||||
async chat(
|
||||
params: ChatEngineParamsStreaming,
|
||||
params: StreamingChatEngineParams,
|
||||
): Promise<ReadableStream<EngineResponse>>;
|
||||
@wrapEventCaller
|
||||
async chat(
|
||||
params: ChatEngineParamsNonStreaming | ChatEngineParamsStreaming,
|
||||
params: NonStreamingChatEngineParams | StreamingChatEngineParams,
|
||||
): Promise<EngineResponse | ReadableStream<EngineResponse>> {
|
||||
let chatHistory: ChatMessage<AdditionalMessageOptions>[] = [];
|
||||
|
||||
if (params.chatHistory instanceof BaseMemory) {
|
||||
chatHistory = (await params.chatHistory.getMessages(
|
||||
params.message,
|
||||
)) as ChatMessage<AdditionalMessageOptions>[];
|
||||
chatHistory =
|
||||
(await params.chatHistory.getMessages()) as ChatMessage<AdditionalMessageOptions>[];
|
||||
} else {
|
||||
chatHistory =
|
||||
params.chatHistory as ChatMessage<AdditionalMessageOptions>[];
|
||||
@@ -375,7 +372,7 @@ export abstract class AgentRunner<
|
||||
this.#chatHistory = [...stepOutput.taskStep.context.store.messages];
|
||||
if (stepOutput.isLast) {
|
||||
const { output } = stepOutput;
|
||||
if (isReadableStream(output)) {
|
||||
if (output instanceof ReadableStream) {
|
||||
return output.pipeThrough<EngineResponse>(
|
||||
new TransformStream({
|
||||
transform(chunk, controller) {
|
||||
@@ -0,0 +1,11 @@
|
||||
export { AgentRunner, AgentWorker, type AgentParamsBase } from "./base.js";
|
||||
export { LLMAgent, LLMAgentWorker, type LLMAgentParams } from "./llm.js";
|
||||
export type { AgentEndEvent, AgentStartEvent, TaskHandler } from "./types.js";
|
||||
export {
|
||||
callTool,
|
||||
consumeAsyncIterable,
|
||||
createReadableStream,
|
||||
stepTools,
|
||||
stepToolsStreaming,
|
||||
validateAgentParams,
|
||||
} from "./utils.js";
|
||||
@@ -1,6 +1,6 @@
|
||||
import type { BaseToolWithCall, LLM } from "@llamaindex/core/llms";
|
||||
import { ObjectRetriever } from "../objects/index.js";
|
||||
import { Settings } from "../Settings.js";
|
||||
import { Settings } from "../global";
|
||||
import type { BaseToolWithCall, LLM } from "../llms";
|
||||
import { ObjectRetriever } from "../objects";
|
||||
import { AgentRunner, AgentWorker, type AgentParamsBase } from "./base.js";
|
||||
import { validateAgentParams } from "./utils.js";
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import type { Logger } from "@llamaindex/env";
|
||||
import type { UUID } from "../global";
|
||||
import type {
|
||||
BaseToolWithCall,
|
||||
ChatMessage,
|
||||
@@ -6,9 +8,7 @@ import type {
|
||||
LLM,
|
||||
MessageContent,
|
||||
ToolOutput,
|
||||
} from "@llamaindex/core/llms";
|
||||
import type { Logger } from "../internal/logger.js";
|
||||
import type { UUID } from "../types.js";
|
||||
} from "../llms";
|
||||
|
||||
export type AgentTaskContext<
|
||||
Model extends LLM,
|
||||
@@ -1,8 +1,6 @@
|
||||
import {
|
||||
type JSONObject,
|
||||
type JSONValue,
|
||||
Settings,
|
||||
} from "@llamaindex/core/global";
|
||||
import type { Logger } from "@llamaindex/env";
|
||||
import { z } from "zod";
|
||||
import { type JSONObject, type JSONValue, Settings } from "../global";
|
||||
import type {
|
||||
BaseTool,
|
||||
ChatMessage,
|
||||
@@ -14,15 +12,13 @@ import type {
|
||||
ToolCall,
|
||||
ToolCallLLMMessageOptions,
|
||||
ToolOutput,
|
||||
} from "@llamaindex/core/llms";
|
||||
import { baseToolWithCallSchema } from "@llamaindex/core/schema";
|
||||
import { z } from "zod";
|
||||
import type { Logger } from "../internal/logger.js";
|
||||
} from "../llms";
|
||||
import { baseToolWithCallSchema } from "../schema";
|
||||
import {
|
||||
isAsyncIterable,
|
||||
prettifyError,
|
||||
stringifyJSONToMessageContent,
|
||||
} from "../internal/utils.js";
|
||||
} from "../utils";
|
||||
import type { AgentParamsBase } from "./base.js";
|
||||
import type { TaskHandler } from "./types.js";
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
import type { ChatMessage, MessageContent } from "../llms";
|
||||
import type { BaseMemory } from "../memory";
|
||||
import { EngineResponse } from "../schema";
|
||||
|
||||
export interface BaseChatEngineParams<
|
||||
AdditionalMessageOptions extends object = object,
|
||||
> {
|
||||
message: MessageContent;
|
||||
/**
|
||||
* Optional chat history if you want to customize the chat history.
|
||||
*/
|
||||
chatHistory?:
|
||||
| ChatMessage<AdditionalMessageOptions>[]
|
||||
| BaseMemory<AdditionalMessageOptions>;
|
||||
}
|
||||
|
||||
export interface StreamingChatEngineParams<
|
||||
AdditionalMessageOptions extends object = object,
|
||||
> extends BaseChatEngineParams<AdditionalMessageOptions> {
|
||||
stream: true;
|
||||
}
|
||||
|
||||
export interface NonStreamingChatEngineParams<
|
||||
AdditionalMessageOptions extends object = object,
|
||||
> extends BaseChatEngineParams<AdditionalMessageOptions> {
|
||||
stream?: false;
|
||||
}
|
||||
|
||||
export abstract class BaseChatEngine {
|
||||
abstract chat(params: NonStreamingChatEngineParams): Promise<EngineResponse>;
|
||||
abstract chat(
|
||||
params: StreamingChatEngineParams,
|
||||
): Promise<AsyncIterable<EngineResponse>>;
|
||||
|
||||
abstract chatHistory: ChatMessage[] | Promise<ChatMessage[]>;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import { AsyncLocalStorage } from "@llamaindex/env";
|
||||
import { withEventCaller } from "../global";
|
||||
import { isAsyncIterable, isIterable } from "../utils";
|
||||
|
||||
export function wrapEventCaller<This, Result, Args extends unknown[]>(
|
||||
originalMethod: (this: This, ...args: Args) => Result,
|
||||
context: ClassMethodDecoratorContext<object>,
|
||||
) {
|
||||
const name = context.name;
|
||||
context.addInitializer(function () {
|
||||
// @ts-expect-error
|
||||
const fn = this[name].bind(this);
|
||||
// @ts-expect-error
|
||||
this[name] = (...args: unknown[]) => {
|
||||
return withEventCaller(this, () => fn(...args));
|
||||
};
|
||||
});
|
||||
return function (this: This, ...args: Args): Result {
|
||||
const result = originalMethod.call(this, ...args);
|
||||
// patch for iterators because AsyncLocalStorage doesn't work with them
|
||||
if (isAsyncIterable(result)) {
|
||||
const iter = result[Symbol.asyncIterator]();
|
||||
const snapshot = AsyncLocalStorage.snapshot();
|
||||
return (async function* asyncGeneratorWrapper() {
|
||||
while (true) {
|
||||
const { value, done } = await snapshot(() => iter.next());
|
||||
if (done) {
|
||||
break;
|
||||
}
|
||||
yield value;
|
||||
}
|
||||
})() as Result;
|
||||
} else if (isIterable(result)) {
|
||||
const iter = result[Symbol.iterator]();
|
||||
const snapshot = AsyncLocalStorage.snapshot();
|
||||
return (function* generatorWrapper() {
|
||||
while (true) {
|
||||
const { value, done } = snapshot(() => iter.next());
|
||||
if (done) {
|
||||
break;
|
||||
}
|
||||
yield value;
|
||||
}
|
||||
})() as Result;
|
||||
}
|
||||
return result;
|
||||
};
|
||||
}
|
||||
@@ -1,67 +1,3 @@
|
||||
import { getEnv } from "@llamaindex/env";
|
||||
import { Settings } from "../global";
|
||||
import type { BaseNode } from "../schema/node";
|
||||
|
||||
const emitOnce = false;
|
||||
|
||||
export function chunkSizeCheck<
|
||||
This extends { id_: string },
|
||||
Args extends any[],
|
||||
Return,
|
||||
>(
|
||||
contentGetter: (this: This, ...args: Args) => string,
|
||||
_context: ClassMethodDecoratorContext<
|
||||
This,
|
||||
(this: This, ...args: Args) => Return
|
||||
>,
|
||||
) {
|
||||
return function (this: This, ...args: Args) {
|
||||
const content = contentGetter.call(this, ...args);
|
||||
const chunkSize = Settings.chunkSize;
|
||||
const enableChunkSizeCheck = getEnv("ENABLE_CHUNK_SIZE_CHECK") === "true";
|
||||
if (
|
||||
enableChunkSizeCheck &&
|
||||
chunkSize !== undefined &&
|
||||
content.length > chunkSize
|
||||
) {
|
||||
console.warn(
|
||||
`Node (${this.id_}) is larger than chunk size: ${content.length} > ${chunkSize}`,
|
||||
);
|
||||
if (!emitOnce) {
|
||||
console.warn(
|
||||
"Will truncate the content if it is larger than chunk size",
|
||||
);
|
||||
console.warn("If you want to disable this behavior:");
|
||||
console.warn(" 1. Set Settings.chunkSize = undefined");
|
||||
console.warn(" 2. Set Settings.chunkSize to a larger value");
|
||||
console.warn(
|
||||
" 3. Change the way of splitting content into smaller chunks",
|
||||
);
|
||||
}
|
||||
return content.slice(0, chunkSize);
|
||||
}
|
||||
return content;
|
||||
};
|
||||
}
|
||||
|
||||
export function lazyInitHash(
|
||||
value: ClassAccessorDecoratorTarget<BaseNode, string>,
|
||||
_context: ClassAccessorDecoratorContext,
|
||||
): ClassAccessorDecoratorResult<BaseNode, string> {
|
||||
return {
|
||||
get() {
|
||||
const oldValue = value.get.call(this);
|
||||
if (oldValue === "") {
|
||||
const hash = this.generateHash();
|
||||
value.set.call(this, hash);
|
||||
}
|
||||
return value.get.call(this);
|
||||
},
|
||||
set(newValue: string) {
|
||||
value.set.call(this, newValue);
|
||||
},
|
||||
init(value: string): string {
|
||||
return value;
|
||||
},
|
||||
};
|
||||
}
|
||||
export { wrapEventCaller } from "./event-caller";
|
||||
export { lazyInitHash } from "./lazy-init-hash";
|
||||
export { wrapLLMEvent } from "./wrap-llm-event";
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
import type { BaseNode } from "../schema";
|
||||
|
||||
export function lazyInitHash(
|
||||
value: ClassAccessorDecoratorTarget<BaseNode, string>,
|
||||
_context: ClassAccessorDecoratorContext,
|
||||
): ClassAccessorDecoratorResult<BaseNode, string> {
|
||||
return {
|
||||
get() {
|
||||
const oldValue = value.get.call(this);
|
||||
if (oldValue === "") {
|
||||
const hash = this.generateHash();
|
||||
value.set.call(this, hash);
|
||||
}
|
||||
return value.get.call(this);
|
||||
},
|
||||
set(newValue: string) {
|
||||
value.set.call(this, newValue);
|
||||
},
|
||||
init(value: string): string {
|
||||
return value;
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -9,4 +9,9 @@ export type {
|
||||
LLMToolResultEvent,
|
||||
LlamaIndexEventMaps,
|
||||
} from "./settings/callback-manager";
|
||||
export type { JSONArray, JSONObject, JSONValue } from "./type";
|
||||
export {
|
||||
EventCaller,
|
||||
getEventCaller,
|
||||
withEventCaller,
|
||||
} from "./settings/event-caller";
|
||||
export type { JSONArray, JSONObject, JSONValue, UUID } from "./type";
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { Tokenizer } from "@llamaindex/env";
|
||||
import { getEnv, type Tokenizer } from "@llamaindex/env";
|
||||
import type { LLM } from "../llms";
|
||||
import {
|
||||
type CallbackManager,
|
||||
@@ -61,4 +61,16 @@ export const Settings = {
|
||||
): Result {
|
||||
return withCallbackManager(callbackManager, fn);
|
||||
},
|
||||
|
||||
get debug() {
|
||||
let debug = getEnv("DEBUG");
|
||||
if (typeof window !== "undefined") {
|
||||
debug ||= window.localStorage.debug;
|
||||
}
|
||||
return (
|
||||
(Boolean(debug) && debug?.includes("llamaindex")) ||
|
||||
debug === "*" ||
|
||||
debug === "true"
|
||||
);
|
||||
},
|
||||
};
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { AsyncLocalStorage, CustomEvent } from "@llamaindex/env";
|
||||
import type { AgentEndEvent, AgentStartEvent } from "../../agent";
|
||||
import type {
|
||||
ChatMessage,
|
||||
ChatResponse,
|
||||
@@ -6,9 +7,15 @@ import type {
|
||||
ToolCall,
|
||||
ToolOutput,
|
||||
} from "../../llms";
|
||||
import type { QueryEndEvent, QueryStartEvent } from "../../query-engine";
|
||||
import type {
|
||||
SynthesizeEndEvent,
|
||||
SynthesizeStartEvent,
|
||||
} from "../../response-synthesizers";
|
||||
import type { RetrieveEndEvent, RetrieveStartEvent } from "../../retriever";
|
||||
import { TextNode } from "../../schema";
|
||||
import { EventCaller, getEventCaller } from "../../utils/event-caller";
|
||||
import type { UUID } from "../type";
|
||||
import { EventCaller, getEventCaller } from "./event-caller";
|
||||
|
||||
export type LLMStartEvent = {
|
||||
id: UUID;
|
||||
@@ -60,6 +67,14 @@ export interface LlamaIndexEventMaps {
|
||||
"chunking-end": ChunkingEndEvent;
|
||||
"node-parsing-start": NodeParsingStartEvent;
|
||||
"node-parsing-end": NodeParsingEndEvent;
|
||||
"query-start": QueryStartEvent;
|
||||
"query-end": QueryEndEvent;
|
||||
"synthesize-start": SynthesizeStartEvent;
|
||||
"synthesize-end": SynthesizeEndEvent;
|
||||
"retrieve-start": RetrieveStartEvent;
|
||||
"retrieve-end": RetrieveEndEvent;
|
||||
"agent-start": AgentStartEvent;
|
||||
"agent-end": AgentEndEvent;
|
||||
}
|
||||
|
||||
export class LlamaIndexCustomEvent<T = any> extends CustomEvent<T> {
|
||||
@@ -119,16 +134,29 @@ export class CallbackManager {
|
||||
dispatchEvent<K extends keyof LlamaIndexEventMaps>(
|
||||
event: K,
|
||||
detail: LlamaIndexEventMaps[K],
|
||||
sync = false,
|
||||
) {
|
||||
const cbs = this.#handlers.get(event);
|
||||
if (!cbs) {
|
||||
return;
|
||||
}
|
||||
queueMicrotask(() => {
|
||||
if (typeof queueMicrotask === "undefined") {
|
||||
console.warn(
|
||||
"queueMicrotask is not available, dispatching synchronously",
|
||||
);
|
||||
sync = true;
|
||||
}
|
||||
if (sync) {
|
||||
cbs.forEach((handler) =>
|
||||
handler(LlamaIndexCustomEvent.fromEvent(event, { ...detail })),
|
||||
);
|
||||
});
|
||||
} else {
|
||||
queueMicrotask(() => {
|
||||
cbs.forEach((handler) =>
|
||||
handler(LlamaIndexCustomEvent.fromEvent(event, { ...detail })),
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
import { AsyncLocalStorage, randomUUID } from "@llamaindex/env";
|
||||
|
||||
const eventReasonAsyncLocalStorage = new AsyncLocalStorage<EventCaller>();
|
||||
|
||||
/**
|
||||
* EventCaller is used to track the caller of an event.
|
||||
*/
|
||||
export class EventCaller {
|
||||
public readonly id = randomUUID();
|
||||
|
||||
private constructor(
|
||||
public readonly caller: unknown,
|
||||
public readonly parent: EventCaller | null,
|
||||
) {}
|
||||
|
||||
#computedCallers: unknown[] | null = null;
|
||||
|
||||
public get computedCallers(): unknown[] {
|
||||
if (this.#computedCallers != null) {
|
||||
return this.#computedCallers;
|
||||
}
|
||||
const callers = [this.caller];
|
||||
let parent = this.parent;
|
||||
while (parent != null) {
|
||||
callers.push(parent.caller);
|
||||
parent = parent.parent;
|
||||
}
|
||||
this.#computedCallers = callers;
|
||||
return callers;
|
||||
}
|
||||
|
||||
public static create(
|
||||
caller: unknown,
|
||||
parent: EventCaller | null,
|
||||
): EventCaller {
|
||||
return new EventCaller(caller, parent);
|
||||
}
|
||||
}
|
||||
|
||||
export function getEventCaller(): EventCaller | null {
|
||||
return eventReasonAsyncLocalStorage.getStore() ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param caller who is calling this function, pass in `this` if it's a class method
|
||||
* @param fn
|
||||
*/
|
||||
export function withEventCaller<T>(caller: unknown, fn: () => T) {
|
||||
// create a chain of event callers
|
||||
const parentCaller = getEventCaller();
|
||||
return eventReasonAsyncLocalStorage.run(
|
||||
EventCaller.create(caller, parentCaller),
|
||||
fn,
|
||||
);
|
||||
}
|
||||
@@ -1,10 +1,13 @@
|
||||
import { type Tokenizer, tokenizers } from "@llamaindex/env";
|
||||
import {
|
||||
DEFAULT_CHUNK_OVERLAP_RATIO,
|
||||
DEFAULT_CHUNK_SIZE,
|
||||
DEFAULT_CONTEXT_WINDOW,
|
||||
DEFAULT_NUM_OUTPUTS,
|
||||
DEFAULT_PADDING,
|
||||
Settings,
|
||||
} from "../global";
|
||||
import type { LLMMetadata } from "../llms";
|
||||
import { SentenceSplitter } from "../node-parser";
|
||||
import type { PromptTemplate } from "../prompts";
|
||||
|
||||
@@ -133,4 +136,29 @@ export class PromptHelper {
|
||||
const combinedStr = textChunks.join("\n\n");
|
||||
return textSplitter.splitText(combinedStr);
|
||||
}
|
||||
|
||||
static fromLLMMetadata(
|
||||
metadata: LLMMetadata,
|
||||
options?: {
|
||||
chunkOverlapRatio?: number;
|
||||
chunkSizeLimit?: number;
|
||||
tokenizer?: Tokenizer;
|
||||
separator?: string;
|
||||
},
|
||||
) {
|
||||
const {
|
||||
chunkOverlapRatio = DEFAULT_CHUNK_OVERLAP_RATIO,
|
||||
chunkSizeLimit = DEFAULT_CHUNK_SIZE,
|
||||
tokenizer = Settings.tokenizer,
|
||||
separator = " ",
|
||||
} = options ?? {};
|
||||
return new PromptHelper({
|
||||
contextWindow: metadata.contextWindow,
|
||||
numOutput: metadata.maxTokens ?? DEFAULT_NUM_OUTPUTS,
|
||||
chunkOverlapRatio,
|
||||
chunkSizeLimit,
|
||||
tokenizer,
|
||||
separator,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Settings } from "../global";
|
||||
import type { ChatMessage, MessageContent } from "../llms";
|
||||
import type { ChatMessage } from "../llms";
|
||||
import { type BaseChatStore, SimpleChatStore } from "../storage/chat-store";
|
||||
import { extractText } from "../utils";
|
||||
|
||||
@@ -12,15 +12,36 @@ export const DEFAULT_CHAT_STORE_KEY = "chat_history";
|
||||
export abstract class BaseMemory<
|
||||
AdditionalMessageOptions extends object = object,
|
||||
> {
|
||||
/**
|
||||
* Retrieves messages from the memory, optionally including transient messages.
|
||||
* Compared to getAllMessages, this method a) allows for transient messages to be included in the retrieval and b) may return a subset of the total messages by applying a token limit.
|
||||
* @param transientMessages Optional array of temporary messages to be included in the retrieval.
|
||||
* These messages are not stored in the memory but are considered for the current interaction.
|
||||
* @returns An array of chat messages, either synchronously or as a Promise.
|
||||
*/
|
||||
abstract getMessages(
|
||||
input?: MessageContent | undefined,
|
||||
transientMessages?: ChatMessage<AdditionalMessageOptions>[] | undefined,
|
||||
):
|
||||
| ChatMessage<AdditionalMessageOptions>[]
|
||||
| Promise<ChatMessage<AdditionalMessageOptions>[]>;
|
||||
|
||||
/**
|
||||
* Retrieves all messages stored in the memory.
|
||||
* @returns An array of all chat messages, either synchronously or as a Promise.
|
||||
*/
|
||||
abstract getAllMessages():
|
||||
| ChatMessage<AdditionalMessageOptions>[]
|
||||
| Promise<ChatMessage<AdditionalMessageOptions>[]>;
|
||||
|
||||
/**
|
||||
* Adds a new message to the memory.
|
||||
* @param messages The chat message to be added to the memory.
|
||||
*/
|
||||
abstract put(messages: ChatMessage<AdditionalMessageOptions>): void;
|
||||
|
||||
/**
|
||||
* Clears all messages from the memory.
|
||||
*/
|
||||
abstract reset(): void;
|
||||
|
||||
protected _tokenCountForMessages(messages: ChatMessage[]): number {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Settings } from "../global";
|
||||
import type { ChatMessage, LLM, MessageContent } from "../llms";
|
||||
import type { ChatMessage, LLM } from "../llms";
|
||||
import { type BaseChatStore } from "../storage/chat-store";
|
||||
import { BaseChatStoreMemory, DEFAULT_TOKEN_LIMIT_RATIO } from "./base";
|
||||
|
||||
@@ -34,7 +34,7 @@ export class ChatMemoryBuffer<
|
||||
}
|
||||
|
||||
getMessages(
|
||||
input?: MessageContent | undefined,
|
||||
transientMessages?: ChatMessage<AdditionalMessageOptions>[] | undefined,
|
||||
initialTokenCount: number = 0,
|
||||
) {
|
||||
const messages = this.getAllMessages();
|
||||
@@ -43,16 +43,22 @@ export class ChatMemoryBuffer<
|
||||
throw new Error("Initial token count exceeds token limit");
|
||||
}
|
||||
|
||||
let messageCount = messages.length;
|
||||
let currentMessages = messages.slice(-messageCount);
|
||||
let tokenCount = this._tokenCountForMessages(messages) + initialTokenCount;
|
||||
// Add input messages as transient messages
|
||||
const messagesWithInput = transientMessages
|
||||
? [...transientMessages, ...messages]
|
||||
: messages;
|
||||
|
||||
let messageCount = messagesWithInput.length;
|
||||
let currentMessages = messagesWithInput.slice(-messageCount);
|
||||
let tokenCount =
|
||||
this._tokenCountForMessages(messagesWithInput) + initialTokenCount;
|
||||
|
||||
while (tokenCount > this.tokenLimit && messageCount > 1) {
|
||||
messageCount -= 1;
|
||||
if (messages.at(-messageCount)!.role === "assistant") {
|
||||
if (messagesWithInput.at(-messageCount)!.role === "assistant") {
|
||||
messageCount -= 1;
|
||||
}
|
||||
currentMessages = messages.slice(-messageCount);
|
||||
currentMessages = messagesWithInput.slice(-messageCount);
|
||||
tokenCount =
|
||||
this._tokenCountForMessages(currentMessages) + initialTokenCount;
|
||||
}
|
||||
@@ -60,6 +66,6 @@ export class ChatMemoryBuffer<
|
||||
if (tokenCount > this.tokenLimit && messageCount <= 0) {
|
||||
return [];
|
||||
}
|
||||
return messages.slice(-messageCount);
|
||||
return messagesWithInput.slice(-messageCount);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,18 +114,22 @@ export class ChatSummaryMemoryBuffer extends BaseMemory {
|
||||
}
|
||||
}
|
||||
|
||||
private calcCurrentRequestMessages() {
|
||||
// TODO: check order: currently, we're sending:
|
||||
private calcCurrentRequestMessages(transientMessages?: ChatMessage[]) {
|
||||
// currently, we're sending:
|
||||
// system messages first, then transient messages and then the messages that describe the conversation so far
|
||||
return [...this.systemMessages, ...this.calcConversationMessages(true)];
|
||||
return [
|
||||
...this.systemMessages,
|
||||
...(transientMessages ? transientMessages : []),
|
||||
...this.calcConversationMessages(true),
|
||||
];
|
||||
}
|
||||
|
||||
reset() {
|
||||
this.messages = [];
|
||||
}
|
||||
|
||||
async getMessages(): Promise<ChatMessage[]> {
|
||||
const requestMessages = this.calcCurrentRequestMessages();
|
||||
async getMessages(transientMessages?: ChatMessage[]): Promise<ChatMessage[]> {
|
||||
const requestMessages = this.calcCurrentRequestMessages(transientMessages);
|
||||
|
||||
// get tokens of current request messages and the transient messages
|
||||
const tokens = requestMessages.reduce(
|
||||
@@ -149,7 +153,7 @@ export class ChatSummaryMemoryBuffer extends BaseMemory {
|
||||
// TODO: we still might have too many tokens
|
||||
// e.g. too large system messages or transient messages
|
||||
// how should we deal with that?
|
||||
return this.calcCurrentRequestMessages();
|
||||
return this.calcCurrentRequestMessages(transientMessages);
|
||||
}
|
||||
return requestMessages;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* @todo refactor this module, most of the part is broken
|
||||
* reference
|
||||
* - https://github.com/run-llama/LlamaIndexTS/pull/531
|
||||
* - https://github.com/run-llama/LlamaIndexTS/pull/416
|
||||
*/
|
||||
import type { MessageContent } from "../llms";
|
||||
import { BaseRetriever } from "../retriever";
|
||||
import { BaseNode, TextNode } from "../schema";
|
||||
import { extractText } from "../utils";
|
||||
|
||||
// Assuming that necessary interfaces and classes (like OT, TextNode, BaseNode, etc.) are defined elsewhere
|
||||
// Import statements (e.g., for TextNode, BaseNode) should be added based on your project's structure
|
||||
export abstract class BaseObjectNodeMapping {
|
||||
// TypeScript doesn't support Python's classmethod directly, but we can use static methods as an alternative
|
||||
abstract fromObjects<OT>(objs: OT[], ...args: any[]): BaseObjectNodeMapping;
|
||||
|
||||
// Abstract methods in TypeScript
|
||||
abstract objNodeMapping(): Record<any, any>;
|
||||
abstract toNode(obj: any): TextNode;
|
||||
|
||||
// Concrete methods can be defined as usual
|
||||
validateObject(obj: any): void {}
|
||||
|
||||
// Implementing the add object logic
|
||||
addObj(obj: any): void {
|
||||
this.validateObject(obj);
|
||||
this._addObj(obj);
|
||||
}
|
||||
|
||||
// Abstract method for internal add object logic
|
||||
abstract _addObj(obj: any): void;
|
||||
|
||||
// Implementing toNodes method
|
||||
toNodes(objs: any[]): TextNode[] {
|
||||
return objs.map((obj) => this.toNode(obj));
|
||||
}
|
||||
|
||||
// Abstract method for internal from node logic
|
||||
abstract _fromNode(node: BaseNode): any;
|
||||
|
||||
// Implementing fromNode method
|
||||
fromNode(node: BaseNode): any {
|
||||
const obj = this._fromNode(node);
|
||||
this.validateObject(obj);
|
||||
return obj;
|
||||
}
|
||||
|
||||
// Abstract methods for persistence
|
||||
abstract persist(persistDir: string, objNodeMappingFilename: string): void;
|
||||
}
|
||||
|
||||
export class ObjectRetriever<T = unknown> {
|
||||
_retriever: BaseRetriever;
|
||||
_objectNodeMapping: BaseObjectNodeMapping;
|
||||
|
||||
constructor(
|
||||
retriever: BaseRetriever,
|
||||
objectNodeMapping: BaseObjectNodeMapping,
|
||||
) {
|
||||
this._retriever = retriever;
|
||||
this._objectNodeMapping = objectNodeMapping;
|
||||
}
|
||||
|
||||
// In TypeScript, getters are defined like this.
|
||||
get retriever(): BaseRetriever {
|
||||
return this._retriever;
|
||||
}
|
||||
|
||||
// Translating the retrieve method
|
||||
async retrieve(strOrQueryBundle: MessageContent): Promise<T[]> {
|
||||
const nodes = await this.retriever.retrieve({
|
||||
query: extractText(strOrQueryBundle),
|
||||
});
|
||||
const objs = nodes.map((n) => this._objectNodeMapping.fromNode(n.node));
|
||||
return objs;
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,8 @@
|
||||
import { randomUUID } from "@llamaindex/env";
|
||||
import { wrapEventCaller } from "../decorator";
|
||||
import { Settings } from "../global";
|
||||
import type { MessageContent } from "../llms";
|
||||
import { PromptMixin } from "../prompts";
|
||||
import { EngineResponse, type NodeWithScore } from "../schema";
|
||||
|
||||
/**
|
||||
@@ -14,16 +18,52 @@ export type QueryBundle = {
|
||||
|
||||
export type QueryType = string | QueryBundle;
|
||||
|
||||
export interface BaseQueryEngine {
|
||||
query(
|
||||
strOrQueryBundle: QueryType,
|
||||
stream: true,
|
||||
): Promise<AsyncIterable<EngineResponse>>;
|
||||
query(strOrQueryBundle: QueryType, stream?: false): Promise<EngineResponse>;
|
||||
export type BaseQueryParams = {
|
||||
query: QueryType;
|
||||
};
|
||||
|
||||
synthesize?(
|
||||
strOrQueryBundle: QueryType,
|
||||
nodes: NodeWithScore[],
|
||||
additionalSources?: Iterator<NodeWithScore>,
|
||||
): Promise<EngineResponse>;
|
||||
export interface StreamingQueryParams extends BaseQueryParams {
|
||||
stream: true;
|
||||
}
|
||||
|
||||
export interface NonStreamingQueryParams extends BaseQueryParams {
|
||||
stream?: false;
|
||||
}
|
||||
|
||||
export type QueryFn = (
|
||||
strOrQueryBundle: QueryType,
|
||||
stream?: boolean,
|
||||
) => Promise<AsyncIterable<EngineResponse> | EngineResponse>;
|
||||
|
||||
export abstract class BaseQueryEngine extends PromptMixin {
|
||||
protected constructor(protected readonly _query: QueryFn) {
|
||||
super();
|
||||
}
|
||||
|
||||
async retrieve(params: QueryType): Promise<NodeWithScore[]> {
|
||||
throw new Error(
|
||||
"This query engine does not support retrieve, use query directly",
|
||||
);
|
||||
}
|
||||
|
||||
query(params: StreamingQueryParams): Promise<AsyncIterable<EngineResponse>>;
|
||||
query(params: NonStreamingQueryParams): Promise<EngineResponse>;
|
||||
@wrapEventCaller
|
||||
async query(
|
||||
params: StreamingQueryParams | NonStreamingQueryParams,
|
||||
): Promise<EngineResponse | AsyncIterable<EngineResponse>> {
|
||||
const { stream, query } = params;
|
||||
const id = randomUUID();
|
||||
const callbackManager = Settings.callbackManager;
|
||||
callbackManager.dispatchEvent("query-start", {
|
||||
id,
|
||||
query,
|
||||
});
|
||||
const response = await this._query(query, stream);
|
||||
callbackManager.dispatchEvent("query-end", {
|
||||
id,
|
||||
response,
|
||||
});
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
export type { BaseQueryEngine, QueryBundle, QueryType } from "./base";
|
||||
export { BaseQueryEngine, type QueryBundle, type QueryType } from "./base";
|
||||
export type { QueryEndEvent, QueryStartEvent } from "./type";
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import { EngineResponse } from "../schema";
|
||||
import type { QueryType } from "./base";
|
||||
|
||||
export type QueryStartEvent = {
|
||||
id: string;
|
||||
query: QueryType;
|
||||
};
|
||||
|
||||
export type QueryEndEvent = {
|
||||
id: string;
|
||||
response: EngineResponse | AsyncIterable<EngineResponse>;
|
||||
};
|
||||
@@ -0,0 +1,58 @@
|
||||
import { randomUUID } from "@llamaindex/env";
|
||||
import { Settings } from "../global";
|
||||
import { PromptHelper } from "../indices";
|
||||
import type { LLM, MessageContent } from "../llms";
|
||||
import { PromptMixin } from "../prompts";
|
||||
import { EngineResponse, type NodeWithScore } from "../schema";
|
||||
import type { SynthesizeQuery } from "./type";
|
||||
|
||||
export type BaseSynthesizerOptions = {
|
||||
llm?: LLM;
|
||||
promptHelper?: PromptHelper;
|
||||
};
|
||||
|
||||
export abstract class BaseSynthesizer extends PromptMixin {
|
||||
llm: LLM;
|
||||
promptHelper: PromptHelper;
|
||||
|
||||
protected constructor(options: Partial<BaseSynthesizerOptions>) {
|
||||
super();
|
||||
this.llm = options.llm ?? Settings.llm;
|
||||
this.promptHelper =
|
||||
options.promptHelper ?? PromptHelper.fromLLMMetadata(this.llm.metadata);
|
||||
}
|
||||
|
||||
protected abstract getResponse(
|
||||
query: MessageContent,
|
||||
textChunks: NodeWithScore[],
|
||||
stream: boolean,
|
||||
): Promise<EngineResponse | AsyncIterable<EngineResponse>>;
|
||||
|
||||
synthesize(
|
||||
query: SynthesizeQuery,
|
||||
stream: true,
|
||||
): Promise<AsyncIterable<EngineResponse>>;
|
||||
synthesize(query: SynthesizeQuery, stream?: false): Promise<EngineResponse>;
|
||||
async synthesize(
|
||||
query: SynthesizeQuery,
|
||||
stream = false,
|
||||
): Promise<EngineResponse | AsyncIterable<EngineResponse>> {
|
||||
const callbackManager = Settings.callbackManager;
|
||||
const id = randomUUID();
|
||||
callbackManager.dispatchEvent("synthesize-start", { id, query });
|
||||
let response: EngineResponse | AsyncIterable<EngineResponse>;
|
||||
if (query.nodes.length === 0) {
|
||||
if (stream) {
|
||||
response = EngineResponse.fromResponse("Empty Response", true);
|
||||
} else {
|
||||
response = EngineResponse.fromResponse("Empty Response", false);
|
||||
}
|
||||
} else {
|
||||
const queryMessage: MessageContent =
|
||||
typeof query.query === "string" ? query.query : query.query.query;
|
||||
response = await this.getResponse(queryMessage, query.nodes, stream);
|
||||
}
|
||||
callbackManager.dispatchEvent("synthesize-end", { id, query, response });
|
||||
return response;
|
||||
}
|
||||
}
|
||||
+192
-164
@@ -1,108 +1,53 @@
|
||||
import { getBiggestPrompt, type PromptHelper } from "@llamaindex/core/indices";
|
||||
import type { LLM } from "@llamaindex/core/llms";
|
||||
import { z } from "zod";
|
||||
import { getBiggestPrompt } from "../indices";
|
||||
import type { MessageContent } from "../llms";
|
||||
import {
|
||||
PromptMixin,
|
||||
defaultRefinePrompt,
|
||||
defaultTextQAPrompt,
|
||||
defaultTreeSummarizePrompt,
|
||||
type ModuleRecord,
|
||||
type PromptsRecord,
|
||||
type RefinePrompt,
|
||||
type TextQAPrompt,
|
||||
type TreeSummarizePrompt,
|
||||
} from "@llamaindex/core/prompts";
|
||||
import type { QueryType } from "@llamaindex/core/query-engine";
|
||||
import { extractText, streamConverter } from "@llamaindex/core/utils";
|
||||
import type { ServiceContext } from "../ServiceContext.js";
|
||||
} from "../prompts";
|
||||
import {
|
||||
llmFromSettingsOrContext,
|
||||
promptHelperFromSettingsOrContext,
|
||||
} from "../Settings.js";
|
||||
import type { ResponseBuilder, ResponseBuilderQuery } from "./types.js";
|
||||
EngineResponse,
|
||||
MetadataMode,
|
||||
type NodeWithScore,
|
||||
TextNode,
|
||||
} from "../schema";
|
||||
import { extractText, streamConverter } from "../utils";
|
||||
import {
|
||||
BaseSynthesizer,
|
||||
type BaseSynthesizerOptions,
|
||||
} from "./base-synthesizer";
|
||||
import { createMessageContent } from "./utils";
|
||||
|
||||
/**
|
||||
* Response modes of the response synthesizer
|
||||
*/
|
||||
enum ResponseMode {
|
||||
REFINE = "refine",
|
||||
COMPACT = "compact",
|
||||
TREE_SUMMARIZE = "tree_summarize",
|
||||
SIMPLE = "simple",
|
||||
}
|
||||
const responseModeSchema = z.enum([
|
||||
"refine",
|
||||
"compact",
|
||||
"tree_summarize",
|
||||
"multi_modal",
|
||||
]);
|
||||
|
||||
/**
|
||||
* A response builder that just concatenates responses.
|
||||
*/
|
||||
export class SimpleResponseBuilder
|
||||
extends PromptMixin
|
||||
implements ResponseBuilder
|
||||
{
|
||||
llm: LLM;
|
||||
textQATemplate: TextQAPrompt;
|
||||
|
||||
constructor(serviceContext?: ServiceContext, textQATemplate?: TextQAPrompt) {
|
||||
super();
|
||||
this.llm = llmFromSettingsOrContext(serviceContext);
|
||||
this.textQATemplate = textQATemplate ?? defaultTextQAPrompt;
|
||||
}
|
||||
|
||||
protected _getPrompts(): PromptsRecord {
|
||||
return {
|
||||
textQATemplate: this.textQATemplate,
|
||||
};
|
||||
}
|
||||
protected _updatePrompts(prompts: { textQATemplate: TextQAPrompt }): void {
|
||||
if (prompts.textQATemplate) {
|
||||
this.textQATemplate = prompts.textQATemplate;
|
||||
}
|
||||
}
|
||||
protected _getPromptModules(): ModuleRecord {
|
||||
return {};
|
||||
}
|
||||
|
||||
getResponse(
|
||||
query: ResponseBuilderQuery,
|
||||
stream: true,
|
||||
): Promise<AsyncIterable<string>>;
|
||||
getResponse(query: ResponseBuilderQuery, stream?: false): Promise<string>;
|
||||
async getResponse(
|
||||
{ query, textChunks }: ResponseBuilderQuery,
|
||||
stream?: boolean,
|
||||
): Promise<AsyncIterable<string> | string> {
|
||||
const prompt = this.textQATemplate.format({
|
||||
query: extractText(query),
|
||||
context: textChunks.join("\n\n"),
|
||||
});
|
||||
if (stream) {
|
||||
const response = await this.llm.complete({ prompt, stream: true });
|
||||
return streamConverter(response, (chunk) => chunk.text);
|
||||
} else {
|
||||
const response = await this.llm.complete({ prompt, stream: false });
|
||||
return response.text;
|
||||
}
|
||||
}
|
||||
}
|
||||
export type ResponseMode = z.infer<typeof responseModeSchema>;
|
||||
|
||||
/**
|
||||
* A response builder that uses the query to ask the LLM generate a better response using multiple text chunks.
|
||||
*/
|
||||
export class Refine extends PromptMixin implements ResponseBuilder {
|
||||
llm: LLM;
|
||||
promptHelper: PromptHelper;
|
||||
class Refine extends BaseSynthesizer {
|
||||
textQATemplate: TextQAPrompt;
|
||||
refineTemplate: RefinePrompt;
|
||||
|
||||
constructor(
|
||||
serviceContext?: ServiceContext,
|
||||
textQATemplate?: TextQAPrompt,
|
||||
refineTemplate?: RefinePrompt,
|
||||
options: BaseSynthesizerOptions & {
|
||||
textQATemplate?: TextQAPrompt | undefined;
|
||||
refineTemplate?: RefinePrompt | undefined;
|
||||
},
|
||||
) {
|
||||
super();
|
||||
|
||||
this.llm = llmFromSettingsOrContext(serviceContext);
|
||||
this.promptHelper = promptHelperFromSettingsOrContext(serviceContext);
|
||||
this.textQATemplate = textQATemplate ?? defaultTextQAPrompt;
|
||||
this.refineTemplate = refineTemplate ?? defaultRefinePrompt;
|
||||
super(options);
|
||||
this.textQATemplate = options.textQATemplate ?? defaultTextQAPrompt;
|
||||
this.refineTemplate = options.refineTemplate ?? defaultRefinePrompt;
|
||||
}
|
||||
|
||||
protected _getPromptModules(): ModuleRecord {
|
||||
@@ -132,41 +77,47 @@ export class Refine extends PromptMixin implements ResponseBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
getResponse(
|
||||
query: ResponseBuilderQuery,
|
||||
stream: true,
|
||||
): Promise<AsyncIterable<string>>;
|
||||
getResponse(query: ResponseBuilderQuery, stream?: false): Promise<string>;
|
||||
async getResponse(
|
||||
{ query, textChunks, prevResponse }: ResponseBuilderQuery,
|
||||
stream?: boolean,
|
||||
): Promise<AsyncIterable<string> | string> {
|
||||
let response: AsyncIterable<string> | string | undefined = prevResponse;
|
||||
query: MessageContent,
|
||||
nodes: NodeWithScore[],
|
||||
stream: boolean,
|
||||
): Promise<EngineResponse | AsyncIterable<EngineResponse>> {
|
||||
let response: AsyncIterable<string> | string | undefined = undefined;
|
||||
const textChunks = nodes.map(({ node }) =>
|
||||
node.getContent(MetadataMode.LLM),
|
||||
);
|
||||
|
||||
for (let i = 0; i < textChunks.length; i++) {
|
||||
const chunk = textChunks[i]!;
|
||||
const text = textChunks[i]!;
|
||||
const lastChunk = i === textChunks.length - 1;
|
||||
if (!response) {
|
||||
response = await this.giveResponseSingle(
|
||||
query,
|
||||
chunk,
|
||||
text,
|
||||
!!stream && lastChunk,
|
||||
);
|
||||
} else {
|
||||
response = await this.refineResponseSingle(
|
||||
response as string,
|
||||
query,
|
||||
chunk,
|
||||
text,
|
||||
!!stream && lastChunk,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return response ?? "Empty Response";
|
||||
// fixme: no source nodes provided, cannot fix right now due to lack of context
|
||||
if (typeof response === "string") {
|
||||
return EngineResponse.fromResponse(response, false);
|
||||
} else {
|
||||
return streamConverter(response!, (text) =>
|
||||
EngineResponse.fromResponse(text, true),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private async giveResponseSingle(
|
||||
query: QueryType,
|
||||
query: MessageContent,
|
||||
textChunk: string,
|
||||
stream: boolean,
|
||||
): Promise<AsyncIterable<string> | string> {
|
||||
@@ -203,10 +154,10 @@ export class Refine extends PromptMixin implements ResponseBuilder {
|
||||
// eslint-disable-next-line max-params
|
||||
private async refineResponseSingle(
|
||||
initialReponse: string,
|
||||
query: QueryType,
|
||||
query: MessageContent,
|
||||
textChunk: string,
|
||||
stream: boolean,
|
||||
) {
|
||||
): Promise<AsyncIterable<string> | string> {
|
||||
const refineTemplate: RefinePrompt = this.refineTemplate.partialFormat({
|
||||
query: extractText(query),
|
||||
});
|
||||
@@ -246,59 +197,54 @@ export class Refine extends PromptMixin implements ResponseBuilder {
|
||||
/**
|
||||
* CompactAndRefine is a slight variation of Refine that first compacts the text chunks into the smallest possible number of chunks.
|
||||
*/
|
||||
export class CompactAndRefine extends Refine {
|
||||
getResponse(
|
||||
query: ResponseBuilderQuery,
|
||||
stream: true,
|
||||
): Promise<AsyncIterable<string>>;
|
||||
getResponse(query: ResponseBuilderQuery, stream?: false): Promise<string>;
|
||||
class CompactAndRefine extends Refine {
|
||||
async getResponse(
|
||||
{ query, textChunks, prevResponse }: ResponseBuilderQuery,
|
||||
stream?: boolean,
|
||||
): Promise<AsyncIterable<string> | string> {
|
||||
query: MessageContent,
|
||||
nodes: NodeWithScore[],
|
||||
stream: boolean,
|
||||
): Promise<EngineResponse | AsyncIterable<EngineResponse>> {
|
||||
const textQATemplate: TextQAPrompt = this.textQATemplate.partialFormat({
|
||||
query: extractText(query),
|
||||
});
|
||||
const refineTemplate: RefinePrompt = this.refineTemplate.partialFormat({
|
||||
query: extractText(query),
|
||||
});
|
||||
const textChunks = nodes.map(({ node }) =>
|
||||
node.getContent(MetadataMode.LLM),
|
||||
);
|
||||
|
||||
const maxPrompt = getBiggestPrompt([textQATemplate, refineTemplate]);
|
||||
const newTexts = this.promptHelper.repack(maxPrompt, textChunks);
|
||||
const params = {
|
||||
query,
|
||||
textChunks: newTexts,
|
||||
prevResponse,
|
||||
};
|
||||
const newNodes = newTexts.map((text) => new TextNode({ text }));
|
||||
if (stream) {
|
||||
return super.getResponse(
|
||||
{
|
||||
...params,
|
||||
},
|
||||
query,
|
||||
newNodes.map((node) => ({ node })),
|
||||
true,
|
||||
);
|
||||
}
|
||||
return super.getResponse(params);
|
||||
return super.getResponse(
|
||||
query,
|
||||
newNodes.map((node) => ({ node })),
|
||||
false,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* TreeSummarize repacks the text chunks into the smallest possible number of chunks and then summarizes them, then recursively does so until there's one chunk left.
|
||||
*/
|
||||
export class TreeSummarize extends PromptMixin implements ResponseBuilder {
|
||||
llm: LLM;
|
||||
promptHelper: PromptHelper;
|
||||
class TreeSummarize extends BaseSynthesizer {
|
||||
summaryTemplate: TreeSummarizePrompt;
|
||||
|
||||
constructor(
|
||||
serviceContext?: ServiceContext,
|
||||
summaryTemplate?: TreeSummarizePrompt,
|
||||
options: BaseSynthesizerOptions & {
|
||||
summaryTemplate?: TreeSummarizePrompt;
|
||||
},
|
||||
) {
|
||||
super();
|
||||
|
||||
this.llm = llmFromSettingsOrContext(serviceContext);
|
||||
this.promptHelper = promptHelperFromSettingsOrContext(serviceContext);
|
||||
this.summaryTemplate = summaryTemplate ?? defaultTreeSummarizePrompt;
|
||||
super(options);
|
||||
this.summaryTemplate =
|
||||
options.summaryTemplate ?? defaultTreeSummarizePrompt;
|
||||
}
|
||||
|
||||
protected _getPromptModules(): ModuleRecord {
|
||||
@@ -319,15 +265,14 @@ export class TreeSummarize extends PromptMixin implements ResponseBuilder {
|
||||
}
|
||||
}
|
||||
|
||||
getResponse(
|
||||
query: ResponseBuilderQuery,
|
||||
stream: true,
|
||||
): Promise<AsyncIterable<string>>;
|
||||
getResponse(query: ResponseBuilderQuery, stream?: false): Promise<string>;
|
||||
async getResponse(
|
||||
{ query, textChunks }: ResponseBuilderQuery,
|
||||
stream?: boolean,
|
||||
): Promise<AsyncIterable<string> | string> {
|
||||
query: MessageContent,
|
||||
nodes: NodeWithScore[],
|
||||
stream: boolean,
|
||||
): Promise<EngineResponse | AsyncIterable<EngineResponse>> {
|
||||
const textChunks = nodes.map(({ node }) =>
|
||||
node.getContent(MetadataMode.LLM),
|
||||
);
|
||||
if (!textChunks || textChunks.length === 0) {
|
||||
throw new Error("Must have at least one text chunk");
|
||||
}
|
||||
@@ -347,9 +292,14 @@ export class TreeSummarize extends PromptMixin implements ResponseBuilder {
|
||||
};
|
||||
if (stream) {
|
||||
const response = await this.llm.complete({ ...params, stream });
|
||||
return streamConverter(response, (chunk) => chunk.text);
|
||||
return streamConverter(response, (chunk) =>
|
||||
EngineResponse.fromResponse(chunk.text, true),
|
||||
);
|
||||
}
|
||||
return (await this.llm.complete(params)).text;
|
||||
return EngineResponse.fromResponse(
|
||||
(await this.llm.complete(params)).text,
|
||||
false,
|
||||
);
|
||||
} else {
|
||||
const summaries = await Promise.all(
|
||||
packedTextChunks.map((chunk) =>
|
||||
@@ -362,40 +312,118 @@ export class TreeSummarize extends PromptMixin implements ResponseBuilder {
|
||||
),
|
||||
);
|
||||
|
||||
const params = {
|
||||
query,
|
||||
textChunks: summaries.map((s) => s.text),
|
||||
};
|
||||
if (stream) {
|
||||
return this.getResponse(
|
||||
{
|
||||
...params,
|
||||
},
|
||||
query,
|
||||
summaries.map((s) => ({
|
||||
node: new TextNode({
|
||||
text: s.text,
|
||||
}),
|
||||
})),
|
||||
true,
|
||||
);
|
||||
}
|
||||
return this.getResponse(params);
|
||||
return this.getResponse(
|
||||
query,
|
||||
summaries.map((s) => ({
|
||||
node: new TextNode({
|
||||
text: s.text,
|
||||
}),
|
||||
})),
|
||||
false,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function getResponseBuilder(
|
||||
serviceContext?: ServiceContext,
|
||||
responseMode?: ResponseMode,
|
||||
): ResponseBuilder {
|
||||
switch (responseMode) {
|
||||
case ResponseMode.SIMPLE:
|
||||
return new SimpleResponseBuilder(serviceContext);
|
||||
case ResponseMode.REFINE:
|
||||
return new Refine(serviceContext);
|
||||
case ResponseMode.TREE_SUMMARIZE:
|
||||
return new TreeSummarize(serviceContext);
|
||||
default:
|
||||
return new CompactAndRefine(serviceContext);
|
||||
class MultiModal extends BaseSynthesizer {
|
||||
metadataMode: MetadataMode;
|
||||
textQATemplate: TextQAPrompt;
|
||||
|
||||
constructor({
|
||||
textQATemplate,
|
||||
metadataMode,
|
||||
...options
|
||||
}: BaseSynthesizerOptions & {
|
||||
textQATemplate?: TextQAPrompt;
|
||||
metadataMode?: MetadataMode;
|
||||
} = {}) {
|
||||
super(options);
|
||||
|
||||
this.metadataMode = metadataMode ?? MetadataMode.NONE;
|
||||
this.textQATemplate = textQATemplate ?? defaultTextQAPrompt;
|
||||
}
|
||||
|
||||
protected _getPromptModules(): ModuleRecord {
|
||||
return {};
|
||||
}
|
||||
|
||||
protected _getPrompts(): { textQATemplate: TextQAPrompt } {
|
||||
return {
|
||||
textQATemplate: this.textQATemplate,
|
||||
};
|
||||
}
|
||||
|
||||
protected _updatePrompts(promptsDict: {
|
||||
textQATemplate: TextQAPrompt;
|
||||
}): void {
|
||||
if (promptsDict.textQATemplate) {
|
||||
this.textQATemplate = promptsDict.textQATemplate;
|
||||
}
|
||||
}
|
||||
|
||||
protected async getResponse(
|
||||
query: MessageContent,
|
||||
nodes: NodeWithScore[],
|
||||
stream: boolean,
|
||||
): Promise<EngineResponse | AsyncIterable<EngineResponse>> {
|
||||
const prompt = await createMessageContent(
|
||||
this.textQATemplate,
|
||||
nodes.map(({ node }) => node),
|
||||
// this might not be good as this remove the image information
|
||||
{ query: extractText(query) },
|
||||
this.metadataMode,
|
||||
);
|
||||
|
||||
const llm = this.llm;
|
||||
|
||||
if (stream) {
|
||||
const response = await llm.complete({
|
||||
prompt,
|
||||
stream,
|
||||
});
|
||||
return streamConverter(response, ({ text }) =>
|
||||
EngineResponse.fromResponse(text, true),
|
||||
);
|
||||
}
|
||||
const response = await llm.complete({
|
||||
prompt,
|
||||
});
|
||||
return EngineResponse.fromResponse(response.text, false);
|
||||
}
|
||||
}
|
||||
|
||||
export type ResponseBuilderPrompts =
|
||||
| TextQAPrompt
|
||||
| TreeSummarizePrompt
|
||||
| RefinePrompt;
|
||||
export function getResponseSynthesizer(
|
||||
mode: ResponseMode,
|
||||
options: BaseSynthesizerOptions & {
|
||||
textQATemplate?: TextQAPrompt;
|
||||
refineTemplate?: RefinePrompt;
|
||||
summaryTemplate?: TreeSummarizePrompt;
|
||||
metadataMode?: MetadataMode;
|
||||
} = {},
|
||||
) {
|
||||
switch (mode) {
|
||||
case "compact": {
|
||||
return new CompactAndRefine(options);
|
||||
}
|
||||
case "refine": {
|
||||
return new Refine(options);
|
||||
}
|
||||
case "tree_summarize": {
|
||||
return new TreeSummarize(options);
|
||||
}
|
||||
case "multi_modal": {
|
||||
return new MultiModal(options);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
export {
|
||||
BaseSynthesizer,
|
||||
type BaseSynthesizerOptions,
|
||||
} from "./base-synthesizer";
|
||||
export { getResponseSynthesizer, type ResponseMode } from "./factory";
|
||||
export type {
|
||||
SynthesizeEndEvent,
|
||||
SynthesizeQuery,
|
||||
SynthesizeStartEvent,
|
||||
} from "./type";
|
||||
export { createMessageContent } from "./utils";
|
||||
@@ -0,0 +1,19 @@
|
||||
import type { QueryType } from "../query-engine";
|
||||
import { EngineResponse, type NodeWithScore } from "../schema";
|
||||
|
||||
export type SynthesizeQuery = {
|
||||
query: QueryType;
|
||||
nodes: NodeWithScore[];
|
||||
additionalSourceNodes?: NodeWithScore[];
|
||||
};
|
||||
|
||||
export type SynthesizeStartEvent = {
|
||||
id: string;
|
||||
query: SynthesizeQuery;
|
||||
};
|
||||
|
||||
export type SynthesizeEndEvent = {
|
||||
id: string;
|
||||
query: SynthesizeQuery;
|
||||
response: EngineResponse | AsyncIterable<EngineResponse>;
|
||||
};
|
||||
+33
-33
@@ -1,46 +1,20 @@
|
||||
import type { MessageContentDetail } from "@llamaindex/core/llms";
|
||||
import type { BasePromptTemplate } from "@llamaindex/core/prompts";
|
||||
// eslint-disable-next-line max-params
|
||||
import type { MessageContentDetail } from "../llms";
|
||||
import type { BasePromptTemplate } from "../prompts";
|
||||
import {
|
||||
type BaseNode,
|
||||
ImageNode,
|
||||
MetadataMode,
|
||||
ModalityType,
|
||||
splitNodesByType,
|
||||
type BaseNode,
|
||||
} from "@llamaindex/core/schema";
|
||||
import { imageToDataUrl } from "../internal/utils.js";
|
||||
} from "../schema";
|
||||
import { imageToDataUrl } from "../utils";
|
||||
|
||||
export async function createMessageContent(
|
||||
prompt: BasePromptTemplate,
|
||||
nodes: BaseNode[],
|
||||
extraParams: Record<string, string | undefined> = {},
|
||||
metadataMode: MetadataMode = MetadataMode.NONE,
|
||||
): Promise<MessageContentDetail[]> {
|
||||
const content: MessageContentDetail[] = [];
|
||||
const nodeMap = splitNodesByType(nodes);
|
||||
for (const type in nodeMap) {
|
||||
// for each retrieved modality type, create message content
|
||||
const nodes = nodeMap[type as ModalityType];
|
||||
if (nodes) {
|
||||
content.push(
|
||||
...(await createContentPerModality(
|
||||
prompt,
|
||||
type as ModalityType,
|
||||
nodes,
|
||||
extraParams,
|
||||
metadataMode,
|
||||
)),
|
||||
);
|
||||
}
|
||||
}
|
||||
return content;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line max-params
|
||||
async function createContentPerModality(
|
||||
prompt: BasePromptTemplate,
|
||||
type: ModalityType,
|
||||
nodes: BaseNode[],
|
||||
extraParams: Record<string, string | undefined>,
|
||||
extraParams: Record<string, string>,
|
||||
metadataMode: MetadataMode,
|
||||
): Promise<MessageContentDetail[]> {
|
||||
switch (type) {
|
||||
@@ -70,3 +44,29 @@ async function createContentPerModality(
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export async function createMessageContent(
|
||||
prompt: BasePromptTemplate,
|
||||
nodes: BaseNode[],
|
||||
extraParams: Record<string, string> = {},
|
||||
metadataMode: MetadataMode = MetadataMode.NONE,
|
||||
): Promise<MessageContentDetail[]> {
|
||||
const content: MessageContentDetail[] = [];
|
||||
const nodeMap = splitNodesByType(nodes);
|
||||
for (const type in nodeMap) {
|
||||
// for each retrieved modality type, create message content
|
||||
const nodes = nodeMap[type as ModalityType];
|
||||
if (nodes) {
|
||||
content.push(
|
||||
...(await createContentPerModality(
|
||||
prompt,
|
||||
type as ModalityType,
|
||||
nodes,
|
||||
extraParams,
|
||||
metadataMode,
|
||||
)),
|
||||
);
|
||||
}
|
||||
}
|
||||
return content;
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
import { randomUUID } from "@llamaindex/env";
|
||||
import { Settings } from "../global";
|
||||
import type { MessageContent } from "../llms";
|
||||
import { PromptMixin } from "../prompts";
|
||||
import type { QueryBundle, QueryType } from "../query-engine";
|
||||
import { BaseNode, IndexNode, type NodeWithScore, ObjectType } from "../schema";
|
||||
|
||||
export type RetrieveParams = {
|
||||
query: MessageContent;
|
||||
preFilters?: unknown;
|
||||
};
|
||||
|
||||
export type RetrieveStartEvent = {
|
||||
id: string;
|
||||
query: QueryBundle;
|
||||
};
|
||||
|
||||
export type RetrieveEndEvent = {
|
||||
id: string;
|
||||
query: QueryBundle;
|
||||
nodes: NodeWithScore[];
|
||||
};
|
||||
|
||||
export abstract class BaseRetriever extends PromptMixin {
|
||||
objectMap: Map<string, unknown> = new Map();
|
||||
|
||||
protected _updatePrompts() {}
|
||||
protected _getPrompts() {
|
||||
return {};
|
||||
}
|
||||
|
||||
protected _getPromptModules() {
|
||||
return {};
|
||||
}
|
||||
|
||||
protected constructor() {
|
||||
super();
|
||||
}
|
||||
|
||||
public async retrieve(params: QueryType): Promise<NodeWithScore[]> {
|
||||
const cb = Settings.callbackManager;
|
||||
const queryBundle = typeof params === "string" ? { query: params } : params;
|
||||
const id = randomUUID();
|
||||
cb.dispatchEvent("retrieve-start", { id, query: queryBundle });
|
||||
let response = await this._retrieve(queryBundle);
|
||||
response = await this._handleRecursiveRetrieval(queryBundle, response);
|
||||
cb.dispatchEvent("retrieve-end", {
|
||||
id,
|
||||
query: queryBundle,
|
||||
nodes: response,
|
||||
});
|
||||
return response;
|
||||
}
|
||||
|
||||
abstract _retrieve(params: QueryBundle): Promise<NodeWithScore[]>;
|
||||
|
||||
async _handleRecursiveRetrieval(
|
||||
params: QueryBundle,
|
||||
nodes: NodeWithScore[],
|
||||
): Promise<NodeWithScore[]> {
|
||||
const retrievedNodes = [];
|
||||
for (const { node, score = 1.0 } of nodes) {
|
||||
if (node.type === ObjectType.INDEX) {
|
||||
const indexNode = node as IndexNode;
|
||||
const object = this.objectMap.get(indexNode.indexId);
|
||||
if (object !== undefined) {
|
||||
retrievedNodes.push(
|
||||
...this._retrieveFromObject(object, params, score),
|
||||
);
|
||||
} else {
|
||||
retrievedNodes.push({ node, score });
|
||||
}
|
||||
} else {
|
||||
retrievedNodes.push({ node, score });
|
||||
}
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
|
||||
_retrieveFromObject(
|
||||
object: unknown,
|
||||
queryBundle: QueryBundle,
|
||||
score: number,
|
||||
): NodeWithScore[] {
|
||||
if (object == null) {
|
||||
throw new TypeError("Object is not retrievable");
|
||||
}
|
||||
if (typeof object !== "object") {
|
||||
throw new TypeError("Object is not retrievable");
|
||||
}
|
||||
if ("node" in object && object.node instanceof BaseNode) {
|
||||
return [
|
||||
{
|
||||
node: object.node,
|
||||
score:
|
||||
"score" in object && typeof object.score === "number"
|
||||
? object.score
|
||||
: score,
|
||||
},
|
||||
];
|
||||
}
|
||||
if (object instanceof BaseNode) {
|
||||
return [{ node: object, score }];
|
||||
} else {
|
||||
// todo: support other types
|
||||
// BaseQueryEngine
|
||||
// BaseRetriever
|
||||
// QueryComponent
|
||||
throw new TypeError("Object is not retrievable");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { createSHA256, path, randomUUID } from "@llamaindex/env";
|
||||
import { chunkSizeCheck, lazyInitHash } from "../decorator";
|
||||
import { lazyInitHash } from "../decorator";
|
||||
import { chunkSizeCheck } from "./utils/chunk-size-check";
|
||||
|
||||
export enum NodeRelationship {
|
||||
SOURCE = "SOURCE",
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import { getEnv } from "@llamaindex/env";
|
||||
import { Settings } from "../../global";
|
||||
|
||||
const emitOnce = false;
|
||||
|
||||
export function chunkSizeCheck<
|
||||
This extends { id_: string },
|
||||
Args extends any[],
|
||||
Return,
|
||||
>(
|
||||
contentGetter: (this: This, ...args: Args) => string,
|
||||
_context: ClassMethodDecoratorContext<
|
||||
This,
|
||||
(this: This, ...args: Args) => Return
|
||||
>,
|
||||
) {
|
||||
return function (this: This, ...args: Args) {
|
||||
const content = contentGetter.call(this, ...args);
|
||||
const chunkSize = Settings.chunkSize;
|
||||
const enableChunkSizeCheck = getEnv("ENABLE_CHUNK_SIZE_CHECK") === "true";
|
||||
if (
|
||||
enableChunkSizeCheck &&
|
||||
chunkSize !== undefined &&
|
||||
content.length > chunkSize
|
||||
) {
|
||||
console.warn(
|
||||
`Node (${this.id_}) is larger than chunk size: ${content.length} > ${chunkSize}`,
|
||||
);
|
||||
if (!emitOnce) {
|
||||
console.warn(
|
||||
"Will truncate the content if it is larger than chunk size",
|
||||
);
|
||||
console.warn("If you want to disable this behavior:");
|
||||
console.warn(" 1. Set Settings.chunkSize = undefined");
|
||||
console.warn(" 2. Set Settings.chunkSize to a larger value");
|
||||
console.warn(
|
||||
" 3. Change the way of splitting content into smaller chunks",
|
||||
);
|
||||
}
|
||||
return content.slice(0, chunkSize);
|
||||
}
|
||||
return content;
|
||||
};
|
||||
}
|
||||
@@ -1,110 +0,0 @@
|
||||
import { AsyncLocalStorage, randomUUID } from "@llamaindex/env";
|
||||
|
||||
export const isAsyncIterable = (
|
||||
obj: unknown,
|
||||
): obj is AsyncIterable<unknown> => {
|
||||
return obj != null && typeof obj === "object" && Symbol.asyncIterator in obj;
|
||||
};
|
||||
|
||||
export const isIterable = (obj: unknown): obj is Iterable<unknown> => {
|
||||
return obj != null && typeof obj === "object" && Symbol.iterator in obj;
|
||||
};
|
||||
|
||||
const eventReasonAsyncLocalStorage = new AsyncLocalStorage<EventCaller>();
|
||||
|
||||
/**
|
||||
* EventCaller is used to track the caller of an event.
|
||||
*/
|
||||
export class EventCaller {
|
||||
public readonly id = randomUUID();
|
||||
|
||||
private constructor(
|
||||
public readonly caller: unknown,
|
||||
public readonly parent: EventCaller | null,
|
||||
) {}
|
||||
|
||||
#computedCallers: unknown[] | null = null;
|
||||
|
||||
public get computedCallers(): unknown[] {
|
||||
if (this.#computedCallers != null) {
|
||||
return this.#computedCallers;
|
||||
}
|
||||
const callers = [this.caller];
|
||||
let parent = this.parent;
|
||||
while (parent != null) {
|
||||
callers.push(parent.caller);
|
||||
parent = parent.parent;
|
||||
}
|
||||
this.#computedCallers = callers;
|
||||
return callers;
|
||||
}
|
||||
|
||||
public static create(
|
||||
caller: unknown,
|
||||
parent: EventCaller | null,
|
||||
): EventCaller {
|
||||
return new EventCaller(caller, parent);
|
||||
}
|
||||
}
|
||||
|
||||
export function getEventCaller(): EventCaller | null {
|
||||
return eventReasonAsyncLocalStorage.getStore() ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param caller who is calling this function, pass in `this` if it's a class method
|
||||
* @param fn
|
||||
*/
|
||||
function withEventCaller<T>(caller: unknown, fn: () => T) {
|
||||
// create a chain of event callers
|
||||
const parentCaller = getEventCaller();
|
||||
return eventReasonAsyncLocalStorage.run(
|
||||
EventCaller.create(caller, parentCaller),
|
||||
fn,
|
||||
);
|
||||
}
|
||||
|
||||
export function wrapEventCaller<This, Result, Args extends unknown[]>(
|
||||
originalMethod: (this: This, ...args: Args) => Result,
|
||||
context: ClassMethodDecoratorContext<object>,
|
||||
) {
|
||||
const name = context.name;
|
||||
context.addInitializer(function () {
|
||||
// @ts-expect-error
|
||||
const fn = this[name].bind(this);
|
||||
// @ts-expect-error
|
||||
this[name] = (...args: unknown[]) => {
|
||||
return withEventCaller(this, () => fn(...args));
|
||||
};
|
||||
});
|
||||
return function (this: This, ...args: Args): Result {
|
||||
const result = originalMethod.call(this, ...args);
|
||||
// patch for iterators because AsyncLocalStorage doesn't work with them
|
||||
if (isAsyncIterable(result)) {
|
||||
const iter = result[Symbol.asyncIterator]();
|
||||
const snapshot = AsyncLocalStorage.snapshot();
|
||||
return (async function* asyncGeneratorWrapper() {
|
||||
while (true) {
|
||||
const { value, done } = await snapshot(() => iter.next());
|
||||
if (done) {
|
||||
break;
|
||||
}
|
||||
yield value;
|
||||
}
|
||||
})() as Result;
|
||||
} else if (isIterable(result)) {
|
||||
const iter = result[Symbol.iterator]();
|
||||
const snapshot = AsyncLocalStorage.snapshot();
|
||||
return (function* generatorWrapper() {
|
||||
while (true) {
|
||||
const { value, done } = snapshot(() => iter.next());
|
||||
if (done) {
|
||||
break;
|
||||
}
|
||||
yield value;
|
||||
}
|
||||
})() as Result;
|
||||
}
|
||||
return result;
|
||||
};
|
||||
}
|
||||
@@ -1,4 +1,14 @@
|
||||
export { wrapEventCaller } from "./event-caller";
|
||||
import type { JSONValue } from "../global";
|
||||
|
||||
export const isAsyncIterable = (
|
||||
obj: unknown,
|
||||
): obj is AsyncIterable<unknown> => {
|
||||
return obj != null && typeof obj === "object" && Symbol.asyncIterator in obj;
|
||||
};
|
||||
|
||||
export const isIterable = (obj: unknown): obj is Iterable<unknown> => {
|
||||
return obj != null && typeof obj === "object" && Symbol.iterator in obj;
|
||||
};
|
||||
|
||||
export async function* streamConverter<S, D>(
|
||||
stream: AsyncIterable<S>,
|
||||
@@ -44,13 +54,27 @@ export async function* streamReducer<S, D>(params: {
|
||||
}
|
||||
}
|
||||
|
||||
export { wrapLLMEvent } from "./wrap-llm-event";
|
||||
/**
|
||||
* Prettify an error for AI to read
|
||||
*/
|
||||
export function prettifyError(error: unknown): string {
|
||||
if (error instanceof Error) {
|
||||
return `Error(${error.name}): ${error.message}`;
|
||||
} else {
|
||||
return `${error}`;
|
||||
}
|
||||
}
|
||||
|
||||
export function stringifyJSONToMessageContent(value: JSONValue): string {
|
||||
return JSON.stringify(value, null, 2).replace(/"([^"]*)"/g, "$1");
|
||||
}
|
||||
|
||||
export {
|
||||
extractDataUrlComponents,
|
||||
extractImage,
|
||||
extractSingleText,
|
||||
extractText,
|
||||
imageToDataUrl,
|
||||
messagesToHistory,
|
||||
toToolDescriptions,
|
||||
} from "./llms";
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { fs } from "@llamaindex/env";
|
||||
import { filetypemime } from "magic-bytes.js";
|
||||
import type {
|
||||
ChatMessage,
|
||||
MessageContent,
|
||||
@@ -107,3 +109,37 @@ export function toToolDescriptions(tools: ToolMetadata[]): string {
|
||||
|
||||
return JSON.stringify(toolsObj, null, 4);
|
||||
}
|
||||
|
||||
async function blobToDataUrl(input: Blob) {
|
||||
const buffer = Buffer.from(await input.arrayBuffer());
|
||||
const mimes = filetypemime(buffer);
|
||||
if (mimes.length < 1) {
|
||||
throw new Error("Unsupported image type");
|
||||
}
|
||||
return "data:" + mimes[0] + ";base64," + buffer.toString("base64");
|
||||
}
|
||||
|
||||
export async function imageToDataUrl(
|
||||
input: ImageType | Uint8Array,
|
||||
): Promise<string> {
|
||||
// first ensure, that the input is a Blob
|
||||
if (
|
||||
(input instanceof URL && input.protocol === "file:") ||
|
||||
typeof input === "string"
|
||||
) {
|
||||
// string or file URL
|
||||
const dataBuffer = await fs.readFile(
|
||||
input instanceof URL ? input.pathname : input,
|
||||
);
|
||||
input = new Blob([dataBuffer]);
|
||||
} else if (!(input instanceof Blob)) {
|
||||
if (input instanceof URL) {
|
||||
throw new Error(`Unsupported URL with protocol: ${input.protocol}`);
|
||||
} else if (input instanceof Uint8Array) {
|
||||
input = new Blob([input]); // convert Uint8Array to Blob
|
||||
} else {
|
||||
throw new Error(`Unsupported input type: ${typeof input}`);
|
||||
}
|
||||
}
|
||||
return await blobToDataUrl(input);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
/**
|
||||
* should compatible with npm:pg and npm:postgres
|
||||
*/
|
||||
export interface IsomorphicDB {
|
||||
query: (sql: string, params?: any[]) => Promise<any[]>;
|
||||
// begin will wrap the multiple queries in a transaction
|
||||
begin: <T>(fn: (query: IsomorphicDB["query"]) => Promise<T>) => Promise<T>;
|
||||
|
||||
// event handler
|
||||
connect: () => Promise<void>;
|
||||
close: () => Promise<void>;
|
||||
onCloseEvent: (listener: () => void) => void;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"type": "module",
|
||||
"main": "./dist/index.cjs",
|
||||
"module": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"exports": "./dist/index.js",
|
||||
"private": true
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user