Skip to content

Commit 5c9d739

Browse files
committed
merge the biz
1 parent 9b08306 commit 5c9d739

File tree

3 files changed

+50
-22
lines changed

3 files changed

+50
-22
lines changed

packages/client-twitter/src/base.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export class ClientBase extends EventEmitter {
8888
runtime: IAgentRuntime;
8989
directions: string;
9090
lastCheckedTweetId: number | null = null;
91-
tweetCacheFilePath = "tweetcache/latest_checked_tweet_id.txt";
91+
tweetCacheFilePath = __dirname + "/tweetcache/latest_checked_tweet_id.txt";
9292
imageDescriptionService: IImageDescriptionService;
9393
temperature: number = 0.5;
9494

@@ -103,7 +103,7 @@ export class ClientBase extends EventEmitter {
103103
}
104104
const cacheDir = path.join(
105105
__dirname,
106-
"@ai16z/eliza/tweetcache",
106+
"tweetcache",
107107
tweet.conversationId,
108108
`${tweet.id}.json`
109109
);
@@ -172,11 +172,14 @@ export class ClientBase extends EventEmitter {
172172
this.runtime.character.style.post.join();
173173

174174
try {
175+
console.log("this.tweetCacheFilePath", this.tweetCacheFilePath)
175176
if (fs.existsSync(this.tweetCacheFilePath)) {
177+
// make it?
176178
const data = fs.readFileSync(this.tweetCacheFilePath, "utf-8");
177179
this.lastCheckedTweetId = parseInt(data.trim());
178180
} else {
179181
console.warn("Tweet cache file not found.");
182+
console.warn(this.tweetCacheFilePath)
180183
}
181184
} catch (error) {
182185
console.error(

packages/client-twitter/src/interactions.ts

+19-14
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,14 @@ export class TwitterInteractionClient extends ClientBase {
146146
this.lastCheckedTweetId = parseInt(tweet.id);
147147

148148
try {
149-
fs.writeFileSync(
150-
this.tweetCacheFilePath,
151-
this.lastCheckedTweetId.toString(),
152-
"utf-8"
153-
);
149+
if (this.lastCheckedTweetId) {
150+
151+
fs.writeFileSync(
152+
this.tweetCacheFilePath,
153+
this.lastCheckedTweetId.toString(),
154+
"utf-8"
155+
);
156+
}
154157
} catch (error) {
155158
console.error(
156159
"Error saving latest checked tweet ID to file:",
@@ -162,11 +165,13 @@ export class TwitterInteractionClient extends ClientBase {
162165

163166
// Save the latest checked tweet ID to the file
164167
try {
165-
fs.writeFileSync(
166-
this.tweetCacheFilePath,
167-
this.lastCheckedTweetId.toString(),
168-
"utf-8"
169-
);
168+
if (this.lastCheckedTweetId) {
169+
fs.writeFileSync(
170+
this.tweetCacheFilePath,
171+
this.lastCheckedTweetId.toString(),
172+
"utf-8"
173+
);
174+
}
170175
} catch (error) {
171176
console.error(
172177
"Error saving latest checked tweet ID to file:",
@@ -252,10 +257,10 @@ export class TwitterInteractionClient extends ClientBase {
252257
url: tweet.permanentUrl,
253258
inReplyTo: tweet.inReplyToStatusId
254259
? stringToUuid(
255-
tweet.inReplyToStatusId +
256-
"-" +
257-
this.runtime.agentId
258-
)
260+
tweet.inReplyToStatusId +
261+
"-" +
262+
this.runtime.agentId
263+
)
259264
: undefined,
260265
},
261266
userId: userIdUUID,

packages/core/src/embedding.ts

+26-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1+
import { EmbeddingModel, FlagEmbedding } from "fastembed";
12
import path from "path";
3+
import { fileURLToPath } from "url";
24
import models from "./models.ts";
35
import {
46
IAgentRuntime,
5-
ITextGenerationService,
6-
ModelProviderName,
7-
ServiceType,
7+
ModelProviderName
88
} from "./types.ts";
99
import fs from "fs";
10-
import { EmbeddingModel, FlagEmbedding } from "fastembed";
10+
import { trimTokens } from "./generation.ts";
11+
12+
function getRootPath() {
13+
const __filename = fileURLToPath(import.meta.url);
14+
const __dirname = path.dirname(__filename);
15+
16+
const rootPath = path.resolve(__dirname, "..");
17+
if (rootPath.includes("/eliza/")) {
18+
return rootPath.split("/eliza/")[0] + "/eliza/";
19+
}
20+
21+
return path.resolve(__dirname, "..");
22+
}
1123

1224
/**
1325
* Send a message to the OpenAI API for embedding.
@@ -25,12 +37,20 @@ export async function embed(runtime: IAgentRuntime, input: string) {
2537
) {
2638

2739
// make sure to trim tokens to 8192
40+
const cacheDir = getRootPath() + "/cache/";
41+
42+
// if the cache directory doesn't exist, create it
43+
if (!fs.existsSync(cacheDir)) {
44+
fs.mkdirSync(cacheDir, { recursive: true });
45+
}
2846

2947
const embeddingModel = await FlagEmbedding.init({
30-
model: EmbeddingModel.BGEBaseEN
48+
cacheDir: cacheDir
3149
});
50+
51+
const trimmedInput = trimTokens(input, 8000, "gpt-4o-mini");
3252

33-
const embedding: number[] = await embeddingModel.queryEmbed(input);
53+
const embedding: number[] = await embeddingModel.queryEmbed(trimmedInput);
3454
console.log("Embedding dimensions: ", embedding.length);
3555
return embedding;
3656

0 commit comments

Comments
 (0)