Skip to content

Commit fff0a3f

Browse files
committed
feat: base url retrieval
1 parent 739c88a commit fff0a3f

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

posthog/ai/langchain/callbacks.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class RunMetadata(TypedDict, total=False):
3434
provider: str
3535
model: str
3636
model_params: Dict[str, Any]
37+
base_url: str
3738
start_time: float
3839
end_time: float
3940

@@ -105,7 +106,7 @@ def on_chat_model_start(
105106
):
106107
self._set_parent_of_run(run_id, parent_run_id)
107108
input = [_convert_message_to_dict(message) for row in messages for message in row]
108-
self._set_run_metadata(run_id, input, **kwargs)
109+
self._set_run_metadata(serialized, run_id, input, **kwargs)
109110

110111
def on_llm_start(
111112
self,
@@ -117,7 +118,7 @@ def on_llm_start(
117118
**kwargs: Any,
118119
):
119120
self._set_parent_of_run(run_id, parent_run_id)
120-
self._set_run_metadata(run_id, prompts, **kwargs)
121+
self._set_run_metadata(serialized, run_id, prompts, **kwargs)
121122

122123
def on_chain_end(
123124
self,
@@ -171,6 +172,7 @@ def on_llm_end(
171172
"$ai_latency": latency,
172173
"$ai_trace_id": trace_id,
173174
"$ai_posthog_properties": self._properties,
175+
"$ai_base_url": run.get("base_url"),
174176
}
175177
if self._distinct_id is None:
176178
event_properties["$process_person_profile"] = False
@@ -215,6 +217,7 @@ def on_llm_error(
215217
"$ai_latency": latency,
216218
"$ai_trace_id": trace_id,
217219
"$ai_posthog_properties": self._properties,
220+
"$ai_base_url": run.get("base_url"),
218221
}
219222
if self._distinct_id is None:
220223
event_properties["$process_person_profile"] = False
@@ -251,6 +254,7 @@ def _find_root_run(self, run_id: UUID) -> UUID:
251254

252255
def _set_run_metadata(
253256
self,
257+
serialized: Dict[str, Any],
254258
run_id: UUID,
255259
messages: Union[List[Dict[str, Any]], List[str]],
256260
metadata: Optional[Dict[str, Any]] = None,
@@ -268,6 +272,12 @@ def _set_run_metadata(
268272
run["model"] = model
269273
if provider := metadata.get("ls_provider"):
270274
run["provider"] = provider
275+
try:
276+
base_url = serialized["kwargs"]["openai_api_base"]
277+
if base_url is not None:
278+
run["base_url"] = base_url
279+
except KeyError:
280+
pass
271281
self._runs[run_id] = run
272282

273283
def _pop_run_metadata(self, run_id: UUID) -> Optional[RunMetadata]:

posthog/test/ai/langchain/test_callbacks.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def test_metadata_capture(mock_client):
6161
run_id = uuid.uuid4()
6262
with patch("time.time", return_value=1234567890):
6363
callbacks._set_run_metadata(
64+
{"kwargs": {"openai_api_base": "https://us.posthog.com"}},
6465
run_id,
6566
messages=[{"role": "user", "content": "Who won the world series in 2020?"}],
6667
invocation_params={"temperature": 0.5},
@@ -72,6 +73,7 @@ def test_metadata_capture(mock_client):
7273
"start_time": 1234567890,
7374
"model_params": {"temperature": 0.5},
7475
"provider": "posthog",
76+
"base_url": "https://us.posthog.com",
7577
}
7678
assert callbacks._runs[run_id] == expected
7779
with patch("time.time", return_value=1234567891):
@@ -577,3 +579,19 @@ async def test_async_openai_streaming(mock_client):
577579
assert first_call_props["$ai_http_status"] == 200
578580
assert first_call_props["$ai_input_tokens"] == 20
579581
assert first_call_props["$ai_output_tokens"] == 1
582+
583+
584+
def test_base_url_retrieval(mock_client):
585+
prompt = ChatPromptTemplate.from_messages([("user", "Foo")])
586+
chain = prompt | ChatOpenAI(
587+
api_key="test",
588+
model="posthog-mini",
589+
base_url="https://test.posthog.com",
590+
)
591+
callbacks = CallbackHandler(mock_client)
592+
with pytest.raises(Exception):
593+
chain.invoke({}, config={"callbacks": [callbacks]})
594+
595+
assert mock_client.capture.call_count == 1
596+
call = mock_client.capture.call_args[1]
597+
assert call["properties"]["$ai_base_url"] == "https://test.posthog.com"

0 commit comments

Comments
 (0)