Compare commits

..

10 Commits

Author SHA1 Message Date
Marcus Schiesser 9b8fd336ee Merge branch 'main' into feat/chat-ui-with-rsc-example 2024-11-15 11:51:47 +08:00
Marcus Schiesser 13731fa502 add docs 2024-11-15 11:49:35 +08:00
Thuc Pham 6f9aa60bec feat: mock readable stream 2024-11-15 10:44:24 +07:00
Thuc Pham c8e0d84ad8 fix: remove unneccesary change 2024-11-15 09:36:42 +07:00
Thuc Pham c8613301b0 specifiy use server and add doc step 2024-11-15 09:35:33 +07:00
Thuc Pham 3e6dfc9ab6 docs: add chat RSC demo to doc 2024-11-14 17:07:18 +07:00
Thuc Pham 6f72e899e3 add rsc example 2024-11-14 16:54:38 +07:00
Thuc Pham 082a03849a use 0.0.7 chat-ui 2024-11-14 16:50:12 +07:00
Thuc Pham 82308917cd refactor: move example to chat folder 2024-11-14 16:46:00 +07:00
Thuc Pham 77a7404a95 update to latest chat-ui 2024-11-14 16:45:38 +07:00
28 changed files with 13 additions and 451 deletions
-7
View File
@@ -1,12 +1,5 @@
# docs
## 0.0.119
### Patch Changes
- Updated dependencies [630b425]
- llamaindex@0.8.14
## 0.0.118
### Patch Changes
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "docs",
"version": "0.0.119",
"version": "0.0.118",
"private": true,
"scripts": {
"docusaurus": "docusaurus",
-7
View File
@@ -1,12 +1,5 @@
# @llamaindex/doc
## 0.0.17
### Patch Changes
- Updated dependencies [630b425]
- llamaindex@0.8.14
## 0.0.16
### Patch Changes
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/doc",
"version": "0.0.17",
"version": "0.0.16",
"private": true,
"scripts": {
"build": "pnpm run build:docs && next build",
@@ -1,12 +1,5 @@
# @llamaindex/cloudflare-worker-agent-test
## 0.0.110
### Patch Changes
- Updated dependencies [630b425]
- llamaindex@0.8.14
## 0.0.109
### Patch Changes
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/cloudflare-worker-agent-test",
"version": "0.0.110",
"version": "0.0.109",
"type": "module",
"private": true,
"scripts": {
-7
View File
@@ -1,12 +1,5 @@
# @llamaindex/next-agent-test
## 0.1.110
### Patch Changes
- Updated dependencies [630b425]
- llamaindex@0.8.14
## 0.1.109
### Patch Changes
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/next-agent-test",
"version": "0.1.110",
"version": "0.1.109",
"private": true,
"scripts": {
"dev": "next dev",
@@ -1,12 +1,5 @@
# test-edge-runtime
## 0.1.109
### Patch Changes
- Updated dependencies [630b425]
- llamaindex@0.8.14
## 0.1.108
### Patch Changes
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/nextjs-edge-runtime-test",
"version": "0.1.109",
"version": "0.1.108",
"private": true,
"scripts": {
"dev": "next dev",
@@ -1,12 +1,5 @@
# @llamaindex/next-node-runtime
## 0.0.91
### Patch Changes
- Updated dependencies [630b425]
- llamaindex@0.8.14
## 0.0.90
### Patch Changes
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/next-node-runtime-test",
"version": "0.0.91",
"version": "0.0.90",
"private": true,
"scripts": {
"dev": "next dev",
@@ -1,12 +1,5 @@
# @llamaindex/waku-query-engine-test
## 0.0.110
### Patch Changes
- Updated dependencies [630b425]
- llamaindex@0.8.14
## 0.0.109
### Patch Changes
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@llamaindex/waku-query-engine-test",
"version": "0.0.110",
"version": "0.0.109",
"type": "module",
"private": true,
"scripts": {
-7
View File
@@ -1,12 +1,5 @@
# @llamaindex/autotool
## 5.0.14
### Patch Changes
- Updated dependencies [630b425]
- llamaindex@0.8.14
## 5.0.13
### Patch Changes
@@ -1,13 +1,5 @@
# @llamaindex/autotool-01-node-example
## 0.0.57
### Patch Changes
- Updated dependencies [630b425]
- llamaindex@0.8.14
- @llamaindex/autotool@5.0.14
## 0.0.56
### Patch Changes
@@ -13,5 +13,5 @@
"scripts": {
"start": "node --import tsx --import @llamaindex/autotool/node ./src/index.ts"
},
"version": "0.0.57"
"version": "0.0.56"
}
@@ -1,13 +1,5 @@
# @llamaindex/autotool-02-next-example
## 0.1.101
### Patch Changes
- Updated dependencies [630b425]
- llamaindex@0.8.14
- @llamaindex/autotool@5.0.14
## 0.1.100
### Patch Changes
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/autotool-02-next-example",
"private": true,
"version": "0.1.101",
"version": "0.1.100",
"scripts": {
"dev": "next dev",
"build": "next build",
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/autotool",
"type": "module",
"version": "5.0.14",
"version": "5.0.13",
"description": "auto transpile your JS function to LLM Agent compatible",
"files": [
"dist",
-7
View File
@@ -1,12 +1,5 @@
# @llamaindex/experimental
## 0.0.126
### Patch Changes
- Updated dependencies [630b425]
- llamaindex@0.8.14
## 0.0.125
### Patch Changes
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/experimental",
"description": "Experimental package for LlamaIndexTS",
"version": "0.0.126",
"version": "0.0.125",
"type": "module",
"types": "dist/type/index.d.ts",
"main": "dist/cjs/index.js",
-6
View File
@@ -1,11 +1,5 @@
# llamaindex
## 0.8.14
### Patch Changes
- 630b425: feat: add Azure CosmosDB NoSql Chat store
## 0.8.13
### Patch Changes
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "llamaindex",
"version": "0.8.14",
"version": "0.8.13",
"license": "MIT",
"type": "module",
"keywords": [
@@ -1,345 +0,0 @@
import { CosmosClient, type Container, type Database } from "@azure/cosmos";
import { DefaultAzureCredential, type TokenCredential } from "@azure/identity";
import type {
ChatMessage,
MessageContent,
MessageType,
} from "@llamaindex/core/llms";
import { BaseChatStore } from "@llamaindex/core/storage/chat-store";
import { getEnv } from "@llamaindex/env";
const USER_AGENT_SUFFIX = "llamaindex-cdbnosql-chatstore-javascript";
const DEFAULT_CHAT_DATABASE = "ChatMessagesDB";
const DEFAULT_CHAT_CONTAINER = "ChatMessagesContainer";
const DEFAULT_OFFER_THROUGHPUT = 400;
function parseConnectionString(connectionString: string): {
endpoint: string;
key: string;
} {
const parts = connectionString.split(";");
let endpoint = "";
let accountKey = "";
parts.forEach((part) => {
const [key, value] = part.split("=");
if (key && key.trim() === "AccountEndpoint") {
endpoint = value?.trim() ?? "";
} else if ((key ?? "").trim() === "AccountKey") {
accountKey = value?.trim() ?? "";
}
});
if (!endpoint || !accountKey) {
throw new Error(
"Invalid connection string: missing AccountEndpoint or AccountKey.",
);
}
return { endpoint, key: accountKey };
}
export interface AzureCosmosChatDatabaseProperties {
throughput?: number;
}
export interface AzureCosmosChatContainerProperties {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
}
export interface AzureCosmosNoSqlChatStoreConfig {
cosmosClient?: CosmosClient;
dbName?: string;
containerName?: string;
userId?: string;
sessionId?: string;
cosmosContainerProperties?: AzureCosmosChatContainerProperties;
cosmosDatabaseProperties?: AzureCosmosChatDatabaseProperties;
ttlInSeconds?: number;
}
export class AzureCosmosNoSqlChatStore<
AdditionalMessageOptions extends object = object,
> extends BaseChatStore<AdditionalMessageOptions> {
private userId: string;
private ttl: number;
private cosmosClient: CosmosClient;
private database!: Database;
private container!: Container;
private initPromise?: Promise<void>;
private dbName: string;
private containerName: string;
private cosmosContainerProperties: AzureCosmosChatContainerProperties;
private cosmosDatabaseProperties: AzureCosmosChatDatabaseProperties;
private initialize: () => Promise<void>;
constructor({
cosmosClient,
dbName = DEFAULT_CHAT_DATABASE,
containerName = DEFAULT_CHAT_CONTAINER,
cosmosContainerProperties = { partitionKey: "/userId" },
cosmosDatabaseProperties = {},
ttlInSeconds = -1,
}: AzureCosmosNoSqlChatStoreConfig) {
super();
if (!cosmosClient) {
throw new Error(
"CosmosClient is required for AzureCosmosDBNoSQLChatStore initialization",
);
}
this.ttl = ttlInSeconds;
this.userId = cosmosContainerProperties.userId || "anonymous";
this.cosmosClient = cosmosClient;
this.dbName = dbName;
this.containerName = containerName;
this.cosmosContainerProperties = cosmosContainerProperties;
this.cosmosDatabaseProperties = cosmosDatabaseProperties;
this.initialize = () => {
if (this.initPromise === undefined) {
this.initPromise = this.init().catch((error) => {
console.error(
"Error during AzureCosmosDBNoSQLChatStore initialization",
error,
);
});
}
return this.initPromise;
};
}
client(): CosmosClient {
return this.cosmosClient;
}
// Asynchronous initialization method to create database and container
private async init(): Promise<void> {
// Set default throughput if not provided
const throughput =
this.cosmosDatabaseProperties?.throughput || DEFAULT_OFFER_THROUGHPUT;
// Create the database if it doesn't exist
const { database } = await this.cosmosClient.databases.createIfNotExists({
id: this.dbName,
throughput,
});
this.database = database;
// Create the container if it doesn't exist
const { container } = await this.database.containers.createIfNotExists({
id: this.containerName,
throughput: this.cosmosContainerProperties?.throughput,
partitionKey: "/userId",
indexingPolicy: this.cosmosContainerProperties?.indexingPolicy,
defaultTtl: this.ttl,
uniqueKeyPolicy: this.cosmosContainerProperties?.uniqueKeyPolicy,
conflictResolutionPolicy:
this.cosmosContainerProperties?.conflictResolutionPolicy,
computedProperties: this.cosmosContainerProperties?.computedProperties,
});
this.container = container;
}
/**
* Static method for creating an instance using a connection string.
* If no connection string is provided, it will attempt to use the env variable `AZURE_COSMOSDB_NOSQL_CONNECTION_STRING` as connection string.
* @returns Instance of AzureCosmosNoSqlKVStore
*/
static fromConnectionString(
config: {
connectionString?: string;
} & AzureCosmosNoSqlChatStoreConfig = {},
): AzureCosmosNoSqlChatStore {
const cosmosConnectionString =
config.connectionString ||
(getEnv("AZURE_COSMOSDB_NOSQL_CONNECTION_STRING") as string);
if (!cosmosConnectionString) {
throw new Error("Azure CosmosDB connection string must be provided");
}
const { endpoint, key } = parseConnectionString(cosmosConnectionString);
const cosmosClient = new CosmosClient({
endpoint,
key,
userAgentSuffix: USER_AGENT_SUFFIX,
});
return new AzureCosmosNoSqlChatStore({
...config,
cosmosClient,
});
}
/**
* Static method for creating an instance using a account endpoint and key.
* If no endpoint and key is provided, it will attempt to use the env variable `AZURE_COSMOSDB_NOSQL_ACCOUNT_ENDPOINT` as enpoint and `AZURE_COSMOSDB_NOSQL_ACCOUNT_KEY` as key.
* @returns Instance of AzureCosmosNoSqlKVStore
*/
static fromAccountAndKey(
config: {
endpoint?: string;
key?: string;
} & AzureCosmosNoSqlChatStoreConfig = {},
): AzureCosmosNoSqlChatStore {
const cosmosEndpoint =
config.endpoint ||
(getEnv("AZURE_COSMOSDB_NOSQL_ACCOUNT_ENDPOINT") as string);
const cosmosKey =
config.key || (getEnv("AZURE_COSMOSDB_NOSQL_ACCOUNT_KEY") as string);
if (!cosmosEndpoint || !cosmosKey) {
throw new Error(
"Azure CosmosDB account endpoint and key must be provided",
);
}
const cosmosClient = new CosmosClient({
endpoint: cosmosEndpoint,
key: cosmosKey,
userAgentSuffix: USER_AGENT_SUFFIX,
});
return new AzureCosmosNoSqlChatStore({
...config,
cosmosClient,
});
}
/**
* Static method for creating an instance using AAD token.
* If no endpoint and credentials are provided, it will attempt to use the env variable `AZURE_COSMOSDB_NOSQL_ACCOUNT_ENDPOINT` as endpoint and use DefaultAzureCredential() as credentials.
* @returns Instance of AzureCosmosNoSqlKVStore
*/
static fromAadToken(
config: {
endpoint?: string;
credential?: TokenCredential;
} & AzureCosmosNoSqlChatStoreConfig = {},
): AzureCosmosNoSqlChatStore {
const cosmosEndpoint =
config.endpoint ||
(getEnv("AZURE_COSMOSDB_NOSQL_CONNECTION_STRING") as string);
if (!cosmosEndpoint) {
throw new Error("Azure CosmosDB account endpoint must be provided");
}
const credentials = config.credential ?? new DefaultAzureCredential();
const cosmosClient = new CosmosClient({
endpoint: cosmosEndpoint,
aadCredentials: credentials,
userAgentSuffix: USER_AGENT_SUFFIX,
});
return new AzureCosmosNoSqlChatStore({
...config,
cosmosClient,
});
}
private convertToChatMessage(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
message: any,
): ChatMessage<AdditionalMessageOptions> {
return {
content: message.content as MessageContent,
role: message.role as MessageType,
options: message.options as AdditionalMessageOptions,
} as ChatMessage<AdditionalMessageOptions>;
}
private convertToCosmosMessage(
message: ChatMessage<AdditionalMessageOptions>,
): // eslint-disable-next-line @typescript-eslint/no-explicit-any
any {
return {
content: message.content,
role: message.role,
options: message.options,
};
}
/**
* Set messages for a given key.
*/
async setMessages(
key: string,
messages: ChatMessage<AdditionalMessageOptions>[],
): Promise<void> {
await this.initialize();
const inputMessages = messages.map(this.convertToCosmosMessage);
await this.container.items.upsert({
id: key,
messages: inputMessages,
userId: this.userId,
});
}
/**
* Get messages for a given key.
*/
async getMessages(
key: string,
): Promise<ChatMessage<AdditionalMessageOptions>[]> {
await this.initialize();
const res = await this.container.item(key, this.userId).read();
const messageHistory = res?.resource?.messages ?? [];
const result = messageHistory.map(this.convertToChatMessage);
return result;
}
/**
* Add a message for a given key.
*/
async addMessage(
key: string,
message: ChatMessage<AdditionalMessageOptions>,
idx?: number,
): Promise<void> {
await this.initialize();
const res = await this.container.item(key, this.userId).read();
const messageHistory = res?.resource?.messages ?? [];
if (idx === undefined) {
messageHistory.push(this.convertToCosmosMessage(message));
} else {
messageHistory.splice(idx, 0, this.convertToCosmosMessage(message));
}
await this.setMessages(key, messageHistory);
}
/**
* Deletes all messages for a given key.
*/
async deleteMessages(key: string): Promise<void> {
await this.initialize();
try {
await this.container.item(key, this.userId).delete();
// eslint-disable-next-line no-empty
} catch (e) {}
}
/**
* Deletes one message at idx index for a given key.
*/
async deleteMessage(key: string, idx: number): Promise<void> {
await this.initialize();
const res = await this.container.item(key, this.userId).read();
const messageHistory = res?.resource?.messages ?? [];
if (idx >= 0 && idx < messageHistory.length) {
messageHistory.splice(idx, 1);
await this.setMessages(key, messageHistory);
}
}
/**
* Get all keys.
*/
async getKeys(): Promise<IterableIterator<string>> {
await this.initialize();
const result = await this.container.items
.query("Select c.id from c")
.fetchAll();
const keys = result.resources.map((res: { id: string }) => res.id);
function* keyGenerator(): IterableIterator<string> {
for (const key of keys) {
yield key;
}
}
return keyGenerator();
}
}
-1
View File
@@ -2,7 +2,6 @@ export * from "@llamaindex/core/storage/chat-store";
export * from "@llamaindex/core/storage/doc-store";
export * from "@llamaindex/core/storage/index-store";
export * from "@llamaindex/core/storage/kv-store";
export * from "./chatStore/AzureCosmosNoSqlChatStore.js";
export * from "./docStore/AzureCosmosNoSqlDocumentStore.js";
export { PostgresDocumentStore } from "./docStore/PostgresDocumentStore.js";
export { SimpleDocumentStore } from "./docStore/SimpleDocumentStore.js";
-7
View File
@@ -1,12 +1,5 @@
# @llamaindex/unit-test
## 0.0.24
### Patch Changes
- Updated dependencies [630b425]
- llamaindex@0.8.14
## 0.0.23
### Patch Changes
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "@llamaindex/unit-test",
"private": true,
"version": "0.0.24",
"version": "0.0.23",
"type": "module",
"scripts": {
"test": "vitest run"