ai16z / eliza

Conversational Agent for Twitter and Discord
https://ai16z.github.io/eliza/
MIT License
716 stars 215 forks source link

getCachedEmbeddings broken for sqlite adapter #251

Open boldkoala4615 opened 3 days ago

boldkoala4615 commented 3 days ago

Describe the bug

The getCachedEmbeddings for the sqlite adapter is flawed with the recent introduction of vec_distance_L2.

packages/adapter-sqlite/src/index.ts:226
const memories = this.db.prepare(sql).all(opts.query_table_name, new Float32Array(opts.query_input.split(",").map(Number)), // Convert string to Float32Array
                                              ^
RangeError: Too few parameter values were provided
  1. The sql query expects 5 values, but only 4 are passed. query_match_count should be used for LIMIT
  2. The creation of the float array seems off. The query_input does not include any comma separated values. The resulting array is [NaN]
  3. Distance comparison is between a float array and a string. It should probably be compared to query_threshold, not query_input

Related function is here: https://github.com/ai16z/eliza/blob/15f7ba88d04d9d7d1cf0f608762fa84da06cb313/packages/adapter-sqlite/src/index.ts#L332C11-L360

I'm happy to contribute if someone can guide me. I have little clue about vec_distance and didn't manage to infer what the intention of the change was exactly.

To Reproduce

pnpm start --characters="characters/custom.character.json" and type something

Expected behavior

No sqlite errors

Sid31 commented 2 days ago

Similar issue here using ollama local ` Creating runtime for character Eliza Agent ID b850bc30-45f8-0041-a00a-83df46d8555d ["✓ Registering action: CONTINUE"]

["✓ Registering action: FOLLOW_ROOM"]

["✓ Registering action: UNFOLLOW_ROOM"]

["✓ Registering action: IGNORE"]

["✓ Registering action: NONE"]

["✓ Registering action: MUTE_ROOM"]

["✓ Registering action: UNMUTE_ROOM"]

Registering service: browser Registering service: image_description Registering service: text_generation Registering service: pdf Registering service: speech_generation Registering service: transcription Registering service: video Chat started. Type 'exit' to quit. You: Server running at http://localhost:3000/ hi hi DirectClient message sql SELECT * FROM memories WHERE type = ? AND vec_distance_L2(content, ?) <= ? ORDER BY vec_distance_L2(content, ?) ASC LIMIT ?

opts.query_input (just now) [94959] User12dea96f-ec20-0935-a6ab-75692c994959: hi (4 minutes ago) [94959] User12dea96f-ec20-0935-a6ab-75692c994959: hey\ (9 minutes ago) [94959] User12dea96f-ec20-0935-a6ab-75692c994959: hi (15 minutes ago) [94959] User12dea96f-ec20-0935-a6ab-75692c994959: w (9 hours ago) [94959] User12dea96f-ec20-0935-a6ab-75692c994959: hy (9 hours ago) [94959] User12dea96f-ec20-0935-a6ab-75692c994959: s (9 hours ago) [94959] User12dea96f-ec20-0935-a6ab-75692c994959: (9 hours ago) [94959] User12dea96f-ec20-0935-a6ab-75692c994959: (9 hours ago) [94959] User12dea96f-ec20-0935-a6ab-75692c994959: hi (10 hours ago) [94959] User12dea96f-ec20-0935-a6ab-75692c994959: hey file:///home/sid/Documents/sid/eliza/packages/adapter-sqlite/src/index.ts:226 const memories = this.db.prepare(sql).all(opts.query_table_name, new Float32Array(opts.query_input.split(",").map(Number)), // Convert string to Float32Array ^

RangeError: Too few parameter values were provided at SqliteDatabaseAdapter.getCachedEmbeddings (file:///home/sid/Documents/sid/eliza/packages/adapter-sqlite/src/index.ts:226:47) at MemoryManager.getCachedEmbeddings (file:///home/sid/Documents/sid/eliza/packages/core/src/memory.ts:66:59) at retrieveCachedEmbedding (file:///home/sid/Documents/sid/eliza/packages/core/src/embedding.ts:95:65) at embed (file:///home/sid/Documents/sid/eliza/packages/core/src/embedding.ts:52:35) at Object.get (file:///home/sid/Documents/sid/eliza/packages/plugin-bootstrap/src/providers/facts.ts:12:33) at file:///home/sid/Documents/sid/eliza/packages/core/src/providers.ts:10:31 at Array.map () at getProviders (file:///home/sid/Documents/sid/eliza/packages/core/src/providers.ts:9:65) at AgentRuntime.composeState (file:///home/sid/Documents/sid/eliza/packages/core/src/runtime.ts:749:13) at process.processTicksAndRejections (node:internal/process/task_queues:105:5)

Node.js v23.1.0 [nodemon] app crashed - waiting for file changes before starting... ^Csid@pop-os:~/Documents/sid/eliza$ `

sirkitree commented 2 days ago

similar error here:

(2 hours ago) [0bf50] sirkitree: hi <@1300173507702816888>
Error handling message: RangeError: Too few parameter values were provided
    at SqliteDatabaseAdapter.getCachedEmbeddings (file:///C:/Users/sirki/repos/eliza/packages/adapter-sqlite/src/index.ts:226:47)
    at MemoryManager.getCachedEmbeddings (file:///C:/Users/sirki/repos/eliza/packages/core/src/memory.ts:66:59)
    at retrieveCachedEmbedding (file:///C:/Users/sirki/repos/eliza/packages/core/src/embedding.ts:94:65)
    at embed (file:///C:/Users/sirki/repos/eliza/packages/core/src/embedding.ts:64:35)
    at Object.get (file:///C:/Users/sirki/repos/eliza/packages/plugin-bootstrap/src/providers/facts.ts:12:33)
    at file:///C:/Users/sirki/repos/eliza/packages/core/src/providers.ts:10:31
    at Array.map (<anonymous>)
    at getProviders (file:///C:/Users/sirki/repos/eliza/packages/core/src/providers.ts:9:65)
    at AgentRuntime.composeState (file:///C:/Users/sirki/repos/eliza/packages/core/src/runtime.ts:749:13)
    at process.processTicksAndRejections (node:internal/process/task_queues:105:5)