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

Abstracts Eliza into a Package to enble publishing onto NPM along with plugin system #214

Merged
merged 23 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
12e4d5d
loggin
ponderingdemocritus Nov 3, 2024
b2b1eb4
Merge branch 'main' of https://github.com/ponderingdemocritus/eliza
ponderingdemocritus Nov 3, 2024
31fea5e
Merge branch 'main' of https://github.com/ai16z/eliza
ponderingdemocritus Nov 3, 2024
6d4f41a
Merge branch 'main' of https://github.com/ai16z/eliza
ponderingdemocritus Nov 3, 2024
80af67a
Merge branch 'main' of https://github.com/ai16z/eliza
ponderingdemocritus Nov 4, 2024
11e5465
merge
ponderingdemocritus Nov 4, 2024
3460319
Merge branch 'main' of https://github.com/ai16z/eliza
ponderingdemocritus Nov 4, 2024
52467d6
Merge branch 'main' of https://github.com/ai16z/eliza
ponderingdemocritus Nov 5, 2024
04c9bac
package up core and introduce plugins
ponderingdemocritus Nov 6, 2024
fd2ad8c
exports
ponderingdemocritus Nov 6, 2024
c4d87d9
plugin
ponderingdemocritus Nov 6, 2024
041dc3a
plugin
ponderingdemocritus Nov 7, 2024
754941c
fix
ponderingdemocritus Nov 7, 2024
b93024d
fix
ponderingdemocritus Nov 7, 2024
f330fc1
Default character
ponderingdemocritus Nov 7, 2024
eefa2f7
prettier
lalalune Nov 7, 2024
9e30f89
model endpoint override
lalalune Nov 7, 2024
bcb2179
starts now
lalalune Nov 7, 2024
371f7e8
smol fixes to enable plugins
lalalune Nov 7, 2024
c96957e
Merge branch 'main' of https://github.com/ai16z/eliza
lalalune Nov 7, 2024
ad34b78
Load characters from arg in the new setup and rebuild docs
lalalune Nov 7, 2024
84f2da0
prettier
lalalune Nov 7, 2024
b06b302
Merge pull request #1 from ai16z/shaw/plugins
ponderingdemocritus Nov 7, 2024
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
6 changes: 6 additions & 0 deletions agent/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.ts
!index.ts
!character.ts
.env
*.env
.env*
20 changes: 20 additions & 0 deletions agent/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@eliza/agent",
"version": "0.0.1",
"main": "src/index.ts",
"type": "module",
"scripts": {
"build": "tsup --format esm --dts",
"start": "node --loader ts-node/esm src/index.ts",
"dev": "node --loader ts-node/esm src/index.ts"
},
"dependencies": {
"@eliza/core": "workspace:*",
"@eliza/plugin-image-generation": "workspace:*",
"readline": "^1.3.0",
"tsup": "^8.3.5"
},
"devDependencies": {
"ts-node": "10.9.2"
}
}
158 changes: 158 additions & 0 deletions agent/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import {
AgentRuntime,
boredomProvider,
Character,
defaultActions,
defaultCharacter,
DirectClient,
followRoom,
getTokenForProvider,
IAgentRuntime,
initializeClients,
initializeDatabase,
loadActionConfigs,
loadCharacters,
loadCustomActions,
muteRoom,
parseArguments,
timeProvider,
unfollowRoom,
unmuteRoom,
walletProvider,
} from "@eliza/core";
import readline from "readline";

const args = parseArguments();

console.log("Args are: ", args);

let charactersArg = args.characters || args.character;

let characters = [defaultCharacter];

if (charactersArg) {
characters = loadCharacters(charactersArg);
}

console.log("Characters are: ", characters);

const directClient = new DirectClient();

const serverPort = parseInt(process.env.SERVER_PORT || "3000");
directClient.start(serverPort);

export async function createDirectRuntime(
character: Character,
db: any,
token: string,
configPath: string = "./elizaConfig.yaml"
) {
console.log("Creating runtime for character", character.name);
return new AgentRuntime({
databaseAdapter: db,
token,
modelProvider: character.modelProvider,
evaluators: [],
character,
providers: [
timeProvider,
boredomProvider,
character.settings?.secrets?.WALLET_PUBLIC_KEY && walletProvider,
].filter(Boolean),
actions: [
...defaultActions,

// Custom actions
followRoom,
unfollowRoom,
unmuteRoom,
muteRoom,

// imported from elizaConfig.yaml
...(await loadCustomActions(loadActionConfigs(configPath))),
],
});
}

