Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add caching support for Redis #1295

Merged
merged 6 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Cache Configs
CACHE_STORE=database # Defaults to database. Other available cache store: redis and filesystem
REDIS_URL= # Redis URL - could be a local redis instance or cloud hosted redis. Also support rediss:// urls

# Discord Configuration
DISCORD_APPLICATION_ID=
DISCORD_API_TOKEN= # Bot token
Expand Down
119 changes: 60 additions & 59 deletions agent/package.json
Original file line number Diff line number Diff line change
@@ -1,61 +1,62 @@
{
"name": "@ai16z/agent",
"version": "0.1.6-alpha.4",
"main": "src/index.ts",
"type": "module",
"scripts": {
"start": "node --loader ts-node/esm src/index.ts",
"dev": "node --loader ts-node/esm src/index.ts",
"check-types": "tsc --noEmit"
},
"nodemonConfig": {
"watch": [
"src",
"../core/dist"
],
"ext": "ts,json",
"exec": "node --enable-source-maps --loader ts-node/esm src/index.ts"
},
"dependencies": {
"@ai16z/adapter-postgres": "workspace:*",
"@ai16z/adapter-sqlite": "workspace:*",
"@ai16z/client-auto": "workspace:*",
"@ai16z/client-direct": "workspace:*",
"@ai16z/client-discord": "workspace:*",
"@ai16z/client-farcaster": "workspace:*",
"@ai16z/client-lens": "workspace:*",
"@ai16z/client-telegram": "workspace:*",
"@ai16z/client-twitter": "workspace:*",
"@ai16z/client-slack": "workspace:*",
"@ai16z/eliza": "workspace:*",
"@ai16z/plugin-0g": "workspace:*",
"@ai16z/plugin-aptos": "workspace:*",
"@ai16z/plugin-bootstrap": "workspace:*",
"@ai16z/plugin-intiface": "workspace:*",
"@ai16z/plugin-coinbase": "workspace:*",
"@ai16z/plugin-conflux": "workspace:*",
"@ai16z/plugin-evm": "workspace:*",
"@ai16z/plugin-flow": "workspace:*",
"@ai16z/plugin-story": "workspace:*",
"@ai16z/plugin-goat": "workspace:*",
"@ai16z/plugin-icp": "workspace:*",
"@ai16z/plugin-image-generation": "workspace:*",
"@ai16z/plugin-nft-generation": "workspace:*",
"@ai16z/plugin-node": "workspace:*",
"@ai16z/plugin-solana": "workspace:*",
"@ai16z/plugin-starknet": "workspace:*",
"@ai16z/plugin-ton": "workspace:*",
"@ai16z/plugin-sui": "workspace:*",
"@ai16z/plugin-tee": "workspace:*",
"@ai16z/plugin-multiversx": "workspace:*",
"@ai16z/plugin-near": "workspace:*",
"@ai16z/plugin-zksync-era": "workspace:*",
"readline": "1.3.0",
"ws": "8.18.0",
"yargs": "17.7.2"
},
"devDependencies": {
"ts-node": "10.9.2",
"tsup": "8.3.5"
}
"name": "@ai16z/agent",
"version": "0.1.6-alpha.4",
"main": "src/index.ts",
"type": "module",
"scripts": {
"start": "node --loader ts-node/esm src/index.ts",
"dev": "node --loader ts-node/esm src/index.ts",
"check-types": "tsc --noEmit"
},
"nodemonConfig": {
"watch": [
"src",
"../core/dist"
],
"ext": "ts,json",
"exec": "node --enable-source-maps --loader ts-node/esm src/index.ts"
},
"dependencies": {
"@ai16z/adapter-postgres": "workspace:*",
"@ai16z/adapter-redis": "workspace:*",
"@ai16z/adapter-sqlite": "workspace:*",
"@ai16z/client-auto": "workspace:*",
"@ai16z/client-direct": "workspace:*",
"@ai16z/client-discord": "workspace:*",
"@ai16z/client-farcaster": "workspace:*",
"@ai16z/client-lens": "workspace:*",
"@ai16z/client-telegram": "workspace:*",
"@ai16z/client-twitter": "workspace:*",
"@ai16z/client-slack": "workspace:*",
"@ai16z/eliza": "workspace:*",
"@ai16z/plugin-0g": "workspace:*",
"@ai16z/plugin-aptos": "workspace:*",
"@ai16z/plugin-bootstrap": "workspace:*",
"@ai16z/plugin-intiface": "workspace:*",
"@ai16z/plugin-coinbase": "workspace:*",
"@ai16z/plugin-conflux": "workspace:*",
"@ai16z/plugin-evm": "workspace:*",
"@ai16z/plugin-flow": "workspace:*",
"@ai16z/plugin-story": "workspace:*",
"@ai16z/plugin-goat": "workspace:*",
"@ai16z/plugin-icp": "workspace:*",
"@ai16z/plugin-image-generation": "workspace:*",
"@ai16z/plugin-nft-generation": "workspace:*",
"@ai16z/plugin-node": "workspace:*",
"@ai16z/plugin-solana": "workspace:*",
"@ai16z/plugin-starknet": "workspace:*",
"@ai16z/plugin-ton": "workspace:*",
"@ai16z/plugin-sui": "workspace:*",
"@ai16z/plugin-tee": "workspace:*",
"@ai16z/plugin-multiversx": "workspace:*",
"@ai16z/plugin-near": "workspace:*",
"@ai16z/plugin-zksync-era": "workspace:*",
"readline": "1.3.0",
"ws": "8.18.0",
"yargs": "17.7.2"
},
"devDependencies": {
"ts-node": "10.9.2",
"tsup": "8.3.5"
}
}
59 changes: 52 additions & 7 deletions agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import {
settings,
stringToUuid,
validateCharacterConfig,
CacheStore,
} from "@ai16z/eliza";
import { RedisClient } from "@ai16z/adapter-redis";
import { zgPlugin } from "@ai16z/plugin-0g";
import { bootstrapPlugin } from "@ai16z/plugin-bootstrap";
import createGoatPlugin from "@ai16z/plugin-goat";
Expand Down Expand Up @@ -210,11 +212,11 @@ export async function loadCharacters(
export function getTokenForProvider(
provider: ModelProviderName,
character: Character
):string {
): string {
switch (provider) {
// no key needed for llama_local
case ModelProviderName.LLAMALOCAL:
return ''
return "";
case ModelProviderName.OPENAI:
return (
character.settings?.secrets?.OPENAI_API_KEY ||
Expand Down Expand Up @@ -310,9 +312,9 @@ export function getTokenForProvider(
settings.AKASH_CHAT_API_KEY
);
default:
const errorMessage = `Failed to get token - unsupported model provider: ${provider}`
elizaLogger.error(errorMessage)
throw new Error(errorMessage)
const errorMessage = `Failed to get token - unsupported model provider: ${provider}`;
elizaLogger.error(errorMessage);
throw new Error(errorMessage);
}
}

Expand Down Expand Up @@ -402,7 +404,6 @@ export async function initializeClients(
// TODO: Add Slack client to the list
// Initialize clients as an object


if (clientTypes.includes("slack")) {
const slackClient = await SlackClientInterface.start(runtime);
if (slackClient) clients.slack = slackClient; // Use object property instead of push
Expand Down Expand Up @@ -583,6 +584,45 @@ function initializeDbCache(character: Character, db: IDatabaseCacheAdapter) {
return cache;
}

function initializeCache(
cacheStore: string,
character: Character,
baseDir?: string,
db?: IDatabaseCacheAdapter
) {
switch (cacheStore) {
case CacheStore.REDIS:
if (process.env.REDIS_URL) {
elizaLogger.info("Connecting to Redis...");
const redisClient = new RedisClient(process.env.REDIS_URL);
return new CacheManager(
new DbCacheAdapter(redisClient, character.id) // Using DbCacheAdapter since RedisClient also implements IDatabaseCacheAdapter
);
} else {
throw new Error("REDIS_URL environment variable is not set.");
}

case CacheStore.DATABASE:
if (db) {
elizaLogger.info("Using Database Cache...");
return initializeDbCache(character, db);
} else {
throw new Error(
"Database adapter is not provided for CacheStore.Database."
);
}

case CacheStore.FILESYSTEM:
elizaLogger.info("Using File System Cache...");
return initializeFsCache(baseDir, character);

default:
throw new Error(
`Invalid cache store: ${cacheStore} or required configuration missing.`
);
}
}

async function startAgent(
character: Character,
directClient: DirectClient
Expand All @@ -604,7 +644,12 @@ async function startAgent(

await db.init();

const cache = initializeDbCache(character, db);
const cache = initializeCache(
process.env.CACHE_STORE,
character,
"",
db
); // "" should be replaced with dir for file system caching. THOUGHTS: might probably make this into an env
const runtime: AgentRuntime = await createAgent(
character,
db,
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,12 @@ export type CacheOptions = {
expires?: number;
};

export enum CacheStore {
REDIS = "redis",
DATABASE = "database",
FILESYSTEM = "filesystem",
}

export interface ICacheManager {
get<T = unknown>(key: string): Promise<T | undefined>;
set<T>(key: string, value: T, options?: CacheOptions): Promise<void>;
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading