fix(llma): small fixes for prompt management (#420)

* fix(llma): small fixes for prompt management

* fix(llma): tests

* fix(llma): tests
This commit is contained in:
Radu Raicea
2026-02-04 09:49:19 +02:00
committed by GitHub
parent 72f448816c
commit d4f2d6dfb0
4 changed files with 20 additions and 15 deletions
+4
View File
@@ -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
+9 -8
View File
@@ -10,11 +10,12 @@ import time
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]]
@@ -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.')
@@ -80,7 +81,7 @@ class Prompts:
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 = (
@@ -91,11 +92,11 @@ class Prompts:
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,
@@ -214,7 +215,7 @@ class Prompts:
"""
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:
@@ -233,7 +234,7 @@ class Prompts:
)
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}",
+6 -6
View File
@@ -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()
@@ -61,7 +61,7 @@ class TestPromptsGet(TestPrompts):
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(
@@ -333,7 +333,7 @@ class TestPromptsGet(TestPrompts):
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")
@@ -350,7 +350,7 @@ class TestPromptsGet(TestPrompts):
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"
@@ -363,7 +363,7 @@ class TestPromptsGet(TestPrompts):
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")
@@ -371,7 +371,7 @@ class TestPromptsGet(TestPrompts):
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")
+1 -1
View File
@@ -1,4 +1,4 @@
VERSION = "7.8.0"
VERSION = "7.8.1"
if __name__ == "__main__":
print(VERSION, end="") # noqa: T201