async function startAgent(character: Character) {
try {
const token = getTokenForProvider(character.modelProvider, character);
const db = initializeDatabase();

const runtime = await createDirectRuntime(character, db, token);

const clients = await initializeClients(
character,
runtime as IAgentRuntime
);

directClient.registerAgent(await runtime);

return clients;
} catch (error) {
console.error(
`Error starting agent for character ${character.name}:`,
error
);
throw error; // Re-throw after logging
}
}

const startAgents = async () => {
try {
for (const character of characters) {
await startAgent(character);
}
} catch (error) {
console.error("Error starting agents:", error);
}
};

startAgents().catch((error) => {
console.error("Unhandled error in startAgents:", error);
process.exit(1); // Exit the process after logging
});

const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

async function handleUserInput(input) {
if (input.toLowerCase() === "exit") {
rl.close();
return;
}

const agentId = characters[0].name.toLowerCase();
try {
const response = await fetch(
`http://localhost:3000/${agentId}/message`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
text: input,
userId: "user",
userName: "User",
}),
}
);

const data = await response.json();
data.forEach((message) =>
console.log(`${characters[0].name}: ${message.text}`)
);
} catch (error) {
console.error("Error fetching response:", error);
}

chat();
}

function chat() {
rl.question("You: ", handleUserInput);
}

console.log("Chat started. Type 'exit' to quit.");
chat();
7 changes: 7 additions & 0 deletions agent/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"outDir": "dist"
},
"include": ["."]
}
8 changes: 8 additions & 0 deletions agent/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineConfig } from "tsup";

export default defineConfig({
entry: ["src/index.ts"],
outDir: "dist",
sourcemap: true,
clean: true,
});
20 changes: 8 additions & 12 deletions core/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "eliza",
"name": "@eliza/core",
"version": "1.0.0",
"description": "",
"main": "dist/index.js",
"type": "module",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc -p tsconfig.json",
"build": "tsup --format esm --dts",
"lint": "eslint . --fix",
"start": "node --loader ts-node/esm src/index.ts",
"start": "node --loader ts-node/esm src/index.ts --characters=\"../characters/blobert.character.json\"",
"start:arok": "node --loader ts-node/esm src/index.ts --characters=\"../characters/arok.character.json\"",
"start:service:ruby": "pm2 start pnpm --name=\"ruby\" --restart-delay=3000 --max-restarts=10 -- run start:ruby",
"stop:service:ruby": "pm2 stop ruby",
Expand Down Expand Up @@ -70,11 +70,6 @@
"typescript": "5.6.3",
"wrangler": "3.84.0"
},
"pnpm": {
"overrides": {
"onnxruntime-node": "^1.19.2"
}
},
"dependencies": {
"@ai-sdk/anthropic": "^0.0.53",
"@ai-sdk/google": "^0.0.55",
Expand All @@ -84,8 +79,8 @@
"@anthropic-ai/sdk": "^0.30.1",
"@cliqz/adblocker-playwright": "1.34.0",
"@coral-xyz/anchor": "^0.30.1",
"@discordjs/rest": "2.4.0",
"@discordjs/opus": "github:discordjs/opus",
"@discordjs/rest": "2.4.0",
"@discordjs/voice": "0.17.0",
"@echogarden/espeak-ng-emscripten": "0.3.0",
"@echogarden/kissfft-wasm": "0.2.0",
Expand Down Expand Up @@ -144,7 +139,7 @@
"node-wav": "0.0.2",
"nodejs-whisper": "0.1.18",
"nodemon": "3.1.7",
"onnxruntime-node": "^1.19.2",
"onnxruntime-node": "^1.20.0",
"openai": "4.69.0",
"pdfjs-dist": "4.7.76",
"pg": "^8.13.1",
Expand All @@ -163,6 +158,7 @@
"tiktoken": "1.0.17",
"tinyld": "1.3.4",
"together-ai": "^0.7.0",
"tsup": "^8.3.5",
"unique-names-generator": "4.7.1",
"uuid": "11.0.2",
"wav": "1.0.2",
Expand All @@ -174,12 +170,12 @@
"youtube-dl-exec": "3.0.10"
},
"trustedDependencies": {
"onnxruntime-node": "^1.19.2",
"onnxruntime-node": "^1.20.0",
"@discordjs/opus": "github:discordjs/opus",
"@discordjs/voice": "0.17.0",
"sharp": "^0.33.5"
},
"peerDependencies": {
"onnxruntime-node": "^1.19.2"
"onnxruntime-node": "^1.20.0"
}
}
12 changes: 0 additions & 12 deletions core/src/actions/continue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
generateMessageResponse,
generateTrueOrFalse,
} from "../core/generation.ts";
import { log_to_file } from "../core/logger.ts";
import { booleanFooter, messageCompletionFooter } from "../core/parsing.ts";
import {
Action,
Expand Down Expand Up @@ -137,11 +136,6 @@ export const continueAction: Action = {
runtime.character.templates?.messageHandlerTemplate ||
messageHandlerTemplate,
});
const datestr = new Date().toUTCString().replace(/:/g, "-");

