hindsight-api 0.0.21__py3-none-any.whl → 0.1.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. hindsight_api/__init__.py +10 -2
  2. hindsight_api/alembic/README +1 -0
  3. hindsight_api/alembic/env.py +146 -0
  4. hindsight_api/alembic/script.py.mako +28 -0
  5. hindsight_api/alembic/versions/5a366d414dce_initial_schema.py +274 -0
  6. hindsight_api/alembic/versions/b7c4d8e9f1a2_add_chunks_table.py +70 -0
  7. hindsight_api/alembic/versions/c8e5f2a3b4d1_add_retain_params_to_documents.py +39 -0
  8. hindsight_api/alembic/versions/d9f6a3b4c5e2_rename_bank_to_interactions.py +48 -0
  9. hindsight_api/alembic/versions/e0a1b2c3d4e5_disposition_to_3_traits.py +62 -0
  10. hindsight_api/alembic/versions/rename_personality_to_disposition.py +65 -0
  11. hindsight_api/api/__init__.py +2 -4
  12. hindsight_api/api/http.py +112 -164
  13. hindsight_api/api/mcp.py +2 -1
  14. hindsight_api/config.py +154 -0
  15. hindsight_api/engine/__init__.py +7 -2
  16. hindsight_api/engine/cross_encoder.py +225 -16
  17. hindsight_api/engine/embeddings.py +198 -19
  18. hindsight_api/engine/entity_resolver.py +56 -29
  19. hindsight_api/engine/llm_wrapper.py +147 -106
  20. hindsight_api/engine/memory_engine.py +337 -192
  21. hindsight_api/engine/response_models.py +15 -17
  22. hindsight_api/engine/retain/bank_utils.py +25 -35
  23. hindsight_api/engine/retain/entity_processing.py +5 -5
  24. hindsight_api/engine/retain/fact_extraction.py +86 -24
  25. hindsight_api/engine/retain/fact_storage.py +1 -1
  26. hindsight_api/engine/retain/link_creation.py +12 -6
  27. hindsight_api/engine/retain/link_utils.py +50 -56
  28. hindsight_api/engine/retain/observation_regeneration.py +264 -0
  29. hindsight_api/engine/retain/orchestrator.py +31 -44
  30. hindsight_api/engine/retain/types.py +14 -0
  31. hindsight_api/engine/search/reranking.py +6 -10
  32. hindsight_api/engine/search/retrieval.py +2 -2
  33. hindsight_api/engine/search/think_utils.py +59 -30
  34. hindsight_api/engine/search/tracer.py +1 -1
  35. hindsight_api/main.py +201 -0
  36. hindsight_api/migrations.py +61 -39
  37. hindsight_api/models.py +1 -2
  38. hindsight_api/pg0.py +17 -36
  39. hindsight_api/server.py +43 -0
  40. {hindsight_api-0.0.21.dist-info → hindsight_api-0.1.1.dist-info}/METADATA +2 -3
  41. hindsight_api-0.1.1.dist-info/RECORD +60 -0
  42. hindsight_api-0.1.1.dist-info/entry_points.txt +2 -0
  43. hindsight_api/cli.py +0 -128
  44. hindsight_api/web/__init__.py +0 -12
  45. hindsight_api/web/server.py +0 -109
  46. hindsight_api-0.0.21.dist-info/RECORD +0 -50
  47. hindsight_api-0.0.21.dist-info/entry_points.txt +0 -2
  48. {hindsight_api-0.0.21.dist-info → hindsight_api-0.1.1.dist-info}/WHEEL +0 -0
