Skip to content

Commit

Permalink
fix lateral syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-XT committed Jan 15, 2025
1 parent fbd683e commit 3a3470e
Showing 1 changed file with 10 additions and 22 deletions.
32 changes: 10 additions & 22 deletions agixt/Memories.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,19 +730,18 @@ async def get_memories_data(

try:
if DATABASE_TYPE == "postgresql":
# Cast the input array to vector directly in the SQL
# Simplified query without LATERAL join
stmt = text(
"""
WITH vector_matches AS (
SELECT
m.*,
1 - (embedding <=> array_to_vector(:embedding)) as similarity
FROM memory m,
LATERAL (SELECT :embedding::float[] AS query_embedding) q
WHERE m.agent_id = :agent_id
AND (m.conversation_id = :conversation_id OR m.conversation_id IS NULL)
1 - (m.embedding <=> %(embedding)s::vector) as similarity
FROM memory m
WHERE m.agent_id = %(agent_id)s
AND (m.conversation_id = %(conversation_id)s OR m.conversation_id IS NULL)
ORDER BY similarity DESC
LIMIT :limit
LIMIT %(limit)s
)
SELECT
id,
Expand All @@ -754,24 +753,10 @@ async def get_memories_data(
timestamp,
similarity
FROM vector_matches
WHERE similarity >= :min_score;
WHERE similarity >= %(min_score)s;
"""
)

# Create function to convert array to vector if it doesn't exist
session.execute(
text(
"""
CREATE OR REPLACE FUNCTION array_to_vector(float[])
RETURNS vector
AS $$ SELECT $1::vector $$
LANGUAGE SQL
IMMUTABLE;
"""
)
)
session.commit()

results = session.execute(
stmt,
{
Expand Down Expand Up @@ -858,6 +843,9 @@ async def get_memories_data(
logging.warning(
f"Vector search failed, falling back to basic search: {e}"
)
# Rollback failed transaction before trying fallback
session.rollback()

# Fallback to basic search
results = (
session.query(Memory)
Expand Down

0 comments on commit 3a3470e

Please sign in to comment.