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

feat: Embedded Postgresql (#2134) #2137

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions .github/workflows/test_ollama.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ jobs:
poetry-version: "1.8.2"
install-args: "-E dev -E ollama"

- name: Migrate database
run: poetry run alembic upgrade head

- name: Test LLM Endpoint
run: |
set -e
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/test_openai.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ jobs:
poetry-version: "1.8.2"
install-args: "-E dev -E external-tools"

- name: Migrate database
run: poetry run alembic upgrade head

- name: Test first message contains expected function call and inner monologue
id: test_first_message
env:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ __pycache__/
develop-eggs/
downloads/
eggs#letta/letta-server:0.3.7
installable_apps/.eggs
MANIFEST

# PyInstaller
Expand Down
1 change: 1 addition & 0 deletions letta/server/generate_openapi_schema.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ then
fi

# generate OpenAPI schema
poetry run alembic upgrade head
poetry run python -c 'from letta.server.rest_api.app import app, generate_openapi_schema; generate_openapi_schema(app);'
5 changes: 5 additions & 0 deletions letta/server/rest_api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,12 @@ def on_startup():

# Tool.load_default_tools(get_db_session())

import alembic.config

alembic.config.main(["upgrade", "head"])

generate_openapi_schema(app)
# always migrate now that we have a default in-memory postgres

@app.on_event("shutdown")
def on_shutdown():
Expand Down
26 changes: 17 additions & 9 deletions letta/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from pydantic import Field
from pydantic_settings import BaseSettings, SettingsConfigDict
import pgserver

from letta.local_llm.constants import DEFAULT_WRAPPER_NAME

Expand Down Expand Up @@ -86,18 +87,25 @@ def letta_pg_uri(self) -> str:
elif self.pg_db and self.pg_user and self.pg_password and self.pg_host and self.pg_port:
return f"postgresql+pg8000://{self.pg_user}:{self.pg_password}@{self.pg_host}:{self.pg_port}/{self.pg_db}"
else:
return f"postgresql+pg8000://letta:letta@localhost:5432/letta"
# start the pg binary. This is the default in-memory postgres that replaces SQLite/Chroma
self.pg_uri = self.launch_pg_binary()
return self.pg_uri

# add this property to avoid being returned the default
# reference: https://github.com/cpacker/Letta/issues/1362
@property
def letta_pg_uri_no_default(self) -> str:
if self.pg_uri:
return self.pg_uri
elif self.pg_db and self.pg_user and self.pg_password and self.pg_host and self.pg_port:
return f"postgresql+pg8000://{self.pg_user}:{self.pg_password}@{self.pg_host}:{self.pg_port}/{self.pg_db}"
else:
return None
"""DEPRECATED: now that we have a default in-memory postgres, this is the same as letta_pg_uri"""
return self.letta_pg_uri

def launch_pg_binary(self) -> "str":
pgdata = settings.letta_dir / "pgdata"
pgdata.mkdir(parents=True, exist_ok=True)

database = pgserver.get_server(pgdata)

# create pg vector extension
database.psql("CREATE EXTENSION IF NOT EXISTS vector")

return database.get_uri()


class TestSettings(Settings):
Expand Down
2,151 changes: 1,076 additions & 1,075 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ black = {extras = ["jupyter"], version = "^24.2.0"}
setuptools = "^68.2.2"
datasets = { version = "^2.14.6", optional = true}
prettytable = "^3.9.0"
pgvector = { version = "^0.2.3", optional = true }
#pgvector = { version = "^0.2.3", optional = true }
pgvector = "^0.2.3"
pre-commit = {version = "^3.5.0", optional = true }
pg8000 = {version = "^1.30.3", optional = true}
websockets = {version = "^12.0", optional = true}
Expand Down Expand Up @@ -72,6 +73,7 @@ alembic = "^1.13.3"
pyhumps = "^3.8.0"
psycopg2 = "^2.9.10"
psycopg2-binary = "^2.9.10"
pgserver = "^0.1.4"
e2b-code-interpreter = {version = "^1.0.1", optional = true}
pathvalidate = "^3.2.1"
langchain-community = {version = "^0.3.7", optional = true}
Expand Down
Loading