Skip to content

Commit

Permalink
Merge pull request #14 from ai16z/main
Browse files Browse the repository at this point in the history
merge from main
  • Loading branch information
MarcoMandar authored Nov 3, 2024
2 parents c988337 + e580ac6 commit 1989eae
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 185 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
{
"type": "node",
"request": "launch",
"name": "Launch via npm",
"runtimeExecutable": "npm",
"name": "Launch via pnpm",
"runtimeExecutable": "pnpm",
"runtimeArgs": ["run", "dev"],
"skipFiles": ["<node_internals>/**"]
}
Expand Down
16 changes: 7 additions & 9 deletions core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@
"lint": "eslint . --fix",
"start": "node --loader ts-node/esm src/index.ts",
"start:arok": "node --loader ts-node/esm src/index.ts --characters=\"../characters/arok.character.json\"",
"start:service:ruby": "pm2 start npm --name=\"ruby\" --restart-delay=3000 --max-restarts=10 -- run start:ruby",
"start:service:ruby": "pm2 start pnpm --name=\"ruby\" --restart-delay=3000 --max-restarts=10 -- run start:ruby",
"stop:service:ruby": "pm2 stop ruby",
"start:ruby": "node --loader ts-node/esm src/index.ts --characters=\"../characters/ruby.character.json\"",
"start:service:trump": "pm2 start npm --name=\"trump\" --restart-delay=3000 --max-restarts=10 -- run start:trump",
"start:service:trump": "pm2 start pnpm --name=\"trump\" --restart-delay=3000 --max-restarts=10 -- run start:trump",
"stop:service:trump": "pm2 stop trump",
"start:service:degen": "pm2 start npm --name=\"degen\" --restart-delay=3000 --max-restarts=10 -- run start:degen",
"start:service:degen": "pm2 start pnpm --name=\"degen\" --restart-delay=3000 --max-restarts=10 -- run start:degen",
"stop:service:degen": "pm2 stop degen",
"start:degen": "node --loader ts-node/esm src/index.ts --characters=\"../characters/degenspartan.json\"",
"start:service:all": "pm2 start npm --name=\"all\" --restart-delay=3000 --max-restarts=10 -- run start:degen",
"start:service:all": "pm2 start pnpm --name=\"all\" --restart-delay=3000 --max-restarts=10 -- run start:all",
"stop:service:all": "pm2 stop all",
"start:all": "node --loader ts-node/esm src/index.ts --characters=\"../characters/degenspartan.json\",\"../characters/ruby.character.json\"",
"start:all": "node --loader ts-node/esm src/index.ts --characters=\"../characters/degenspartan.json\",\"../characters/ruby.character.json\",\"../characters/pmairca.character.json\"",
"start:trump": "node --loader ts-node/esm src/index.ts --characters=\"../characters/trump.character.json\"",
"start:service:tate": "pm2 start npm --name=\"tate\" --restart-delay=3000 --max-restarts=10 -- run start:tate",
"start:service:tate": "pm2 start pnpm --name=\"tate\" --restart-delay=3000 --max-restarts=10 -- run start:tate",
"stop:service:tate": "pm2 stop tate",
"start:tate": "node --loader ts-node/esm src/index.ts --characters=\"../characters/tate.character.json\"",
"watch": "tsc --watch",
"dev": "tsc && nodemon",
"build:docs": "cd docs && npm run build",
"build:docs": "cd docs && pnpm run build",
"postinstall": "npx playwright install-deps && npx playwright install",
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" jest --runInBand --watch -f",
"test:sqlite": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" TEST_DATABASE_CLIENT=sqlite jest --runInBand --watch -f",
Expand Down Expand Up @@ -59,7 +59,6 @@
"itty-router": "5.0.18",
"jest": "29.7.0",
"lint-staged": "15.2.10",
"npm-run-all2": "7.0.1",
"prettier": "3.3.3",
"rimraf": "6.0.1",
"rollup": "2.79.2",
Expand All @@ -83,7 +82,6 @@
"@anthropic-ai/sdk": "^0.30.1",
"@cliqz/adblocker-playwright": "1.34.0",
"@coral-xyz/anchor": "^0.30.1",
"@discordjs/opus": "github:discordjs/opus",
"@discordjs/rest": "2.4.0",
"@discordjs/voice": "0.17.0",
"@echogarden/espeak-ng-emscripten": "0.3.0",
Expand Down
4 changes: 2 additions & 2 deletions core/src/adapters/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class PostgresDatabaseAdapter extends DatabaseAdapter {
const placeholders = params.roomIds
.map((_, i) => `$${i + 2}`)
.join(", ");

let query = `SELECT * FROM memories WHERE type = $1 AND "roomId" IN (${placeholders})`;
let queryParams = [params.tableName, ...params.roomIds];

Expand Down Expand Up @@ -635,7 +635,7 @@ export class PostgresDatabaseAdapter extends DatabaseAdapter {

if (params.unique) {
sql += ` AND "unique" = true`;
}
}

// TODO: Test this
if (params.agentId) {
Expand Down
6 changes: 3 additions & 3 deletions core/src/adapters/sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export class SqliteDatabaseAdapter extends DatabaseAdapter {
const placeholders = params.roomIds.map(() => "?").join(", ");
let sql = `SELECT * FROM memories WHERE type = ? AND roomId IN (${placeholders})`;
let queryParams = [params.tableName, ...params.roomIds];

if (params.agentId) {
sql += ` AND userId = ?`;
queryParams.push(params.agentId);
Expand All @@ -171,9 +171,9 @@ export class SqliteDatabaseAdapter extends DatabaseAdapter {
content: string;
})[];

return rows.map(row => ({
return rows.map((row) => ({
...row,
content: JSON.parse(row.content)
content: JSON.parse(row.content),
}));
}

Expand Down
3 changes: 1 addition & 2 deletions core/src/adapters/supabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,15 +318,14 @@ export class SupabaseDatabaseAdapter extends DatabaseAdapter {
tableName: string;
}
): Promise<Memory[]> {

const queryParams = {
query_table_name: params.tableName,
query_roomId: params.roomId,
query_embedding: embedding,
query_match_threshold: params.match_threshold,
query_match_count: params.count,
query_unique: !!params.unique,
}
};
if (params.agentId) {
(queryParams as any).query_agentId = params.agentId;
}
Expand Down
2 changes: 2 additions & 0 deletions core/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ export async function createAgentRuntime(
const actionConfigs = loadActionConfigs(configPath);
const customActions = await loadCustomActions(actionConfigs);

console.log("Creating runtime for character", character.name);

return new AgentRuntime({
databaseAdapter: db,
token,
Expand Down
6 changes: 5 additions & 1 deletion core/src/clients/discord/actions/summarize_conversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,11 @@ const summarizeAction = {
"CONVERSATION_SUMMARY",
],
description: "Summarizes the conversation and attachments.",
validate: async (runtime: IAgentRuntime, message: Memory, _state: State) => {
validate: async (
runtime: IAgentRuntime,
message: Memory,
_state: State
) => {
if (message.content.source !== "discord") {
return false;
}
Expand Down
27 changes: 11 additions & 16 deletions core/src/clients/discord/messages.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { ChannelType, Client, Message as DiscordMessage, TextChannel } from "discord.js";
import {
ChannelType,
Client,
Message as DiscordMessage,
TextChannel,
} from "discord.js";
import { composeContext } from "../../core/context.ts";
import {
generateMessageResponse,
Expand Down Expand Up @@ -153,17 +158,6 @@ export class MessageManager {

const messageId = stringToUuid(message.id);

// Check if the message already exists in the cache or database
const existingMessage =
await this.runtime.messageManager.getMemoryById(messageId);

if (existingMessage) {
// If the message content is the same, return early
if (existingMessage.content.text === message.content) {
return;
}
}

let shouldIgnore = false;
let shouldRespond = true;

Expand Down Expand Up @@ -271,10 +265,11 @@ export class MessageManager {
}
if (message.channel.type === ChannelType.GuildVoice) {
// For voice channels, use text-to-speech
const audioStream = await this.runtime.speechService.generate(
this.runtime,
content.text
);
const audioStream =
await this.runtime.speechService.generate(
this.runtime,
content.text
);
await this.voiceManager.playAudioStream(
userId,
audioStream
Expand Down
7 changes: 5 additions & 2 deletions core/src/clients/twitter/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,8 @@ export class ClientBase extends EventEmitter {
// Save the missing tweets as memories
for (const tweet of tweetsToSave) {
const roomId = stringToUuid(
tweet.conversationId ?? "default-room-" + this.runtime.agentId
tweet.conversationId ??
"default-room-" + this.runtime.agentId
);
const tweetuserId =
tweet.userId === this.twitterUserId
Expand Down Expand Up @@ -508,7 +509,9 @@ export class ClientBase extends EventEmitter {

// Save the new tweets as memories
for (const tweet of tweetsToSave) {
const roomId = stringToUuid(tweet.conversationId ?? "default-room-" + this.runtime.agentId);
const roomId = stringToUuid(
tweet.conversationId ?? "default-room-" + this.runtime.agentId
);
const tweetuserId =
tweet.userId === this.twitterUserId
? this.runtime.agentId
Expand Down
124 changes: 60 additions & 64 deletions core/src/core/generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,64 +68,61 @@ export async function generateText({

switch (provider) {
case ModelProvider.OPENAI:
case ModelProvider.LLAMACLOUD:
{
console.log("Initializing OpenAI model.");
const openai = createOpenAI({ apiKey });

const { text: openaiResponse } = await aiGenerateText({
model: openai.languageModel(model),
prompt: context,
temperature: temperature,
maxTokens: max_response_length,
frequencyPenalty: frequency_penalty,
presencePenalty: presence_penalty,
});

response = openaiResponse;
console.log("Received response from OpenAI model.");
break;
}

case ModelProvider.ANTHROPIC:
{
console.log("Initializing Anthropic model.");
const anthropicVertex = createAnthropicVertex();

const { text: anthropicResponse } = await aiGenerateText({
model: anthropicVertex(model),
prompt: context,
temperature: temperature,
maxTokens: max_response_length,
frequencyPenalty: frequency_penalty,
presencePenalty: presence_penalty,
});

response = anthropicResponse;
console.log("Received response from Anthropic model.");
break;
}

case ModelProvider.GROK:
{
console.log("Initializing Grok model.");
const grok = createGroq({ apiKey });

const { text: grokResponse } = await aiGenerateText({
model: grok.languageModel(model, {
parallelToolCalls: false,
}),
prompt: context,
temperature: temperature,
maxTokens: max_response_length,
frequencyPenalty: frequency_penalty,
presencePenalty: presence_penalty,
});

response = grokResponse;
console.log("Received response from Grok model.");
break;
}
case ModelProvider.LLAMACLOUD: {
console.log("Initializing OpenAI model.");
const openai = createOpenAI({ apiKey });

const { text: openaiResponse } = await aiGenerateText({
model: openai.languageModel(model),
prompt: context,
temperature: temperature,
maxTokens: max_response_length,
frequencyPenalty: frequency_penalty,
presencePenalty: presence_penalty,
});

response = openaiResponse;
console.log("Received response from OpenAI model.");
break;
}

case ModelProvider.ANTHROPIC: {
console.log("Initializing Anthropic model.");
const anthropicVertex = createAnthropicVertex();

const { text: anthropicResponse } = await aiGenerateText({
model: anthropicVertex(model),
prompt: context,
temperature: temperature,
maxTokens: max_response_length,
frequencyPenalty: frequency_penalty,
presencePenalty: presence_penalty,
});

response = anthropicResponse;
console.log("Received response from Anthropic model.");
break;
}

case ModelProvider.GROK: {
console.log("Initializing Grok model.");
const grok = createGroq({ apiKey });

const { text: grokResponse } = await aiGenerateText({
model: grok.languageModel(model, {
parallelToolCalls: false,
}),
prompt: context,
temperature: temperature,
maxTokens: max_response_length,
frequencyPenalty: frequency_penalty,
presencePenalty: presence_penalty,
});

response = grokResponse;
console.log("Received response from Grok model.");
break;
}

case ModelProvider.LLAMALOCAL:
console.log("Using local Llama model for text completion.");
Expand All @@ -140,12 +137,11 @@ export async function generateText({
console.log("Received response from local Llama model.");
break;

default:
{
const errorMessage = `Unsupported provider: ${provider}`;
console.error(errorMessage);
throw new Error(errorMessage);
}
default: {
const errorMessage = `Unsupported provider: ${provider}`;
console.error(errorMessage);
throw new Error(errorMessage);
}
}

return response;
Expand Down
12 changes: 6 additions & 6 deletions core/src/core/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const models: Models = {
model: {
[ModelClass.SMALL]: "gpt-4o-mini",
[ModelClass.MEDIUM]: "gpt-4o",
[ModelClass.LARGE]: "gpt-4-turbo",
[ModelClass.LARGE]: "gpt-4o",
[ModelClass.EMBEDDING]: "text-embedding-3-small",
},
},
Expand Down Expand Up @@ -104,13 +104,13 @@ const models: Models = {
temperature: 0.3,
},
model: {
[ModelClass.SMALL]: "bartowski/Llama-3.2-3B-Instruct-GGUF",
[ModelClass.SMALL]: "NousResearch/Hermes-3-Llama-3.1-8B-GGUF/resolve/main/Hermes-3-Llama-3.1-8B.Q8_0.gguf?download=true",
[ModelClass.MEDIUM]:
"NousResearch/Hermes-3-Llama-3.1-8B-GGUF/resolve/main/Hermes-3-Llama-3.1-8B.Q8_0.gguf", // TODO: ?download=true
"NousResearch/Hermes-3-Llama-3.1-8B-GGUF/resolve/main/Hermes-3-Llama-3.1-8B.Q8_0.gguf?download=true", // TODO: ?download=true
[ModelClass.LARGE]:
"RichardErkhov/NousResearch_-_Meta-Llama-3.1-70B-gguf", // TODO: ?download=true
[ModelClass.EMBEDDING]:
"togethercomputer/m2-bert-80M-32k-retrieval",
"NousResearch/Hermes-3-Llama-3.1-8B-GGUF/resolve/main/Hermes-3-Llama-3.1-8B.Q8_0.gguf?download=true",
// "RichardErkhov/NousResearch_-_Meta-Llama-3.1-70B-gguf", // TODO:
[ModelClass.EMBEDDING]: "togethercomputer/m2-bert-80M-32k-retrieval"
},
},
[ModelProvider.GOOGLE]: {
Expand Down
7 changes: 5 additions & 2 deletions core/src/core/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,12 @@ export class AgentRuntime implements IAgentRuntime {
this.registerContextProvider(provider);
});

if (!this.getSetting("OPENAI_API_KEY") && !this.llamaService) {
if (
this.modelProvider === ModelProvider.LLAMALOCAL &&
!this.llamaService
) {
console.log(
"No OpenAI key found, using LlamaLocal for agent",
"Initializing LlamaLocal service for agent",
this.agentId,
this.character.name
);
Expand Down
5 changes: 1 addition & 4 deletions core/src/services/speech.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,7 @@ async function textToSpeech(runtime: IAgentRuntime, text: string) {
}

export class SpeechService implements ISpeechService {
async generate(
runtime: IAgentRuntime,
text: string
): Promise<Readable> {
async generate(runtime: IAgentRuntime, text: string): Promise<Readable> {
// check for elevenlabs API key
if (runtime.getSetting("ELEVENLABS_XI_API_KEY")) {
return textToSpeech(runtime, text);
Expand Down
Loading

0 comments on commit 1989eae

Please sign in to comment.