Skip to content
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 7.8.1 - 2026-02-03

fix(llma): small fixes for prompt management

# 7.8.0 - 2026-01-28

feat(llma): add prompt management
Expand Down
17 changes: 9 additions & 8 deletions posthog/ai/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
import urllib.parse
from typing import Any, Dict, Optional, Union

from posthog.request import DEFAULT_HOST, USER_AGENT, _get_session
from posthog.request import USER_AGENT, _get_session
from posthog.utils import remove_trailing_slash

log = logging.getLogger("posthog")

APP_ENDPOINT = "https://app.posthog.com"
DEFAULT_CACHE_TTL_SECONDS = 300 # 5 minutes

PromptVariables = Dict[str, Union[str, int, float, bool]]
Expand Down Expand Up @@ -49,11 +50,11 @@ class Prompts:
from posthog.ai.prompts import Prompts

# With PostHog client
posthog = Posthog('phc_xxx', host='https://us.i.posthog.com', personal_api_key='phx_xxx')
posthog = Posthog('phc_xxx', host='https://app.posthog.com', personal_api_key='phx_xxx')
prompts = Prompts(posthog)

# Or with direct options (no PostHog client needed)
prompts = Prompts(personal_api_key='phx_xxx', host='https://us.i.posthog.com')
prompts = Prompts(personal_api_key='phx_xxx', host='https://app.posthog.com')

# Fetch with caching and fallback
template = prompts.get('support-system-prompt', fallback='You are a helpful assistant.')
Expand All @@ -80,7 +81,7 @@ def __init__(
Args:
posthog: PostHog client instance (optional if personal_api_key provided)
personal_api_key: Direct API key (optional if posthog provided)
host: PostHog host (defaults to US ingestion endpoint)
host: PostHog host (defaults to app endpoint)
default_cache_ttl_seconds: Default cache TTL (defaults to 300)
"""
self._default_cache_ttl_seconds = (
Expand All @@ -91,11 +92,11 @@ def __init__(
if posthog is not None:
self._personal_api_key = getattr(posthog, "personal_api_key", None) or ""
self._host = remove_trailing_slash(
getattr(posthog, "raw_host", None) or DEFAULT_HOST
getattr(posthog, "raw_host", None) or APP_ENDPOINT
)
else:
self._personal_api_key = personal_api_key or ""
self._host = remove_trailing_slash(host or DEFAULT_HOST)
self._host = remove_trailing_slash(host or APP_ENDPOINT)

def get(
self,
Expand Down Expand Up @@ -214,7 +215,7 @@ def _fetch_prompt_from_api(self, name: str) -> str:
"""
Fetch prompt from PostHog API.

Endpoint: {host}/api/projects/@current/llm_prompts/name/{encoded_name}/
Endpoint: {host}/api/environments/@current/llm_prompts/name/{encoded_name}/
Auth: Bearer {personal_api_key}

Args:
Expand All @@ -233,7 +234,7 @@ def _fetch_prompt_from_api(self, name: str) -> str:
)

encoded_name = urllib.parse.quote(name, safe="")
url = f"{self._host}/api/projects/@current/llm_prompts/name/{encoded_name}/"
url = f"{self._host}/api/environments/@current/llm_prompts/name/{encoded_name}/"

headers = {
"Authorization": f"Bearer {self._personal_api_key}",
Expand Down
12 changes: 6 additions & 6 deletions posthog/test/ai/test_prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class TestPrompts(unittest.TestCase):
}

def create_mock_posthog(
self, personal_api_key="phx_test_key", host="https://us.i.posthog.com"
self, personal_api_key="phx_test_key", host="https://app.posthog.com"
):
"""Create a mock PostHog client."""
mock = MagicMock()
Expand Down Expand Up @@ -61,7 +61,7 @@ def test_successfully_fetch_a_prompt(self, mock_get_session):
call_args = mock_get.call_args
self.assertEqual(
call_args[0][0],
"https://us.i.posthog.com/api/projects/@current/llm_prompts/name/test-prompt/",
"https://app.posthog.com/api/environments/@current/llm_prompts/name/test-prompt/",
)
self.assertIn("Authorization", call_args[1]["headers"])
self.assertEqual(
Expand Down Expand Up @@ -333,7 +333,7 @@ def test_url_encode_prompt_names_with_special_characters(self, mock_get_session)
call_args = mock_get.call_args
self.assertEqual(
call_args[0][0],
"https://us.i.posthog.com/api/projects/@current/llm_prompts/name/prompt%20with%20spaces%2Fand%2Fslashes/",
"https://app.posthog.com/api/environments/@current/llm_prompts/name/prompt%20with%20spaces%2Fand%2Fslashes/",
)

@patch("posthog.ai.prompts._get_session")
Expand All @@ -350,7 +350,7 @@ def test_work_with_direct_options_no_posthog_client(self, mock_get_session):
call_args = mock_get.call_args
self.assertEqual(
call_args[0][0],
"https://us.i.posthog.com/api/projects/@current/llm_prompts/name/test-prompt/",
"https://app.posthog.com/api/environments/@current/llm_prompts/name/test-prompt/",
)
self.assertEqual(
call_args[1]["headers"]["Authorization"], "Bearer phx_direct_key"
Expand All @@ -363,15 +363,15 @@ def test_use_custom_host_from_direct_options(self, mock_get_session):
mock_get.return_value = MockResponse(json_data=self.mock_prompt_response)

prompts = Prompts(
personal_api_key="phx_direct_key", host="https://eu.i.posthog.com"
personal_api_key="phx_direct_key", host="https://eu.posthog.com"
)

prompts.get("test-prompt")

call_args = mock_get.call_args
self.assertEqual(
call_args[0][0],
"https://eu.i.posthog.com/api/projects/@current/llm_prompts/name/test-prompt/",
"https://eu.posthog.com/api/environments/@current/llm_prompts/name/test-prompt/",
)

@patch("posthog.ai.prompts._get_session")
Expand Down
2 changes: 1 addition & 1 deletion posthog/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = "7.8.0"
VERSION = "7.8.1"

if __name__ == "__main__":
print(VERSION, end="") # noqa: T201
Loading