Skip to content

Commit a25a4be

Browse files
committed
Merge branch 'master' into feat/quota-limited-flags
2 parents f8851e5 + 31652d5 commit a25a4be

File tree

7 files changed

+60
-2
lines changed

7 files changed

+60
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2+
## 3.14.1 - 2025-02-18
3+
4+
1. Add support for Bedrock Anthropic Usage
5+
16
## 3.13.0 - 2025-02-12
27

38
1. Automatically retry connection errors

example.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
"distinct_id_random_22", person_properties={"$geoip_city_name": "Sydney"}, only_evaluate_locally=True
102102
)
103103
)
104+
print(posthog.get_remote_config_payload("encrypted_payload_flag_key"))
104105

105106

106107
posthog.shutdown()

posthog/__init__.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,26 @@ def get_feature_flag_payload(
492492
)
493493

494494

495+
def get_remote_config_payload(
496+
key, # type: str
497+
):
498+
"""Get the payload for a remote config feature flag.
499+
500+
Args:
501+
key: The key of the feature flag
502+
503+
Returns:
504+
The payload associated with the feature flag. If payload is encrypted, the return value will decrypted
505+
506+
Note:
507+
Requires personal_api_key to be set for authentication
508+
"""
509+
return _proxy(
510+
"get_remote_config_payload",
511+
key=key,
512+
)
513+
514+
495515
def get_all_flags_and_payloads(
496516
distinct_id,
497517
groups={},

posthog/ai/langchain/callbacks.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,9 @@ def _parse_usage_model(
597597
# Bedrock: https://docs.aws.amazon.com/bedrock/latest/userguide/monitoring-cw.html#runtime-cloudwatch-metrics
598598
("inputTokenCount", "input"),
599599
("outputTokenCount", "output"),
600+
# Bedrock Anthropic
601+
("prompt_tokens", "input"),
602+
("completion_tokens", "output"),
600603
# langchain-ibm https://pypi.org/project/langchain-ibm/
601604
("input_token_count", "input"),
602605
("generated_token_count", "output"),
@@ -627,6 +630,10 @@ def _parse_usage(response: LLMResult):
627630

628631
if hasattr(response, "generations"):
629632
for generation in response.generations:
633+
if "usage" in generation:
634+
llm_usage = _parse_usage_model(generation["usage"])
635+
break
636+
630637
for generation_chunk in generation:
631638
if generation_chunk.generation_info and ("usage_metadata" in generation_chunk.generation_info):
632639
llm_usage = _parse_usage_model(generation_chunk.generation_info["usage_metadata"])

posthog/client.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from posthog.exception_utils import exc_info_from_error, exceptions_from_error_tuple, handle_in_app
1616
from posthog.feature_flags import InconclusiveMatchError, match_feature_flag_properties
1717
from posthog.poller import Poller
18-
from posthog.request import DEFAULT_HOST, APIError, batch_post, decide, determine_server_host, get
18+
from posthog.request import DEFAULT_HOST, APIError, batch_post, decide, determine_server_host, get, remote_config
1919
from posthog.utils import SizeLimitedDict, clean, guess_timezone, remove_trailing_slash
2020
from posthog.version import VERSION
2121

@@ -856,6 +856,26 @@ def get_feature_flag_payload(
856856

857857
return payload
858858

859+
def get_remote_config_payload(self, key: str):
860+
if self.disabled:
861+
return None
862+
863+
if self.personal_api_key is None:
864+
self.log.warning(
865+
"[FEATURE FLAGS] You have to specify a personal_api_key to fetch decrypted feature flag payloads."
866+
)
867+
return None
868+
869+
try:
870+
return remote_config(
871+
self.personal_api_key,
872+
self.host,
873+
key,
874+
timeout=self.feature_flags_request_timeout_seconds,
875+
)
876+
except Exception as e:
877+
self.log.exception(f"[FEATURE FLAGS] Unable to get decrypted feature flag payload: {e}")
878+
859879
def _compute_payload_locally(self, key, match_value):
860880
payload = None
861881

posthog/request.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ def decide(api_key: str, host: Optional[str] = None, gzip: bool = False, timeout
8888
return _process_response(res, success_message="Feature flags decided successfully")
8989

9090

91+
def remote_config(personal_api_key: str, host: Optional[str] = None, key: str = "", timeout: int = 15) -> Any:
92+
"""Get remote config flag value from remote_config API endpoint"""
93+
return get(personal_api_key, f"/api/projects/@current/feature_flags/{key}/remote_config/", host, timeout)
94+
95+
9196
def batch_post(
9297
api_key: str, host: Optional[str] = None, gzip: bool = False, timeout: int = 15, **kwargs
9398
) -> requests.Response:

posthog/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION = "3.13.0"
1+
VERSION = "3.14.1"
22

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

0 commit comments

Comments
 (0)