Skip to content

Commit c69a105

Browse files
committed
success!!! with direct message!
1 parent 7db2a10 commit c69a105

File tree

8 files changed

+225
-116
lines changed

8 files changed

+225
-116
lines changed

packages/client-direct/src/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import {
2323
type IAgentRuntime,
2424
} from "@elizaos/core";
2525
import { createApiRouter } from "./api.ts";
26-
import * as fs from "fs";
27-
import * as path from "path";
26+
import * as fs from "node:fs";
27+
import * as path from "node:path";
2828
import { createVerifiableLogApiRouter } from "./verifiable-log-api.ts";
2929
import OpenAI from "openai";
3030

@@ -74,7 +74,7 @@ Note that {{agentName}} is capable of reading/seeing/hearing various forms of me
7474
{{actions}}
7575
7676
# Instructions: Write the next message for {{agentName}}.
77-
` + messageCompletionFooter;
77+
${messageCompletionFooter}`;
7878

7979
export const hyperfiHandlerTemplate = `{{actionExamples}}
8080
(Action examples are for reference only. Do not use the information from them in your response.)

packages/core/__tests__/runtime.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ const mockImageSettings: ImageModelSettings = {
9696
};
9797

9898
const mockEmbeddingSettings: EmbeddingModelSettings = {
99-
name: "test-embedding-model",
99+
name: "text-embedding-ada-002",
100100
dimensions: 1536,
101101
};
102102

@@ -331,7 +331,7 @@ describe("Model Provider Configuration", () => {
331331
repetition_penalty: 1.0,
332332
},
333333
[ModelClass.EMBEDDING]: {
334-
name: "test-embedding-model",
334+
name: "text-embedding-ada-002",
335335
dimensions: 1536,
336336
},
337337
[ModelClass.IMAGE]: {

packages/core/src/embedding.ts

+54-44
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
21
// TODO: Maybe create these functions to read from character settings or env
32
// import { getEmbeddingModelSettings, getEndpoint } from "./models.ts";
4-
import { type IAgentRuntime, ModelProviderName } from "./types.ts";
3+
import { type IAgentRuntime, ModelProviderName, ModelClass } from "./types.ts";
54
import settings from "./settings.ts";
65
import elizaLogger from "./logger.ts";
76
import LocalEmbeddingModelManager from "./localembeddingManager.ts";
@@ -30,15 +29,27 @@ export type EmbeddingConfig = {
3029
readonly provider: string;
3130
};
3231

33-
export const getEmbeddingConfig = (): EmbeddingConfig => ({
34-
dimensions:
35-
// TODO: get from env or character settings
36-
384,
37-
model:
38-
// TODO: get from env or character settings
39-
"BGE-small-en-v1.5",
40-
provider: "BGE",
41-
});
32+
export const getEmbeddingConfig = (runtime?: IAgentRuntime): EmbeddingConfig => {
33+
if (runtime) {
34+
const modelProvider = runtime.getModelProvider();
35+
const embeddingModel = modelProvider?.models?.[ModelClass.EMBEDDING];
36+
37+
if (embeddingModel?.name) {
38+
return {
39+
dimensions: embeddingModel.dimensions || 1536,
40+
model: embeddingModel.name,
41+
provider: modelProvider?.provider || EmbeddingProvider.OpenAI,
42+
};
43+
}
44+
}
45+
46+
// Fallback to default config
47+
return {
48+
dimensions: 1536, // OpenAI's text-embedding-ada-002 dimension
49+
model: "text-embedding-3-small", // Default to OpenAI's latest embedding model
50+
provider: EmbeddingProvider.OpenAI
51+
};
52+
};
4253

4354
async function getRemoteEmbedding(
4455
input: string,
@@ -52,6 +63,26 @@ async function getRemoteEmbedding(
5263
// Construct full URL
5364
const fullUrl = `${baseEndpoint}/embeddings`;
5465

66+
elizaLogger.info("Embedding request:", {
67+
modelProvider: options.provider,
68+
useOpenAI: options.provider === EmbeddingProvider.OpenAI,
69+
input: `${input?.slice(0, 50)}...`,
70+
inputType: typeof input,
71+
inputLength: input?.length,
72+
isString: typeof input === "string",
73+
isEmpty: !input,
74+
});
75+
76+
const requestBody: any = {
77+
input,
78+
model: options.model,
79+
};
80+
81+
// Only include dimensions for non-OpenAI providers
82+
if (options.provider !== EmbeddingProvider.OpenAI) {
83+
requestBody.dimensions = options.dimensions || options.length || getEmbeddingConfig().dimensions;
84+
}
85+
5586
const requestOptions = {
5687
method: "POST",
5788
headers: {
@@ -62,19 +93,14 @@ async function getRemoteEmbedding(
6293
}
6394
: {}),
6495
},
65-
body: JSON.stringify({
66-
input,
67-
model: options.model,
68-
dimensions:
69-
options.dimensions ||
70-
options.length ||
71-
getEmbeddingConfig().dimensions, // Prefer dimensions, fallback to length
72-
}),
96+
body: JSON.stringify(requestBody),
7397
};
7498

7599
try {
76100
const response = await fetch(fullUrl, requestOptions);
77101

102+
elizaLogger.info("Embedding response:", requestOptions);
103+
78104
if (!response.ok) {
79105
elizaLogger.error("API Response:", await response.text()); // Debug log
80106
throw new Error(
@@ -158,28 +184,13 @@ export async function embed(runtime: IAgentRuntime, input: string) {
158184
const cachedEmbedding = await retrieveCachedEmbedding(runtime, input);
159185
if (cachedEmbedding) return cachedEmbedding;
160186

161-
const config = getEmbeddingConfig();
162187
const isNode = typeof process !== "undefined" && process.versions?.node;
163188

164-
// use endpoint from model provider
165-
const endpoint = runtime.getSetting("PROVIDER_ENDPOINT");
166-
const apiKey = runtime.getSetting("PROVIDER_API_KEY");
167-
168-
169-
// Determine which embedding settings to use
170-
// TODO: enhance + verify logic to get from character settings or env
171-
if (config.provider) {
172-
return await getRemoteEmbedding(input, {
173-
model: config.model,
174-
endpoint: settings.PROVIDER_ENDPOINT || "https://api.openai.com/v1",
175-
apiKey: settings.PROVIDER_API_KEY,
176-
dimensions: config.dimensions,
177-
});
178-
}
179-
189+
// Get embedding configuration from runtime
190+
const embeddingConfig = getEmbeddingConfig(runtime);
180191

181-
// BGE - try local first if in Node
182-
if (isNode) {
192+
// BGE - try local first if in Node and not using OpenAI
193+
if (isNode && embeddingConfig.provider !== EmbeddingProvider.OpenAI) {
183194
try {
184195
return await getLocalEmbedding(input);
185196
} catch (error) {
@@ -190,14 +201,13 @@ export async function embed(runtime: IAgentRuntime, input: string) {
190201
}
191202
}
192203

193-
// Fallback to remote override
204+
// Use remote embedding
194205
return await getRemoteEmbedding(input, {
195-
model: config.model,
196-
endpoint:
197-
runtime.character.modelEndpointOverride ||
198-
runtime.getSetting("PROVIDER_ENDPOINT"),
206+
model: embeddingConfig.model,
207+
endpoint: runtime.character.modelEndpointOverride || runtime.getSetting("PROVIDER_ENDPOINT") || "https://api.openai.com/v1",
199208
apiKey: runtime.getSetting("PROVIDER_API_KEY") || runtime.token,
200-
dimensions: config.dimensions,
209+
dimensions: embeddingConfig.dimensions,
210+
provider: embeddingConfig.provider
201211
});
202212

203213
async function getLocalEmbedding(input: string): Promise<number[]> {

packages/core/src/environment.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,11 @@ export function validateCharacterConfig(json: unknown): CharacterConfig {
169169
{} as Record<string, string[]>
170170
);
171171

172-
Object.entries(groupedErrors).forEach(([field, messages]) => {
172+
for (const field in groupedErrors) {
173173
elizaLogger.error(
174-
`Validation errors in ${field}: ${messages.join(" - ")}`
174+
`Validation errors in ${field}: ${groupedErrors[field].join(" - ")}`
175175
);
176-
});
176+
}
177177

178178
throw new Error(
179179
"Character configuration validation failed. Check logs for details."

0 commit comments

Comments
 (0)