Compare commits

...

16 Commits

Author SHA1 Message Date
Marcus Schiesser 95a578d184 chore: remove cloud package 2025-07-31 12:34:16 +08:00
github-actions[bot] 12892f6dd9 Release 0.11.25 (#2144)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2025-07-31 12:21:22 +08:00
Marcus Schiesser d138d87663 chore: deprecate cloud packages (#2143) 2025-07-31 12:21:22 +08:00
github-actions[bot] e524b7b08f Release 0.11.24 (#2141)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: marcusschiesser <17126+marcusschiesser@users.noreply.github.com>
2025-07-31 12:20:28 +08:00
Clelia (Astra) Bertelli bbecff27f9 chore: deprecate @llamaindex/cloud in favor of llama-cloud-services 2025-07-30 12:26:57 +02:00
Marcus Schiesser f9f1de9516 chore: use Logger for core (#2139) 2025-07-30 11:43:45 +08:00
Twisha Bansal f576812e7a docs: Using MCP Toolbox for Databases with LlamaIndex (#2138) 2025-07-30 11:19:34 +08:00
Adrian Lyjak c3bf3c7178 Adding support for page citations, and refactor the confidence into the field metadata (#2140) 2025-07-30 10:25:19 +08:00
github-actions[bot] 38487da65d Release 0.11.23 (#2136)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: marcusschiesser <17126+marcusschiesser@users.noreply.github.com>
2025-07-28 14:07:23 +08:00
Marcus Schiesser f29799e385 feat: Add toolcall callbacks to agent workflows (#2137) 2025-07-24 15:37:14 +08:00
Marcus Schiesser 9bca30620b fix: docs build 2025-07-23 12:55:35 +08:00
Marcus Schiesser 7224c06409 feat: Add logger and callbacks to llm.exec (#2135) 2025-07-23 12:37:02 +08:00
github-actions[bot] 29c7cf0989 Release 0.11.22 (#2131)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2025-07-23 11:30:04 +08:00
Marcus Schiesser c65a2dc4a7 chore: Deprecate community package and link to AWS package (#2134) 2025-07-23 11:05:50 +08:00
Terence Sim f1c5079290 docs: updated bedrock import and supported models (#2129)
Co-authored-by: Terence Sim <40583743+InTheAxis@users.noreply.github.com>
2025-07-23 10:40:49 +08:00
Terence Sim 9ed31958a7 chore: add logger as param to AgentWorkflow constructor (#2130)
Co-authored-by: Terence Sim <40583743+InTheAxis@users.noreply.github.com>
Co-authored-by: Marcus Schiesser <marcus.schiesser@googlemail.com>
2025-07-22 16:35:28 +08:00
195 changed files with 4378 additions and 38919 deletions
+45
View File
@@ -1,5 +1,50 @@
# @llamaindex/doc
## 0.2.48
### Patch Changes
- Updated dependencies [049471b]
- Updated dependencies [049471b]
- @llamaindex/cloud@4.1.0
- llamaindex@0.11.25
## 0.2.47
### Patch Changes
- Updated dependencies [c3bf3c7]
- Updated dependencies [f9f1de9]
- @llamaindex/cloud@4.0.28
- @llamaindex/core@0.6.19
- llamaindex@0.11.24
- @llamaindex/node-parser@2.0.19
- @llamaindex/openai@0.4.14
- @llamaindex/readers@3.1.18
- @llamaindex/workflow@1.1.20
## 0.2.46
### Patch Changes
- Updated dependencies [f29799e]
- Updated dependencies [7224c06]
- @llamaindex/workflow@1.1.19
- @llamaindex/core@0.6.18
- llamaindex@0.11.23
- @llamaindex/cloud@4.0.27
- @llamaindex/node-parser@2.0.18
- @llamaindex/openai@0.4.13
- @llamaindex/readers@3.1.17
## 0.2.45
### Patch Changes
- Updated dependencies [9ed3195]
- @llamaindex/workflow@1.1.18
- llamaindex@0.11.22
## 0.2.44
### Patch Changes
+1 -2
View File
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/doc",
"version": "0.2.44",
"version": "0.2.48",
"private": true,
"scripts": {
"postinstall": "fumadocs-mdx",
@@ -16,7 +16,6 @@
"@huggingface/transformers": "^3.5.0",
"@icons-pack/react-simple-icons": "^10.1.0",
"@llamaindex/chat-ui-docs": "^0.0.5",
"@llamaindex/cloud": "workspace:*",
"@llamaindex/core": "workspace:*",
"@llamaindex/node-parser": "workspace:*",
"@llamaindex/openai": "workspace:*",
Binary file not shown.

After

Width:  |  Height:  |  Size: 154 KiB

@@ -0,0 +1,85 @@
---
title: MCP Toolbox For Databases
description: MCP Toolbox for Databases is an open source MCP server for databases.
---
# MCP Toolbox for Databases
[MCP Toolbox for Databases](https://github.com/googleapis/genai-toolbox) is an open source MCP server for databases. It was designed with enterprise-grade and production-quality in mind. It enables you to develop tools easier, faster, and more securely by handling the complexities such as connection pooling, authentication, and more.
Toolbox Tools can be seemlessly integrated with LlamaIndex applications. For more
information on [getting
started](https://googleapis.github.io/genai-toolbox/getting-started/local_quickstart_js/) or
[configuring](https://googleapis.github.io/genai-toolbox/getting-started/configure/)
Toolbox, see the
[documentation](https://googleapis.github.io/genai-toolbox/getting-started/introduction/).
![architecture](/images/mcp_db_toolbox.png)
### Configure and deploy
Toolbox is an open source server that you deploy and manage yourself. For more
instructions on deploying and configuring, see the official Toolbox
documentation:
* [Installing the Server](https://googleapis.github.io/genai-toolbox/getting-started/introduction/#installing-the-server)
* [Configuring Toolbox](https://googleapis.github.io/genai-toolbox/getting-started/configure/)
### Install client SDK
LlamaIndex relies on the `@toolbox-sdk/core` node package to use Toolbox. Install the
package before getting started:
```shell
npm install @toolbox-sdk/core
```
### Loading Toolbox Tools
Once your Toolbox server is configured and up and running, you can load tools
from your server using the SDK:
```javascript
import { gemini, GEMINI_MODEL } from "@llamaindex/google";
import { agent } from "@llamaindex/workflow";
import { tool } from "llamaindex";
import { ToolboxClient } from "@toolbox-sdk/core";
// Initialize LLM
const llm = gemini({
model: GEMINI_MODEL.GEMINI_2_0_FLASH,
apiKey: process.env.GOOGLE_API_KEY,
});
// Replace with your Toolbox Server URL
const URL = 'https://127.0.0.1:5000';
const client = new ToolboxClient("http://127.0.0.1:5000");
const toolboxTools = await client.loadToolset("my-toolset");
const getTool = (toolboxTool) => tool({
name: toolboxTool.getName(),
description: toolboxTool.getDescription(),
parameters: toolboxTool.getParamSchema(),
execute: toolboxTool
});
const tools = toolboxTools.map(getTool);
const myAgent = agent({
tools: tools,
llm,
memory,
systemPrompt: prompt,
});
const result = await myAgent.run(query);
console.log(result);
```
### Advanced Toolbox Features
Toolbox has a variety of features to make developing Gen AI tools for databases seamless.
For more information, read more about the following:
- [Authenticated Parameters](https://googleapis.github.io/genai-toolbox/resources/tools/#authenticated-parameters): bind tool inputs to values from OIDC tokens automatically, making it easy to run sensitive queries without potentially leaking data
- [Authorized Invocations](https://googleapis.github.io/genai-toolbox/resources/tools/#authorized-invocations): restrict access to use a tool based on the users Auth token
- [OpenTelemetry](https://googleapis.github.io/genai-toolbox/how-to/export_telemetry/): get metrics and tracing from Toolbox with [OpenTelemetry](https://opentelemetry.io/docs/)
@@ -1,5 +1,5 @@
{
"title": "Integration",
"description": "See our integrations",
"pages": ["open-llm-metry", "lang-trace", "vercel"]
"pages": ["open-llm-metry", "lang-trace", "mcp-toolbox", "vercel"]
}
@@ -0,0 +1,164 @@
---
title: Low-Level LLM Execution
---
Sometimes your need more control over LLM interactions than what high-level agents provide. The `llm.exec` method makes it simple for you to make a single LLM call with tools but hides the complexity of executing the tools and generating the tool messages.
## When to Use `llm.exec`
Use `llm.exec` when you need to:
- Build custom agent logic in [workflow](/docs/llamaindex/modules/agents/workflows) steps
- Have precise control over message handling and tool execution
## Basic Usage
The `llm.exec` method takes messages and tools as parameter and executes one LLM call.
The LLM might either request to call one or more of the tools or generate an assistant message as result.
For each tool call that is requested, `llm.exec` executes it and generates the two tool call messages (call and result). If no tool call is requested, just the assistant message is returned.
```ts
import { openai } from "@llamaindex/openai";
import { ChatMessage, tool } from "llamaindex";
import z from "zod";
const llm = openai({ model: "gpt-4.1-mini" });
const messages = [
{
content: "What's the weather like in San Francisco?",
role: "user",
} as ChatMessage,
];
const { newMessages, toolCalls } = await llm.exec({
messages,
tools: [
tool({
name: "get_weather",
description: "Get the current weather for a location",
parameters: z.object({
address: z.string().describe("The address"),
}),
execute: ({ address }) => {
return `It's sunny in ${address}!`;
},
}),
],
});
// Add the new messages (including tool calls and responses) to your conversation
messages.push(...newMessages);
```
> `newMessages` is an array as each tool call generates two messages: a tool call message and the tool call result message.
## Agent Loop Pattern
A common pattern is to use `llm.exec` in a loop until the LLM stops making tool calls:
```ts
import { openai } from "@llamaindex/openai";
import { ChatMessage, tool } from "llamaindex";
import z from "zod";
async function runAgentLoop() {
const llm = openai({ model: "gpt-4.1-mini" });
const messages = [
{
content: "What's the weather like in San Francisco?",
role: "user",
} as ChatMessage,
];
let exit = false;
do {
const { newMessages, toolCalls } = await llm.exec({
messages,
tools: [
tool({
name: "get_weather",
description: "Get the current weather for a location",
parameters: z.object({
address: z.string().describe("The address"),
}),
execute: ({ address }) => {
return `It's sunny in ${address}!`;
},
}),
],
});
console.log(newMessages);
messages.push(...newMessages);
// Exit when no more tool calls are made
exit = toolCalls.length === 0;
} while (!exit);
}
```
## Streaming Support
For real-time responses, use the `stream` option to get the assistant's response as streamed tokens:
```ts
import { openai } from "@llamaindex/openai";
import { tool } from "llamaindex";
import z from "zod";
async function streamingAgentLoop() {
const llm = openai({ model: "gpt-4o-mini" });
const messages = [
{
content: "What's the weather like in San Francisco?",
role: "user",
} as ChatMessage,
];
let exit = false;
do {
const { stream, newMessages, toolCalls } = await llm.exec({
messages,
tools: [
tool({
name: "get_weather",
description: "Get the current weather for a location",
parameters: z.object({
address: z.string().describe("The address"),
}),
execute: ({ address }) => {
return `It's sunny in ${address}!`;
},
}),
],
stream: true,
});
// Stream the response token by token
for await (const chunk of stream) {
process.stdout.write(chunk.delta);
}
messages.push(...newMessages());
exit = toolCalls.length === 0;
} while (!exit);
}
```
> `newMessages` is a function when streaming. The reason is that the result only is available after streaming. Calling it before, will throw an error.
## Return Values
`llm.exec` returns an object with:
- **`newMessages`**: Array of new chat messages including the LLM response and any tool call messages (call or result). This is a function return the array when streaming.
- **`toolCalls`**: Array of tool calls made by the LLM
- **`stream`**: Async iterable for streaming responses (only when `stream: true`)
## Best Practices
For using `llm.exec` in an agent loop, take care to:
1. **Maintain message history**: Always add `newMessages` to your conversation history
2. **Set exit conditions**: Implement proper logic to avoid infinite loops
@@ -1,4 +1,10 @@
{
"title": "Agents",
"pages": ["tool", "agent_workflow", "workflows", "natural_language_workflow"]
"pages": [
"tool",
"agent_workflow",
"workflows",
"low-level",
"natural_language_workflow"
]
}
@@ -101,6 +101,9 @@ const agent = agent({
});
```
You can also use [MCP Toolbox for
Databases](/docs/llamaindex/integration/mcp-toolbox) to interact with MCP tools.
## Function tool
@@ -5,13 +5,13 @@ title: Bedrock
## Installation
```package-install
npm i llamaindex @llamaindex/community
npm i llamaindex @llamaindex/aws
```
## Usage
```ts
import { BEDROCK_MODELS, Bedrock } from "@llamaindex/community";
import { BEDROCK_MODELS, Bedrock } from "@llamaindex/aws";
Settings.llm = new Bedrock({
model: BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_HAIKU,
@@ -23,9 +23,19 @@ Settings.llm = new Bedrock({
});
```
Currently only supports Anthropic and Meta models:
Supported models are listed below (accessible by BEDROCK_MODELS).
```ts
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_2 = "anthropic.claude-v2";
ANTHROPIC_CLAUDE_2_1 = "anthropic.claude-v2:1";
@@ -33,7 +43,12 @@ 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"; // available on us-west-2
ANTHROPIC_CLAUDE_3_5_SONNET = "anthropic.claude-3-5-sonnet-20240620-v1:0";
ANTHROPIC_CLAUDE_3_5_SONNET_V2 = "anthropic.claude-3-5-sonnet-20241022-v2:0";
ANTHROPIC_CLAUDE_3_5_HAIKU = "anthropic.claude-3-5-haiku-20241022-v1:0";
ANTHROPIC_CLAUDE_3_7_SONNET = "anthropic.claude-3-7-sonnet-20250219-v1:0";
ANTHROPIC_CLAUDE_4_SONNET = "anthropic.claude-sonnet-4-20250514-v1:0";
ANTHROPIC_CLAUDE_4_OPUS = "anthropic.claude-opus-4-20250514-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";
@@ -45,41 +60,66 @@ META_LLAMA3_2_1B_INSTRUCT = "meta.llama3-2-1b-instruct-v1:0"; // only available
META_LLAMA3_2_3B_INSTRUCT = "meta.llama3-2-3b-instruct-v1:0"; // only available via inference endpoints (see below)
META_LLAMA3_2_11B_INSTRUCT = "meta.llama3-2-11b-instruct-v1:0"; // only available via inference endpoints (see below), multimodal and function call supported
META_LLAMA3_2_90B_INSTRUCT = "meta.llama3-2-90b-instruct-v1:0"; // only available via inference endpoints (see below), multimodal and function call supported
META_LLAMA3_3_70B_INSTRUCT = "meta.llama3-3-70b-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";
AMAZON_NOVA_PREMIER_1 = "amazon.nova-premier-v1:0";
AMAZON_NOVA_PRO_1 = "amazon.nova-pro-v1:0";
AMAZON_NOVA_LITE_1 = "amazon.nova-lite-v1:0";
AMAZON_NOVA_MICRO_1 = "amazon.nova-micro-v1:0";
```
You can also use Bedrock's Inference endpoints by using the model names:
You can also use Bedrock's Inference endpoints by using the model names (accessible by INFERENCE_BEDROCK_MODELS).
Note that the region must be set correctly.
```ts
// US
//US
US_ANTHROPIC_CLAUDE_3_HAIKU = "us.anthropic.claude-3-haiku-20240307-v1:0";
US_ANTHROPIC_CLAUDE_3_5_HAIKU = "us.anthropic.claude-3-5-haiku-20241022-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_ANTHROPIC_CLAUDE_3_5_SONNET_V2 =
"us.anthropic.claude-3-5-sonnet-20241022-v2:0";
US_ANTHROPIC_CLAUDE_3_5_SONNET_V2 = "us.anthropic.claude-3-5-sonnet-20241022-v2:0";
US_ANTHROPIC_CLAUDE_3_7_SONNET = "us.anthropic.claude-3-7-sonnet-20250219-v1:0";
US_ANTHROPIC_CLAUDE_4_SONNET = "us.anthropic.claude-sonnet-4-20250514-v1:0";
US_ANTHROPIC_CLAUDE_4_OPUS = "us.anthropic.claude-opus-4-20250514-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";
US_AMAZON_NOVA_PRO_1 = "us.amazon.nova-premier-v1:0";
US_META_LLAMA_3_3_70B_INSTRUCT = "us.meta.llama3-3-70b-instruct-v1:0";
US_AMAZON_NOVA_PREMIER_1 = "us.amazon.nova-premier-v1:0";
US_AMAZON_NOVA_PRO_1 = "us.amazon.nova-pro-v1:0";
US_AMAZON_NOVA_LITE_1 = "us.amazon.nova-lite-v1:0";
US_AMAZON_NOVA_MICRO_1 = "us.amazon.nova-micro-v1:0";
// EU
//EU
EU_ANTHROPIC_CLAUDE_3_HAIKU = "eu.anthropic.claude-3-haiku-20240307-v1:0";
EU_ANTHROPIC_CLAUDE_3_5_HAIKU = "eu.anthropic.claude-3-5-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_ANTHROPIC_CLAUDE_3_7_SONNET = "eu.anthropic.claude-3-7-sonnet-20250219-v1:0";
EU_ANTHROPIC_CLAUDE_4_SONNET = "eu.anthropic.claude-sonnet-4-20250514-v1:0";
EU_ANTHROPIC_CLAUDE_4_OPUS = "eu.anthropic.claude-opus-4-20250514-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";
EU_AMAZON_NOVA_PRO_1 = "eu.amazon.nova-premier-v1:0";
EU_AMAZON_NOVA_PREMIER_1 = "eu.amazon.nova-premier-v1:0";
EU_AMAZON_NOVA_PRO_1 = "eu.amazon.nova-pro-v1:0";
EU_AMAZON_NOVA_LITE_1 = "eu.amazon.nova-lite-v1:0";
EU_AMAZON_NOVA_MICRO_1 = "eu.amazon.nova-micro-v1:0";
//APAC
APAC_ANTHROPIC_CLAUDE_3_5_SONNET = "apac.anthropic.claude-3-5-sonnet-20240620-v1:0";
APAC_ANTHROPIC_CLAUDE_3_5_SONNET_V2 = "apac.anthropic.claude-3-5-sonnet-20241022-v2:0";
APAC_ANTHROPIC_CLAUDE_3_7_SONNET = "apac.anthropic.claude-3-7-sonnet-20250219-v1:0";
APAC_ANTHROPIC_CLAUDE_3_HAIKU = "apac.anthropic.claude-3-haiku-20240307-v1:0";
APAC_ANTHROPIC_CLAUDE_3_SONNET = "apac.anthropic.claude-3-sonnet-20240229-v1:0";
APAC_AMAZON_NOVA_PRO_1 = "apac.amazon.nova-pro-v1:0";
APAC_AMAZON_NOVA_LITE_1 = "apac.amazon.nova-lite-v1:0";
APAC_AMAZON_NOVA_MICRO_1 = "apac.amazon.nova-micro-v1:0";
```
Sonnet, Haiku and Opus are multimodal, image_url only supports base64 data url format, e.g. `data:image/jpeg;base64,SGVsbG8sIFdvcmxkIQ==`
@@ -87,10 +127,11 @@ Sonnet, Haiku and Opus are multimodal, image_url only supports base64 data url f
## Full Example
```ts
import { BEDROCK_MODELS, Bedrock } from "llamaindex";
import { INFERENCE_BEDROCK_MODELS, Bedrock } from "@llamaindex/aws";
Settings.llm = new Bedrock({
model: BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_HAIKU,
model: INFERENCE_BEDROCK_MODELS.US_ANTHROPIC_CLAUDE_3_SONNET,
region: "us-east-1",
});
async function main() {
@@ -119,7 +160,7 @@ async function main() {
## Agent Example
```ts
import { BEDROCK_MODELS, Bedrock } from "@llamaindex/community";
import { BEDROCK_MODELS, Bedrock } from "@llamaindex/aws";
import { tool } from "llamaindex";
import { agent } from "@llamaindex/workflow";
import { z } from "zod";
@@ -1,5 +1,30 @@
# @llamaindex/cloudflare-worker-agent-test
## 0.0.186
### Patch Changes
- Updated dependencies [049471b]
- llamaindex@0.11.25
## 0.0.185
### Patch Changes
- llamaindex@0.11.24
## 0.0.184
### Patch Changes
- llamaindex@0.11.23
## 0.0.183
### Patch Changes
- llamaindex@0.11.22
## 0.0.182
### Patch Changes
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/cloudflare-worker-agent-test",
"version": "0.0.182",
"version": "0.0.186",
"type": "module",
"private": true,
"scripts": {
@@ -1,5 +1,25 @@
# @llamaindex/llama-parse-browser-test
## 0.0.84
### Patch Changes
- Updated dependencies [049471b]
- @llamaindex/cloud@4.1.0
## 0.0.83
### Patch Changes
- Updated dependencies [c3bf3c7]
- @llamaindex/cloud@4.0.28
## 0.0.82
### Patch Changes
- @llamaindex/cloud@4.0.27
## 0.0.81
### Patch Changes
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/llama-parse-browser-test",
"private": true,
"version": "0.0.81",
"version": "0.0.84",
"type": "module",
"scripts": {
"dev": "vite",
@@ -14,6 +14,6 @@
"vite-plugin-wasm": "^3.4.1"
},
"dependencies": {
"@llamaindex/cloud": "workspace:*"
"llama-cloud-services": "^0.1.0"
}
}
+25
View File
@@ -1,5 +1,30 @@
# @llamaindex/next-agent-test
## 0.1.186
### Patch Changes
- Updated dependencies [049471b]
- llamaindex@0.11.25
## 0.1.185
### Patch Changes
- llamaindex@0.11.24
## 0.1.184
### Patch Changes
- llamaindex@0.11.23
## 0.1.183
### Patch Changes
- llamaindex@0.11.22
## 0.1.182
### Patch Changes
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/next-agent-test",
"version": "0.1.182",
"version": "0.1.186",
"private": true,
"scripts": {
"dev": "next dev",
@@ -1,5 +1,30 @@
# test-edge-runtime
## 0.1.185
### Patch Changes
- Updated dependencies [049471b]
- llamaindex@0.11.25
## 0.1.184
### Patch Changes
- llamaindex@0.11.24
## 0.1.183
### Patch Changes
- llamaindex@0.11.23
## 0.1.182
### Patch Changes
- llamaindex@0.11.22
## 0.1.181
### Patch Changes
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/nextjs-edge-runtime-test",
"version": "0.1.181",
"version": "0.1.185",
"private": true,
"scripts": {
"dev": "next dev",
@@ -1,5 +1,34 @@
# @llamaindex/next-node-runtime
## 0.1.55
### Patch Changes
- Updated dependencies [049471b]
- llamaindex@0.11.25
## 0.1.54
### Patch Changes
- llamaindex@0.11.24
- @llamaindex/huggingface@0.1.24
- @llamaindex/readers@3.1.18
## 0.1.53
### Patch Changes
- llamaindex@0.11.23
- @llamaindex/huggingface@0.1.23
- @llamaindex/readers@3.1.17
## 0.1.52
### Patch Changes
- llamaindex@0.11.22
## 0.1.51
### Patch Changes
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/next-node-runtime-test",
"version": "0.1.51",
"version": "0.1.55",
"private": true,
"scripts": {
"dev": "next dev",
@@ -1,5 +1,30 @@
# vite-import-llamaindex
## 0.0.52
### Patch Changes
- Updated dependencies [049471b]
- llamaindex@0.11.25
## 0.0.51
### Patch Changes
- llamaindex@0.11.24
## 0.0.50
### Patch Changes
- llamaindex@0.11.23
## 0.0.49
### Patch Changes
- llamaindex@0.11.22
## 0.0.48
### Patch Changes
@@ -1,7 +1,7 @@
{
"name": "vite-import-llamaindex",
"private": true,
"version": "0.0.48",
"version": "0.0.52",
"type": "module",
"scripts": {
"build": "vite build",
@@ -1,5 +1,30 @@
# @llamaindex/waku-query-engine-test
## 0.0.186
### Patch Changes
- Updated dependencies [049471b]
- llamaindex@0.11.25
## 0.0.185
### Patch Changes
- llamaindex@0.11.24
## 0.0.184
### Patch Changes
- llamaindex@0.11.23
## 0.0.183
### Patch Changes
- llamaindex@0.11.22
## 0.0.182
### Patch Changes
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/waku-query-engine-test",
"version": "0.0.182",
"version": "0.0.186",
"type": "module",
"private": true,
"scripts": {
+106
View File
@@ -1,5 +1,111 @@
# examples
## 0.3.35
### Patch Changes
- Updated dependencies [c3bf3c7]
- Updated dependencies [f9f1de9]
- @llamaindex/cloud@4.0.28
- @llamaindex/core@0.6.19
- llamaindex@0.11.24
- @llamaindex/node-parser@2.0.19
- @llamaindex/anthropic@0.3.21
- @llamaindex/assemblyai@0.1.18
- @llamaindex/clip@0.0.70
- @llamaindex/cohere@0.0.33
- @llamaindex/deepinfra@0.0.70
- @llamaindex/discord@0.1.18
- @llamaindex/google@0.3.18
- @llamaindex/huggingface@0.1.24
- @llamaindex/jinaai@0.0.30
- @llamaindex/mistral@0.1.19
- @llamaindex/mixedbread@0.0.33
- @llamaindex/notion@0.1.18
- @llamaindex/ollama@0.1.19
- @llamaindex/openai@0.4.14
- @llamaindex/perplexity@0.0.27
- @llamaindex/portkey-ai@0.0.61
- @llamaindex/replicate@0.0.61
- @llamaindex/bm25-retriever@0.0.8
- @llamaindex/astra@0.0.33
- @llamaindex/azure@0.1.31
- @llamaindex/chroma@0.0.33
- @llamaindex/elastic-search@0.1.19
- @llamaindex/firestore@1.0.26
- @llamaindex/milvus@0.1.28
- @llamaindex/mongodb@0.0.34
- @llamaindex/pinecone@0.1.19
- @llamaindex/postgres@0.0.62
- @llamaindex/qdrant@0.1.29
- @llamaindex/supabase@0.1.20
- @llamaindex/upstash@0.0.33
- @llamaindex/weaviate@0.0.34
- @llamaindex/vercel@0.1.19
- @llamaindex/voyage-ai@1.0.25
- @llamaindex/readers@3.1.18
- @llamaindex/tools@0.1.9
- @llamaindex/workflow@1.1.20
- @llamaindex/deepseek@0.0.31
- @llamaindex/fireworks@0.0.30
- @llamaindex/groq@0.0.86
- @llamaindex/together@0.0.30
- @llamaindex/vllm@0.0.56
- @llamaindex/xai@0.0.17
## 0.3.34
### Patch Changes
- Updated dependencies [f29799e]
- Updated dependencies [7224c06]
- @llamaindex/workflow@1.1.19
- @llamaindex/core@0.6.18
- llamaindex@0.11.23
- @llamaindex/cloud@4.0.27
- @llamaindex/node-parser@2.0.18
- @llamaindex/anthropic@0.3.20
- @llamaindex/assemblyai@0.1.17
- @llamaindex/clip@0.0.69
- @llamaindex/cohere@0.0.32
- @llamaindex/deepinfra@0.0.69
- @llamaindex/discord@0.1.17
- @llamaindex/google@0.3.17
- @llamaindex/huggingface@0.1.23
- @llamaindex/jinaai@0.0.29
- @llamaindex/mistral@0.1.18
- @llamaindex/mixedbread@0.0.32
- @llamaindex/notion@0.1.17
- @llamaindex/ollama@0.1.18
- @llamaindex/openai@0.4.13
- @llamaindex/perplexity@0.0.26
- @llamaindex/portkey-ai@0.0.60
- @llamaindex/replicate@0.0.60
- @llamaindex/bm25-retriever@0.0.7
- @llamaindex/astra@0.0.32
- @llamaindex/azure@0.1.30
- @llamaindex/chroma@0.0.32
- @llamaindex/elastic-search@0.1.18
- @llamaindex/firestore@1.0.25
- @llamaindex/milvus@0.1.27
- @llamaindex/mongodb@0.0.33
- @llamaindex/pinecone@0.1.18
- @llamaindex/postgres@0.0.61
- @llamaindex/qdrant@0.1.28
- @llamaindex/supabase@0.1.19
- @llamaindex/upstash@0.0.32
- @llamaindex/weaviate@0.0.33
- @llamaindex/vercel@0.1.18
- @llamaindex/voyage-ai@1.0.24
- @llamaindex/readers@3.1.17
- @llamaindex/tools@0.1.8
- @llamaindex/deepseek@0.0.30
- @llamaindex/fireworks@0.0.29
- @llamaindex/groq@0.0.85
- @llamaindex/together@0.0.29
- @llamaindex/vllm@0.0.55
- @llamaindex/xai@0.0.16
## 0.3.33
### Patch Changes
+47 -47
View File
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/examples",
"version": "0.3.33",
"version": "0.3.35",
"private": true,
"scripts": {
"lint": "eslint .",
@@ -11,52 +11,52 @@
"@azure/cosmos": "^4.1.1",
"@azure/identity": "^4.4.1",
"@azure/search-documents": "^12.1.0",
"@llamaindex/anthropic": "^0.3.19",
"@llamaindex/assemblyai": "^0.1.16",
"@llamaindex/astra": "^0.0.31",
"@llamaindex/azure": "^0.1.29",
"@llamaindex/bm25-retriever": "^0.0.6",
"@llamaindex/chroma": "^0.0.31",
"@llamaindex/clip": "^0.0.68",
"@llamaindex/cloud": "^4.0.26",
"@llamaindex/cohere": "^0.0.31",
"@llamaindex/core": "^0.6.17",
"@llamaindex/deepinfra": "^0.0.68",
"@llamaindex/deepseek": "^0.0.29",
"@llamaindex/discord": "^0.1.16",
"@llamaindex/elastic-search": "^0.1.17",
"@llamaindex/anthropic": "^0.3.21",
"@llamaindex/assemblyai": "^0.1.18",
"@llamaindex/astra": "^0.0.33",
"@llamaindex/azure": "^0.1.31",
"@llamaindex/bm25-retriever": "^0.0.8",
"@llamaindex/chroma": "^0.0.33",
"@llamaindex/clip": "^0.0.70",
"llama-cloud-services": "^0.1.0",
"@llamaindex/cohere": "^0.0.33",
"@llamaindex/core": "^0.6.19",
"@llamaindex/deepinfra": "^0.0.70",
"@llamaindex/deepseek": "^0.0.31",
"@llamaindex/discord": "^0.1.18",
"@llamaindex/elastic-search": "^0.1.19",
"@llamaindex/env": "^0.1.30",
"@llamaindex/firestore": "^1.0.24",
"@llamaindex/fireworks": "^0.0.28",
"@llamaindex/google": "^0.3.16",
"@llamaindex/groq": "^0.0.84",
"@llamaindex/huggingface": "^0.1.22",
"@llamaindex/jinaai": "^0.0.28",
"@llamaindex/milvus": "^0.1.26",
"@llamaindex/mistral": "^0.1.17",
"@llamaindex/mixedbread": "^0.0.31",
"@llamaindex/mongodb": "^0.0.32",
"@llamaindex/node-parser": "^2.0.17",
"@llamaindex/notion": "^0.1.16",
"@llamaindex/ollama": "^0.1.17",
"@llamaindex/openai": "^0.4.12",
"@llamaindex/perplexity": "^0.0.25",
"@llamaindex/pinecone": "^0.1.17",
"@llamaindex/portkey-ai": "^0.0.59",
"@llamaindex/postgres": "^0.0.60",
"@llamaindex/qdrant": "^0.1.27",
"@llamaindex/readers": "^3.1.16",
"@llamaindex/replicate": "^0.0.59",
"@llamaindex/supabase": "^0.1.18",
"@llamaindex/together": "^0.0.28",
"@llamaindex/tools": "^0.1.7",
"@llamaindex/upstash": "^0.0.31",
"@llamaindex/vercel": "^0.1.17",
"@llamaindex/vllm": "^0.0.54",
"@llamaindex/voyage-ai": "^1.0.23",
"@llamaindex/weaviate": "^0.0.32",
"@llamaindex/workflow": "^1.1.17",
"@llamaindex/xai": "^0.0.15",
"@llamaindex/firestore": "^1.0.26",
"@llamaindex/fireworks": "^0.0.30",
"@llamaindex/google": "^0.3.18",
"@llamaindex/groq": "^0.0.86",
"@llamaindex/huggingface": "^0.1.24",
"@llamaindex/jinaai": "^0.0.30",
"@llamaindex/milvus": "^0.1.28",
"@llamaindex/mistral": "^0.1.19",
"@llamaindex/mixedbread": "^0.0.33",
"@llamaindex/mongodb": "^0.0.34",
"@llamaindex/node-parser": "^2.0.19",
"@llamaindex/notion": "^0.1.18",
"@llamaindex/ollama": "^0.1.19",
"@llamaindex/openai": "^0.4.14",
"@llamaindex/perplexity": "^0.0.27",
"@llamaindex/pinecone": "^0.1.19",
"@llamaindex/portkey-ai": "^0.0.61",
"@llamaindex/postgres": "^0.0.62",
"@llamaindex/qdrant": "^0.1.29",
"@llamaindex/readers": "^3.1.18",
"@llamaindex/replicate": "^0.0.61",
"@llamaindex/supabase": "^0.1.20",
"@llamaindex/together": "^0.0.30",
"@llamaindex/tools": "^0.1.9",
"@llamaindex/upstash": "^0.0.33",
"@llamaindex/vercel": "^0.1.19",
"@llamaindex/vllm": "^0.0.56",
"@llamaindex/voyage-ai": "^1.0.25",
"@llamaindex/weaviate": "^0.0.34",
"@llamaindex/workflow": "^1.1.20",
"@llamaindex/xai": "^0.0.17",
"@notionhq/client": "^4.0.0",
"@pinecone-database/pinecone": "^4.0.0",
"@vercel/postgres": "^0.10.0",
@@ -65,7 +65,7 @@
"commander": "^12.1.0",
"dotenv": "^17.2.0",
"js-tiktoken": "^1.0.14",
"llamaindex": "^0.11.21",
"llamaindex": "^0.11.24",
"mongodb": "6.7.0",
"postgres": "^3.4.4",
"wikipedia": "^2.1.2",
+1 -4
View File
@@ -9,10 +9,7 @@
"start:html": "node --import tsx ./src/html.ts",
"start:markdown": "node --import tsx ./src/markdown.ts",
"start:pdf": "node --import tsx ./src/pdf.ts",
"start:llamaparse": "node --import tsx ./src/llamaparse.ts",
"start:notion": "node --import tsx ./src/notion.ts",
"start:llamaparse-dir": "node --import tsx ./src/simple-directory-reader-with-llamaparse.ts",
"start:llamaparse-json": "node --import tsx ./src/llamaparse-json.ts",
"start:discord": "node --import tsx ./src/discord.ts",
"start:json": "node --import tsx ./src/json.ts",
"start:obsidian": "node --import tsx ./src/obsidian.ts",
@@ -20,7 +17,7 @@
"start:excel": "node --import tsx ./src/excel.ts"
},
"dependencies": {
"@llamaindex/cloud": "workspace:* || ^2.0.24",
"llama-cloud-services": "^0.1.0",
"@llamaindex/excel": "workspace:*",
"@llamaindex/readers": "workspace:* || ^1.0.25",
"@notionhq/client": "^4.0.0",
+25
View File
@@ -1,5 +1,30 @@
# @llamaindex/autotool
## 8.0.25
### Patch Changes
- Updated dependencies [049471b]
- llamaindex@0.11.25
## 8.0.24
### Patch Changes
- llamaindex@0.11.24
## 8.0.23
### Patch Changes
- llamaindex@0.11.23
## 8.0.22
### Patch Changes
- llamaindex@0.11.22
## 8.0.21
### Patch Changes
@@ -1,5 +1,34 @@
# @llamaindex/autotool-01-node-example
## 0.0.133
### Patch Changes
- Updated dependencies [049471b]
- llamaindex@0.11.25
- @llamaindex/autotool@8.0.25
## 0.0.132
### Patch Changes
- llamaindex@0.11.24
- @llamaindex/autotool@8.0.24
## 0.0.131
### Patch Changes
- llamaindex@0.11.23
- @llamaindex/autotool@8.0.23
## 0.0.130
### Patch Changes
- llamaindex@0.11.22
- @llamaindex/autotool@8.0.22
## 0.0.129
### Patch Changes
@@ -13,5 +13,5 @@
"scripts": {
"start": "node --import tsx --import @llamaindex/autotool/node ./src/index.ts"
},
"version": "0.0.129"
"version": "0.0.133"
}
+1 -1
View File
@@ -6,7 +6,7 @@
"url": "git+https://github.com/run-llama/LlamaIndexTS.git",
"directory": "packages/autotool"
},
"version": "8.0.21",
"version": "8.0.25",
"description": "auto transpile your JS function to LLM Agent compatible",
"files": [
"dist",
-9
View File
@@ -1,9 +0,0 @@
# @llamaindex/cloud
> LlamaCloud is a new generation of managed parsing, ingestion, and retrieval services, designed to bring production-grade context-augmentation to your LLM and RAG applications.
For more information, see the [API documentation](https://docs.cloud.llamaindex.ai/).
## License
MIT
-8
View File
@@ -1,8 +0,0 @@
{
"type": "module",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": "./dist/index.js",
"private": true
}
-8
View File
@@ -1,8 +0,0 @@
{
"type": "module",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": "./dist/index.js",
"private": true
}
-24
View File
@@ -1,24 +0,0 @@
import { defaultPlugins, defineConfig } from "@hey-api/openapi-ts";
export default defineConfig({
// you can download this file to get the latest version of the OpenAPI document
// @link https://api.cloud.llamaindex.ai/api/openapi.json
input: "./openapi.json",
output: {
path: "./src/client",
format: "prettier",
lint: "eslint",
},
plugins: [
...defaultPlugins,
"@hey-api/client-fetch",
"zod",
"@hey-api/schemas",
"@hey-api/sdk",
{
enums: "javascript",
identifierCase: "PascalCase",
name: "@hey-api/typescript",
},
],
});
File diff suppressed because it is too large Load Diff
-97
View File
@@ -1,97 +0,0 @@
{
"name": "@llamaindex/cloud",
"version": "4.0.26",
"type": "module",
"license": "MIT",
"scripts": {
"generate": "./node_modules/.bin/openapi-ts",
"build": "pnpm run generate && bunchee",
"dev": "bunchee --watch"
},
"files": [
"openapi.json",
"./api",
"./reader",
"./parse",
"./beta/agent"
],
"exports": {
"./openapi.json": "./openapi.json",
"./beta/agent": {
"require": {
"types": "./beta/agent/dist/index.d.cts",
"default": "./beta/agent/dist/index.cjs"
},
"import": {
"types": "./beta/agent/dist/index.d.ts",
"default": "./beta/agent/dist/index.js"
},
"default": "./beta/agent/dist/index.js"
},
"./api": {
"require": {
"types": "./api/dist/index.d.cts",
"default": "./api/dist/index.cjs"
},
"import": {
"types": "./api/dist/index.d.ts",
"default": "./api/dist/index.js"
},
"default": "./api/dist/index.js"
},
"./reader": {
"require": {
"types": "./reader/dist/index.d.cts",
"default": "./reader/dist/index.cjs"
},
"import": {
"types": "./reader/dist/index.d.ts",
"default": "./reader/dist/index.js"
},
"default": "./reader/dist/index.js"
},
"./parse": {
"require": {
"types": "./parse/dist/index.d.cts",
"default": "./parse/dist/index.cjs"
},
"import": {
"types": "./parse/dist/index.d.ts",
"default": "./parse/dist/index.js"
},
"default": "./parse/dist/index.js"
},
".": {
"require": {
"types": "./reader/dist/index.d.cts",
"default": "./reader/dist/index.cjs"
},
"import": {
"types": "./reader/dist/index.d.ts",
"default": "./reader/dist/index.js"
},
"default": "./reader/dist/index.js"
}
},
"repository": {
"type": "git",
"url": "git+https://github.com/run-llama/LlamaIndexTS.git",
"directory": "packages/cloud"
},
"devDependencies": {
"@hey-api/client-fetch": "^0.10.1",
"@hey-api/openapi-ts": "^0.67.5",
"@llama-flow/core": "^0.4.1",
"@llamaindex/core": "workspace:*",
"@llamaindex/env": "workspace:*"
},
"peerDependencies": {
"@llama-flow/core": "^0.4.1",
"@llamaindex/core": "workspace:*",
"@llamaindex/env": "workspace:*"
},
"dependencies": {
"p-retry": "^6.2.1",
"zod": "^3.25.76"
}
}
-8
View File
@@ -1,8 +0,0 @@
{
"type": "module",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": "./dist/index.js",
"private": true
}
-8
View File
@@ -1,8 +0,0 @@
{
"type": "module",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": "./dist/index.js",
"private": true
}
-11
View File
@@ -1,11 +0,0 @@
import { client } from "./client/client.gen";
client.setConfig({
baseUrl: "https://api.cloud.llamaindex.ai/",
headers: {
"X-SDK-Name": "llamaindex-ts",
},
});
export * from "./client";
export { client };
-329
View File
@@ -1,329 +0,0 @@
import { createClient, createConfig } from "@hey-api/client-fetch";
import { getEnv } from "@llamaindex/env";
import {
aggregateAgentDataApiV1BetaAgentDataAggregatePost,
createAgentDataApiV1BetaAgentDataPost,
deleteAgentDataApiV1BetaAgentDataItemIdDelete,
getAgentDataApiV1BetaAgentDataItemIdGet,
searchAgentDataApiV1BetaAgentDataSearchPost,
updateAgentDataApiV1BetaAgentDataItemIdPut,
type AgentData,
type AggregateGroup,
} from "../../client";
import type {
AggregateAgentDataOptions,
SearchAgentDataOptions,
TypedAgentData,
TypedAgentDataItems,
TypedAggregateGroup,
TypedAggregateGroupItems,
} from "./types";
/**
* Async client for agent data operations
*/
export class AgentClient<T = unknown> {
private client: ReturnType<typeof createClient>;
private baseUrl: string;
private headers: Record<string, string>;
private collection: string;
private agentUrlId: string;
constructor({
apiKey = getEnv("LLAMA_CLOUD_API_KEY"),
baseUrl = "https://api.cloud.llamaindex.ai/",
collection = "default",
agentUrlId = "_public",
}: {
apiKey?: string;
baseUrl?: string;
collection?: string;
agentUrlId?: string;
}) {
this.baseUrl = baseUrl;
this.headers = {
"X-SDK-Name": "llamaindex-ts",
...(apiKey && { Authorization: `Bearer ${apiKey}` }),
};
this.client = createClient(
createConfig({
baseUrl: this.baseUrl,
headers: this.headers,
}),
);
this.collection = collection;
this.agentUrlId = agentUrlId;
}
/**
* Create new agent data
*/
async createItem(data: T): Promise<TypedAgentData<T>> {
const response = await createAgentDataApiV1BetaAgentDataPost({
throwOnError: true,
body: {
agent_slug: this.agentUrlId,
collection: this.collection,
data: data as Record<string, unknown>,
},
client: this.client,
});
return this.transformResponse(response.data);
}
/**
* Get agent data by ID
*/
async getItem(id: string): Promise<TypedAgentData<T> | null> {
try {
const response = await getAgentDataApiV1BetaAgentDataItemIdGet({
throwOnError: true,
path: { item_id: id },
client: this.client,
});
return this.transformResponse(response.data);
} catch (error) {
if (
error instanceof Error &&
"response" in error &&
(error as { response?: { status?: number } }).response?.status === 404
) {
return null;
}
throw error;
}
}
/**
* Update agent data
*/
async updateItem(id: string, data: T): Promise<TypedAgentData<T>> {
const response = await updateAgentDataApiV1BetaAgentDataItemIdPut({
throwOnError: true,
path: { item_id: id },
body: {
data: data as Record<string, unknown>,
},
client: this.client,
});
return this.transformResponse(response.data);
}
/**
* Delete agent data
*/
async deleteItem(id: string): Promise<void> {
await deleteAgentDataApiV1BetaAgentDataItemIdDelete({
throwOnError: true,
path: { item_id: id },
client: this.client,
});
}
/**
* Search agent data
*/
async search(
options: SearchAgentDataOptions,
): Promise<TypedAgentDataItems<T>> {
const response = await searchAgentDataApiV1BetaAgentDataSearchPost({
throwOnError: true,
body: {
agent_slug: this.agentUrlId,
...(this.collection !== undefined && {
collection: this.collection,
}),
...(options.filter !== undefined && { filter: options.filter }),
...(options.orderBy !== undefined && { order_by: options.orderBy }),
...(options.pageSize !== undefined && { page_size: options.pageSize }),
...(options.offset !== undefined && { offset: options.offset }),
...(options.includeTotal !== undefined && {
include_total: options.includeTotal,
}),
},
client: this.client,
});
const result: TypedAgentDataItems<T> = {
items: response.data.items.map((item: AgentData) =>
this.transformResponse(item),
),
};
if (
response.data.total_size !== null &&
response.data.total_size !== undefined
) {
result.totalSize = response.data.total_size;
}
if (
response.data.next_page_token !== null &&
response.data.next_page_token !== undefined
) {
result.nextPageToken = response.data.next_page_token;
}
return result;
}
/**
* Aggregate agent data into groups
*/
async aggregate(
options: AggregateAgentDataOptions,
): Promise<TypedAggregateGroupItems<T>> {
const response = await aggregateAgentDataApiV1BetaAgentDataAggregatePost({
throwOnError: true,
body: {
agent_slug: this.agentUrlId,
...(this.collection !== undefined && {
collection: this.collection,
}),
...(options.filter !== undefined && { filter: options.filter }),
...(options.groupBy !== undefined && { group_by: options.groupBy }),
...(options.count !== undefined && { count: options.count }),
...(options.first !== undefined && { first: options.first }),
...(options.orderBy !== undefined && { order_by: options.orderBy }),
...(options.offset !== undefined && { offset: options.offset }),
...(options.pageSize !== undefined && { page_size: options.pageSize }),
},
client: this.client,
});
const result: TypedAggregateGroupItems<T> = {
items: response.data.items.map((item) =>
this.transformAggregateResponse(item),
),
};
if (
response.data.total_size !== null &&
response.data.total_size !== undefined
) {
result.totalSize = response.data.total_size;
}
if (
response.data.next_page_token !== null &&
response.data.next_page_token !== undefined
) {
result.nextPageToken = response.data.next_page_token;
}
return result;
}
/**
* Transform API response to typed data
*/
private transformResponse(data: AgentData): TypedAgentData<T> {
const result: TypedAgentData<T> = {
id: data.id!,
agentUrlId: data.agent_slug,
data: data.data as T,
createdAt: new Date(data.created_at!),
updatedAt: new Date(data.updated_at!),
};
if (data.collection !== undefined) {
result.collection = data.collection;
}
return result;
}
/**
* Transform API aggregate response to typed data
*/
private transformAggregateResponse(
data: AggregateGroup,
): TypedAggregateGroup<T> {
const result: TypedAggregateGroup<T> = {
groupKey: data.group_key,
};
if (data.count !== null && data.count !== undefined) {
result.count = data.count;
}
if (data.first_item !== null && data.first_item !== undefined) {
result.firstItem = data.first_item as T;
}
return result;
}
}
export interface AgentDataClientOptions<T = unknown> {
/** API key for the client */
apiKey?: string;
/** Base URL for the client */
/** Base URL of the llama cloud api */
baseUrl?: string;
/** If running in an agent runtime, optionally provide the window url to infer the agent url id */
windowUrl?: string;
/** Agent URL ID for the client, if not provided, it will be inferred from the window url, or fall back to "default" */
agentUrlId?: string;
/** Collection name for the client, defaults to "default" */
collection?: string;
}
/**
* Create a new AsyncAgentDataClient instance. Does it's best to infer an agent url id from environment.
* Pass in the window url and/or env to infer the agent url id from them.
* @param options - The options for the client
* @returns A new AgentClient instance
*/
export function createAgentDataClient<T = unknown>({
apiKey,
baseUrl,
windowUrl,
env,
agentUrlId,
collection = "default",
}: {
apiKey?: string;
baseUrl?: string;
windowUrl?: string;
env?: Record<string, string>;
agentUrlId?: string;
collection?: string;
} = {}): AgentClient<T> {
if (env && !agentUrlId) {
agentUrlId =
env.LLAMA_DEPLOY_DEPLOYMENT_NAME ||
env.NEXT_PUBLIC_LLAMA_DEPLOY_DEPLOYMENT_NAME ||
env.VITE_LLAMA_DEPLOY_DEPLOYMENT_NAME;
}
if (windowUrl && !agentUrlId) {
try {
const url = new URL(windowUrl);
const path = url.pathname;
const isLocalhost = // local agents should default to _public, otherwise a full deployment is required
url.hostname.includes("localhost") ||
url.hostname.includes("127.0.0.1");
if (path.startsWith("/deployments/") && !isLocalhost) {
// /deployments/<agent-url-id>/ui/ -> ["", "deployments", "<agent-url-id>", "ui"]
agentUrlId = path.split("/")[2];
}
} catch (error) {
console.warn(
"Failed to infer agent url id from window url, falling back to default",
error,
);
}
}
return new AgentClient({
...(apiKey && { apiKey }),
...(baseUrl && { baseUrl }),
...(agentUrlId && { agentUrlId }),
collection,
});
}
-16
View File
@@ -1,16 +0,0 @@
export { AgentClient, createAgentDataClient } from "./client";
export type {
AggregateAgentDataOptions,
ComparisonOperator,
ExtractedData,
FilterOperation,
SearchAgentDataOptions,
StatusType,
TypedAgentData,
TypedAgentDataItems,
TypedAggregateGroup,
TypedAggregateGroupItems,
} from "./types";
export { StatusType as StatusTypeEnum } from "./types";
-138
View File
@@ -1,138 +0,0 @@
import type { FilterOperation as RawFilterOperation } from "../../client/types.gen";
/**
* Status types for agent data processing
*/
export const StatusType = {
ERROR: "error",
ACCEPTED: "accepted",
REJECTED: "rejected",
PENDING_REVIEW: "pending_review",
} as const;
export type StatusType = (typeof StatusType)[keyof typeof StatusType];
export const ComparisonOperator = {
GT: "gt",
GTE: "gte",
LT: "lt",
LTE: "lte",
EQ: "eq",
INCLUDES: "includes",
} as const;
export type ComparisonOperator =
(typeof ComparisonOperator)[keyof typeof ComparisonOperator];
/**
* Filter operation for searching/filtering agent data
*/
export type FilterOperation = RawFilterOperation;
/**
* Base extracted data interface
*/
export interface ExtractedData<T = unknown> {
/** The original data that was extracted from the document. For tracking changes. Should not be updated. */
original_data: T;
/** The latest state of the data. Will differ if data has been updated. */
data?: T;
/** The status of the extracted data. Prefer to use the StatusType values, but any string is allowed. */
status: StatusType | string;
/** Confidence scores, if any, for each primitive field in the original_data data. */
confidence?: Record<string, unknown>;
/** The ID of the file that was used to extract the data. */
file_id?: string;
/** The name of the file that was used to extract the data. */
file_name?: string;
/** The hash of the file that was used to extract the data. */
file_hash?: string;
/** Additional metadata about the extracted data, such as errors, tokens, etc. */
metadata?: Record<string, unknown>;
}
/**
* TypedAgentData interface for typed agent data
*/
export interface TypedAgentData<T = unknown> {
/** The unique ID of the agent data record. */
id: string;
/** The ID of the agent that created the data. */
agentUrlId: string;
/** The collection of the agent data. */
collection?: string;
/** The data of the agent data. Usually an ExtractedData&lt;SomeOtherType&gt; */
data: T;
/** The date and time the data was created. */
createdAt: Date;
/** The date and time the data was last updated. */
updatedAt: Date;
}
/**
* Paginated response of typed agent data items
*/
export interface TypedAgentDataItems<T = unknown> {
items: TypedAgentData<T>[];
totalSize?: number;
nextPageToken?: string;
}
/**
* Options for listing agent data
*/
export interface SearchAgentDataOptions {
/** Filter options for the list. */
filter?: Record<string, FilterOperation>;
/** Order by options for the list. */
orderBy?: string;
/** Page size for the list. */
pageSize?: number;
/** Offset for the list. */
offset?: number;
/**
* Whether to include the total number of items in the response.
* Should use only for first request to build total pagination, and not subsequent requests.
*/
includeTotal?: boolean;
}
/**
* Options for aggregating agent data
*/
export interface AggregateAgentDataOptions {
/** Filter options for the aggregation. */
filter?: Record<string, FilterOperation>;
/** Fields to group by. */
groupBy?: string[];
/** Whether to count the number of items in each group. */
count?: boolean;
/** Whether to return the first item in each group. */
first?: boolean;
/** Order by options for the aggregation. */
orderBy?: string;
/** Offset for the aggregation. */
offset?: number;
/** Page size for the aggregation. */
pageSize?: number;
}
/**
* Single aggregation group result
*/
export interface TypedAggregateGroup<T = unknown> {
/** The group key values */
groupKey: Record<string, unknown>;
/** Count of items in the group */
count?: number;
/** First item in the group */
firstItem?: T;
}
/**
* Paginated response of aggregated agent data
*/
export interface TypedAggregateGroupItems<T = unknown> {
items: TypedAggregateGroup<T>[];
totalSize?: number;
nextPageToken?: string;
}
-55
View File
@@ -1,55 +0,0 @@
import { workflowEvent } from "@llama-flow/core";
import { zodEvent } from "@llama-flow/core/util/zod";
import { z } from "zod";
import { parseFormSchema } from "./schema";
export const uploadEvent = zodEvent(
parseFormSchema.merge(
z.object({
file: z
.string()
.or(z.instanceof(File))
.or(z.instanceof(Blob))
.or(z.instanceof(Uint8Array))
.optional()
.describe("input"),
}),
),
{
debugLabel: "upload",
uniqueId: "52099967-34a8-419b-8950-c859eab60145",
},
);
export const checkStatusEvent = workflowEvent<string>({
debugLabel: "check-status",
uniqueId: "462157fc-1ded-4ba7-9bc4-3e870395bd20",
});
export const checkStatusSuccessEvent = workflowEvent<string>({
debugLabel: "check-status-success",
uniqueId: "360b7641-30f7-456e-851d-104bb5e3f8d2",
});
export const requestMarkdownEvent = workflowEvent<string>({
debugLabel: "markdown-request",
uniqueId: "aa4c2e3c-0f09-4035-aab6-c72719c877cc",
});
export const requestTextEvent = workflowEvent<string>({
debugLabel: "text-request",
uniqueId: "6976536e-5399-4285-9455-0b70f1dfc92b",
});
export const requestJsonEvent = workflowEvent<string>({
debugLabel: "json-request",
uniqueId: "9fc4a330-52ad-4aac-8092-a650998b7f6f",
});
export const markdownResultEvent = workflowEvent<string>({
debugLabel: "markdown-result",
uniqueId: "2dfb57c8-73d1-4394-bea8-f05246d934d4",
});
export const textResultEvent = workflowEvent<string>({
debugLabel: "text-result",
uniqueId: "a27deec6-b24f-4eda-a5ac-ba2fb2bf37c8",
});
export const jsonResultEvent = workflowEvent<unknown>({
debugLabel: "json-result",
uniqueId: "e086e6bd-a612-4f25-ab41-9b31dcb8af86",
});
-225
View File
@@ -1,225 +0,0 @@
import { createClient, createConfig } from "@hey-api/client-fetch";
import { createWorkflow, type InferWorkflowEventData } from "@llama-flow/core";
import { createStatefulMiddleware } from "@llama-flow/core/middleware/state";
import { withTraceEvents } from "@llama-flow/core/middleware/trace-events";
import { pRetryHandler } from "@llama-flow/core/util/p-retry";
import { fs, getEnv, path } from "@llamaindex/env";
import {
type BodyUploadFileApiV1ParsingUploadPost,
getJobApiV1ParsingJobJobIdGet,
getJobJsonResultApiV1ParsingJobJobIdResultJsonGet,
getJobResultApiV1ParsingJobJobIdResultMarkdownGet,
getJobTextResultApiV1ParsingJobJobIdResultTextGet,
type StatusEnum,
uploadFileApiV1ParsingUploadPost,
} from "./client";
import {
checkStatusEvent,
checkStatusSuccessEvent,
jsonResultEvent,
markdownResultEvent,
requestJsonEvent,
requestMarkdownEvent,
requestTextEvent,
textResultEvent,
uploadEvent,
} from "./events";
export type LlamaParseWorkflowParams = {
region?: "us" | "eu" | "us-staging";
apiKey?: string;
};
const URLS = {
us: "https://api.cloud.llamaindex.ai",
eu: "https://api.cloud.eu.llamaindex.ai",
"us-staging": "https://api.staging.llamaindex.ai",
} as const;
const { withState, getContext } = createStatefulMiddleware(
(params: LlamaParseWorkflowParams) => {
const apiKey = params.apiKey ?? getEnv("LLAMA_CLOUD_API_KEY");
const region = params.region ?? "us";
if (!apiKey) {
throw new Error("LLAMA_CLOUD_API_KEY is not set");
}
return {
cache: {} as Record<string, StatusEnum>,
client: createClient(
createConfig({
baseUrl: URLS[region],
headers: {
Authorization: `Bearer ${apiKey}`,
},
}),
),
};
},
);
const llamaParseWorkflow = withState(withTraceEvents(createWorkflow()));
llamaParseWorkflow.handle([uploadEvent], async ({ data: form }) => {
const { state } = getContext();
const finalForm = { ...form };
if ("file" in form) {
// support loads from the file system
const file = form?.file;
const isFilePath = typeof file === "string";
const data = isFilePath ? await fs.readFile(file) : file;
const filename: string | undefined = isFilePath
? path.basename(file)
: undefined;
finalForm.file = data
? globalThis.File && filename
? new File([data], filename)
: new Blob([data])
: undefined;
}
const {
data: { id, status },
} = await uploadFileApiV1ParsingUploadPost({
throwOnError: true,
body: {
...finalForm,
} as BodyUploadFileApiV1ParsingUploadPost,
client: state.client,
});
state.cache[id] = status;
return checkStatusEvent.with(id);
});
llamaParseWorkflow.handle(
[checkStatusEvent],
pRetryHandler(
async ({ data: uuid }) => {
const { state } = getContext();
if (state.cache[uuid] === "SUCCESS") {
return checkStatusSuccessEvent.with(uuid);
}
const {
data: { status },
} = await getJobApiV1ParsingJobJobIdGet({
throwOnError: true,
path: {
job_id: uuid,
},
client: state.client,
});
state.cache[uuid] = status;
if (status === "SUCCESS") {
return checkStatusSuccessEvent.with(uuid);
}
throw new Error(`LLamaParse status: ${status}`);
},
{
retries: 100,
},
),
);
//#region sub workflow
llamaParseWorkflow.handle([requestMarkdownEvent], async ({ data: job_id }) => {
const { state } = getContext();
const { data } = await getJobResultApiV1ParsingJobJobIdResultMarkdownGet({
throwOnError: true,
path: {
job_id,
},
client: state.client,
});
return markdownResultEvent.with(data.markdown);
});
llamaParseWorkflow.handle([requestTextEvent], async ({ data: job_id }) => {
const { state } = getContext();
const { data } = await getJobTextResultApiV1ParsingJobJobIdResultTextGet({
throwOnError: true,
path: {
job_id,
},
client: state.client,
});
return textResultEvent.with(data.text);
});
llamaParseWorkflow.handle([requestJsonEvent], async ({ data: job_id }) => {
const { state } = getContext();
const { data } = await getJobJsonResultApiV1ParsingJobJobIdResultJsonGet({
throwOnError: true,
path: {
job_id,
},
client: state.client,
});
return jsonResultEvent.with(data.pages);
});
//#endregion
export type ParseJob = {
get jobId(): string;
get signal(): AbortSignal;
get context(): ReturnType<typeof llamaParseWorkflow.createContext>;
get form(): InferWorkflowEventData<typeof uploadEvent>;
markdown(): Promise<string>;
text(): Promise<string>;
//eslint-disable-next-line @typescript-eslint/no-explicit-any
json(): Promise<any[]>;
};
export const upload = async (
params: InferWorkflowEventData<typeof uploadEvent> & LlamaParseWorkflowParams,
): Promise<ParseJob> => {
//#region upload event
const context = llamaParseWorkflow.createContext(params);
const { stream, sendEvent } = context;
const ev = uploadEvent.with(params);
sendEvent(ev);
const uploadThread = await llamaParseWorkflow
.substream(ev, stream)
.until((ev) => checkStatusSuccessEvent.include(ev))
.toArray();
//#region
const jobId: string = uploadThread.at(-1)!.data;
return {
get signal() {
// lazy load
return context.signal;
},
get jobId() {
return jobId;
},
get form() {
return ev.data;
},
get context() {
return context;
},
async markdown(): Promise<string> {
const requestEv = requestMarkdownEvent.with(jobId);
const { sendEvent, stream } = llamaParseWorkflow.createContext(params);
sendEvent(requestEv);
const markdownThread = await stream.until(markdownResultEvent).toArray();
return markdownThread.at(-1)!.data;
},
async text(): Promise<string> {
const requestEv = requestTextEvent.with(jobId);
const { sendEvent, stream } = llamaParseWorkflow.createContext(params);
sendEvent(requestEv);
const textThread = await stream.until(textResultEvent).toArray();
return textThread.at(-1)!.data;
},
//eslint-disable-next-line @typescript-eslint/no-explicit-any
async json(): Promise<any[]> {
const requestEv = requestJsonEvent.with(jobId);
const { sendEvent, stream } = llamaParseWorkflow.createContext(params);
sendEvent(requestEv);
const jsonThread = await stream
.until((ev) => jsonResultEvent.include(ev))
.toArray();
return jsonThread.at(-1)!.data;
},
};
};
-781
View File
@@ -1,781 +0,0 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { type Client, createClient, createConfig } from "@hey-api/client-fetch";
import { Document, FileReader } from "@llamaindex/core/schema";
import { fs, getEnv, path } from "@llamaindex/env";
import {
type BodyUploadFileApiParsingUploadPost,
type FailPageMode,
type ParserLanguages,
type ParsingMode,
getJobApiV1ParsingJobJobIdGet,
getJobImageResultApiV1ParsingJobJobIdResultImageNameGet,
getJobJsonResultApiV1ParsingJobJobIdResultJsonGet,
getJobResultApiV1ParsingJobJobIdResultMarkdownGet,
getJobTextResultApiV1ParsingJobJobIdResultTextGet,
uploadFileApiV1ParsingUploadPost,
} from "./api";
import { sleep } from "./utils";
export type Language = ParserLanguages;
export type ResultType = "text" | "markdown" | "json";
// Export the backoff pattern type.
export type BackoffPattern = "constant" | "linear" | "exponential";
// TODO: should move into @llamaindex/env
type WriteStream = {
write: (text: string) => void;
};
// Do not modify this variable or cause type errors
// eslint-disable-next-line no-var
var process: any;
/**
* Represents a reader for parsing files using the LlamaParse API.
* See https://github.com/run-llama/llama_parse
*/
export class LlamaParseReader extends FileReader {
project_id?: string | undefined;
organization_id?: string | undefined;
// The API key for the LlamaParse API. Can be set as an environment variable: LLAMA_CLOUD_API_KEY
apiKey: string;
// The base URL of the Llama Cloud Platform.
baseUrl: string = "https://api.cloud.llamaindex.ai";
// The result type for the parser.
resultType: ResultType = "text";
// The interval in seconds to check if the parsing is done.
checkInterval: number = 1;
// The maximum timeout in seconds to wait for the parsing to finish.
maxTimeout = 2000;
// Whether to print the progress of the parsing.
verbose = true;
// The language to parse the file in.
language: ParserLanguages[] = ["en"];
// New polling options:
// Controls the backoff mode: "constant", "linear", or "exponential".
backoffPattern: BackoffPattern = "linear";
// Maximum interval in seconds between polls.
maxCheckInterval: number = 5;
// Maximum number of retryable errors before giving up.
maxErrorCount: number = 4;
// The parsing instruction for the parser. Backend default is an empty string.
parsingInstruction?: string | undefined;
// Whether to ignore diagonal text (when the text rotation in degrees is not 0, 90, 180, or 270). Backend default is false.
skipDiagonalText?: boolean | undefined;
// Whether to ignore the cache and re-process the document. Documents are cached for 48 hours after job completion. Backend default is false.
invalidateCache?: boolean | undefined;
// Whether the document should not be cached. Backend default is false.
doNotCache?: boolean | undefined;
// Whether to use a faster mode to extract text (skipping OCR and table/heading reconstruction). Not compatible with gpt4oMode. Backend default is false.
fastMode?: boolean | undefined;
// Whether to keep columns in the text according to document layout. May reduce reconstruction accuracy and LLM/embedings performance.
doNotUnrollColumns?: boolean | undefined;
// A templated page separator for splitting text. If not set, default is "\n---\n".
pageSeparator?: string | undefined;
// A templated prefix to add at the beginning of each page.
pagePrefix?: string | undefined;
// A templated suffix to add at the end of each page.
pageSuffix?: string | undefined;
// Deprecated. Use vendorMultimodal params. Whether to use gpt-4o to extract text.
gpt4oMode: boolean = false;
// Deprecated. Use vendorMultimodal params. The API key for GPT-4o. Can be set via LLAMA_CLOUD_GPT4O_API_KEY.
gpt4oApiKey?: string | undefined;
// The bounding box margins as a string.
boundingBox?: string | undefined;
// The target pages (comma separated list, starting at 0).
targetPages?: string | undefined;
// Whether to ignore errors during parsing.
ignoreErrors: boolean = true;
// Whether to split by page using the pageSeparator (or "\n---\n" as default).
splitByPage: boolean = true;
// Whether to use the vendor multimodal API.
useVendorMultimodalModel: boolean = false;
// The model name for the vendor multimodal API.
vendorMultimodalModelName?: string | undefined;
// The API key for the multimodal API. Can be set via 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;
continuousMode?: boolean | undefined;
isFormattingInstruction?: boolean | undefined;
annotateLinks?: boolean | undefined;
azureOpenaiDeploymentName?: string | undefined;
azureOpenaiEndpoint?: string | undefined;
azureOpenaiApiVersion?: string | undefined;
azureOpenaiKey?: string | undefined;
auto_mode?: boolean | undefined;
auto_mode_trigger_on_image_in_page?: boolean | undefined;
auto_mode_trigger_on_table_in_page?: boolean | undefined;
auto_mode_trigger_on_text_in_page?: string | undefined;
auto_mode_trigger_on_regexp_in_page?: string | undefined;
bbox_bottom?: number | undefined;
bbox_left?: number | undefined;
bbox_right?: number | undefined;
bbox_top?: number | undefined;
disable_image_extraction?: boolean | undefined;
extract_charts?: boolean | undefined;
guess_xlsx_sheet_name?: boolean | undefined;
html_make_all_elements_visible?: boolean | undefined;
html_remove_fixed_elements?: boolean | undefined;
html_remove_navigation_elements?: boolean | undefined;
http_proxy?: string | undefined;
input_url?: string | undefined;
max_pages?: number | undefined;
output_pdf_of_document?: boolean | undefined;
structured_output?: boolean | undefined;
structured_output_json_schema?: string | undefined;
structured_output_json_schema_name?: string | undefined;
extract_layout?: boolean | undefined;
// numWorkers is implemented in SimpleDirectoryReader
stdout?: WriteStream | undefined;
readonly #client: Client;
output_tables_as_HTML: boolean = false;
input_s3_region?: string | undefined;
output_s3_region?: string | undefined;
preserve_layout_alignment_across_pages?: boolean | undefined;
spreadsheet_extract_sub_tables?: boolean | undefined;
formatting_instruction?: string | undefined;
parse_mode?: ParsingMode | undefined;
system_prompt?: string | undefined;
system_prompt_append?: string | undefined;
user_prompt?: string | undefined;
job_timeout_in_seconds?: number | undefined;
job_timeout_extra_time_per_page_in_seconds?: number | undefined;
strict_mode_image_extraction?: boolean | undefined;
strict_mode_image_ocr?: boolean | undefined;
strict_mode_reconstruction?: boolean | undefined;
strict_mode_buggy_font?: boolean | undefined;
ignore_document_elements_for_layout_detection?: boolean | undefined;
complemental_formatting_instruction?: string | undefined;
content_guideline_instruction?: string | undefined;
adaptive_long_table?: boolean | undefined;
model?: string | undefined;
auto_mode_configuration_json?: string | undefined;
compact_markdown_table?: boolean | undefined;
markdown_table_multiline_header_separator?: string | undefined;
page_error_tolerance?: number | undefined;
replace_failed_page_mode?: FailPageMode | undefined;
replace_failed_page_with_error_message_prefix?: string | undefined;
replace_failed_page_with_error_message_suffix?: string | undefined;
save_images?: boolean | undefined;
preset?: string | undefined;
high_res_ocr?: boolean | undefined;
outlined_table_extraction?: boolean | undefined;
hide_headers?: boolean | undefined;
hide_footers?: boolean | undefined;
page_header_prefix?: string | undefined;
page_header_suffix?: string | undefined;
page_footer_prefix?: string | undefined;
page_footer_suffix?: string | undefined;
merge_tables_across_pages_in_markdown?: boolean | undefined;
constructor(
params: Partial<Omit<LlamaParseReader, "language" | "apiKey">> & {
language?: ParserLanguages | ParserLanguages[] | undefined;
apiKey?: string | undefined;
} = {},
) {
super();
Object.assign(this, params);
this.language = Array.isArray(this.language)
? this.language
: [this.language];
this.stdout =
(params.stdout ?? typeof process !== "undefined")
? process!.stdout
: undefined;
const apiKey = params.apiKey ?? getEnv("LLAMA_CLOUD_API_KEY");
if (!apiKey) {
throw new Error(
"API Key is required for LlamaParseReader. Please pass the apiKey parameter or set the LLAMA_CLOUD_API_KEY environment variable.",
);
}
this.apiKey = apiKey;
if (this.baseUrl.endsWith("/")) {
this.baseUrl = this.baseUrl.slice(0, -1);
}
if (this.baseUrl.endsWith("/api/parsing")) {
this.baseUrl = this.baseUrl.slice(0, -"/api/parsing".length);
}
if (params.gpt4oMode) {
params.gpt4oApiKey =
params.gpt4oApiKey ?? getEnv("LLAMA_CLOUD_GPT4O_API_KEY");
this.gpt4oApiKey = params.gpt4oApiKey;
}
if (params.useVendorMultimodalModel) {
params.vendorMultimodalApiKey =
params.vendorMultimodalApiKey ??
getEnv("LLAMA_CLOUD_VENDOR_MULTIMODAL_API_KEY");
this.vendorMultimodalApiKey = params.vendorMultimodalApiKey;
}
this.#client = createClient(
createConfig({
headers: {
Authorization: `Bearer ${this.apiKey}`,
},
baseUrl: this.baseUrl,
}),
);
}
/**
* Creates a job for the LlamaParse API.
*
* @param data - The file data as a Uint8Array.
* @param filename - Optional filename for the file.
* @returns A Promise resolving to the job ID as a string.
*/
async #createJob(
data: Uint8Array | string,
filename?: string,
): Promise<string> {
if (this.verbose) {
console.log("Started uploading the file");
}
let file: File | Blob | null = null;
let input_s3_path: string | undefined = this.inputS3Path;
let input_url: string | undefined = this.input_url;
if (typeof data !== "string") {
// TODO: remove Blob usage when we drop Node.js 18 support
file =
globalThis.File && filename
? new File([data], filename)
: new Blob([data]);
} else if (data.startsWith("s3://")) {
input_s3_path = data;
} else if (data.startsWith("http://") || data.startsWith("https://")) {
input_url = data;
}
const body = {
file,
input_s3_path,
input_url,
language: this.language,
parsing_instruction: this.parsingInstruction,
skip_diagonal_text: this.skipDiagonalText,
invalidate_cache: this.invalidateCache,
do_not_cache: this.doNotCache,
fast_mode: this.fastMode,
do_not_unroll_columns: this.doNotUnrollColumns,
page_separator: this.pageSeparator,
page_prefix: this.pagePrefix,
page_suffix: this.pageSuffix,
gpt4o_mode: this.gpt4oMode,
gpt4o_api_key: this.gpt4oApiKey,
bounding_box: this.boundingBox,
target_pages: this.targetPages,
use_vendor_multimodal_model: this.useVendorMultimodalModel,
vendor_multimodal_model_name: this.vendorMultimodalModelName,
vendor_multimodal_api_key: this.vendorMultimodalApiKey,
premium_mode: this.premiumMode,
webhook_url: this.webhookUrl,
take_screenshot: this.takeScreenshot,
disable_ocr: this.disableOcr,
disable_reconstruction: this.disableReconstruction,
output_s3_path_prefix: this.outputS3PathPrefix,
continuous_mode: this.continuousMode,
is_formatting_instruction: this.isFormattingInstruction,
annotate_links: this.annotateLinks,
azure_openai_deployment_name: this.azureOpenaiDeploymentName,
azure_openai_endpoint: this.azureOpenaiEndpoint,
azure_openai_api_version: this.azureOpenaiApiVersion,
azure_openai_key: this.azureOpenaiKey,
auto_mode: this.auto_mode,
auto_mode_trigger_on_image_in_page:
this.auto_mode_trigger_on_image_in_page,
auto_mode_trigger_on_table_in_page:
this.auto_mode_trigger_on_table_in_page,
auto_mode_trigger_on_text_in_page: this.auto_mode_trigger_on_text_in_page,
auto_mode_trigger_on_regexp_in_page:
this.auto_mode_trigger_on_regexp_in_page,
bbox_bottom: this.bbox_bottom,
bbox_left: this.bbox_left,
bbox_right: this.bbox_right,
bbox_top: this.bbox_top,
disable_image_extraction: this.disable_image_extraction,
extract_charts: this.extract_charts,
guess_xlsx_sheet_name: this.guess_xlsx_sheet_name,
html_make_all_elements_visible: this.html_make_all_elements_visible,
html_remove_fixed_elements: this.html_remove_fixed_elements,
html_remove_navigation_elements: this.html_remove_navigation_elements,
http_proxy: this.http_proxy,
max_pages: this.max_pages,
output_pdf_of_document: this.output_pdf_of_document,
structured_output: this.structured_output,
structured_output_json_schema: this.structured_output_json_schema,
structured_output_json_schema_name:
this.structured_output_json_schema_name,
extract_layout: this.extract_layout,
output_tables_as_HTML: this.output_tables_as_HTML,
input_s3_region: this.input_s3_region,
output_s3_region: this.output_s3_region,
preserve_layout_alignment_across_pages:
this.preserve_layout_alignment_across_pages,
spreadsheet_extract_sub_tables: this.spreadsheet_extract_sub_tables,
formatting_instruction: this.formatting_instruction,
parse_mode: this.parse_mode,
system_prompt: this.system_prompt,
system_prompt_append: this.system_prompt_append,
user_prompt: this.user_prompt,
job_timeout_in_seconds: this.job_timeout_in_seconds,
job_timeout_extra_time_per_page_in_seconds:
this.job_timeout_extra_time_per_page_in_seconds,
strict_mode_image_extraction: this.strict_mode_image_extraction,
strict_mode_image_ocr: this.strict_mode_image_ocr,
strict_mode_reconstruction: this.strict_mode_reconstruction,
strict_mode_buggy_font: this.strict_mode_buggy_font,
ignore_document_elements_for_layout_detection:
this.ignore_document_elements_for_layout_detection,
complemental_formatting_instruction:
this.complemental_formatting_instruction,
content_guideline_instruction: this.content_guideline_instruction,
adaptive_long_table: this.adaptive_long_table,
model: this.model,
auto_mode_configuration_json: this.auto_mode_configuration_json,
compact_markdown_table: this.compact_markdown_table,
markdown_table_multiline_header_separator:
this.markdown_table_multiline_header_separator,
page_error_tolerance: this.page_error_tolerance,
replace_failed_page_mode: this.replace_failed_page_mode,
replace_failed_page_with_error_message_prefix:
this.replace_failed_page_with_error_message_prefix,
replace_failed_page_with_error_message_suffix:
this.replace_failed_page_with_error_message_suffix,
save_images: this.save_images,
preset: this.preset,
high_res_ocr: this.high_res_ocr,
outlined_table_extraction: this.outlined_table_extraction,
hide_headers: this.hide_headers,
hide_footers: this.hide_footers,
page_header_prefix: this.page_header_prefix,
page_header_suffix: this.page_header_suffix,
page_footer_prefix: this.page_footer_prefix,
page_footer_suffix: this.page_footer_suffix,
merge_tables_across_pages_in_markdown:
this.merge_tables_across_pages_in_markdown,
} satisfies {
[Key in keyof BodyUploadFileApiParsingUploadPost]-?:
| BodyUploadFileApiParsingUploadPost[Key]
| undefined;
} as unknown as BodyUploadFileApiParsingUploadPost;
const response = await uploadFileApiV1ParsingUploadPost({
client: this.#client,
throwOnError: true,
query: {
project_id: this.project_id ?? null,
organization_id: this.organization_id ?? null,
},
signal: AbortSignal.timeout(this.maxTimeout * 1000),
body,
});
return response.data.id;
}
/**
* Retrieves the result of a parsing job.
*
* Uses a polling loop with retry logic. Each API call is retried
* up to maxErrorCount times if it fails with a 5XX or socket error.
* The delay between polls increases according to the specified backoffPattern ("constant", "linear", or "exponential"),
* capped by maxCheckInterval.
*
* @param jobId - The job ID.
* @param resultType - The type of result to fetch ("text", "json", or "markdown").
* @returns A Promise resolving to the job result.
*/
private async getJobResult(
jobId: string,
resultType: "text" | "json" | "markdown",
): Promise<any> {
let tries = 0;
let currentInterval = this.checkInterval;
const { default: pRetry } = await import("p-retry");
while (true) {
await sleep(currentInterval * 1000);
// Wraps the API call in a retry
let result;
try {
result = await pRetry(
() =>
getJobApiV1ParsingJobJobIdGet({
client: this.#client,
throwOnError: true,
path: { job_id: jobId },
signal: AbortSignal.timeout(this.maxTimeout * 1000),
}),
{
retries: this.maxErrorCount,
onFailedAttempt: (error) => {
// Retry only on 5XX or socket errors.
const status = (error.cause as any)?.response?.status;
if (
!(
(status && status >= 500 && status < 600) ||
((error.cause as any)?.code &&
((error.cause as any).code === "ECONNRESET" ||
(error.cause as any).code === "ETIMEDOUT" ||
(error.cause as any).code === "ECONNREFUSED"))
)
) {
throw error;
}
if (this.verbose) {
console.warn(
`Attempting to get job ${jobId} result (attempt ${error.attemptNumber}) failed. Retrying...`,
);
}
},
},
);
} catch (e: any) {
throw new Error(
`Max error count reached for job ${jobId}: ${e.message}`,
);
}
const { data } = result;
const status = (data as Record<string, unknown>)["status"];
if (status === "SUCCESS") {
let resultData;
switch (resultType) {
case "json": {
resultData =
await getJobJsonResultApiV1ParsingJobJobIdResultJsonGet({
client: this.#client,
throwOnError: true,
path: { job_id: jobId },
query: {
organization_id: this.organization_id ?? null,
},
signal: AbortSignal.timeout(this.maxTimeout * 1000),
});
break;
}
case "markdown": {
resultData =
await getJobResultApiV1ParsingJobJobIdResultMarkdownGet({
client: this.#client,
throwOnError: true,
path: { job_id: jobId },
query: {
organization_id: this.organization_id ?? null,
},
signal: AbortSignal.timeout(this.maxTimeout * 1000),
});
break;
}
case "text": {
resultData =
await getJobTextResultApiV1ParsingJobJobIdResultTextGet({
client: this.#client,
throwOnError: true,
path: { job_id: jobId },
query: {
organization_id: this.organization_id ?? null,
},
signal: AbortSignal.timeout(this.maxTimeout * 1000),
});
break;
}
}
return resultData.data;
} else if (status === "PENDING") {
if (this.verbose && tries % 10 === 0) {
this.stdout?.write(".");
}
tries++;
} else {
if (this.verbose) {
console.error(
`Received error response ${status} for job ${jobId}. Got Error Code: ${data.error_code} and Error Message: ${data.error_message}`,
);
}
throw new Error(
`Failed to parse the file: ${jobId}, status: ${status}`,
);
}
// Adjust the polling interval based on the backoff pattern.
if (this.backoffPattern === "exponential") {
currentInterval = Math.min(currentInterval * 2, this.maxCheckInterval);
} else if (this.backoffPattern === "linear") {
currentInterval = Math.min(
currentInterval + this.checkInterval,
this.maxCheckInterval,
);
} else if (this.backoffPattern === "constant") {
currentInterval = this.checkInterval;
}
}
}
override async loadData(filePath?: string): Promise<Document[]> {
if (!filePath) {
if (this.input_url) {
return this.loadDataAsContent(this.input_url, this.input_url);
} else if (this.inputS3Path) {
return this.loadDataAsContent(this.inputS3Path, this.inputS3Path);
} else {
throw new TypeError("File path is required");
}
} else {
const data =
filePath.startsWith("s3://") ||
filePath.startsWith("http://") ||
filePath.startsWith("https://")
? filePath
: await fs.readFile(filePath);
return this.loadDataAsContent(data, filePath);
}
}
/**
* Loads data from a file and returns an array of Document objects.
* To be used with resultType "text" or "markdown".
*
* @param fileContent - The content of the file as a Uint8Array.
* @param filename - Optional filename for the file.
* @returns A Promise that resolves to an array of Document objects.
*/
async loadDataAsContent(
fileContent: Uint8Array | string,
filename?: string,
): Promise<Document[]> {
return this.#createJob(fileContent, filename)
.then(async (jobId) => {
if (this.verbose) {
console.log(`Started parsing the file under job id ${jobId}`);
}
// Return results as Document objects.
const jobResults = await this.getJobResult(jobId, this.resultType);
const resultText = jobResults[this.resultType];
// Split the text by separator if splitByPage is true.
if (this.splitByPage) {
return this.splitTextBySeparator(resultText);
}
return [new Document({ text: resultText })];
})
.catch((error) => {
console.warn(
`Error while parsing the file with: ${error.message ?? error.detail}`,
);
if (this.ignoreErrors) {
return [];
} else {
throw error;
}
});
}
/**
* Loads data from a file and returns an array of JSON objects.
* To be used with resultType "json".
*
* @param filePathOrContent - The file path or the file content as a Uint8Array.
* @returns A Promise that resolves to an array of JSON objects.
*/
async loadJson(
filePathOrContent: string | Uint8Array,
): Promise<Record<string, any>[]> {
let jobId;
const isFilePath =
typeof filePathOrContent === "string" &&
!(
filePathOrContent.startsWith("s3://") ||
filePathOrContent.startsWith("http://") ||
filePathOrContent.startsWith("https://")
);
try {
const data = isFilePath
? await fs.readFile(filePathOrContent)
: filePathOrContent;
// Create a job for the file.
jobId = await this.#createJob(
data,
isFilePath ? path.basename(filePathOrContent) : undefined,
);
if (this.verbose) {
console.log(`Started parsing the file under job id ${jobId}`);
}
// Return results as an array of JSON objects.
const resultJson = await this.getJobResult(jobId, "json");
resultJson.job_id = jobId;
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) {
return [];
} else {
throw e;
}
}
}
/**
* Downloads and saves images from a given JSON result to a specified download path.
* Currently only supports resultType "json".
*
* @param jsonResult - The JSON result containing image information.
* @param downloadPath - The path where the downloaded images will be saved.
* @returns A Promise that resolves to an array of image objects.
*/
async getImages(
jsonResult: Record<string, any>[],
downloadPath: string,
): Promise<Record<string, any>[]> {
try {
// Create download directory if it doesn't exist (checks for write access).
try {
await fs.access(downloadPath);
} catch {
await fs.mkdir(downloadPath, { recursive: true });
}
const images: Record<string, any>[] = [];
for (const result of jsonResult) {
const jobId = result.job_id;
for (const page of result.pages) {
if (this.verbose) {
console.log(`> Image for page ${page.page}: ${page.images}`);
}
for (const image of page.images) {
const imageName = image.name;
const imagePath = await this.getImagePath(
downloadPath,
jobId,
imageName,
);
await this.fetchAndSaveImage(imageName, imagePath, jobId);
// Assign metadata to the image.
image.path = imagePath;
image.job_id = jobId;
image.original_pdf_path = result.file_path;
image.page_number = page.page;
images.push(image);
}
}
}
return images;
} catch (e) {
console.error(`Error while downloading images from the parsed result`, e);
if (this.ignoreErrors) {
return [];
} else {
throw e;
}
}
}
/**
* Constructs the file path for an image.
*
* @param downloadPath - The base download directory.
* @param jobId - The job ID.
* @param imageName - The image name.
* @returns A Promise that resolves to the full image path.
*/
private async getImagePath(
downloadPath: string,
jobId: string,
imageName: string,
): Promise<string> {
return path.join(downloadPath, `${jobId}-${imageName}`);
}
/**
* Fetches an image from the API and saves it to the specified path.
*
* @param imageName - The name of the image.
* @param imagePath - The local path to save the image.
* @param jobId - The associated job ID.
*/
private async fetchAndSaveImage(
imageName: string,
imagePath: string,
jobId: string,
): Promise<void> {
const response =
await getJobImageResultApiV1ParsingJobJobIdResultImageNameGet({
client: this.#client,
path: {
job_id: jobId,
name: imageName,
},
});
if (response.error) {
throw new Error(`Failed to download image: ${response.error.detail}`);
}
const blob = (await response.data) as Blob;
// Write the image buffer to the specified imagePath.
await fs.writeFile(imagePath, new Uint8Array(await blob.arrayBuffer()));
}
/**
* Filters out invalid values (null, undefined, empty string) for specific parameters.
*
* @param params - The parameters object.
* @param keysToCheck - The keys to check for valid values.
* @returns A new object with filtered parameters.
*/
private filterSpecificParams(
params: Record<string, any>,
keysToCheck: string[],
): Record<string, any> {
const filteredParams: Record<string, any> = {};
for (const [key, value] of Object.entries(params)) {
if (keysToCheck.includes(key)) {
if (value !== null && value !== undefined && value !== "") {
filteredParams[key] = value;
}
} else {
filteredParams[key] = value;
}
}
return filteredParams;
}
/**
* Splits text into Document objects using the page separator.
*
* @param text - The text to be split.
* @returns An array of Document objects.
*/
private splitTextBySeparator(text: string): Document[] {
const separator = this.pageSeparator ?? "\n---\n";
const textChunks = text.split(separator);
return textChunks.map(
(docChunk: string) =>
new Document({
text: docChunk,
}),
);
}
}
-131
View File
@@ -1,131 +0,0 @@
import { FailPageMode, ParserLanguages, ParsingMode } from "./client";
import { z } from "zod";
type Language = ParserLanguages;
const VALUES: [Language, ...Language[]] = [
ParserLanguages.EN,
...Object.values(ParserLanguages),
];
const languageSchema = z.enum(VALUES);
const PARSE_PRESETS = [
"fast",
"balanced",
"premium",
"structured",
"auto",
"scientific",
"invoice",
"slides",
"_carlyle",
] as const;
export const parsePresetSchema = z.enum(PARSE_PRESETS);
export const parseFormSchema = z.object({
adaptive_long_table: z.boolean().optional(),
annotate_links: z.boolean().optional(),
auto_mode: z.boolean().optional(),
auto_mode_trigger_on_image_in_page: z.boolean().optional(),
auto_mode_trigger_on_table_in_page: z.boolean().optional(),
auto_mode_trigger_on_text_in_page: z.string().optional(),
auto_mode_trigger_on_regexp_in_page: z.string().optional(),
auto_mode_configuration_json: z.string().optional(),
azure_openai_api_version: z.string().optional(),
azure_openai_deployment_name: z.string().optional(),
azure_openai_endpoint: z.string().optional(),
azure_openai_key: z.string().optional(),
bbox_bottom: z.number().min(0).max(1).optional(),
bbox_left: z.number().min(0).max(1).optional(),
bbox_right: z.number().min(0).max(1).optional(),
bbox_top: z.number().min(0).max(1).optional(),
disable_ocr: z.boolean().optional(),
disable_reconstruction: z.boolean().optional(),
disable_image_extraction: z.boolean().optional(),
do_not_cache: z.coerce.boolean().optional(),
do_not_unroll_columns: z.coerce.boolean().optional(),
extract_charts: z.boolean().optional(),
guess_xlsx_sheet_name: z.boolean().optional(),
html_make_all_elements_visible: z.boolean().optional(),
html_remove_fixed_elements: z.boolean().optional(),
html_remove_navigation_elements: z.boolean().optional(),
http_proxy: z
.string()
.url(
'Set a valid URL for the HTTP proxy, e.g., "http://proxy.example.com:8080"',
)
.refine(
(url) => {
try {
const parsedUrl = new URL(url);
return (
parsedUrl.protocol === "http:" || parsedUrl.protocol === "https:"
);
} catch {
return false;
}
},
{
message: "Invalid HTTP proxy URL",
},
)
.optional(),
input_s3_path: z.string().optional(),
input_s3_region: z.string().optional(),
input_url: z.string().optional(),
invalidate_cache: z.boolean().optional(),
language: z.array(languageSchema).optional(),
extract_layout: z.boolean().optional(),
max_pages: z.number().nullable().optional(),
output_pdf_of_document: z.boolean().optional(),
output_s3_path_prefix: z.string().optional(),
output_s3_region: z.string().optional(),
page_prefix: z.string().optional(),
page_separator: z.string().optional(),
page_suffix: z.string().optional(),
preserve_layout_alignment_across_pages: z.boolean().optional(),
skip_diagonal_text: z.boolean().optional(),
spreadsheet_extract_sub_tables: z.boolean().optional(),
structured_output: z.boolean().optional(),
structured_output_json_schema: z.string().optional(),
structured_output_json_schema_name: z.string().optional(),
take_screenshot: z.boolean().optional(),
target_pages: z.string().optional(),
vendor_multimodal_api_key: z.string().optional(),
vendor_multimodal_model_name: z.string().optional(),
model: z.string().optional(),
webhook_url: z.string().url().optional(),
parse_mode: z.nativeEnum(ParsingMode).nullable().optional(),
system_prompt: z.string().optional(),
system_prompt_append: z.string().optional(),
user_prompt: z.string().optional(),
job_timeout_in_seconds: z.number().optional(),
job_timeout_extra_time_per_page_in_seconds: z.number().optional(),
strict_mode_image_extraction: z.boolean().optional(),
strict_mode_image_ocr: z.boolean().optional(),
strict_mode_reconstruction: z.boolean().optional(),
strict_mode_buggy_font: z.boolean().optional(),
save_images: z.boolean().optional(),
ignore_document_elements_for_layout_detection: z.boolean().optional(),
output_tables_as_HTML: z.boolean().optional(),
use_vendor_multimodal_model: z.boolean().optional(),
bounding_box: z.string().optional(),
gpt4o_mode: z.boolean().optional(),
gpt4o_api_key: z.string().optional(),
complemental_formatting_instruction: z.string().optional(),
content_guideline_instruction: z.string().optional(),
premium_mode: z.boolean().optional(),
is_formatting_instruction: z.boolean().optional(),
continuous_mode: z.boolean().optional(),
parsing_instruction: z.string().optional(),
fast_mode: z.boolean().optional(),
formatting_instruction: z.string().optional(),
preset: parsePresetSchema.optional(),
compact_markdown_table: z.boolean().optional(),
markdown_table_multiline_header_separator: z.string().optional(),
page_error_tolerance: z.number().min(0).max(1).optional(),
replace_failed_page_mode: z.nativeEnum(FailPageMode).nullable().optional(),
replace_failed_page_with_error_message_prefix: z.string().optional(),
replace_failed_page_with_error_message_suffix: z.string().optional(),
});
-3
View File
@@ -1,3 +0,0 @@
export async function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
-9
View File
@@ -1,9 +0,0 @@
{
"$schema": "https://turbo.build/schema.json",
"extends": ["//"],
"tasks": {
"build": {
"outputs": ["**/dist/**", "src/client/**"]
}
}
}
@@ -1,152 +1,27 @@
# @llamaindex/cloud
# @llamaindex/community
## 4.0.26
## 0.0.101
### Patch Changes
- Updated dependencies [38da40b]
- @llamaindex/core@0.6.17
- Updated dependencies [f9f1de9]
- @llamaindex/core@0.6.19
## 4.0.25
## 0.0.100
### Patch Changes
- 2967d57: Default to \_public agent url id
- Updated dependencies [a8ec08c]
- @llamaindex/core@0.6.16
- Updated dependencies [f29799e]
- Updated dependencies [7224c06]
- @llamaindex/core@0.6.18
## 4.0.24
## 0.0.99
### Patch Changes
- Updated dependencies [7ad3411]
- Updated dependencies [5da5b3c]
- @llamaindex/core@0.6.15
- c65a2dc: Deprecate community package and link to AWS package
## 4.0.23
### Patch Changes
- a1b1598: fix: add generic types into agent data responses
## 4.0.22
### Patch Changes
- d2be868: Bug fixes for new beta agent-data cloud API
## 4.0.21
### Patch Changes
- 579ca0c: chore: bump sdk version
## 4.0.20
### Patch Changes
- 48b0d88: fix: exports in `api` submodule
- f185772: fix(cloud): missing file
## 4.0.19
### Patch Changes
- 5a0ed1f: feat: init agent api on cloud sdk
- 5a0ed1f: feat: init agent api on cloud sdk
- Updated dependencies [8eeac33]
- @llamaindex/core@0.6.14
## 4.0.18
### Patch Changes
- 47a7555: chore: bump sdk version
## 4.0.17
### Patch Changes
- Updated dependencies [d578889]
- Updated dependencies [0fcc92f]
- Updated dependencies [515a8b9]
- @llamaindex/core@0.6.13
## 4.0.16
### Patch Changes
- Updated dependencies [7039e1a]
- Updated dependencies [7039e1a]
- @llamaindex/core@0.6.12
## 4.0.15
### Patch Changes
- Updated dependencies [a89e187]
- Updated dependencies [62699b7]
- Updated dependencies [c5b2691]
- Updated dependencies [d8ac8d3]
- @llamaindex/core@0.6.11
## 4.0.14
### Patch Changes
- Updated dependencies [1b5af14]
- @llamaindex/core@0.6.10
## 4.0.13
### Patch Changes
- Updated dependencies [71598f8]
- @llamaindex/core@0.6.9
## 4.0.12
### Patch Changes
- Updated dependencies [c927457]
- @llamaindex/core@0.6.8
## 4.0.11
### Patch Changes
- 76ff23d: Fix pRetry not working with CommonJS
## 4.0.10
### Patch Changes
- Updated dependencies [59601dd]
- @llamaindex/core@0.6.7
## 4.0.9
### Patch Changes
- 3703f90: feat(parse): add upload API
## 4.0.8
### Patch Changes
- Updated dependencies [680b529]
- Updated dependencies [361a685]
- @llamaindex/core@0.6.6
## 4.0.7
### Patch Changes
- 40f5f41: Improve the loadJson function in LlamaParseReader to align with loadData by allowing URL inputs. Ensures s3://, http://, and https:// paths are not treated as local file paths.
- Updated dependencies [d671ed6]
- @llamaindex/core@0.6.5
## 4.0.6
## 0.0.98
### Patch Changes
@@ -154,33 +29,33 @@
- @llamaindex/core@0.6.4
- @llamaindex/env@0.1.30
## 4.0.5
### Patch Changes
- 2225ffd: feat: bump llama cloud sdk
## 4.0.4
## 0.0.97
### Patch Changes
- Updated dependencies [3ee8c83]
- @llamaindex/core@0.6.3
## 4.0.3
## 0.0.96
### Patch Changes
- 41191d0: fix(parse): file input
- e9bf442: fix: update the tool call schema for nova
## 4.0.2
## 0.0.95
### Patch Changes
- 411dcea: Add Nova Premier to AWS Nova models. Add EU endpoints
## 0.0.94
### Patch Changes
- Updated dependencies [9c63f3f]
- @llamaindex/core@0.6.2
## 4.0.1
## 0.0.93
### Patch Changes
@@ -188,75 +63,77 @@
- Updated dependencies [eaf326e]
- @llamaindex/core@0.6.1
## 4.0.0
## 0.0.92
### Patch Changes
- 1325178: fix: stringify all tool results for anthropic on bedrock
## 0.0.91
### Patch Changes
- bf56fc0: chore: bump sdk openapi.json
- 5189b44: fix: add retry handling logic to parser reader and fix lint issues
- 3fd4cc3: feat: use google's new gen ai library to support multimodal output
- Updated dependencies [21bebfc]
- Updated dependencies [93bc0ff]
- Updated dependencies [91a18e7]
- Updated dependencies [5189b44]
- @llamaindex/core@0.6.0
## 3.0.9
## 0.0.90
### Patch Changes
- Updated dependencies [40ee761]
- @llamaindex/core@0.5.8
## 3.0.8
## 0.0.89
### Patch Changes
- Updated dependencies [4bac71d]
- @llamaindex/core@0.5.7
## 3.0.7
## 0.0.88
### Patch Changes
- e28c29d: Added Llama 3.3 70B Instruct support
- Updated dependencies [beb922b]
- @llamaindex/env@0.1.29
- @llamaindex/core@0.5.6
## 3.0.6
## 0.0.87
### Patch Changes
- Updated dependencies [5668970]
- @llamaindex/core@0.5.5
## 3.0.5
## 0.0.86
### Patch Changes
- Updated dependencies [ad3c7f1]
- @llamaindex/core@0.5.4
## 3.0.4
## 0.0.85
### Patch Changes
- 1914b52: Added Claude 3.7 Sonnet support
- Updated dependencies [cb021e7]
- @llamaindex/core@0.5.3
## 3.0.3
## 0.0.84
### Patch Changes
- Updated dependencies [d952e68]
- @llamaindex/core@0.5.2
## 3.0.2
### Patch Changes
- c902fcb: chore: bump llamacloud openapi
## 3.0.1
## 0.0.83
### Patch Changes
@@ -264,7 +141,7 @@
- @llamaindex/env@0.1.28
- @llamaindex/core@0.5.1
## 3.0.0
## 0.0.82
### Patch Changes
@@ -272,7 +149,7 @@
- Updated dependencies [d924c63]
- @llamaindex/core@0.5.0
## 2.0.24
## 0.0.81
### Patch Changes
@@ -281,7 +158,7 @@
- @llamaindex/core@0.4.23
- @llamaindex/env@0.1.27
## 2.0.23
## 0.0.80
### Patch Changes
@@ -290,54 +167,47 @@
- @llamaindex/core@0.4.22
- @llamaindex/env@0.1.26
## 2.0.22
## 0.0.79
### Patch Changes
- d6c270e: feat: support pass project and org id to llama parse reader
- Updated dependencies [9456616]
- Updated dependencies [1931bbc]
- @llamaindex/core@0.4.21
## 2.0.21
## 0.0.78
### Patch Changes
- 5dec9f9: chore: bump sdk deps version
- fd9c829: chore: bump llamacloud openapi
- Updated dependencies [d211b7a]
- @llamaindex/core@0.4.20
## 2.0.20
### Patch Changes
- 012495b: chore: bump llamacloud sdk
## 2.0.19
## 0.0.77
### Patch Changes
- 24caf93: fix: added inference profile mapping for nova models"
- Updated dependencies [a9b5b99]
- @llamaindex/core@0.4.19
## 2.0.18
## 0.0.76
### Patch Changes
- c1850ee: feat: Amazon Nova support via Bedrock
- Updated dependencies [b504303]
- Updated dependencies [e0f6cc3]
- @llamaindex/env@0.1.25
- @llamaindex/core@0.4.18
## 2.0.17
## 0.0.75
### Patch Changes
- Updated dependencies [3d1808b]
- @llamaindex/core@0.4.17
## 2.0.16
## 0.0.74
### Patch Changes
@@ -346,7 +216,7 @@
- @llamaindex/core@0.4.16
- @llamaindex/env@0.1.24
## 2.0.15
## 0.0.73
### Patch Changes
@@ -354,7 +224,7 @@
- @llamaindex/env@0.1.23
- @llamaindex/core@0.4.15
## 2.0.14
## 0.0.72
### Patch Changes
@@ -362,7 +232,7 @@
- @llamaindex/env@0.1.22
- @llamaindex/core@0.4.14
## 2.0.13
## 0.0.71
### Patch Changes
@@ -371,21 +241,21 @@
- @llamaindex/core@0.4.13
- @llamaindex/env@0.1.21
## 2.0.12
## 0.0.70
### Patch Changes
- Updated dependencies [ef4f63d]
- @llamaindex/core@0.4.12
## 2.0.11
## 0.0.69
### Patch Changes
- Updated dependencies [6d22fa2]
- @llamaindex/core@0.4.11
## 2.0.10
## 0.0.68
### Patch Changes
@@ -393,21 +263,21 @@
- Updated dependencies [c69605f]
- @llamaindex/core@0.4.10
## 2.0.9
## 0.0.67
### Patch Changes
- Updated dependencies [7ae6eaa]
- @llamaindex/core@0.4.9
## 2.0.8
## 0.0.66
### Patch Changes
- Updated dependencies [f865c98]
- @llamaindex/core@0.4.8
## 2.0.7
## 0.0.65
### Patch Changes
@@ -415,7 +285,7 @@
- Updated dependencies [fd8c882]
- @llamaindex/core@0.4.7
## 2.0.6
## 0.0.64
### Patch Changes
@@ -423,7 +293,7 @@
- @llamaindex/env@0.1.20
- @llamaindex/core@0.4.6
## 2.0.5
## 0.0.63
### Patch Changes
@@ -431,7 +301,7 @@
- @llamaindex/core@0.4.5
- @llamaindex/env@0.1.19
## 2.0.4
## 0.0.62
### Patch Changes
@@ -439,14 +309,15 @@
- @llamaindex/env@0.1.18
- @llamaindex/core@0.4.4
## 2.0.3
## 0.0.61
### Patch Changes
- 487782c: Add missing inference endpoints for Haiku 3.5
- Updated dependencies [95a5cc6]
- @llamaindex/core@0.4.3
## 2.0.2
## 0.0.60
### Patch Changes
@@ -454,14 +325,20 @@
- @llamaindex/env@0.1.17
- @llamaindex/core@0.4.2
## 2.0.1
## 0.0.59
### Patch Changes
- 47a7c3e: feat: added support for Haiku 3.5 via Bedrock
## 0.0.58
### Patch Changes
- Updated dependencies [9c73f0a]
- @llamaindex/core@0.4.1
## 2.0.0
## 0.0.57
### Patch Changes
@@ -471,21 +348,21 @@
- Updated dependencies [620c63c]
- @llamaindex/core@0.4.0
## 1.0.8
## 0.0.56
### Patch Changes
- Updated dependencies [60b185f]
- @llamaindex/core@0.3.7
## 1.0.7
## 0.0.55
### Patch Changes
- Updated dependencies [691c5bc]
- @llamaindex/core@0.3.6
## 1.0.6
## 0.0.54
### Patch Changes
@@ -493,27 +370,27 @@
- @llamaindex/env@0.1.16
- @llamaindex/core@0.3.5
## 1.0.5
## 0.0.53
### Patch Changes
- Updated dependencies [e2a0876]
- @llamaindex/core@0.3.4
## 1.0.4
## 0.0.52
### Patch Changes
- 06f632b: fix(cloud): allow filename in llama parse
- a5a75f6: feat: added sonnet 3.5 v2
## 1.0.3
## 0.0.51
### Patch Changes
- Updated dependencies [0493f67]
- @llamaindex/core@0.3.3
## 1.0.2
## 0.0.50
### Patch Changes
@@ -521,19 +398,17 @@
- @llamaindex/env@0.1.15
- @llamaindex/core@0.3.2
## 1.0.1
## 0.0.49
### Patch Changes
- 4c38c1b: fix(cloud): do not detect file type in llama parse
- 24d065f: Log Parse Job Errors when verbose is enabled
- a75af83: refactor: move some llm and embedding to single package
- Updated dependencies [ae49ff4]
- Updated dependencies [a75af83]
- @llamaindex/env@0.1.14
- @llamaindex/core@0.3.1
## 1.0.0
## 0.0.48
### Patch Changes
@@ -541,21 +416,21 @@
- Updated dependencies [96fc69c]
- @llamaindex/core@0.3.0
## 0.2.14
## 0.0.47
### Patch Changes
- Updated dependencies [5f67820]
- @llamaindex/core@0.2.12
## 0.2.13
## 0.0.46
### Patch Changes
- Updated dependencies [ee697fb]
- @llamaindex/core@0.2.11
## 0.2.12
## 0.0.45
### Patch Changes
@@ -563,19 +438,20 @@
- Updated dependencies [468bda5]
- @llamaindex/core@0.2.10
## 0.2.11
## 0.0.44
### Patch Changes
- 0b20ff9: fix: package.json format
- Updated dependencies [b17d439]
- @llamaindex/core@0.2.9
## 0.2.10
## 0.0.43
### Patch Changes
- 981811e: fix(cloud): llama parse reader save image incorrectly
- 2774e80: feat: added meta3.2 support via Bedrock including vision, tool call and inference region support
## 0.2.9
## 0.0.42
### Patch Changes
@@ -584,41 +460,54 @@
- @llamaindex/core@0.2.8
- @llamaindex/env@0.1.13
## 0.2.8
## 0.0.41
### Patch Changes
- ac41ed3: feat: bump cloud sdk version
- Updated dependencies [6cce3b1]
- @llamaindex/core@0.2.7
## 0.2.7
## 0.0.40
### Patch Changes
- fb36eff: fix: backport for node.js 18
- 50e6b57: feat: add Amazon Bedrock Retriever
- Updated dependencies [8b7fdba]
- @llamaindex/core@0.2.6
There could have one missing API in the node.js 18, so we need to backport it to make it work.
## 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
- d24d3d1: fix: print warning when llama parse reader has error
- Updated dependencies [2cd1383]
- @llamaindex/core@0.2.3
## 0.2.6
## 0.0.36
### Patch Changes
- b42adeb: fix: get job result in llama parse reader
- Updated dependencies [749b43a]
- @llamaindex/core@0.2.2
## 0.2.5
## 0.0.35
### Patch Changes
- 85c2e19: feat: `@llamaindex/cloud` package update
- Bump to latest openapi schema
- Move LlamaParse class from llamaindex, this will allow you use llamaparse in more non-node.js environment
- Updated dependencies [ac07e3c]
- Updated dependencies [70ccb4a]
- Updated dependencies [1a6137b]
@@ -626,56 +515,263 @@
- @llamaindex/core@0.2.1
- @llamaindex/env@0.1.11
## 0.2.4
## 0.0.34
### Patch Changes
- 4810364: fix: bump version
- Updated dependencies [11feef8]
- @llamaindex/core@0.2.0
## 0.2.3
## 0.0.33
### Patch Changes
- 0bf8d80: fix: bump version
- Updated dependencies [711c814]
- @llamaindex/core@0.1.12
## 0.2.2
## 0.0.32
### Patch Changes
- Updated dependencies [4648da6]
- @llamaindex/env@0.1.10
- @llamaindex/core@0.1.11
## 0.0.31
### Patch Changes
- Updated dependencies [0148354]
- @llamaindex/core@0.1.10
## 0.0.30
### Patch Changes
- Updated dependencies [e27e7dd]
- @llamaindex/core@0.1.9
## 0.0.29
### Patch Changes
- 58abc57: fix: align version
- Updated dependencies [58abc57]
- @llamaindex/core@0.1.8
- @llamaindex/env@0.1.9
## 0.2.1
## 0.0.28
### Patch Changes
- 1f680d7: chore: bump llamacloud api
- Updated dependencies [04b2f8e]
- @llamaindex/core@0.1.7
## 0.2.0
### Minor Changes
- 3ed6acc: feat: cloud api change
## 0.1.4
## 0.0.27
### Patch Changes
- 36ddec4: fix: typo in custom page separator parameter for LlamaParse
- Updated dependencies [0452af9]
- @llamaindex/core@0.1.6
## 0.1.3
## 0.0.26
### Patch Changes
- 1c444d5: feat(cloud): update openapi.json
- 224d507: fix: prevent tool calling getting mixed with conversation
- 376d29a: feat: added tool calling and agent support for llama3.1 504B
## 0.1.2
## 0.0.25
### Patch Changes
- Updated dependencies [91d02a4]
- @llamaindex/core@0.1.5
## 0.0.24
### Patch Changes
- 3d9a802: feat: added llama 3.1
- Updated dependencies [15962b3]
- @llamaindex/core@0.1.4
## 0.0.23
### Patch Changes
- Updated dependencies [6cf6ae6]
- @llamaindex/core@0.1.3
## 0.0.22
### Patch Changes
- Updated dependencies [b974eea]
- @llamaindex/core@0.1.2
## 0.0.21
### Patch Changes
- Updated dependencies [b3681bf]
- @llamaindex/core@0.1.1
## 0.0.20
### Patch Changes
- 56746c2: fix: llama3 patched to handle empty content (can happen with system) and added max tokens export
## 0.0.19
### Patch Changes
- 16ef5dd: refactor: depends on core pacakge instead of llamaindex
- Updated dependencies [16ef5dd]
- Updated dependencies [16ef5dd]
- @llamaindex/core@0.1.0
## 0.0.18
### Patch Changes
- llamaindex@0.4.14
## 0.0.17
### Patch Changes
- Updated dependencies [e8f8bea]
- Updated dependencies [304484b]
- llamaindex@0.4.13
## 0.0.16
### Patch Changes
- f326ab8: chore: bump version
- Updated dependencies [f326ab8]
- llamaindex@0.4.12
## 0.1.1
## 0.0.15
### Patch Changes
- 321c39d: fix: generate api as class
- Updated dependencies [8bf5b4a]
- llamaindex@0.4.11
## 0.0.14
### Patch Changes
- Updated dependencies [7dce3d2]
- llamaindex@0.4.10
## 0.0.13
### Patch Changes
- Updated dependencies [3a96a48]
- llamaindex@0.4.9
## 0.0.12
### Patch Changes
- Updated dependencies [83ebdfb]
- llamaindex@0.4.8
## 0.0.11
### Patch Changes
- Updated dependencies [41fe871]
- Updated dependencies [321c39d]
- Updated dependencies [f7f1af0]
- llamaindex@0.4.7
## 0.0.10
### Patch Changes
- Updated dependencies [1feb23b]
- Updated dependencies [08c55ec]
- llamaindex@0.4.6
## 0.0.9
### Patch Changes
- Updated dependencies [6c3e5d0]
- llamaindex@0.4.5
## 0.0.8
### Patch Changes
- Updated dependencies [42eb73a]
- llamaindex@0.4.4
## 0.0.7
### Patch Changes
- Updated dependencies [2ef62a9]
- llamaindex@0.4.3
## 0.0.6
### Patch Changes
- a87a4d1: feat: added tool support calling for Bedrock's Calude and general llm support for agents
- Updated dependencies [a87a4d1]
- Updated dependencies [0730140]
- llamaindex@0.4.2
## 0.0.5
### Patch Changes
- ed467a9: Add model ids for Anthropic Claude 3.5 Sonnet model on Anthropic and Bedrock
- Updated dependencies [3c47910]
- Updated dependencies [ed467a9]
- Updated dependencies [cba5406]
- llamaindex@0.4.1
## 0.0.4
### Patch Changes
- b1a4a74: docs: updated Bedrock Opus region and added a basic README
- Updated dependencies [436bc41]
- Updated dependencies [a44e54f]
- Updated dependencies [a51ed8d]
- Updated dependencies [d3b635b]
- llamaindex@0.4.0
## 0.0.3
### Patch Changes
- Updated dependencies [6bc5bdd]
- Updated dependencies [bf25ff6]
- Updated dependencies [e6d6576]
- llamaindex@0.3.17
## 0.0.2
### Patch Changes
- 8832669: Community bedrock support added
- Updated dependencies [11ae926]
- Updated dependencies [631f000]
- Updated dependencies [1378ec4]
- Updated dependencies [6b1ded4]
- Updated dependencies [4d4bd85]
- Updated dependencies [24a9d1e]
- Updated dependencies [45952de]
- Updated dependencies [54230f0]
- Updated dependencies [a29d835]
- Updated dependencies [73819bf]
- llamaindex@0.3.16
+17
View File
@@ -0,0 +1,17 @@
# @llamaindex/community
AWS package for LlamaIndexTS, deprecated, use [@llamaindex/aws](https://www.npmjs.com/package/@llamaindex/aws) instead.
## Current Features:
- Bedrock support for Amazon Nova models Pro, Lite and Micro
- Bedrock support for the Anthropic Claude Models [usage](https://ts.llamaindex.ai/docs/llamaindex/modules/llms/bedrock) including the latest Sonnet 3.5 v2 and Haiku 3.5
- Bedrock support for the Meta LLama 2, 3, 3.1 and 3.2 Models [usage](https://ts.llamaindex.ai/docs/llamaindex/modules/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
MIT
+53
View File
@@ -0,0 +1,53 @@
{
"name": "@llamaindex/community",
"description": "Community package for LlamaIndexTS",
"version": "0.0.101",
"type": "module",
"types": "dist/type/index.d.ts",
"main": "dist/cjs/index.js",
"exports": {
".": {
"import": {
"types": "./dist/type/index.d.ts",
"default": "./dist/index.js"
},
"require": {
"types": "./dist/type/index.d.ts",
"default": "./dist/index.cjs"
}
},
"./llm/bedrock": {
"import": {
"types": "./dist/type/llm/bedrock.d.ts",
"default": "./dist/llm/bedrock/index.js"
},
"require": {
"types": "./dist/type/llm/bedrock.d.ts",
"default": "./dist/llm/bedrock/index.cjs"
}
}
},
"files": [
"dist",
"CHANGELOG.md",
"!**/*.tsbuildinfo"
],
"repository": {
"type": "git",
"url": "git+https://github.com/run-llama/LlamaIndexTS.git",
"directory": "packages/community"
},
"scripts": {
"build": "bunchee",
"dev": "bunchee --watch"
},
"devDependencies": {
"@types/node": "^22.9.0"
},
"dependencies": {
"@aws-sdk/client-bedrock-agent-runtime": "^3.706.0",
"@aws-sdk/client-bedrock-runtime": "^3.706.0",
"@llamaindex/core": "workspace:*",
"@llamaindex/env": "workspace:*"
}
}
+8
View File
@@ -0,0 +1,8 @@
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";
@@ -0,0 +1,134 @@
import type {
ContentBlockDelta,
ConverseOutput,
ConverseRequest,
ConverseResponse,
ConverseStreamOutput,
InvokeModelCommandInput,
InvokeModelWithResponseStreamCommandInput,
ResponseStream,
} from "@aws-sdk/client-bedrock-runtime";
import type {
BaseTool,
ChatMessage,
LLMMetadata,
ToolCall,
ToolCallLLMMessageOptions,
} from "@llamaindex/core/llms";
import { toUtf8 } from "../utils";
import { Provider, type BedrockChatStreamResponse } from "../provider";
import {
mapBaseToolsToAmazonTools,
mapChatMessagesToAmazonMessages,
} from "./utils";
export class AmazonProvider extends Provider<ConverseStreamOutput> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
getResultFromResponse(response: Record<string, any>): ConverseResponse {
return JSON.parse(toUtf8(response.body));
}
getToolsFromResponse<ToolContent>(response: ConverseOutput): ToolContent[] {
return (
response.message?.content
?.filter((item) => item.toolUse)
.map(
(item) =>
({
id: item.toolUse!.toolUseId,
name: item.toolUse!.name,
input: item.toolUse!.input
? JSON.parse(item.toolUse!.input as string)
: "",
}) as ToolContent,
) ?? []
);
}
getTextFromResponse(response: ConverseResponse): string {
const result = this.getResultFromResponse(response);
const content = result.output?.message?.content ?? [];
return content.map((item) => item.text).join(" ");
}
getTextFromStreamResponse(response: ResponseStream): string {
const event: ConverseStreamOutput | undefined =
this.getStreamingEventResponse(response);
if (!event || !event.contentBlockDelta) return "";
const delta: ContentBlockDelta | undefined = event.contentBlockDelta.delta;
return delta?.text || "";
}
async *reduceStream(
stream: AsyncIterable<ResponseStream>,
): BedrockChatStreamResponse {
let toolId: string | undefined = undefined;
let toolName: string | undefined = undefined;
for await (const response of stream) {
const event = this.getStreamingEventResponse(response);
const delta = this.getTextFromStreamResponse(response);
let options: undefined | ToolCallLLMMessageOptions = undefined;
if (event?.contentBlockStart && event.contentBlockStart.start?.toolUse) {
toolId = event.contentBlockStart.start?.toolUse.toolUseId;
toolName = event.contentBlockStart.start?.toolUse.name;
continue;
}
if (
toolId &&
toolName &&
event?.contentBlockDelta?.delta?.toolUse?.input
) {
options = {
toolCall: [
{
id: toolId,
name: toolName,
input: JSON.parse(event?.contentBlockDelta?.delta?.toolUse.input),
} as ToolCall,
],
};
toolId = undefined;
toolName = undefined;
}
if (!delta && !options) continue;
yield {
delta: options ? "" : delta,
options,
raw: response,
};
}
}
getRequestBody<T extends ChatMessage>(
metadata: LLMMetadata,
messages: T[],
tools: BaseTool[] = [],
options: Omit<ConverseRequest, "modelId" | "messages" | "inferenceConfig">,
): InvokeModelCommandInput | InvokeModelWithResponseStreamCommandInput {
const request: Omit<ConverseRequest, "modelId"> = {
...options,
messages: mapChatMessagesToAmazonMessages(messages),
inferenceConfig: {
maxTokens: metadata.maxTokens,
temperature: metadata.temperature,
topP: metadata.topP,
},
};
if (tools.length) {
request.toolConfig = {
tools: mapBaseToolsToAmazonTools(tools),
};
}
return {
modelId: metadata.model,
contentType: "application/json",
accept: "application/json",
body: JSON.stringify(request),
};
}
}
@@ -0,0 +1,5 @@
import type { ConverseRequest, Message } from "@aws-sdk/client-bedrock-runtime";
export type AmazonMessages = ConverseRequest["messages"];
export type AmazonMessage = Message;
@@ -0,0 +1,141 @@
import type {
ImageBlock,
ImageFormat,
Message,
Tool,
} from "@aws-sdk/client-bedrock-runtime";
import type {
BaseTool,
ChatMessage,
MessageContentDetail,
ToolCallLLMMessageOptions,
} from "@llamaindex/core/llms";
import { extractDataUrlComponents } from "../utils";
import type { JSONObject } from "@llamaindex/core/global";
import { mapMessageContentToMessageContentDetails } from "../../utils";
import type { AmazonMessage, AmazonMessages } from "./types";
const ACCEPTED_IMAGE_MIME_TYPES = [
"image/jpeg",
"image/png",
"image/webp",
"image/gif",
] as const;
const ACCEPTED_IMAGE_MIME_TYPE_FORMAT_MAP: Record<
(typeof ACCEPTED_IMAGE_MIME_TYPES)[number],
ImageFormat
> = {
"image/jpeg": "jpeg",
"image/png": "png",
"image/webp": "webp",
"image/gif": "gif",
};
export const mapImageContent = (imageUrl: string): ImageBlock => {
if (!imageUrl.startsWith("data:"))
throw new Error(
"For Amazon please only use base64 data url, e.g.: data:image/jpeg;base64,SGVsbG8sIFdvcmxkIQ==",
);
const { mimeType, base64: data } = extractDataUrlComponents(imageUrl);
if (
!ACCEPTED_IMAGE_MIME_TYPES.includes(
mimeType as keyof typeof ACCEPTED_IMAGE_MIME_TYPE_FORMAT_MAP,
)
)
throw new Error(
`Amazon only accepts the following mimeTypes: ${ACCEPTED_IMAGE_MIME_TYPES.join("\n")}`,
);
return {
format:
ACCEPTED_IMAGE_MIME_TYPE_FORMAT_MAP[
mimeType as keyof typeof ACCEPTED_IMAGE_MIME_TYPE_FORMAT_MAP
],
// @ts-expect-error: there's a mistake in the "@aws-sdk/client-bedrock-runtime" compared to the actual api
source: { bytes: data },
};
};
export const mapMessageContentDetailToAmazonContent = <
T extends MessageContentDetail,
>(
detail: T,
): Message["content"] => {
let content: Message["content"] = [];
if (detail.type === "text") {
content = [{ text: detail.text }];
} else if (detail.type === "image_url") {
content = [{ image: mapImageContent(detail.image_url.url) }];
} else {
throw new Error("Unsupported content detail type");
}
return content;
};
export const mapChatMessagesToAmazonMessages = <
T extends ChatMessage<ToolCallLLMMessageOptions>,
>(
messages: T[],
): AmazonMessages => {
return messages.flatMap((msg: T): AmazonMessage[] => {
return mapMessageContentToMessageContentDetails(msg.content).map(
(detail: MessageContentDetail): AmazonMessage => {
if (msg.options && "toolCall" in msg.options) {
return {
role: "assistant",
content: msg.options.toolCall.map((call) => ({
toolUse: {
toolUseId: call.id,
name: call.name,
input: call.input as JSONObject,
},
})),
};
}
if (msg.options && "toolResult" in msg.options) {
return {
role: "user",
content: [
{
toolResult: {
toolUseId: msg.options.toolResult.id,
content: [
{
text: msg.options.toolResult.result,
},
],
},
},
],
};
}
return {
role: msg.role === "assistant" ? "assistant" : "user",
content: mapMessageContentDetailToAmazonContent(detail),
};
},
);
});
};
export const mapBaseToolsToAmazonTools = (tools?: BaseTool[]): Tool[] => {
if (!tools) return [];
return tools.map((tool: BaseTool) => {
const {
metadata: { parameters, ...options },
} = tool;
return {
toolSpec: {
...options,
inputSchema: {
json: parameters,
},
},
} as Tool;
});
};
@@ -0,0 +1,156 @@
import {
type InvokeModelCommandInput,
type InvokeModelWithResponseStreamCommandInput,
ResponseStream,
} from "@aws-sdk/client-bedrock-runtime";
import type {
BaseTool,
ChatMessage,
LLMMetadata,
PartialToolCall,
ToolCall,
ToolCallLLMMessageOptions,
} from "@llamaindex/core/llms";
import { type BedrockChatStreamResponse, Provider } from "../provider";
import { toUtf8 } from "../utils";
import type {
AnthropicAdditionalChatOptions,
AnthropicNoneStreamingResponse,
AnthropicStreamEvent,
AnthropicTextContent,
ToolBlock,
} from "./types";
import {
mapBaseToolsToAnthropicTools,
mapChatMessagesToAnthropicMessages,
} from "./utils";
export class AnthropicProvider extends Provider<AnthropicStreamEvent> {
getResultFromResponse(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
response: Record<string, any>,
): AnthropicNoneStreamingResponse {
return JSON.parse(toUtf8(response.body));
}
getToolsFromResponse<AnthropicToolContent>(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
response: Record<string, any>,
): AnthropicToolContent[] {
const result = this.getResultFromResponse(response);
return result.content
.filter((item) => item.type === "tool_use")
.map((item) => item as AnthropicToolContent);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
getTextFromResponse(response: Record<string, any>): string {
const result = this.getResultFromResponse(response);
return result.content
.filter((item) => item.type === "text")
.map((item) => (item as AnthropicTextContent).text)
.join(" ");
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
getTextFromStreamResponse(response: Record<string, any>): string {
const event = this.getStreamingEventResponse(response);
if (event?.type === "content_block_delta") {
if (event.delta.type === "text_delta") return event.delta.text;
if (event.delta.type === "input_json_delta")
return event.delta.partial_json;
}
return "";
}
async *reduceStream(
stream: AsyncIterable<ResponseStream>,
): BedrockChatStreamResponse {
let collecting = [];
let tool: ToolBlock | undefined = undefined;
// #TODO this should be broken down into a separate consumer
for await (const response of stream) {
const delta = this.getTextFromStreamResponse(response);
const event = this.getStreamingEventResponse(response);
if (
event?.type === "content_block_start" &&
event.content_block.type === "tool_use"
) {
tool = event.content_block;
continue;
}
if (
event?.type === "content_block_delta" &&
event.delta.type === "input_json_delta"
) {
collecting.push(event.delta.partial_json);
}
let options: undefined | ToolCallLLMMessageOptions = undefined;
if (tool && collecting.length) {
const input = collecting.filter((item) => item).join("");
// We have all we need to parse the tool_use json
if (event?.type === "content_block_stop") {
options = {
toolCall: [
{
id: tool.id,
name: tool.name,
input: JSON.parse(input),
} as ToolCall,
],
};
// reset the collection/tool
collecting = [];
tool = undefined;
} else {
options = {
toolCall: [
{
id: tool.id,
name: tool.name,
input,
} as PartialToolCall,
],
};
}
}
if (!delta && !options) continue;
yield {
delta: options ? "" : delta,
options,
raw: response,
};
}
}
getRequestBody<T extends ChatMessage<ToolCallLLMMessageOptions>>(
metadata: LLMMetadata,
messages: T[],
tools?: BaseTool[],
options?: AnthropicAdditionalChatOptions,
): InvokeModelCommandInput | InvokeModelWithResponseStreamCommandInput {
const extra: Record<string, unknown> = {};
if (options?.toolChoice) {
extra["tool_choice"] = options?.toolChoice;
}
const mapped = mapChatMessagesToAnthropicMessages(messages);
return {
modelId: metadata.model,
contentType: "application/json",
accept: "application/json",
body: JSON.stringify({
anthropic_version: "bedrock-2023-05-31",
messages: mapped,
tools: mapBaseToolsToAnthropicTools(tools),
max_tokens: metadata.maxTokens,
temperature: metadata.temperature,
top_p: metadata.topP,
...extra,
}),
};
}
}
@@ -0,0 +1,161 @@
import type { ToolMetadata } from "@llamaindex/core/llms";
import type { InvocationMetrics } from "../types";
export type ToolChoice =
| { type: "any" }
| { type: "auto" }
| { type: "tool"; name: string };
export interface ThinkingConfigDisabled {
type: "disabled";
}
export interface ThinkingConfigEnabled {
budget_tokens: number;
type: "enabled";
}
export type AnthropicAdditionalChatOptions = {
toolChoice: ToolChoice;
thinking?: ThinkingConfigDisabled | ThinkingConfigEnabled;
};
type Usage = {
input_tokens: number;
output_tokens: number;
};
type Message = {
id: string;
type: string;
role: string;
content: string[];
model: string;
stop_reason: string | null;
stop_sequence: string | null;
usage: Usage;
};
export type ToolBlock = {
id: string;
input: unknown;
name: string;
type: "tool_use";
};
export type TextBlock = {
type: "text";
text: string;
};
type ContentBlockStart = {
type: "content_block_start";
index: number;
content_block: ToolBlock | TextBlock;
};
type Delta =
| {
type: "text_delta";
text: string;
}
| {
type: "input_json_delta";
partial_json: string;
};
type ContentBlockDelta = {
type: "content_block_delta";
index: number;
delta: Delta;
};
type ContentBlockStop = {
type: "content_block_stop";
index: number;
};
type MessageDelta = {
type: "message_delta";
delta: {
stop_reason: string;
stop_sequence: string | null;
};
usage: Usage;
};
export type MessageStop = {
type: "message_stop";
"amazon-bedrock-invocationMetrics": InvocationMetrics;
};
export type AnthropicStreamEvent =
| { type: "message_start"; message: Message }
| ContentBlockStart
| ContentBlockDelta
| ContentBlockStop
| MessageDelta
| MessageStop;
export type AnthropicContent =
| AnthropicTextContent
| AnthropicImageContent
| AnthropicToolContent
| AnthropicToolResultContent;
export type AnthropicTextContent = {
type: "text";
text: string;
};
export type AnthropicToolContent = {
type: "tool_use";
id: string;
name: string;
input: Record<string, unknown>;
};
export type AnthropicToolResultContent = {
type: "tool_result";
tool_use_id: string;
content: string;
};
export type AnthropicMediaTypes =
| "image/jpeg"
| "image/png"
| "image/webp"
| "image/gif";
export type AnthropicImageSource = {
type: "base64";
media_type: AnthropicMediaTypes;
data: string; // base64 encoded image bytes
};
export type AnthropicImageContent = {
type: "image";
source: AnthropicImageSource;
};
export type AnthropicMessage = {
role: "user" | "assistant";
content: AnthropicContent[];
};
export type AnthropicNoneStreamingResponse = {
id: string;
type: "message";
role: "assistant";
content: AnthropicContent[];
model: string;
stop_reason: "end_turn" | "max_tokens" | "stop_sequence";
stop_sequence?: string;
usage: { input_tokens: number; output_tokens: number };
};
export type AnthropicTool = {
name: string;
description: string;
input_schema: ToolMetadata["parameters"];
};
@@ -0,0 +1,166 @@
import type { JSONObject } from "@llamaindex/core/global";
import type {
BaseTool,
ChatMessage,
MessageContent,
MessageContentDetail,
ToolCallLLMMessageOptions,
} from "@llamaindex/core/llms";
import { mapMessageContentToMessageContentDetails } from "../../utils";
import { extractDataUrlComponents } from "../utils";
import type {
AnthropicContent,
AnthropicImageContent,
AnthropicMediaTypes,
AnthropicMessage,
AnthropicTextContent,
AnthropicTool,
} from "./types.js";
const ACCEPTED_IMAGE_MIME_TYPES = [
"image/jpeg",
"image/png",
"image/webp",
"image/gif",
];
export const mergeNeighboringSameRoleMessages = (
messages: AnthropicMessage[],
): AnthropicMessage[] => {
return messages.reduce(
(result: AnthropicMessage[], current: AnthropicMessage, index: number) => {
if (index > 0 && messages[index - 1]!.role === current.role) {
result[result.length - 1]!.content = [
...result[result.length - 1]!.content,
...current.content,
];
} else {
result.push(current);
}
return result;
},
[],
);
};
export const mapMessageContentDetailToAnthropicContent = <
T extends MessageContentDetail,
>(
detail: T,
): AnthropicContent => {
let content: AnthropicContent;
if (detail.type === "text") {
content = mapTextContent(detail.text);
} else if (detail.type === "image_url") {
content = mapImageContent(detail.image_url.url);
} else {
throw new Error("Unsupported content detail type");
}
return content;
};
export const mapMessageContentToAnthropicContent = <T extends MessageContent>(
content: T,
): AnthropicContent[] => {
return mapMessageContentToMessageContentDetails(content).map(
mapMessageContentDetailToAnthropicContent,
);
};
export const mapBaseToolsToAnthropicTools = (
tools?: BaseTool[],
): AnthropicTool[] => {
if (!tools) return [];
return tools.map((tool: BaseTool) => {
const {
metadata: { parameters, ...options },
} = tool;
return {
...options,
input_schema: parameters,
};
});
};
export const mapChatMessagesToAnthropicMessages = <
T extends ChatMessage<ToolCallLLMMessageOptions>,
>(
messages: T[],
): AnthropicMessage[] => {
const mapped = messages
.flatMap((msg: T): AnthropicMessage[] => {
if (msg.options && "toolCall" in msg.options) {
return [
{
role: "assistant",
content: msg.options.toolCall.map((call) => ({
type: "tool_use",
id: call.id,
name: call.name,
input: call.input as JSONObject,
})),
},
];
}
if (msg.options && "toolResult" in msg.options) {
return [
{
role: "user",
content: [
{
type: "tool_result",
tool_use_id: msg.options.toolResult.id,
content: JSON.stringify(msg.options.toolResult.result),
},
],
},
];
}
return mapMessageContentToMessageContentDetails(msg.content).map(
(detail: MessageContentDetail): AnthropicMessage => {
const content = mapMessageContentDetailToAnthropicContent(detail);
return {
role: msg.role === "assistant" ? "assistant" : "user",
content: [content],
};
},
);
})
.filter((message: AnthropicMessage) => {
const content = message.content[0]!;
if (content.type === "text" && !content.text) return false;
if (content.type === "image" && !content.source.data) return false;
if (content.type === "image" && message.role === "assistant")
return false;
return true;
});
return mergeNeighboringSameRoleMessages(mapped);
};
export const mapTextContent = (text: string): AnthropicTextContent => {
return { type: "text", text };
};
export const mapImageContent = (imageUrl: string): AnthropicImageContent => {
if (!imageUrl.startsWith("data:"))
throw new Error(
"For Anthropic please only use base64 data url, e.g.: data:image/jpeg;base64,SGVsbG8sIFdvcmxkIQ==",
);
const { mimeType, base64: data } = extractDataUrlComponents(imageUrl);
if (!ACCEPTED_IMAGE_MIME_TYPES.includes(mimeType))
throw new Error(
`Anthropic only accepts the following mimeTypes: ${ACCEPTED_IMAGE_MIME_TYPES.join("\n")}`,
);
return {
type: "image",
source: {
type: "base64",
media_type: mimeType as AnthropicMediaTypes,
data,
},
};
};
+513
View File
@@ -0,0 +1,513 @@
import {
BedrockRuntimeClient,
type BedrockRuntimeClientConfig,
InvokeModelCommand,
InvokeModelWithResponseStreamCommand,
} from "@aws-sdk/client-bedrock-runtime";
import {
type ChatMessage,
type ChatResponse,
type CompletionResponse,
type LLMChatParamsNonStreaming,
type LLMChatParamsStreaming,
type LLMCompletionParamsNonStreaming,
type LLMCompletionParamsStreaming,
type LLMMetadata,
ToolCallLLM,
type ToolCallLLMMessageOptions,
} from "@llamaindex/core/llms";
import { streamConverter } from "@llamaindex/core/utils";
import {
type BedrockAdditionalChatOptions,
type BedrockChatStreamResponse,
Provider,
} from "./provider";
import { wrapLLMEvent } from "@llamaindex/core/decorator";
import { mapMessageContentToMessageContentDetails } from "../utils";
import { AmazonProvider } from "./amazon/provider";
import { AnthropicProvider } from "./anthropic/provider";
import { MetaProvider } from "./meta/provider";
// Other providers should go here
export const PROVIDERS: { [key: string]: Provider } = {
anthropic: new AnthropicProvider(),
meta: new MetaProvider(),
amazon: new AmazonProvider(),
};
export type BedrockChatParamsStreaming = LLMChatParamsStreaming<
BedrockAdditionalChatOptions,
ToolCallLLMMessageOptions
>;
export type BedrockChatParamsNonStreaming = LLMChatParamsNonStreaming<
BedrockAdditionalChatOptions,
ToolCallLLMMessageOptions
>;
export type BedrockChatNonStreamResponse =
ChatResponse<ToolCallLLMMessageOptions>;
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",
ANTHROPIC_CLAUDE_3_5_SONNET_V2: "anthropic.claude-3-5-sonnet-20241022-v2:0",
ANTHROPIC_CLAUDE_3_5_HAIKU: "anthropic.claude-3-5-haiku-20241022-v1:0",
ANTHROPIC_CLAUDE_3_7_SONNET: "anthropic.claude-3-7-sonnet-20250219-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",
META_LLAMA3_3_70B_INSTRUCT: "meta.llama3-3-70b-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",
AMAZON_NOVA_PREMIER_1: "amazon.nova-premier-v1:0",
AMAZON_NOVA_PRO_1: "amazon.nova-pro-v1:0",
AMAZON_NOVA_LITE_1: "amazon.nova-lite-v1:0",
AMAZON_NOVA_MICRO_1: "amazon.nova-micro-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_5_HAIKU: "us.anthropic.claude-3-5-haiku-20241022-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_ANTHROPIC_CLAUDE_3_5_SONNET_V2:
"us.anthropic.claude-3-5-sonnet-20241022-v2:0",
US_ANTHROPIC_CLAUDE_3_7_SONNET:
"us.anthropic.claude-3-7-sonnet-20250219-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",
US_META_LLAMA_3_3_70B_INSTRUCT: "us.meta.llama3-3-70b-instruct-v1:0",
US_AMAZON_NOVA_PREMIER_1: "us.amazon.nova-premier-v1:0",
US_AMAZON_NOVA_PRO_1: "us.amazon.nova-pro-v1:0",
US_AMAZON_NOVA_LITE_1: "us.amazon.nova-lite-v1:0",
US_AMAZON_NOVA_MICRO_1: "us.amazon.nova-micro-v1:0",
EU_ANTHROPIC_CLAUDE_3_HAIKU: "eu.anthropic.claude-3-haiku-20240307-v1:0",
EU_ANTHROPIC_CLAUDE_3_5_HAIKU: "eu.anthropic.claude-3-5-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_ANTHROPIC_CLAUDE_3_7_SONNET:
"eu.anthropic.claude-3-7-sonnet-20250219-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",
EU_AMAZON_NOVA_PREMIER_1: "eu.amazon.nova-premier-v1:0",
EU_AMAZON_NOVA_PRO_1: "eu.amazon.nova-pro-v1:0",
EU_AMAZON_NOVA_LITE_1: "eu.amazon.nova-lite-v1:0",
EU_AMAZON_NOVA_MICRO_1: "eu.amazon.nova-micro-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_ANTHROPIC_CLAUDE_3_7_SONNET]:
BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_7_SONNET,
[INFERENCE_BEDROCK_MODELS.US_ANTHROPIC_CLAUDE_3_5_SONNET_V2]:
BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_5_SONNET_V2,
[INFERENCE_BEDROCK_MODELS.US_ANTHROPIC_CLAUDE_3_5_HAIKU]:
BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_5_HAIKU,
[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.US_META_LLAMA_3_3_70B_INSTRUCT]:
BEDROCK_MODELS.META_LLAMA3_3_70B_INSTRUCT,
[INFERENCE_BEDROCK_MODELS.US_AMAZON_NOVA_PREMIER_1]:
BEDROCK_MODELS.AMAZON_NOVA_PREMIER_1,
[INFERENCE_BEDROCK_MODELS.US_AMAZON_NOVA_PRO_1]:
BEDROCK_MODELS.AMAZON_NOVA_PRO_1,
[INFERENCE_BEDROCK_MODELS.US_AMAZON_NOVA_LITE_1]:
BEDROCK_MODELS.AMAZON_NOVA_LITE_1,
[INFERENCE_BEDROCK_MODELS.US_AMAZON_NOVA_MICRO_1]:
BEDROCK_MODELS.AMAZON_NOVA_MICRO_1,
[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_ANTHROPIC_CLAUDE_3_7_SONNET]:
BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_7_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,
[INFERENCE_BEDROCK_MODELS.EU_AMAZON_NOVA_PREMIER_1]:
BEDROCK_MODELS.AMAZON_NOVA_PREMIER_1,
[INFERENCE_BEDROCK_MODELS.EU_AMAZON_NOVA_PRO_1]:
BEDROCK_MODELS.AMAZON_NOVA_PRO_1,
[INFERENCE_BEDROCK_MODELS.EU_AMAZON_NOVA_LITE_1]:
BEDROCK_MODELS.AMAZON_NOVA_LITE_1,
[INFERENCE_BEDROCK_MODELS.EU_AMAZON_NOVA_MICRO_1]:
BEDROCK_MODELS.AMAZON_NOVA_MICRO_1,
};
/*
* Values taken from https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters.html#model-parameters-claude
*/
const COMPLETION_MODELS = {
[BEDROCK_MODELS.AMAZON_TITAN_TG1_LARGE]: 8000,
[BEDROCK_MODELS.AMAZON_TITAN_TEXT_EXPRESS_V1]: 8000,
[BEDROCK_MODELS.AI21_J2_GRANDE_INSTRUCT]: 8000,
[BEDROCK_MODELS.AI21_J2_JUMBO_INSTRUCT]: 8000,
[BEDROCK_MODELS.AI21_J2_MID]: 8000,
[BEDROCK_MODELS.AI21_J2_MID_V1]: 8000,
[BEDROCK_MODELS.AI21_J2_ULTRA]: 8000,
[BEDROCK_MODELS.AI21_J2_ULTRA_V1]: 8000,
[BEDROCK_MODELS.COHERE_COMMAND_TEXT_V14]: 4096,
};
const CHAT_ONLY_MODELS = {
[BEDROCK_MODELS.ANTHROPIC_CLAUDE_INSTANT_1]: 100000,
[BEDROCK_MODELS.ANTHROPIC_CLAUDE_1]: 100000,
[BEDROCK_MODELS.ANTHROPIC_CLAUDE_2]: 100000,
[BEDROCK_MODELS.ANTHROPIC_CLAUDE_2_1]: 200000,
[BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_SONNET]: 200000,
[BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_HAIKU]: 200000,
[BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_OPUS]: 200000,
[BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_5_SONNET]: 200000,
[BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_5_SONNET_V2]: 200000,
[BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_5_HAIKU]: 200000,
[BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_7_SONNET]: 200000,
[BEDROCK_MODELS.META_LLAMA2_13B_CHAT]: 2048,
[BEDROCK_MODELS.META_LLAMA2_70B_CHAT]: 4096,
[BEDROCK_MODELS.META_LLAMA3_8B_INSTRUCT]: 8192,
[BEDROCK_MODELS.META_LLAMA3_70B_INSTRUCT]: 8192,
[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.META_LLAMA3_3_70B_INSTRUCT]: 128000,
[BEDROCK_MODELS.MISTRAL_7B_INSTRUCT]: 32000,
[BEDROCK_MODELS.MISTRAL_MIXTRAL_7B_INSTRUCT]: 32000,
[BEDROCK_MODELS.MISTRAL_MIXTRAL_LARGE_2402]: 32000,
[BEDROCK_MODELS.AMAZON_NOVA_PREMIER_1]: 300000,
[BEDROCK_MODELS.AMAZON_NOVA_PRO_1]: 300000,
[BEDROCK_MODELS.AMAZON_NOVA_LITE_1]: 300000,
[BEDROCK_MODELS.AMAZON_NOVA_MICRO_1]: 130000,
};
const BEDROCK_FOUNDATION_LLMS = { ...COMPLETION_MODELS, ...CHAT_ONLY_MODELS };
/*
* Only the following models support streaming as
* per result of Bedrock.Client.list_foundation_models
* https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/bedrock/client/list_foundation_models.html
*/
export const STREAMING_MODELS = new Set([
BEDROCK_MODELS.AMAZON_TITAN_TG1_LARGE,
BEDROCK_MODELS.AMAZON_TITAN_TEXT_EXPRESS_V1,
BEDROCK_MODELS.ANTHROPIC_CLAUDE_INSTANT_1,
BEDROCK_MODELS.ANTHROPIC_CLAUDE_1,
BEDROCK_MODELS.ANTHROPIC_CLAUDE_2,
BEDROCK_MODELS.ANTHROPIC_CLAUDE_2_1,
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.ANTHROPIC_CLAUDE_3_5_SONNET_V2,
BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_5_HAIKU,
BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_7_SONNET,
BEDROCK_MODELS.META_LLAMA2_13B_CHAT,
BEDROCK_MODELS.META_LLAMA2_70B_CHAT,
BEDROCK_MODELS.META_LLAMA3_8B_INSTRUCT,
BEDROCK_MODELS.META_LLAMA3_70B_INSTRUCT,
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.META_LLAMA3_3_70B_INSTRUCT,
BEDROCK_MODELS.MISTRAL_7B_INSTRUCT,
BEDROCK_MODELS.MISTRAL_MIXTRAL_7B_INSTRUCT,
BEDROCK_MODELS.MISTRAL_MIXTRAL_LARGE_2402,
BEDROCK_MODELS.AMAZON_NOVA_PREMIER_1,
BEDROCK_MODELS.AMAZON_NOVA_PRO_1,
BEDROCK_MODELS.AMAZON_NOVA_LITE_1,
BEDROCK_MODELS.AMAZON_NOVA_MICRO_1,
]);
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.ANTHROPIC_CLAUDE_3_5_SONNET_V2,
BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_5_HAIKU,
BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_7_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,
BEDROCK_MODELS.META_LLAMA3_3_70B_INSTRUCT,
BEDROCK_MODELS.AMAZON_NOVA_PREMIER_1,
BEDROCK_MODELS.AMAZON_NOVA_PRO_1,
BEDROCK_MODELS.AMAZON_NOVA_LITE_1,
BEDROCK_MODELS.AMAZON_NOVA_MICRO_1,
];
const getProvider = (model: string): Provider => {
const providerName = model.split(".")[0];
if (!providerName) {
throw new Error(`Model ${model} is not supported`);
}
if (!(providerName in PROVIDERS)) {
throw new Error(
`Provider ${providerName} for model ${model} is not supported`,
);
}
return PROVIDERS[providerName]!;
};
export type BedrockModelParams = {
model: BEDROCK_MODELS | INFERENCE_BEDROCK_MODELS;
temperature?: number;
topP?: number;
maxTokens?: number;
};
export const BEDROCK_MODEL_MAX_TOKENS: Partial<Record<BEDROCK_MODELS, number>> =
{
[BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_SONNET]: 4096,
[BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_HAIKU]: 4096,
[BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_OPUS]: 4096,
[BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_5_SONNET]: 4096,
[BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_5_SONNET_V2]: 8192,
[BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_5_HAIKU]: 8192,
[BEDROCK_MODELS.ANTHROPIC_CLAUDE_3_7_SONNET]: 8192,
[BEDROCK_MODELS.META_LLAMA2_13B_CHAT]: 2048,
[BEDROCK_MODELS.META_LLAMA2_70B_CHAT]: 2048,
[BEDROCK_MODELS.META_LLAMA3_8B_INSTRUCT]: 2048,
[BEDROCK_MODELS.META_LLAMA3_70B_INSTRUCT]: 2048,
[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,
[BEDROCK_MODELS.META_LLAMA3_3_70B_INSTRUCT]: 2048,
};
const DEFAULT_BEDROCK_PARAMS = {
temperature: 0.1,
topP: 1,
maxTokens: 1024, // required by anthropic
};
export type BedrockParams = BedrockRuntimeClientConfig & BedrockModelParams;
/**
* ToolCallLLM for Bedrock
*/
export class Bedrock extends ToolCallLLM<BedrockAdditionalChatOptions> {
private client: BedrockRuntimeClient;
protected actualModel: BEDROCK_MODELS | INFERENCE_BEDROCK_MODELS;
model: BEDROCK_MODELS;
temperature: number;
topP: number;
maxTokens?: number;
provider: Provider;
topK?: number;
// there should be no check for env variables. Bedrock can be authenticated in various ways
// AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and AWS_REGION are the env variables used directly by the sdk
constructor({
temperature,
topP,
maxTokens,
model,
...params
}: BedrockParams) {
super();
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;
this.topP = topP ?? DEFAULT_BEDROCK_PARAMS.topP;
this.client = new BedrockRuntimeClient(params);
}
get supportToolCall(): boolean {
return TOOL_CALL_MODELS.includes(this.model);
}
get metadata(): LLMMetadata {
// NOTE, Anthropic supports top_k but LLMMetadata does not
return {
model: this.model,
temperature: this.temperature,
topP: this.topP,
maxTokens: this.maxTokens,
contextWindow: BEDROCK_FOUNDATION_LLMS[this.model] ?? 128000,
tokenizer: undefined,
structuredOutput: false,
};
}
protected async nonStreamChat(
params: BedrockChatParamsNonStreaming,
): Promise<BedrockChatNonStreamResponse> {
if (!this.supportToolCall && params.tools?.length) {
console.warn(`The model "${this.model}" doesn't support ToolCall`);
}
const input = this.provider.getRequestBody(
this.metadata,
params.messages,
params.tools,
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) {
const tools = this.provider.getToolsFromResponse(response);
if (tools.length) {
options = { toolCall: tools };
}
}
return {
raw: response,
message: {
role: "assistant",
content: this.provider.getTextFromResponse(response),
options,
},
};
}
protected async *streamChat(
params: BedrockChatParamsStreaming,
): BedrockChatStreamResponse {
if (!STREAMING_MODELS.has(this.model))
throw new Error(`The model: ${this.model} does not support streaming`);
if (!this.supportToolCall && params.tools?.length) {
console.warn(`The model "${this.model}" doesn't support ToolCall`);
}
const input = this.provider.getRequestBody(
this.metadata,
params.messages,
params.tools,
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);
}
chat(params: BedrockChatParamsStreaming): Promise<BedrockChatStreamResponse>;
chat(
params: BedrockChatParamsNonStreaming,
): Promise<BedrockChatNonStreamResponse>;
@wrapLLMEvent
async chat(
params: BedrockChatParamsStreaming | BedrockChatParamsNonStreaming,
): Promise<BedrockChatStreamResponse | BedrockChatNonStreamResponse> {
if (params.stream) {
return this.streamChat(params);
}
return this.nonStreamChat(params);
}
complete(
params: LLMCompletionParamsStreaming,
): Promise<AsyncIterable<CompletionResponse>>;
complete(
params: LLMCompletionParamsNonStreaming,
): Promise<CompletionResponse>;
async complete(
params: LLMCompletionParamsStreaming | LLMCompletionParamsNonStreaming,
): Promise<CompletionResponse | AsyncIterable<CompletionResponse>> {
const message: ChatMessage = {
role: "user",
content: mapMessageContentToMessageContentDetails(params.prompt),
};
const input = this.provider.getRequestBody(this.metadata, [message]);
if (params.stream) {
const command = new InvokeModelWithResponseStreamCommand(input);
const response = await this.client.send(command);
if (response.body)
return streamConverter(response.body, (response) => {
return {
text: this.provider.getTextFromStreamResponse(response),
raw: response,
};
});
}
const command = new InvokeModelCommand(input);
const response = await this.client.send(command);
return {
text: this.provider.getTextFromResponse(response),
raw: response,
};
}
}
@@ -0,0 +1,3 @@
export const TOKENS = {
TOOL_CALL: "<|python_tag|>",
};
@@ -0,0 +1,153 @@
import type {
InvokeModelCommandInput,
InvokeModelWithResponseStreamCommandInput,
ResponseStream,
} from "@aws-sdk/client-bedrock-runtime";
import type {
BaseTool,
ChatMessage,
LLMMetadata,
ToolCall,
ToolCallLLMMessageOptions,
} from "@llamaindex/core/llms";
import { toUtf8 } from "../utils";
import type { MetaNoneStreamingResponse, MetaStreamEvent } from "./types";
import { randomUUID } from "@llamaindex/env";
import { Provider, type BedrockChatStreamResponse } from "../provider";
import { TOKENS } from "./constants";
import {
mapChatMessagesToMetaLlama2Messages,
mapChatMessagesToMetaLlama3Messages,
} from "./utils";
export class MetaProvider extends Provider<MetaStreamEvent> {
getResultFromResponse(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
response: Record<string, any>,
): MetaNoneStreamingResponse {
return JSON.parse(toUtf8(response.body));
}
getToolsFromResponse<ToolContent>(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
response: Record<string, any>,
): ToolContent[] {
const result = this.getResultFromResponse(response);
if (!result.generation.trim().startsWith(TOKENS.TOOL_CALL)) return [];
const tool = JSON.parse(
result.generation.trim().split(TOKENS.TOOL_CALL)[1]!,
);
return [
{
id: randomUUID(),
name: tool.name,
input: tool.parameters,
} as ToolContent,
];
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
getTextFromResponse(response: Record<string, any>): string {
const result = this.getResultFromResponse(response);
if (result.generation.trim().startsWith(TOKENS.TOOL_CALL)) return "";
return result.generation;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
getTextFromStreamResponse(response: Record<string, any>): string {
const event = this.getStreamingEventResponse(response);
if (event?.generation) {
return event.generation;
}
return "";
}
async *reduceStream(
stream: AsyncIterable<ResponseStream>,
): BedrockChatStreamResponse {
const collecting: string[] = [];
let toolId: string | undefined = undefined;
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
)
continue;
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: [
{
id: toolId,
name: tool.name,
input: tool.parameters,
} as ToolCall,
],
};
} else if (toolId && !event?.stop_reason) {
collecting.push(delta);
continue;
}
if (!delta && !options) continue;
yield {
delta: options ? "" : delta,
options,
raw: response,
};
}
}
getRequestBody<T extends ChatMessage>(
metadata: LLMMetadata,
messages: T[],
tools: BaseTool[] = [],
): InvokeModelCommandInput | InvokeModelWithResponseStreamCommandInput {
let prompt: string = "";
let images: string[] = [];
if (metadata.model.startsWith("meta.llama3")) {
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 {
throw new Error(`Meta model ${metadata.model} is not supported`);
}
return {
modelId: metadata.model,
contentType: "application/json",
accept: "application/json",
body: JSON.stringify({
prompt,
images: images.length ? images : undefined,
max_gen_len: metadata.maxTokens,
temperature: metadata.temperature,
top_p: metadata.topP,
}),
};
}
}
@@ -0,0 +1,21 @@
import type { InvocationMetrics } from "../types";
export type MetaTextContent = string;
export type MetaMessage = {
role: "user" | "assistant" | "system" | "ipython";
content: MetaTextContent;
};
type MetaResponse = {
generation: string;
prompt_token_count: number;
generation_token_count: number;
stop_reason: "stop" | "length";
};
export type MetaStreamEvent = MetaResponse & {
"amazon-bedrock-invocationMetrics": InvocationMetrics;
};
export type MetaNoneStreamingResponse = MetaResponse;
@@ -0,0 +1,273 @@
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 => {
return `Use the function '${tool.metadata.name}' to '${tool.metadata.description}'`;
};
const getToolCallParametersString = (tool: BaseTool): string => {
return JSON.stringify({
name: tool.metadata.name,
description: tool.metadata.description,
parameters: tool.metadata.parameters
? Object.entries(tool.metadata.parameters.properties).map(
([name, definition]) => ({ [name]: definition }),
)
: {},
});
};
// 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_3_1 = (tools?: BaseTool[]) => {
if (!tools?.length) return "";
const customToolParams = tools.map((tool) => {
return [
getToolCallInstructionString(tool),
getToolCallParametersString(tool),
].join("\n\n");
});
return `
Environment: node
# Tool Instructions
- Never use ipython, always use javascript in node
Cutting Knowledge Date: December 2023
Today Date: ${new Date().toLocaleString("en-US", { year: "numeric", month: "long" })}
You have access to the following functions:
${customToolParams}
Think very carefully before calling functions.
If a you choose to call a function ONLY reply in the following json format:
{
"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
- Put the entire function call reply on one line
- Always add your sources when using search results to answer the user query
`;
};
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"] => {
if (role === "assistant") return "assistant";
if (role === "user") return "user";
return "system";
};
export const mapChatMessagesToMetaMessages = <
T extends ChatMessage<ToolCallLLMMessageOptions>,
>(
messages: T[],
): MetaMessage[] => {
return messages.flatMap((msg) => {
if (msg.options && "toolCall" in msg.options) {
return msg.options.toolCall.map((call) => ({
role: "assistant",
content: JSON.stringify({
id: call.id,
name: call.name,
parameters: call.input,
}),
}));
}
if (msg.options && "toolResult" in msg.options) {
return {
role: "ipython",
content: JSON.stringify(msg.options.toolResult),
};
}
let content: string = "";
if (typeof msg.content === "string") {
content = msg.content;
} else if (msg.content.length) {
content = (msg.content[0] as MessageContentTextDetail).text;
}
return {
role: mapChatRoleToMetaRole(msg.role),
content,
};
});
};
/**
* Documentation at https://llama.meta.com/docs/model-cards-and-prompt-formats/meta-llama-3
*/
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[] = [];
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|>",
toolsPrompt,
"<|eot_id|>",
);
}
const mapped = mapChatMessagesToMetaMessages(messages).map((message) => {
return [
"<|start_header_id|>",
message.role,
"<|end_header_id|>",
message.content,
"<|eot_id|>",
].join("\n");
});
parts.push(
"<|begin_of_text|>",
...mapped,
"<|start_header_id|>assistant<|end_header_id|>",
);
const prompt = parts.join("\n");
return { prompt, images };
};
/**
* Documentation at https://llama.meta.com/docs/model-cards-and-prompt-formats/meta-llama-2
*/
export const mapChatMessagesToMetaLlama2Messages = <T extends ChatMessage>(
messages: T[],
): string => {
const mapped = mapChatMessagesToMetaMessages(messages);
let output = "<s>";
let insideInst = false;
let needsStartAgain = false;
for (const message of mapped) {
if (needsStartAgain) {
output += "<s>";
needsStartAgain = false;
}
const text = message.content;
if (message.role === "system") {
if (!insideInst) {
output += "[INST] ";
insideInst = true;
}
output += `<<SYS>>\n${text}\n<</SYS>>\n`;
} else if (message.role === "user") {
output += text;
if (insideInst) {
output += " [/INST]";
insideInst = false;
}
} else if (message.role === "assistant") {
if (insideInst) {
output += " [/INST]";
insideInst = false;
}
output += ` ${text} </s>\n`;
needsStartAgain = true;
}
}
return output;
};
@@ -0,0 +1,63 @@
import {
type InvokeModelCommandInput,
type InvokeModelWithResponseStreamCommandInput,
ResponseStream,
} from "@aws-sdk/client-bedrock-runtime";
import {
type BaseTool,
type ChatMessage,
type ChatResponseChunk,
type LLMMetadata,
type ToolCallLLMMessageOptions,
} from "@llamaindex/core/llms";
import { streamConverter } from "@llamaindex/core/utils";
import { toUtf8 } from "./utils";
export type BedrockAdditionalChatOptions = Record<string, unknown>;
export type BedrockChatStreamResponse = AsyncIterable<
ChatResponseChunk<ToolCallLLMMessageOptions>
>;
export abstract class Provider<ProviderStreamEvent extends object = object> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
abstract getTextFromResponse(response: Record<string, any>): string;
// Return tool calls from none streaming calls
abstract getToolsFromResponse<T extends object = object>(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
response: Record<string, any>,
): T[];
getStreamingEventResponse(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
response: Record<string, any>,
): ProviderStreamEvent | undefined {
return response.chunk?.bytes
? (JSON.parse(toUtf8(response.chunk?.bytes)) as ProviderStreamEvent)
: undefined;
}
async *reduceStream(
stream: AsyncIterable<ResponseStream>,
): BedrockChatStreamResponse {
yield* streamConverter(stream, (response) => {
return {
delta: this.getTextFromStreamResponse(response),
raw: response,
};
});
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
getTextFromStreamResponse(response: Record<string, any>): string {
return this.getTextFromResponse(response);
}
abstract getRequestBody<T extends ChatMessage>(
metadata: LLMMetadata,
messages: T[],
tools?: BaseTool[],
options?: BedrockAdditionalChatOptions,
): InvokeModelCommandInput | InvokeModelWithResponseStreamCommandInput;
}
@@ -0,0 +1,6 @@
export type InvocationMetrics = {
inputTokenCount: number;
outputTokenCount: number;
invocationLatency: number;
firstByteLatency: number;
};
@@ -0,0 +1,23 @@
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,
};
};
+10
View File
@@ -0,0 +1,10 @@
import type {
MessageContent,
MessageContentDetail,
} from "@llamaindex/core/llms";
export const mapMessageContentToMessageContentDetails = (
content: MessageContent,
): MessageContentDetail[] => {
return Array.isArray(content) ? content : [{ type: "text", text: content }];
};
@@ -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,
);
}
}
@@ -5,19 +5,15 @@
"outDir": "./dist/type",
"tsBuildInfoFile": "./dist/.tsbuildinfo",
"emitDeclarationOnly": true,
"moduleResolution": "Bundler",
"skipLibCheck": true,
"strict": true,
"types": []
"module": "ESNext",
"moduleResolution": "bundler",
"types": ["node"]
},
"include": ["./src"],
"exclude": ["node_modules"],
"references": [
{
"path": "../core/tsconfig.json"
},
{
"path": "../env/tsconfig.json"
"path": "../llamaindex/tsconfig.json"
}
]
}
+13
View File
@@ -1,5 +1,18 @@
# @llamaindex/core
## 0.6.19
### Patch Changes
- f9f1de9: Use logger interface instead of directly hardcoding console.log
## 0.6.18
### Patch Changes
- f29799e: Add toolcall callbacks to agent workflows
- 7224c06: Add logger and callbacks to llm.exec
## 0.6.17
### Patch Changes
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/core",
"type": "module",
"version": "0.6.17",
"version": "0.6.19",
"description": "LlamaIndex Core Module",
"exports": {
"./agent": {
+2
View File
@@ -15,6 +15,7 @@ import type {
} from "../llms";
import { baseToolWithCallSchema } from "../schema";
import {
assertIsJSONValue,
isAsyncIterable,
prettifyError,
stringifyJSONToMessageContent,
@@ -227,6 +228,7 @@ export async function callTool(
`Tool ${tool.metadata.name} (remote:${toolCall.name}) succeeded.`,
);
logger.log(`Output: ${JSON.stringify(output)}`);
assertIsJSONValue(output);
const toolOutput: ToolOutput = {
tool,
input,
+6 -1
View File
@@ -1,3 +1,4 @@
import { consoleLogger, emptyLogger, type Logger } from "@llamaindex/env";
import type { Tokenizers } from "@llamaindex/env/tokenizers";
import type { MessageContentDetail } from "../llms";
import { BaseNode, MetadataMode, TransformComponent } from "../schema";
@@ -18,6 +19,7 @@ export type EmbeddingInfo = {
export type BaseEmbeddingOptions = {
logProgress?: boolean;
progressCallback?: (current: number, total: number) => void;
logger?: Logger;
};
export abstract class BaseEmbedding extends TransformComponent<
@@ -133,6 +135,9 @@ export async function batchEmbeddings<T>(
const curBatch: T[] = [];
const logger =
options?.logger ?? (options?.logProgress ? consoleLogger : emptyLogger);
for (let i = 0; i < queue.length; i++) {
curBatch.push(queue[i]!);
if (i == queue.length - 1 || curBatch.length == chunkSize) {
@@ -143,7 +148,7 @@ export async function batchEmbeddings<T>(
options?.progressCallback?.(i + 1, queue.length);
}
if (options?.logProgress) {
console.log(`getting embedding progress: ${i + 1} / ${queue.length}`);
logger.log(`getting embedding progress: ${i + 1} / ${queue.length}`);
}
curBatch.length = 0;
+16 -9
View File
@@ -1,6 +1,7 @@
import { emptyLogger } from "@llamaindex/env";
import { extractText } from "../utils/llms";
import { streamConverter } from "../utils/stream";
import { callTool, getToolCallsFromResponse } from "./tool-call";
import { callToolToMessage, getToolCallsFromResponse } from "./tool-call";
import type {
ChatMessage,
ChatResponse,
@@ -99,16 +100,19 @@ export abstract class BaseLLM<
if (params.stream) {
return this.streamExec(params);
}
const logger = params.logger ?? emptyLogger;
const newMessages: ChatMessage<AdditionalMessageOptions>[] = [];
const response = await this.chat(params);
newMessages.push(response.message);
const toolCalls = getToolCallsFromResponse(response);
if (params.tools && toolCalls.length > 0) {
for (const toolCall of toolCalls) {
const toolResultMessage = await callTool<AdditionalMessageOptions>(
params.tools,
toolCall,
);
const toolResultMessage =
await callToolToMessage<AdditionalMessageOptions>(
params.tools,
toolCall,
logger,
);
if (toolResultMessage) {
newMessages.push(toolResultMessage);
}
@@ -126,6 +130,7 @@ export abstract class BaseLLM<
AdditionalMessageOptions
>,
): Promise<ExecStreamResponse<AdditionalMessageOptions>> {
const logger = params.logger ?? emptyLogger;
const responseStream = await this.chat(params);
const iterator = responseStream[Symbol.asyncIterator]();
const first = await iterator.next();
@@ -220,10 +225,12 @@ export abstract class BaseLLM<
} as AdditionalMessageOptions,
});
for (const toolCall of toolCalls) {
const toolResultMessage = await callTool<AdditionalMessageOptions>(
params.tools,
toolCall,
);
const toolResultMessage =
await callToolToMessage<AdditionalMessageOptions>(
params.tools,
toolCall,
logger,
);
if (toolResultMessage) {
messages.push(toolResultMessage);
}
+20 -17
View File
@@ -1,3 +1,5 @@
import { type Logger } from "@llamaindex/env";
import { callTool } from "../agent/utils.js";
import { stringifyJSONToMessageContent } from "../utils";
import type {
BaseTool,
@@ -35,27 +37,28 @@ export const getToolCallsFromResponse = (
return [];
};
export const callTool = async <
export const callToolToMessage = async <
AdditionalMessageOptions extends object = object,
>(
tools: BaseTool[],
toolCall: ToolCall,
logger: Logger,
): Promise<ChatMessage<AdditionalMessageOptions> | null> => {
const tool = tools?.find((t) => t.metadata.name === toolCall.name);
// TODO: consider using BaseToolWithCall instead of BaseTool to avoid checking for tool.call
if (tool && tool.call) {
const result = await tool.call(toolCall.input);
const toolResultMessage: ChatMessage<AdditionalMessageOptions> = {
role: "user",
content: stringifyJSONToMessageContent(result),
options: {
toolResult: {
id: toolCall.id,
result,
},
} as AdditionalMessageOptions,
};
return toolResultMessage;
}
return null;
const toolOutput = await callTool(tool, toolCall, logger);
const toolResultMessage: ChatMessage<AdditionalMessageOptions> = {
role: "user",
content: stringifyJSONToMessageContent(toolOutput.output),
options: {
toolResult: {
id: toolCall.id,
result: toolOutput.output,
isError: toolOutput.isError,
},
} as AdditionalMessageOptions,
};
return toolResultMessage;
};
+2
View File
@@ -1,3 +1,4 @@
import type { Logger } from "@llamaindex/env";
import type { Tokenizers } from "@llamaindex/env/tokenizers";
import type { JSONSchemaType } from "ajv";
import { z } from "zod";
@@ -139,6 +140,7 @@ export interface LLMChatParamsBase<
additionalChatOptions?: AdditionalChatOptions | undefined;
tools?: BaseTool[] | undefined;
responseFormat?: z.ZodType | object | undefined;
logger?: Logger | undefined;
}
export interface LLMChatParamsStreaming<
+13 -2
View File
@@ -1,3 +1,4 @@
import { consoleLogger, type Logger } from "@llamaindex/env";
import { Settings } from "../global";
import type { ChatMessage, LLM } from "../llms";
import { extractText } from "../utils";
@@ -38,6 +39,11 @@ export type MemoryOptions<TMessageOptions extends object = object> = {
* This default LLM can be overridden by the LLM passed in the `getLLM` method.
*/
llm?: LLM | undefined;
/**
* Logger for memory operations
*/
logger?: Logger;
};
export class Memory<
@@ -76,6 +82,10 @@ export class Memory<
* The default LLM to use for memory retrieval.
*/
private llm: LLM | undefined;
/**
* Logger for memory operations
*/
private logger: Logger;
constructor(
messages: MemoryMessage<TMessageOptions>[] = [],
@@ -87,6 +97,7 @@ export class Memory<
options.shortTermTokenLimitRatio ?? DEFAULT_SHORT_TERM_TOKEN_LIMIT_RATIO;
this.memoryBlocks = options.memoryBlocks ?? [];
this.memoryCursor = options.memoryCursor ?? 0;
this.logger = options.logger ?? consoleLogger;
this.initLLM(options.llm);
this.adapters = {
@@ -309,7 +320,7 @@ export class Memory<
addedTokenCount += messageTokenCount;
}
} catch (error) {
console.warn(
this.logger.warn(
`Failed to get content from memory block ${block.id}:`,
error,
);
@@ -371,7 +382,7 @@ export class Memory<
try {
await block.put(newMessages);
} catch (error) {
console.warn(
this.logger.warn(
`Failed to process messages into memory block ${block.id}:`,
error,
);
@@ -1,3 +1,4 @@
import { consoleLogger, type Logger } from "@llamaindex/env";
import type { Tokenizer } from "@llamaindex/env/tokenizers";
import { z } from "zod";
import { Settings } from "../global";
@@ -48,9 +49,11 @@ export class SentenceSplitter extends MetadataAwareTextSplitter {
#splitFns: Set<TextSplitterFn> = new Set();
#subSentenceSplitFns: Set<TextSplitterFn> = new Set();
#tokenizer: Tokenizer;
#logger: Logger;
constructor(
params?: z.input<typeof sentenceSplitterSchema> & SplitterParams,
params?: z.input<typeof sentenceSplitterSchema> &
SplitterParams & { logger?: Logger },
) {
super();
if (params) {
@@ -66,6 +69,7 @@ export class SentenceSplitter extends MetadataAwareTextSplitter {
this.extraAbbreviations,
);
this.#tokenizer = params?.tokenizer ?? Settings.tokenizer;
this.#logger = params?.logger ?? consoleLogger;
this.#splitFns.add(splitBySep(this.paragraphSeparator));
this.#splitFns.add(this.#chunkingTokenizerFn);
@@ -82,7 +86,7 @@ export class SentenceSplitter extends MetadataAwareTextSplitter {
`Metadata length (${metadataLength}) is longer than chunk size (${this.chunkSize}). Consider increasing the chunk size or decreasing the size of your metadata to avoid this.`,
);
} else if (effectiveChunkSize < 50) {
console.log(
this.#logger.log(
`Metadata length (${metadataLength}) is close to chunk size (${this.chunkSize}). Resulting chunks are less than 50 tokens. Consider increasing the chunk size or decreasing the size of your metadata to avoid this.`,
);
}
@@ -1,3 +1,4 @@
import { consoleLogger, type Logger } from "@llamaindex/env";
import type { Tokenizer } from "@llamaindex/env/tokenizers";
import { z } from "zod";
import { DEFAULT_CHUNK_OVERLAP, DEFAULT_CHUNK_SIZE, Settings } from "../global";
@@ -21,9 +22,11 @@ export class TokenTextSplitter extends MetadataAwareTextSplitter {
backupSeparators: string[] = ["\n"];
#tokenizer: Tokenizer;
#splitFns: Array<(text: string) => string[]> = [];
#logger: Logger;
constructor(
params?: SplitterParams & Partial<z.infer<typeof tokenTextSplitterSchema>>,
params?: SplitterParams &
Partial<z.infer<typeof tokenTextSplitterSchema>> & { logger?: Logger },
) {
super();
@@ -42,6 +45,7 @@ export class TokenTextSplitter extends MetadataAwareTextSplitter {
}
this.#tokenizer = params?.tokenizer ?? Settings.tokenizer;
this.#logger = params?.logger ?? consoleLogger;
const allSeparators = [this.separator, ...this.backupSeparators];
this.#splitFns = allSeparators.map((sep) => splitBySep(sep));
@@ -65,7 +69,7 @@ export class TokenTextSplitter extends MetadataAwareTextSplitter {
`Consider increasing the chunk size or decreasing the size of your metadata to avoid this.`,
);
} else if (effectiveChunkSize < 50) {
console.warn(
this.#logger.warn(
`Metadata length (${metadataLength}) is close to chunk size (${this.chunkSize}). ` +
`Resulting chunks are less than 50 tokens. Consider increasing the chunk size or decreasing the size of your metadata to avoid this.`,
);
@@ -148,7 +152,7 @@ export class TokenTextSplitter extends MetadataAwareTextSplitter {
const splitLength = this.tokenSize(split);
if (splitLength > chunkSize) {
console.warn(
this.#logger.warn(
`Got a split of size ${splitLength}, larger than chunk size ${chunkSize}.`,
);
}
@@ -1,3 +1,4 @@
import { consoleLogger, type Logger } from "@llamaindex/env";
import { DEFAULT_NAMESPACE } from "../../global";
import { BaseNode, ObjectType, type StoredValue } from "../../schema";
import type { BaseKVStore } from "../kv-store";
@@ -16,13 +17,19 @@ export class KVDocumentStore extends BaseDocumentStore {
private nodeCollection: string;
private refDocCollection: string;
private metadataCollection: string;
private logger: Logger;
constructor(kvstore: BaseKVStore, namespace: string = DEFAULT_NAMESPACE) {
constructor(
kvstore: BaseKVStore,
namespace: string = DEFAULT_NAMESPACE,
options?: { logger?: Logger },
) {
super();
this.kvstore = kvstore;
this.nodeCollection = `${namespace}/data`;
this.refDocCollection = `${namespace}/ref_doc_info`;
this.metadataCollection = `${namespace}/metadata`;
this.logger = options?.logger ?? consoleLogger;
}
async docs(): Promise<Record<string, BaseNode>> {
@@ -33,7 +40,7 @@ export class KVDocumentStore extends BaseDocumentStore {
if (isValidDocJson(value)) {
docs[key] = jsonToDoc(value, this.serializer);
} else {
console.warn(`Invalid JSON for docId ${key}`);
this.logger.warn(`Invalid JSON for docId ${key}`);
}
}
return docs;
+12 -5
View File
@@ -1,4 +1,4 @@
import { path } from "@llamaindex/env";
import { path, type Logger } from "@llamaindex/env";
import { IndexStruct, jsonToIndexStruct } from "../../data-structs";
import {
DEFAULT_INDEX_STORE_PERSIST_FILENAME,
@@ -8,8 +8,8 @@ import {
import {
BaseInMemoryKVStore,
BaseKVStore,
type DataType,
SimpleKVStore,
type DataType,
} from "../kv-store";
export const DEFAULT_PERSIST_PATH = path.join(
@@ -84,16 +84,23 @@ export class SimpleIndexStore extends KVIndexStore {
static async fromPersistDir(
persistDir: string = DEFAULT_PERSIST_DIR,
options?: { logger?: Logger },
): Promise<SimpleIndexStore> {
const persistPath = path.join(
persistDir,
DEFAULT_INDEX_STORE_PERSIST_FILENAME,
);
return this.fromPersistPath(persistPath);
return this.fromPersistPath(persistPath, options);
}
static async fromPersistPath(persistPath: string): Promise<SimpleIndexStore> {
const simpleKVStore = await SimpleKVStore.fromPersistPath(persistPath);
static async fromPersistPath(
persistPath: string,
options?: { logger?: Logger },
): Promise<SimpleIndexStore> {
const simpleKVStore = await SimpleKVStore.fromPersistPath(
persistPath,
options,
);
return new SimpleIndexStore(simpleKVStore);
}
+7 -3
View File
@@ -1,4 +1,4 @@
import { fs, path } from "@llamaindex/env";
import { consoleLogger, fs, path, type Logger } from "@llamaindex/env";
import { DEFAULT_COLLECTION } from "../../global";
import type { StoredValue } from "../../schema";
@@ -98,7 +98,11 @@ export class SimpleKVStore extends BaseKVStore {
await fs.writeFile(persistPath, JSON.stringify(this.data));
}
static async fromPersistPath(persistPath: string): Promise<SimpleKVStore> {
static async fromPersistPath(
persistPath: string,
options?: { logger?: Logger },
): Promise<SimpleKVStore> {
const logger = options?.logger ?? consoleLogger;
const dirPath = path.dirname(persistPath);
if (!(await exists(dirPath))) {
await fs.mkdir(dirPath, { recursive: true });
@@ -106,7 +110,7 @@ export class SimpleKVStore extends BaseKVStore {
let data: DataType = {};
if (!(await exists(persistPath))) {
console.info(`Starting new store from path: ${persistPath}`);
logger.log(`Starting new store from path: ${persistPath}`);
} else {
try {
const fileData = await fs.readFile(persistPath);
+5 -1
View File
@@ -1,3 +1,4 @@
import { consoleLogger, type Logger } from "@llamaindex/env";
import type { JSONSchemaType } from "ajv";
import { z } from "zod";
import { zodToJsonSchema } from "zod-to-json-schema";
@@ -14,11 +15,13 @@ export class FunctionTool<
#additionalArg: AdditionalToolArgument | undefined;
readonly #metadata: ToolMetadata<JSONSchemaType<T>>;
readonly #zodType: z.ZodType<T> | null = null;
readonly #logger: Logger;
constructor(
fn: (input: T, additionalArg?: AdditionalToolArgument) => R,
metadata: ToolMetadata<JSONSchemaType<T>>,
zodType?: z.ZodType<T>,
additionalArg?: AdditionalToolArgument,
logger?: Logger,
) {
this.#fn = fn;
this.#metadata = metadata;
@@ -26,6 +29,7 @@ export class FunctionTool<
this.#zodType = zodType;
}
this.#additionalArg = additionalArg;
this.#logger = logger ?? consoleLogger;
}
static from<T, AdditionalToolArgument extends object = object>(
@@ -140,7 +144,7 @@ export class FunctionTool<
if (result.success) {
params = result.data;
} else {
console.warn(result.error.errors);
this.#logger.warn(result.error.errors);
}
}
return this.#fn.call(null, params, this.#additionalArg);
+25
View File
@@ -1,5 +1,30 @@
# @llamaindex/experimental
## 0.0.202
### Patch Changes
- Updated dependencies [049471b]
- llamaindex@0.11.25
## 0.0.201
### Patch Changes
- llamaindex@0.11.24
## 0.0.200
### Patch Changes
- llamaindex@0.11.23
## 0.0.199
### Patch Changes
- llamaindex@0.11.22
## 0.0.198
### Patch Changes
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/experimental",
"description": "Experimental package for LlamaIndexTS",
"version": "0.0.198",
"version": "0.0.202",
"type": "module",
"types": "dist/type/index.d.ts",
"main": "dist/cjs/index.js",
+37
View File
@@ -1,5 +1,42 @@
# llamaindex
## 0.11.25
### Patch Changes
- 049471b: Moved LlamaCloudFileService, LlamaCloudIndex and LlamaCloudRetriever to llama-cloud-services
- Updated dependencies [049471b]
- @llamaindex/cloud@4.1.0
## 0.11.24
### Patch Changes
- Updated dependencies [c3bf3c7]
- Updated dependencies [f9f1de9]
- @llamaindex/cloud@4.0.28
- @llamaindex/core@0.6.19
- @llamaindex/node-parser@2.0.19
- @llamaindex/workflow@1.1.20
## 0.11.23
### Patch Changes
- Updated dependencies [f29799e]
- Updated dependencies [7224c06]
- @llamaindex/workflow@1.1.19
- @llamaindex/core@0.6.18
- @llamaindex/cloud@4.0.27
- @llamaindex/node-parser@2.0.18
## 0.11.22
### Patch Changes
- Updated dependencies [9ed3195]
- @llamaindex/workflow@1.1.18
## 0.11.21
### Patch Changes
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "llamaindex",
"version": "0.11.21",
"version": "0.11.25",
"license": "MIT",
"type": "module",
"keywords": [
@@ -20,13 +20,13 @@
"llamaindex"
],
"dependencies": {
"@llamaindex/cloud": "workspace:*",
"@llamaindex/core": "workspace:*",
"@llamaindex/env": "workspace:*",
"@llamaindex/node-parser": "workspace:*",
"@llamaindex/workflow": "workspace:*",
"@types/lodash": "^4.17.7",
"@types/node": "^24.0.13",
"llama-cloud-services": "^0.1.0",
"lodash": "^4.17.21",
"magic-bytes.js": "^1.10.0"
},
@@ -1,120 +0,0 @@
import {
addFilesToPipelineApiApiV1PipelinesPipelineIdFilesPut,
getPipelineFileStatusApiV1PipelinesPipelineIdFilesFileIdStatusGet,
listPipelineFilesApiV1PipelinesPipelineIdFilesGet,
listProjectsApiV1ProjectsGet,
readFileContentApiV1FilesIdContentGet,
searchPipelinesApiV1PipelinesGet,
uploadFileApiV1FilesPost,
} from "@llamaindex/cloud/api";
import { initService } from "./utils.js";
export class LLamaCloudFileService {
/**
* Get list of projects, each project contains a list of pipelines
*/
public static async getAllProjectsWithPipelines() {
initService();
try {
const { data: projects } = await listProjectsApiV1ProjectsGet({
throwOnError: true,
});
const { data: pipelines } = await searchPipelinesApiV1PipelinesGet({
throwOnError: true,
});
return projects.map((project) => ({
...project,
pipelines: pipelines.filter((p) => p.project_id === project.id),
}));
} catch (error) {
console.error("Error listing projects and pipelines:", error);
return [];
}
}
/**
* Upload a file to a pipeline in LlamaCloud
*/
public static async addFileToPipeline(
projectId: string,
pipelineId: string,
uploadFile: File | Blob,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
customMetadata: Record<string, any> = {},
) {
initService();
const { data: file } = await uploadFileApiV1FilesPost({
query: { project_id: projectId },
body: {
upload_file: uploadFile,
},
throwOnError: true,
});
const files = [
{
file_id: file.id,
custom_metadata: { file_id: file.id, ...customMetadata },
},
];
await addFilesToPipelineApiApiV1PipelinesPipelineIdFilesPut({
path: {
pipeline_id: pipelineId,
},
body: files,
});
// Wait 2s for the file to be processed
const maxAttempts = 20;
let attempt = 0;
while (attempt < maxAttempts) {
const { data: result } =
await getPipelineFileStatusApiV1PipelinesPipelineIdFilesFileIdStatusGet(
{
path: {
pipeline_id: pipelineId,
file_id: file.id,
},
throwOnError: true,
},
);
if (result.status === "ERROR") {
throw new Error(`File processing failed: ${JSON.stringify(result)}`);
}
if (result.status === "SUCCESS") {
// File is ingested - return the file id
return file.id;
}
attempt += 1;
await new Promise((resolve) => setTimeout(resolve, 100)); // Sleep for 100ms
}
throw new Error(
`File processing did not complete after ${maxAttempts} attempts. Check your LlamaCloud index at https://cloud.llamaindex.ai/project/${projectId}/deploy/${pipelineId} for more details.`,
);
}
/**
* Get download URL for a file in LlamaCloud
*/
public static async getFileUrl(pipelineId: string, filename: string) {
initService();
const { data: allPipelineFiles } =
await listPipelineFilesApiV1PipelinesPipelineIdFilesGet({
path: {
pipeline_id: pipelineId,
},
throwOnError: true,
});
const file = allPipelineFiles.find((file) => file.name === filename);
if (!file?.file_id) return null;
const { data: fileContent } = await readFileContentApiV1FilesIdContentGet({
path: {
id: file.file_id,
},
query: {
project_id: file.project_id,
},
throwOnError: true,
});
return fileContent.url;
}
}
@@ -1,414 +0,0 @@
import type { BaseNodePostprocessor } from "@llamaindex/core/postprocessor";
import type { BaseQueryEngine } from "@llamaindex/core/query-engine";
import type { BaseSynthesizer } from "@llamaindex/core/response-synthesizers";
import type { Document } from "@llamaindex/core/schema";
import { RetrieverQueryEngine } from "../engines/query/RetrieverQueryEngine.js";
import type { CloudRetrieveParams } from "./LlamaCloudRetriever.js";
import { LlamaCloudRetriever } from "./LlamaCloudRetriever.js";
import type { CloudConstructorParams } from "./type.js";
import {
getAppBaseUrl,
getPipelineId,
getProjectId,
initService,
} from "./utils.js";
import {
createBatchPipelineDocumentsApiV1PipelinesPipelineIdDocumentsPost,
deletePipelineDocumentApiV1PipelinesPipelineIdDocumentsDocumentIdDelete,
getPipelineDocumentStatusApiV1PipelinesPipelineIdDocumentsDocumentIdStatusGet,
getPipelineStatusApiV1PipelinesPipelineIdStatusGet,
type PipelineCreateReadable,
searchPipelinesApiV1PipelinesGet,
upsertBatchPipelineDocumentsApiV1PipelinesPipelineIdDocumentsPut,
upsertPipelineApiV1PipelinesPut,
} from "@llamaindex/cloud/api";
import type { BaseRetriever } from "@llamaindex/core/retriever";
import { getEnv } from "@llamaindex/env";
import type { QueryToolParams } from "../indices/BaseIndex.js";
import { Settings } from "../Settings.js";
import { QueryEngineTool } from "../tools/QueryEngineTool.js";
export class LlamaCloudIndex {
params: CloudConstructorParams;
constructor(params: CloudConstructorParams) {
this.params = params;
initService(this.params);
}
private async waitForPipelineIngestion(
verbose = Settings.debug,
raiseOnError = false,
): Promise<void> {
const pipelineId = await this.getPipelineId();
if (verbose) {
console.log("Waiting for pipeline ingestion: ");
}
while (true) {
const { data: pipelineStatus } =
await getPipelineStatusApiV1PipelinesPipelineIdStatusGet({
path: {
pipeline_id: pipelineId,
},
throwOnError: true,
});
if (pipelineStatus.status === "SUCCESS") {
if (verbose) {
console.log("Pipeline ingestion completed successfully");
}
break;
}
if (pipelineStatus.status === "ERROR") {
if (verbose) {
console.error("Pipeline ingestion failed");
}
if (raiseOnError) {
throw new Error("Pipeline ingestion failed");
}
}
if (verbose) {
process.stdout.write(".");
}
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
private async waitForDocumentIngestion(
docIds: string[],
verbose = Settings.debug,
raiseOnError = false,
): Promise<void> {
const pipelineId = await this.getPipelineId();
if (verbose) {
console.log("Loading data: ");
}
const pendingDocs = new Set(docIds);
while (pendingDocs.size) {
const docsToRemove = new Set<string>();
for (const doc of pendingDocs) {
const {
data: { status },
} =
await getPipelineDocumentStatusApiV1PipelinesPipelineIdDocumentsDocumentIdStatusGet(
{
path: { pipeline_id: pipelineId, document_id: doc },
throwOnError: true,
},
);
if (status === "NOT_STARTED" || status === "IN_PROGRESS") {
continue;
}
if (status === "ERROR") {
if (verbose) {
console.error(`Document ingestion failed for ${doc}`);
}
if (raiseOnError) {
throw new Error(`Document ingestion failed for ${doc}`);
}
}
docsToRemove.add(doc);
}
for (const doc of docsToRemove) {
pendingDocs.delete(doc);
}
if (pendingDocs.size) {
if (verbose) {
process.stdout.write(".");
}
await new Promise((resolve) => setTimeout(resolve, 500));
}
}
if (verbose) {
console.log("Done!");
}
await this.waitForPipelineIngestion(verbose, raiseOnError);
}
public async getPipelineId(
name?: string,
projectName?: string,
organizationId?: string,
): Promise<string> {
return await getPipelineId(
name ?? this.params.name,
projectName ?? this.params.projectName,
organizationId ?? this.params.organizationId,
);
}
public async getProjectId(
projectName?: string,
organizationId?: string,
): Promise<string> {
return await getProjectId(
projectName ?? this.params.projectName,
organizationId ?? this.params.organizationId,
);
}
/**
* Adds documents to the given index parameters. If the index does not exist, it will be created.
*
* @param params - An object containing the following properties:
* - documents: An array of Document objects to be added to the index.
* - verbose: Optional boolean to enable verbose logging.
* - Additional properties from CloudConstructorParams.
* @returns A Promise that resolves to a new LlamaCloudIndex instance.
*/
static async fromDocuments(
params: {
documents: Document[];
verbose?: boolean;
} & CloudConstructorParams,
config?: {
embedding: PipelineCreateReadable["embedding_config"];
transform: PipelineCreateReadable["transform_config"];
},
): Promise<LlamaCloudIndex> {
const index = new LlamaCloudIndex({ ...params });
await index.ensureIndex({ ...config, verbose: params.verbose ?? false });
await index.addDocuments(params.documents, params.verbose);
return index;
}
async addDocuments(documents: Document[], verbose?: boolean): Promise<void> {
const apiUrl = getAppBaseUrl();
const projectId = await this.getProjectId();
const pipelineId = await this.getPipelineId();
await upsertBatchPipelineDocumentsApiV1PipelinesPipelineIdDocumentsPut({
path: {
pipeline_id: pipelineId,
},
body: documents.map((doc) => ({
metadata: doc.metadata,
text: doc.text,
excluded_embed_metadata_keys: doc.excludedEmbedMetadataKeys,
excluded_llm_metadata_keys: doc.excludedEmbedMetadataKeys,
id: doc.id_,
})),
});
while (true) {
const { data: pipelineStatus } =
await getPipelineStatusApiV1PipelinesPipelineIdStatusGet({
path: { pipeline_id: pipelineId },
throwOnError: true,
});
if (pipelineStatus.status === "SUCCESS") {
console.info(
"Documents ingested successfully, pipeline is ready to use",
);
break;
}
if (pipelineStatus.status === "ERROR") {
console.error(
`Some documents failed to ingest, check your pipeline logs at ${apiUrl}/project/${projectId}/deploy/${pipelineId}`,
);
throw new Error("Some documents failed to ingest");
}
if (pipelineStatus.status === "PARTIAL_SUCCESS") {
console.info(
`Documents ingestion partially succeeded, to check a more complete status check your pipeline at ${apiUrl}/project/${projectId}/deploy/${pipelineId}`,
);
break;
}
if (verbose) {
process.stdout.write(".");
}
await new Promise((resolve) => setTimeout(resolve, 1000));
}
if (verbose) {
console.info(
`Ingestion completed, find your index at ${apiUrl}/project/${projectId}/deploy/${pipelineId}`,
);
}
}
asRetriever(params: CloudRetrieveParams = {}): BaseRetriever {
return new LlamaCloudRetriever({ ...this.params, ...params });
}
asQueryEngine(
params?: {
responseSynthesizer?: BaseSynthesizer;
preFilters?: unknown;
nodePostprocessors?: BaseNodePostprocessor[];
} & CloudRetrieveParams,
): BaseQueryEngine {
const retriever = new LlamaCloudRetriever({
...this.params,
...params,
});
return new RetrieverQueryEngine(
retriever,
params?.responseSynthesizer,
params?.nodePostprocessors,
);
}
asQueryTool(params: QueryToolParams): QueryEngineTool {
if (params.options) {
params.retriever = this.asRetriever(params.options);
}
return new QueryEngineTool({
queryEngine: this.asQueryEngine(params),
metadata: params?.metadata,
includeSourceNodes: params?.includeSourceNodes ?? false,
});
}
queryTool(params: QueryToolParams): QueryEngineTool {
return this.asQueryTool(params);
}
async insert(document: Document) {
const pipelineId = await this.getPipelineId();
await createBatchPipelineDocumentsApiV1PipelinesPipelineIdDocumentsPost({
path: {
pipeline_id: pipelineId,
},
body: [
{
metadata: document.metadata,
text: document.text,
excluded_embed_metadata_keys: document.excludedLlmMetadataKeys,
excluded_llm_metadata_keys: document.excludedEmbedMetadataKeys,
id: document.id_,
},
],
});
await this.waitForDocumentIngestion([document.id_]);
}
async delete(document: Document) {
const pipelineId = await this.getPipelineId();
await deletePipelineDocumentApiV1PipelinesPipelineIdDocumentsDocumentIdDelete(
{
path: {
pipeline_id: pipelineId,
document_id: document.id_,
},
},
);
await this.waitForPipelineIngestion();
}
async refreshDoc(document: Document) {
const pipelineId = await this.getPipelineId();
await upsertBatchPipelineDocumentsApiV1PipelinesPipelineIdDocumentsPut({
path: {
pipeline_id: pipelineId,
},
body: [
{
metadata: document.metadata,
text: document.text,
excluded_embed_metadata_keys: document.excludedLlmMetadataKeys,
excluded_llm_metadata_keys: document.excludedEmbedMetadataKeys,
id: document.id_,
},
],
});
await this.waitForDocumentIngestion([document.id_]);
}
public async ensureIndex(config?: {
embedding?: PipelineCreateReadable["embedding_config"];
transform?: PipelineCreateReadable["transform_config"];
verbose?: boolean;
}): Promise<void> {
const projectId = await this.getProjectId();
const { data: pipelines } = await searchPipelinesApiV1PipelinesGet({
query: {
project_id: projectId,
pipeline_name: this.params.name,
},
throwOnError: true,
});
if (pipelines.length === 0) {
// no pipeline found, create a new one
let embeddingConfig = config?.embedding;
if (!embeddingConfig) {
// no embedding config provided, use OpenAI as default
const openAIApiKey = getEnv("OPENAI_API_KEY");
const embeddingModel = getEnv("EMBEDDING_MODEL");
if (!openAIApiKey || !embeddingModel) {
throw new Error(
"No embedding configuration provided. Fallback to OpenAI embedding model. OPENAI_API_KEY and EMBEDDING_MODEL environment variables must be set.",
);
}
embeddingConfig = {
type: "OPENAI_EMBEDDING",
component: {
api_key: openAIApiKey,
model_name: embeddingModel,
},
};
}
let transformConfig = config?.transform;
if (!transformConfig) {
transformConfig = {
mode: "auto",
chunk_size: 1024,
chunk_overlap: 200,
};
}
const { data: pipeline } = await upsertPipelineApiV1PipelinesPut({
query: {
project_id: projectId,
},
body: {
name: this.params.name,
embedding_config: embeddingConfig,
transform_config: transformConfig,
},
throwOnError: true,
});
if (config?.verbose) {
console.log(
`Created pipeline ${pipeline.id} with name ${pipeline.name}`,
);
}
}
}
}
@@ -1,103 +0,0 @@
import {
type MetadataFilter,
type MetadataFilters,
type RetrievalParams,
runSearchApiV1PipelinesPipelineIdRetrievePost,
type TextNodeWithScore,
} from "@llamaindex/cloud/api";
import { DEFAULT_PROJECT_NAME } from "@llamaindex/core/global";
import type { QueryBundle } from "@llamaindex/core/query-engine";
import { BaseRetriever } from "@llamaindex/core/retriever";
import type { NodeWithScore } from "@llamaindex/core/schema";
import { jsonToNode, ObjectType } from "@llamaindex/core/schema";
import { extractText } from "@llamaindex/core/utils";
import type { ClientParams, CloudConstructorParams } from "./type.js";
import { getPipelineId, initService } from "./utils.js";
export type CloudRetrieveParams = Omit<
RetrievalParams,
"query" | "search_filters" | "dense_similarity_top_k"
> & { similarityTopK?: number; filters?: MetadataFilters };
export class LlamaCloudRetriever extends BaseRetriever {
clientParams: ClientParams;
retrieveParams: CloudRetrieveParams;
organizationId?: string;
projectName: string = DEFAULT_PROJECT_NAME;
pipelineName: string;
private resultNodesToNodeWithScore(
nodes: TextNodeWithScore[],
): NodeWithScore[] {
return nodes.map((node: TextNodeWithScore) => {
const textNode = jsonToNode(node.node, ObjectType.TEXT);
textNode.metadata = {
...textNode.metadata,
...node.node.extra_info, // append LlamaCloud extra_info to node metadata (file_name, pipeline_id, etc.)
};
return {
// Currently LlamaCloud only supports text nodes
node: textNode,
score: node.score ?? undefined,
};
});
}
// LlamaCloud expects null values for filters, but LlamaIndexTS uses undefined for empty values
// This function converts the undefined values to null
private convertFilter(filters?: MetadataFilters): MetadataFilters | null {
if (!filters) return null;
const processFilter = (
filter: MetadataFilter | MetadataFilters,
): MetadataFilter | MetadataFilters => {
if ("filters" in filter) {
// type MetadataFilters
return { ...filter, filters: filter.filters.map(processFilter) };
}
return { ...filter, value: filter.value ?? null };
};
return { ...filters, filters: filters.filters.map(processFilter) };
}
constructor(params: CloudConstructorParams & CloudRetrieveParams) {
super();
this.clientParams = { apiKey: params.apiKey, baseUrl: params.baseUrl };
initService(this.clientParams);
this.retrieveParams = params;
this.pipelineName = params.name;
if (params.projectName) {
this.projectName = params.projectName;
}
if (params.organizationId) {
this.organizationId = params.organizationId;
}
}
async _retrieve(query: QueryBundle): Promise<NodeWithScore[]> {
const pipelineId = await getPipelineId(
this.pipelineName,
this.projectName,
this.organizationId,
);
const filters = this.convertFilter(this.retrieveParams.filters);
const { data: results } =
await runSearchApiV1PipelinesPipelineIdRetrievePost({
throwOnError: true,
path: {
pipeline_id: pipelineId,
},
body: {
...this.retrieveParams,
query: extractText(query),
search_filters: filters,
dense_similarity_top_k: this.retrieveParams.similarityTopK!,
},
});
return this.resultNodesToNodeWithScore(results.retrieval_nodes);
}
}
+7 -7
View File
@@ -1,7 +1,7 @@
export { LLamaCloudFileService } from "./LLamaCloudFileService.js";
export { LlamaCloudIndex } from "./LlamaCloudIndex.js";
export {
LlamaCloudRetriever,
type CloudRetrieveParams,
} from "./LlamaCloudRetriever.js";
export type { CloudConstructorParams } from "./type.js";
console.warn(`
The classes LlamaCloudFileService, LlamaCloudIndex and LlamaCloudRetriever have been moved to the package llama-cloud-services.
* Please migrate your imports to llama-cloud-services, e.g. import { LlamaCloudIndex } from "llama-cloud-services";
* See the documentation: https://docs.cloud.llamaindex.ai
`);
export * from "llama-cloud-services";
-10
View File
@@ -1,10 +0,0 @@
export type ClientParams = {
apiKey?: string | undefined;
baseUrl?: string | undefined;
};
export type CloudConstructorParams = {
name: string;
projectName: string;
organizationId?: string | undefined;
} & ClientParams;
-97
View File
@@ -1,97 +0,0 @@
import {
client,
listProjectsApiV1ProjectsGet,
searchPipelinesApiV1PipelinesGet,
} from "@llamaindex/cloud/api";
import { DEFAULT_BASE_URL } from "@llamaindex/core/global";
import { getEnv } from "@llamaindex/env";
import type { ClientParams } from "./type.js";
function getBaseUrl(baseUrl?: string): string {
return baseUrl ?? getEnv("LLAMA_CLOUD_BASE_URL") ?? DEFAULT_BASE_URL;
}
export function getAppBaseUrl(): string {
return client.getConfig().baseUrl?.replace(/api\./, "") ?? "";
}
// fixme: refactor this to init at the top level or module level
let initOnce = false;
export function initService({ apiKey, baseUrl }: ClientParams = {}) {
if (initOnce) {
return;
}
initOnce = true;
client.setConfig({
baseUrl: getBaseUrl(baseUrl),
throwOnError: true,
});
const token = apiKey ?? getEnv("LLAMA_CLOUD_API_KEY");
client.interceptors.request.use((request) => {
request.headers.set("Authorization", `Bearer ${token}`);
return request;
});
client.interceptors.error.use((error) => {
throw new Error(
`LlamaCloud API request failed. Error details: ${JSON.stringify(error)}`,
);
});
if (!token) {
throw new Error(
"API Key is required for LlamaCloudIndex. Please pass the apiKey parameter",
);
}
}
export async function getProjectId(
projectName: string,
organizationId?: string,
): Promise<string> {
const { data: projects } = await listProjectsApiV1ProjectsGet({
query: {
project_name: projectName,
organization_id: organizationId ?? null,
},
throwOnError: true,
});
if (projects.length === 0) {
throw new Error(
`Unknown project name ${projectName}. Please confirm a managed project with this name exists.`,
);
} else if (projects.length > 1) {
throw new Error(
`Multiple projects found with name ${projectName}. Please specify organization_id.`,
);
}
const project = projects[0]!;
if (!project.id) {
throw new Error(`No project found with name ${projectName}`);
}
return project.id;
}
export async function getPipelineId(
name: string,
projectName: string,
organizationId?: string,
): Promise<string> {
const { data: pipelines } = await searchPipelinesApiV1PipelinesGet({
query: {
project_id: await getProjectId(projectName, organizationId),
pipeline_name: name,
},
throwOnError: true,
});
if (pipelines.length === 0 || !pipelines[0]!.id) {
throw new Error(
`No pipeline found with name ${name} in project ${projectName}`,
);
}
return pipelines[0]!.id;
}
@@ -8,7 +8,7 @@ import {
BaseInMemoryKVStore,
SimpleKVStore,
} from "@llamaindex/core/storage/kv-store";
import { path } from "@llamaindex/env";
import { path, type Logger } from "@llamaindex/env";
import _ from "lodash";
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -27,19 +27,28 @@ export class SimpleDocumentStore extends KVDocumentStore {
static async fromPersistDir(
persistDir: string = DEFAULT_PERSIST_DIR,
namespace?: string,
options?: { logger?: Logger },
): Promise<SimpleDocumentStore> {
const persistPath = path.join(
persistDir,
DEFAULT_DOC_STORE_PERSIST_FILENAME,
);
return await SimpleDocumentStore.fromPersistPath(persistPath, namespace);
return await SimpleDocumentStore.fromPersistPath(
persistPath,
namespace,
options,
);
}
static async fromPersistPath(
persistPath: string,
namespace?: string,
options?: { logger?: Logger },
): Promise<SimpleDocumentStore> {
const simpleKVStore = await SimpleKVStore.fromPersistPath(persistPath);
const simpleKVStore = await SimpleKVStore.fromPersistPath(
persistPath,
options,
);
return new SimpleDocumentStore(simpleKVStore, namespace);
}
@@ -18,7 +18,7 @@ import {
type VectorStoreQuery,
type VectorStoreQueryResult,
} from "@llamaindex/core/vector-store";
import { fs, path } from "@llamaindex/env";
import { consoleLogger, fs, path, type Logger } from "@llamaindex/env";
import { exists } from "../storage/FileSystem.js";
const LEARNER_MODES = new Set<VectorStoreQueryMode>([
@@ -139,9 +139,14 @@ export class SimpleVectorStore extends BaseVectorStore {
static async fromPersistDir(
persistDir: string = DEFAULT_PERSIST_DIR,
embedModel?: BaseEmbedding,
options?: { logger?: Logger },
): Promise<SimpleVectorStore> {
const persistPath = path.join(persistDir, "vector_store.json");
return await SimpleVectorStore.fromPersistPath(persistPath, embedModel);
return await SimpleVectorStore.fromPersistPath(
persistPath,
embedModel,
options,
);
}
client() {
@@ -273,7 +278,9 @@ export class SimpleVectorStore extends BaseVectorStore {
static async fromPersistPath(
persistPath: string,
embedModel?: BaseEmbedding,
options?: { logger?: Logger },
): Promise<SimpleVectorStore> {
const logger = options?.logger ?? consoleLogger;
const dirPath = path.dirname(persistPath);
if (!(await exists(dirPath))) {
await fs.mkdir(dirPath, { recursive: true });
@@ -281,7 +288,7 @@ export class SimpleVectorStore extends BaseVectorStore {
let dataDict: Record<string, unknown> = {};
if (!(await exists(persistPath))) {
console.info(`Starting new store from path: ${persistPath}`);
logger.log(`Starting new store from path: ${persistPath}`);
} else {
try {
const fileData = await fs.readFile(persistPath);
+12
View File
@@ -1,5 +1,17 @@
# @llamaindex/core-test
## 0.1.15
### Patch Changes
- @llamaindex/openai@0.4.14
## 0.1.14
### Patch Changes
- @llamaindex/openai@0.4.13
## 0.1.13
### Patch Changes
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/llamaindex-test",
"private": true,
"version": "0.1.13",
"version": "0.1.15",
"type": "module",
"scripts": {
"test": "vitest run"
@@ -47,22 +47,31 @@ describe("StorageContext", () => {
test("persists and loads", async () => {
const doc = new Document({ text: "test document" });
const consoleInfoSpy = vi
.spyOn(console, "info")
.mockImplementation(() => {});
// Create a Logger that spies on log (info) calls
const spyLogger = {
log: vi.fn(),
error: vi.fn(),
warn: vi.fn(),
};
// storage context from individual stores
const storageContext = await storageContextFromDefaults({
docStore: await SimpleDocumentStore.fromPersistDir(testDir),
vectorStore: await SimpleVectorStore.fromPersistDir(testDir),
indexStore: await SimpleIndexStore.fromPersistDir(testDir),
docStore: await SimpleDocumentStore.fromPersistDir(testDir, undefined, {
logger: spyLogger,
}),
vectorStore: await SimpleVectorStore.fromPersistDir(testDir, undefined, {
logger: spyLogger,
}),
indexStore: await SimpleIndexStore.fromPersistDir(testDir, {
logger: spyLogger,
}),
});
const index = await VectorStoreIndex.fromDocuments([doc], {
storageContext,
});
expect(consoleInfoSpy).toHaveBeenCalledTimes(3);
expect(consoleInfoSpy).toHaveBeenCalledWith(
expect(spyLogger.log).toHaveBeenCalledTimes(3);
expect(spyLogger.log).toHaveBeenCalledWith(
expect.stringContaining("Starting new store"),
);
expect(index).toBeDefined();
@@ -75,13 +84,19 @@ describe("StorageContext", () => {
// Check that the test data files exist
await expectTestDataFilesExist(testDir);
consoleInfoSpy.mockClear();
spyLogger.log.mockClear();
// Now, load it again. Since data was persisted, we should not see the error.
const newStorageContext = await storageContextFromDefaults({
docStore: await SimpleDocumentStore.fromPersistDir(testDir),
vectorStore: await SimpleVectorStore.fromPersistDir(testDir),
indexStore: await SimpleIndexStore.fromPersistDir(testDir),
docStore: await SimpleDocumentStore.fromPersistDir(testDir, undefined, {
logger: spyLogger,
}),
vectorStore: await SimpleVectorStore.fromPersistDir(testDir, undefined, {
logger: spyLogger,
}),
indexStore: await SimpleIndexStore.fromPersistDir(testDir, {
logger: spyLogger,
}),
});
const loadedIndex = await VectorStoreIndex.init({
@@ -94,9 +109,7 @@ describe("StorageContext", () => {
await expectTestDataFilesExist(testDir);
expect(consoleInfoSpy).not.toHaveBeenCalled();
consoleInfoSpy.mockRestore();
expect(spyLogger.log).not.toHaveBeenCalled();
});
test("throws error on corrupted data", async () => {
+15
View File
@@ -1,5 +1,20 @@
# @llamaindex/node-parser
## 2.0.19
### Patch Changes
- Updated dependencies [f9f1de9]
- @llamaindex/core@0.6.19
## 2.0.18
### Patch Changes
- Updated dependencies [f29799e]
- Updated dependencies [7224c06]
- @llamaindex/core@0.6.18
## 2.0.17
### Patch Changes
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/node-parser",
"version": "2.0.17",
"version": "2.0.19",
"description": "Node parser for LlamaIndex",
"type": "module",
"exports": {

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