// log context to file
log_to_file(`${state.agentName}_${datestr}_continue_context`, context);

const { userId, roomId } = message;

const response = await generateMessageResponse({
Expand All @@ -152,12 +146,6 @@ export const continueAction: Action = {

response.inReplyTo = message.id;

// log response to file
log_to_file(
`${state.agentName}_${datestr}_continue_response`,
JSON.stringify(response)
);

runtime.databaseAdapter.log({
body: { message, context, response },
userId,
Expand Down
20 changes: 11 additions & 9 deletions core/src/actions/imageGeneration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import {
State,
Action,
} from "../core/types.ts";
import { prettyConsole } from "../index.ts";
import { elizaLogger } from "../index.ts";
import { generateCaption, generateImage } from "./imageGenerationUtils.ts";

export const imageGeneration: Action = {
name: "GENERATE_IMAGE",
similes: ["IMAGE_GENERATION", "IMAGE_GEN", "CREATE_IMAGE", "MAKE_PICTURE"],
description: "Generate an image to go along with the message.",
validate: async (runtime: IAgentRuntime, message: Memory) => {
// TODO: Abstract this to an image provider thing

const anthropicApiKeyOk = !!runtime.getSetting("ANTHROPIC_API_KEY");
const togetherApiKeyOk = !!runtime.getSetting("TOGETHER_API_KEY");

Expand All @@ -27,19 +29,19 @@ export const imageGeneration: Action = {
options: any,
callback: HandlerCallback
) => {
prettyConsole.log("Composing state for message:", message);
elizaLogger.log("Composing state for message:", message);
state = (await runtime.composeState(message)) as State;
const userId = runtime.agentId;
prettyConsole.log("User ID:", userId);
elizaLogger.log("User ID:", userId);

const imagePrompt = message.content.text;
prettyConsole.log("Image prompt received:", imagePrompt);
elizaLogger.log("Image prompt received:", imagePrompt);

// TODO: Generate a prompt for the image

const res: { image: string; caption: string }[] = [];

prettyConsole.log("Generating image with prompt:", imagePrompt);
elizaLogger.log("Generating image with prompt:", imagePrompt);
const images = await generateImage(
{
prompt: imagePrompt,
Expand All @@ -51,13 +53,13 @@ export const imageGeneration: Action = {
);

if (images.success && images.data && images.data.length > 0) {
prettyConsole.log(
elizaLogger.log(
"Image generation successful, number of images:",
images.data.length
);
for (let i = 0; i < images.data.length; i++) {
const image = images.data[i];
prettyConsole.log(`Processing image ${i + 1}:`, image);
elizaLogger.log(`Processing image ${i + 1}:`, image);

const caption = await generateCaption(
{
Expand All @@ -66,7 +68,7 @@ export const imageGeneration: Action = {
runtime
);

prettyConsole.log(
elizaLogger.log(
`Generated caption for image ${i + 1}:`,
caption.title
);
Expand All @@ -90,7 +92,7 @@ export const imageGeneration: Action = {
);
}
} else {
prettyConsole.error("Image generation failed or returned no data.");
elizaLogger.error("Image generation failed or returned no data.");
}
},
examples: [
Expand Down
Loading