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

cachedEmbeddings fix #262

Merged
merged 3 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
45 changes: 20 additions & 25 deletions packages/adapter-sqlite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,34 +336,29 @@ export class SqliteDatabaseAdapter extends DatabaseAdapter {
query_field_name: string;
query_field_sub_name: string;
query_match_count: number;
}): Promise<
{
embedding: number[];
levenshtein_score: number;
}[]
> {
}): Promise<{ embedding: number[]; levenshtein_score: number }[]> {
const sql = `
SELECT *
FROM memories
WHERE type = ?
AND vec_distance_L2(${opts.query_field_name}, ?) <= ?
ORDER BY vec_distance_L2(${opts.query_field_name}, ?) ASC
LIMIT ?
`;
console.log("sql", sql)
console.log("opts.query_input", opts.query_input)
const memories = this.db.prepare(sql).all(
SELECT
embedding,
0 as levenshtein_score -- Using 0 as placeholder score
FROM memories
WHERE type = ?
AND json_extract(content, '$.' || ? || '.' || ?) IS NOT NULL
LIMIT ?
`;

const params = [
opts.query_table_name,
new Float32Array(opts.query_input.split(",").map(Number)), // Convert string to Float32Array
opts.query_input,
new Float32Array(opts.query_input.split(",").map(Number))
) as Memory[];
opts.query_field_name,
opts.query_field_sub_name,
opts.query_match_count
];

return memories.map((memory) => ({
embedding: Array.from(
new Float32Array(memory.embedding as unknown as Buffer)
), // Convert Buffer to number[]
levenshtein_score: 0,
const rows = this.db.prepare(sql).all(...params);

return rows.map((row) => ({
embedding: row.embedding,
levenshtein_score: 0
}));
}

Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/defaultCharacter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ export const defaultCharacter: Character = {
name: "Eliza",
plugins: [],
clients: [],
modelProvider: ModelProviderName.LLAMALOCAL,
modelProvider: ModelProviderName.OPENAI,
settings: {
secrets: {},
secrets: {
},
voice: {
model: "en_US-hfc_female-medium",
},
Expand Down
39 changes: 26 additions & 13 deletions packages/core/src/embedding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { fileURLToPath } from "url";
import models from "./models.ts";
import {
IAgentRuntime,
ModelProviderName
ModelProviderName,
ModelClass
} from "./types.ts";
import fs from "fs";
import { trimTokens } from "./generation.ts";
Expand All @@ -18,7 +19,7 @@ function getRootPath() {
if (rootPath.includes("/eliza/")) {
return rootPath.split("/eliza/")[0] + "/eliza/";
}

return path.resolve(__dirname, "..");
}

Expand All @@ -32,13 +33,13 @@ interface EmbeddingOptions {

async function getRemoteEmbedding(input: string, options: EmbeddingOptions): Promise<number[]> {
// Ensure endpoint ends with /v1 for OpenAI
const baseEndpoint = options.endpoint.endsWith('/v1') ?
options.endpoint :
const baseEndpoint = options.endpoint.endsWith('/v1') ?
options.endpoint :
`${options.endpoint}${options.isOllama ? '/v1' : ''}`;

// Construct full URL
const fullUrl = `${baseEndpoint}/embeddings`;

//console.log("Calling embedding API at:", fullUrl); // Debug log

const requestOptions = {
Expand Down Expand Up @@ -87,7 +88,18 @@ async function getRemoteEmbedding(input: string, options: EmbeddingOptions): Pro
export async function embed(runtime: IAgentRuntime, input: string) {
const modelProvider = models[runtime.character.modelProvider];
//need to have env override for this to select what to use for embedding if provider doesnt provide or using openai
const embeddingModel = modelProvider.model.embedding;
const embeddingModel = (
settings.USE_OPENAI_EMBEDDING ? "text-embedding-3-small" : // Use OpenAI if specified
modelProvider.model?.[ModelClass.EMBEDDING] || // Use provider's embedding model if available
models[ModelProviderName.OPENAI].model[ModelClass.EMBEDDING] // Fallback to OpenAI
);

if (!embeddingModel) {
throw new Error('No embedding model configured');
}

console.log("embeddingModel", embeddingModel);


// Try local embedding first
if (
Expand All @@ -107,16 +119,17 @@ export async function embed(runtime: IAgentRuntime, input: string) {
// Get remote embedding
return await getRemoteEmbedding(input, {
model: embeddingModel,
endpoint: settings.USE_OPENAI_EMBEDDING ?
endpoint: settings.USE_OPENAI_EMBEDDING ?
'https://api.openai.com/v1' : // Always use OpenAI endpoint when USE_OPENAI_EMBEDDING is true
(runtime.character.modelEndpointOverride || modelProvider.endpoint),
apiKey: settings.USE_OPENAI_EMBEDDING ?
apiKey: settings.USE_OPENAI_EMBEDDING ?
settings.OPENAI_API_KEY : // Use OpenAI key from settings when USE_OPENAI_EMBEDDING is true
runtime.token, // Use runtime token for other providers
isOllama: runtime.character.modelProvider === ModelProviderName.OLLAMA && !settings.USE_OPENAI_EMBEDDING
});
}


async function getLocalEmbedding(input: string): Promise<number[]> {
const cacheDir = getRootPath() + "/cache/";
if (!fs.existsSync(cacheDir)) {
Expand All @@ -137,13 +150,13 @@ export async function retrieveCachedEmbedding(
runtime: IAgentRuntime,
input: string
) {
if(!input) {
if (!input) {
console.log("No input to retrieve cached embedding for");
return null;
}
const similaritySearchResult = [];
// await runtime.messageManager.getCachedEmbeddings(input);

const similaritySearchResult =
await runtime.messageManager.getCachedEmbeddings(input);
if (similaritySearchResult.length > 0) {
return similaritySearchResult[0].embedding;
}
Expand Down
Loading