Skip to content

Commit

Permalink
Merge remote-tracking branch 'ai16z/develop' into realitySpiral/integ…
Browse files Browse the repository at this point in the history
…ration-tests
  • Loading branch information
jzvikart committed Dec 17, 2024
2 parents 3fab472 + 0a23d6d commit 9888ced
Show file tree
Hide file tree
Showing 25 changed files with 23,715 additions and 17,827 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,12 @@ MEDIUM_VENICE_MODEL= # Default: llama-3.3-70b
LARGE_VENICE_MODEL= # Default: llama-3.1-405b
IMAGE_VENICE_MODEL= # Default: fluently-xl

# Akash Chat API Configuration docs: https://chatapi.akash.network/documentation
AKASH_CHAT_API_KEY= # Get from https://chatapi.akash.network/
SMALL_AKASH_CHAT_API_MODEL= # Default: Meta-Llama-3-2-3B-Instruct
MEDIUM_AKASH_CHAT_API_MODEL= # Default: Meta-Llama-3-3-70B-Instruct
LARGE_AKASH_CHAT_API_MODEL= # Default: Meta-Llama-3-1-405B-Instruct-FP8

# fal.ai Configuration
FAL_API_KEY=
FAL_AI_LORA_PATH=
Expand Down
5 changes: 5 additions & 0 deletions agent/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,11 @@ export function getTokenForProvider(
character.settings?.secrets?.VENICE_API_KEY ||
settings.VENICE_API_KEY
);
case ModelProviderName.AKASH_CHAT_API:
return (
character.settings?.secrets?.AKASH_CHAT_API_KEY ||
settings.AKASH_CHAT_API_KEY
);
}
}

Expand Down
File renamed without changes.
1 change: 0 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
services:
tee:
command: ["pnpm", "start"]
build:
context: .
dockerfile: Dockerfile
Expand Down
87 changes: 64 additions & 23 deletions docs/docs/api/functions/composeContext.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,85 @@

> **composeContext**(`params`): `string`
Composes a context string by replacing placeholders in a template with corresponding values from the state.

This function takes a template string with placeholders in the format `{{placeholder}}` and a state object.
It replaces each placeholder with the value from the state object that matches the placeholder's name.
If a matching key is not found in the state object for a given placeholder, the placeholder is replaced with an empty string.
Composes a context string by replacing placeholders in a template with values from a state object. Supports both simple string replacement and the Handlebars templating engine.

## Parameters

**params**

The parameters for composing the context.
### **params**: `Object`

**params.state**: [`State`](../interfaces/State.md)
An object containing the following properties:

The state object containing values to replace the placeholders in the template.
- **state**: `State`
The state object containing key-value pairs for replacing placeholders in the template.

**params.template**: `string`
- **template**: `string`
A string containing placeholders in the format `{{placeholder}}`.

The template string containing placeholders to be replaced with state values.
- **templatingEngine**: `"handlebars" | undefined` *(optional)*
The templating engine to use. If set to `"handlebars"`, the Handlebars engine is used for template compilation. Defaults to `undefined` (simple string replacement).

## Returns

`string`

The composed context string with placeholders replaced by corresponding state values.
The context string with placeholders replaced by corresponding values from the state object. If a placeholder has no matching key in the state, it is replaced with an empty string.

## Examples

## Example
### Simple Example

```ts
// Given a state object and a template
```javascript
const state = { userName: "Alice", userAge: 30 };
const template = "Hello, {{userName}}! You are {{userAge}} years old";
const template = "Hello, {{userName}}! You are {{userAge}} years old.";

// Composing the context will result in:
// "Hello, Alice! You are 30 years old."
const context = composeContext({ state, template });
```
// Simple string replacement
const contextSimple = composeContext({ state, template });
// Output: "Hello, Alice! You are 30 years old."

## Defined in
// Handlebars templating
const contextHandlebars = composeContext({ state, template, templatingEngine: 'handlebars' });
// Output: "Hello, Alice! You are 30 years old."
```