@@ -0,0 +1,62 @@
1
+ """disposition_to_3_traits
2
+
3
+ Revision ID: e0a1b2c3d4e5
4
+ Revises: rename_personality
5
+ Create Date: 2024-12-08
6
+
7
+ Migrate disposition traits from Big Five (openness, conscientiousness, extraversion,
8
+ agreeableness, neuroticism, bias_strength with 0-1 float values) to the new 3-trait
9
+ system (skepticism, literalism, empathy with 1-5 integer values).
10
+ """
11
+ from typing import Sequence, Union
12
+
13
+ from alembic import op
14
+ import sqlalchemy as sa
15
+
16
+
17
+ # revision identifiers, used by Alembic.
18
+ revision: str = 'e0a1b2c3d4e5'
19
+ down_revision: Union[str, Sequence[str], None] = 'rename_personality'
20
+ branch_labels: Union[str, Sequence[str], None] = None
21
+ depends_on: Union[str, Sequence[str], None] = None
22
+
23
+
24
+ def upgrade() -> None:
25
+ """Convert Big Five disposition to 3-trait disposition."""
26
+ conn = op.get_bind()
27
+
28
+ # Update all existing banks to use the new disposition format
29
+ # Convert from old format to new format with reasonable mappings:
30
+ # - skepticism: derived from inverse of agreeableness (skeptical people are less agreeable)
31
+ # - literalism: derived from conscientiousness (detail-oriented people are more literal)
32
+ # - empathy: derived from agreeableness + inverse of neuroticism
33
+ # Default all to 3 (neutral) for simplicity
34
+ conn.execute(sa.text("""
35
+ UPDATE banks
36
+ SET disposition = '{"skepticism": 3, "literalism": 3, "empathy": 3}'::jsonb
37
+ WHERE disposition IS NOT NULL
38
+ """))
39
+
40
+ # Update the default for new banks
41
+ conn.execute(sa.text("""
42
+ ALTER TABLE banks
43
+ ALTER COLUMN disposition SET DEFAULT '{"skepticism": 3, "literalism": 3, "empathy": 3}'::jsonb
44
+ """))
45
+
46
+
47
+ def downgrade() -> None:
48
+ """Convert back to Big Five disposition."""
49
+ conn = op.get_bind()
50
+
51
+ # Revert to Big Five format with default values
52
+ conn.execute(sa.text("""
53
+ UPDATE banks
54
+ SET disposition = '{"openness": 0.5, "conscientiousness": 0.5, "extraversion": 0.5, "agreeableness": 0.5, "neuroticism": 0.5, "bias_strength": 0.5}'::jsonb
55
+ WHERE disposition IS NOT NULL
56
+ """))
57
+
58
+ # Update the default for new banks
59
+ conn.execute(sa.text("""
60
+ ALTER TABLE banks
61
+ ALTER COLUMN disposition SET DEFAULT '{"openness": 0.5, "conscientiousness": 0.5, "extraversion": 0.5, "agreeableness": 0.5, "neuroticism": 0.5, "bias_strength": 0.5}'::jsonb
62
+ """))
@@ -0,0 +1,65 @@
1
+ """rename_personality_to_disposition
2
+
3
+ Revision ID: rename_personality
4
+ Revises: d9f6a3b4c5e2
5
+ Create Date: 2024-12-04
6
+
7
+ """
8
+ from typing import Sequence, Union
9
+
10
+ from alembic import op
11
+ import sqlalchemy as sa
12
+ from sqlalchemy.dialects import postgresql
13
+
14
+
15
+ # revision identifiers, used by Alembic.
16
+ revision: str = 'rename_personality'
17
+ down_revision: Union[str, Sequence[str], None] = 'd9f6a3b4c5e2'
18
+ branch_labels: Union[str, Sequence[str], None] = None
19
+ depends_on: Union[str, Sequence[str], None] = None
20
+
21
+
22
+ def upgrade() -> None:
23
+ """Rename personality column to disposition in banks table (if it exists)."""
24
+ conn = op.get_bind()
25
+
26
+ # Check if 'personality' column exists (old database)
27
+ result = conn.execute(sa.text("""
28
+ SELECT column_name
29
+ FROM information_schema.columns
30
+ WHERE table_name = 'banks' AND column_name = 'personality'
31
+ """))
32
+ has_personality = result.fetchone() is not None
33
+
34
+ # Check if 'disposition' column exists (new database)
35
+ result = conn.execute(sa.text("""
36
+ SELECT column_name
37
+ FROM information_schema.columns
38
+ WHERE table_name = 'banks' AND column_name = 'disposition'
39
+ """))
40
+ has_disposition = result.fetchone() is not None
41
+
42
+ if has_personality and not has_disposition:
43
+ # Old database: rename personality -> disposition
44
+ op.alter_column('banks', 'personality', new_column_name='disposition')
45
+ elif not has_personality and not has_disposition:
46
+ # Neither exists (shouldn't happen, but be safe): add disposition column
47
+ op.add_column('banks', sa.Column(
48
+ 'disposition',
49
+ postgresql.JSONB(astext_type=sa.Text()),
50
+ server_default=sa.text("'{}'::jsonb"),
51
+ nullable=False
52
+ ))
53
+ # else: disposition already exists, nothing to do
54
+
55
+
56
+ def downgrade() -> None:
57
+ """Revert disposition column back to personality."""
58
+ conn = op.get_bind()
59
+ result = conn.execute(sa.text("""
60
+ SELECT column_name
61
+ FROM information_schema.columns
62
+ WHERE table_name = 'banks' AND column_name = 'disposition'
63
+ """))
64
+ if result.fetchone():
65
+ op.alter_column('banks', 'disposition', new_column_name='personality')
@@ -17,18 +17,17 @@ def create_app(
17
17
  http_api_enabled: bool = True,
18
18
  mcp_api_enabled: bool = False,
19
19
  mcp_mount_path: str = "/mcp",
20
- run_migrations: bool = True,
21
20
  initialize_memory: bool = True
22
21
  ) -> FastAPI:
23
22
  """
24
23
  Create and configure the unified Hindsight API application.
25
24
 
26
25
  Args:
27
- memory: MemoryEngine instance (already initialized with required parameters)
26
+ memory: MemoryEngine instance (already initialized with required parameters).
27
+ Migrations are controlled by the MemoryEngine's run_migrations parameter.
28
28
  http_api_enabled: Whether to enable HTTP REST API endpoints (default: True)
29
29
  mcp_api_enabled: Whether to enable MCP server (default: False)
30
30
  mcp_mount_path: Path to mount MCP server (default: /mcp)
31
- run_migrations: Whether to run database migrations on startup (default: True)
32
31
  initialize_memory: Whether to initialize memory system on startup (default: True)
33
32
 
34
33
  Returns:
@@ -50,7 +49,6 @@ def create_app(
50
49
  from .http import create_app as create_http_app
51
50
  app = create_http_app(
52
51
  memory=memory,
53
- run_migrations=run_migrations,
54
52
  initialize_memory=initialize_memory
55
53
  )
56
54
  logger.info("HTTP REST API enabled")