[packages/core/src/context.ts:24](https://github.com/ai16z/eliza/blob/7fcf54e7fb2ba027d110afcc319c0b01b3f181dc/packages/core/src/context.ts#L24)
### Advanced Example

```javascript
const advancedTemplate = `
{{#if userAge}}
Hello, {{userName}}!
{{#if (gt userAge 18)}}You are an adult.{{else}}You are a minor.{{/if}}
{{else}}
Hello! We don't know your age.
{{/if}}
{{#if favoriteColors.length}}
Your favorite colors are:
{{#each favoriteColors}}
- {{this}}
{{/each}}
{{else}}
You didn't specify any favorite colors.
{{/if}}
`;

const advancedState = {
userName: "Alice",
userAge: 30,
favoriteColors: ["blue", "green", "red"]
};

// Composing the context with Handlebars
const advancedContextHandlebars = composeContext({
state: advancedState,
template: advancedTemplate,
templatingEngine: 'handlebars'
});
// Output:
// Hello, Alice!
// You are an adult.
//
// Your favorite colors are:
// - blue
// - green
// - red
```
38 changes: 29 additions & 9 deletions packages/client-discord/src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,24 +508,44 @@ export class MessageManager {
}

private _isMessageForMe(message: DiscordMessage): boolean {
const isMentioned = message.mentions.users?.has(this.client.user?.id as string);
const isMentioned = message.mentions.users?.has(
this.client.user?.id as string
);
const guild = message.guild;
const member = guild?.members.cache.get(this.client.user?.id as string);
const nickname = member?.nickname;
const memberId = member?.id;

// Don't consider role mentions as direct mentions
const hasRoleMentionOnly = message.mentions.roles.size > 0 && !isMentioned;
const hasRoleMentionOnly =
message.mentions.roles.size > 0 && !isMentioned;

// If it's only a role mention and we're in team mode, let team logic handle it
if (hasRoleMentionOnly && this.runtime.character.clientConfig?.discord?.isPartOfTeam) {
if (
hasRoleMentionOnly &&
this.runtime.character.clientConfig?.discord?.isPartOfTeam
) {
return false;
}

return isMentioned || (!this.runtime.character.clientConfig?.discord?.shouldRespondOnlyToMentions && (
message.content.toLowerCase().includes(this.client.user?.username.toLowerCase() as string) ||
message.content.toLowerCase().includes(this.client.user?.tag.toLowerCase() as string) ||
(nickname && message.content.toLowerCase().includes(nickname.toLowerCase()))));
return (
isMentioned ||
(!this.runtime.character.clientConfig?.discord
?.shouldRespondOnlyToMentions &&
(message.content
.toLowerCase()
.includes(
this.client.user?.username.toLowerCase() as string
) ||
message.content
.toLowerCase()
.includes(
this.client.user?.tag.toLowerCase() as string
) ||
(nickname &&
message.content
.toLowerCase()
.includes(nickname.toLowerCase()))))
);
}

async processMessageMedia(
Expand Down Expand Up @@ -1287,4 +1307,4 @@ export class MessageManager {
const data = await response.json();
return data.username;
}
}
}
23 changes: 5 additions & 18 deletions packages/client-telegram/src/messageManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,15 @@ export class MessageManager {
'caption' in message ? (message as any).caption : '';
if (!messageText) return false;

const isReplyToBot = (message as any).reply_to_message?.from?.is_bot === true &&
(message as any).reply_to_message?.from?.username === botUsername;
const isMentioned = messageText.includes(`@${botUsername}`);
const hasUsername = messageText.toLowerCase().includes(botUsername.toLowerCase());

return isMentioned || (!this.runtime.character.clientConfig?.telegram?.shouldRespondOnlyToMentions && hasUsername);
return isReplyToBot || isMentioned || (!this.runtime.character.clientConfig?.telegram?.shouldRespondOnlyToMentions && hasUsername);
}


private _checkInterest(chatId: string): boolean {
const chatState = this.interestChats[chatId];
if (!chatState) return false;
Expand Down Expand Up @@ -360,22 +363,6 @@ export class MessageManager {
return true;
}

private _isMessageForMe(message: Message): boolean {
const botUsername = this.bot.botInfo?.username;
if (!botUsername) return false;

const messageText = 'text' in message ? message.text :
'caption' in message ? (message as any).caption : '';
if (!messageText) return false;

const isReplyToBot = (message as any).reply_to_message?.from?.is_bot === true &&
(message as any).reply_to_message?.from?.username === botUsername;
const isMentioned = messageText.includes(`@${botUsername}`);
const hasUsername = messageText.toLowerCase().includes(botUsername.toLowerCase());

return isReplyToBot || isMentioned || (!this.runtime.character.clientConfig?.telegram?.shouldRespondOnlyToMentions && hasUsername);
}

// Process image messages and generate descriptions
private async processImage(
message: Message
Expand Down Expand Up @@ -422,7 +409,7 @@ export class MessageManager {
message: Message,
state: State
): Promise<boolean> {

if (this.runtime.character.clientConfig?.telegram?.shouldRespondOnlyToMentions) {
return this._isMessageForMe(message);
}
Expand Down
5 changes: 4 additions & 1 deletion packages/client-twitter/src/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,11 @@ export class TwitterPostClient {
const removeQuotes = (str: string) =>
str.replace(/^['"](.*)['"]$/, "$1");

const fixNewLines = (str: string) =>
str.replaceAll(/\\n/g, "\n");

// Final cleaning
cleanedContent = removeQuotes(content);
cleanedContent = removeQuotes(fixNewLines(content));

if (this.runtime.getSetting("TWITTER_DRY_RUN") === "true") {
elizaLogger.info(
Expand Down
23 changes: 0 additions & 23 deletions packages/client-whatsapp/package.json

This file was deleted.

Empty file.
21 changes: 0 additions & 21 deletions packages/client-whatsapp/src/environment.ts

This file was deleted.

28 changes: 0 additions & 28 deletions packages/client-whatsapp/src/index.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"fastestsmallesttextencoderdecoder": "1.0.22",
"gaxios": "6.7.1",
"glob": "11.0.0",
"handlebars": "^4.7.8",
"js-sha1": "0.7.0",
"js-tiktoken": "1.0.15",
"langchain": "0.3.6",
Expand Down
15 changes: 13 additions & 2 deletions packages/core/src/context.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import handlebars from "handlebars";
import { type State } from "./types.ts";

/**
Expand All @@ -7,27 +8,37 @@ import { type State } from "./types.ts";
* It replaces each placeholder with the value from the state object that matches the placeholder's name.
* If a matching key is not found in the state object for a given placeholder, the placeholder is replaced with an empty string.
*
* By default, this function uses a simple string replacement approach. However, when `templatingEngine` is set to `'handlebars'`, it uses Handlebars templating engine instead, compiling the template into a reusable function and evaluating it with the provided state object.
*
* @param {Object} params - The parameters for composing the context.
* @param {State} params.state - The state object containing values to replace the placeholders in the template.
* @param {string} params.template - The template string containing placeholders to be replaced with state values.
* @param {"handlebars" | undefined} [params.templatingEngine] - The templating engine to use for compiling and evaluating the template (optional, default: `undefined`).
* @returns {string} The composed context string with placeholders replaced by corresponding state values.
*
* @example
* // Given a state object and a template
* const state = { userName: "Alice", userAge: 30 };
* const template = "Hello, {{userName}}! You are {{userAge}} years old";
*
* // Composing the context will result in:
* // Composing the context with simple string replacement will result in:
* // "Hello, Alice! You are 30 years old."
* const context = composeContext({ state, template });
* const contextSimple = composeContext({ state, template });
*/
export const composeContext = ({
state,
template,
templatingEngine,
}: {
state: State;
template: string;
templatingEngine?: "handlebars";
}) => {
if (templatingEngine === "handlebars") {
const templateFunction = handlebars.compile(template);
return templateFunction(state);
}

// @ts-expect-error match isn't working as expected
const out = template.replace(/{{\w+}}/g, (match) => {
const key = match.replace(/{{|}}/g, "");
Expand Down
Loading

0 comments on commit 9888ced

Please sign in to comment.