Skip to content

Commit

Permalink
feat: Update Influx schema with new fields/tags #29 (#30)
Browse files Browse the repository at this point in the history
* feat: Update Influx schema with new fields/tags #29

* fix: change execution path to JSON array

---------

Co-authored-by: Aliaksandr Stsiapanay <aliaksandr_stsiapanay@epam.com>
  • Loading branch information
astsiapanay and astsiapanay authored Jan 26, 2024
1 parent a35e08e commit ed35a09
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
13 changes: 13 additions & 0 deletions aidial_analytics_realtime/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def make_point(
rates_calculator: RatesCalculator,
parent_deployment: str | None,
trace: dict | None,
execution_path: list | None,
):
topic = None
response_content = ""
Expand All @@ -94,8 +95,10 @@ def make_point(
)

price = Decimal(0)
deployment_price = Decimal(0)
if usage is not None and usage.get("price") is not None:
price = usage["price"]
deployment_price = usage.get("deployment_price", Decimal(0))
else:
price = rates_calculator.calculate_price(
deployment, model, request_content, response_content, usage
Expand All @@ -106,6 +109,10 @@ def make_point(
.tag("model", model)
.tag("deployment", deployment)
.tag("parent_deployment", to_string(parent_deployment))
.tag(
"execution_path",
"undefined" if not execution_path else "/".join(execution_path),
)
.tag("trace_id", "undefined" if not trace else trace["trace_id"])
.tag(
"core_span_id", "undefined" if not trace else trace["core_span_id"]
Expand Down Expand Up @@ -134,6 +141,7 @@ def make_point(
)
.field("user_hash", to_string(user_hash))
.field("price", price)
.field("deployment_price", deployment_price)
.field(
"number_request_messages",
len(request["messages"])
Expand Down Expand Up @@ -198,6 +206,7 @@ async def on_message(
token_usage: dict | None,
parent_deployment: str | None,
trace: dict | None,
execution_path: list | None,
):
logger.info(f"Chat completion response length {len(response)}")

Expand All @@ -220,6 +229,7 @@ async def on_message(
rates_calculator,
parent_deployment,
trace,
execution_path,
)
await influx_writer(point)
elif len(usage_per_model) == 0:
Expand All @@ -240,6 +250,7 @@ async def on_message(
rates_calculator,
parent_deployment,
trace,
execution_path,
)
await influx_writer(point)
else:
Expand All @@ -260,6 +271,7 @@ async def on_message(
rates_calculator,
parent_deployment,
trace,
execution_path,
)
await influx_writer(point)

Expand All @@ -281,5 +293,6 @@ async def on_message(
rates_calculator,
parent_deployment,
trace,
execution_path,
)
await influx_writer(point)
7 changes: 7 additions & 0 deletions aidial_analytics_realtime/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ async def on_chat_completion_message(
token_usage: dict | None,
parent_deployment: str | None,
trace: dict | None,
execution_path: list | None,
):
if response["status"] != "200":
return
Expand Down Expand Up @@ -124,6 +125,7 @@ async def on_chat_completion_message(
token_usage,
parent_deployment,
trace,
execution_path,
)


Expand All @@ -143,6 +145,7 @@ async def on_embedding_message(
token_usage: dict | None,
parent_deployment: str | None,
trace: dict | None,
execution_path: list | None,
):
if response["status"] != "200":
return
Expand All @@ -166,6 +169,7 @@ async def on_embedding_message(
token_usage,
parent_deployment,
trace,
execution_path,
)


Expand All @@ -191,6 +195,7 @@ async def on_log_message(
token_usage = message.get("token_usage", None)
trace = message.get("trace", None)
parent_deployment = message.get("parent_deployment", None)
execution_path = message.get("execution_path", None)

match = re.search(RATE_PATTERN, uri)
if match:
Expand All @@ -215,6 +220,7 @@ async def on_log_message(
token_usage,
parent_deployment,
trace,
execution_path,
)

match = re.search(EMBEDDING_PATTERN, uri)
Expand All @@ -236,6 +242,7 @@ async def on_log_message(
token_usage,
parent_deployment,
trace,
execution_path,
)


Expand Down
10 changes: 6 additions & 4 deletions tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ def test_data_request():
)
assert response.status_code == 200
assert write_api_mock.points == [
'analytics,core_parent_span_id=undefined,core_span_id=undefined,deployment=gpt-4,language=undefined,model=gpt-4,parent_deployment=undefined,project_id=PROJECT-KEY,response_id=chatcmpl-1,title=undefined,topic=TestTopic,trace_id=undefined,upstream=undefined chat_id="chat-1",completion_tokens=189i,number_request_messages=2i,price=0,prompt_tokens=22i,user_hash="undefined" 1692214959997000000',
'analytics,core_parent_span_id=undefined,core_span_id=undefined,deployment=gpt-4,language=undefined,model=gpt-4,parent_deployment=undefined,project_id=PROJECT-KEY-2,response_id=chatcmpl-2,title=undefined,topic=TestTopic,trace_id=undefined,upstream=undefined chat_id="chat-2",completion_tokens=189i,number_request_messages=2i,price=0,prompt_tokens=22i,user_hash="undefined" 1700796820390000000',
'analytics,core_parent_span_id=undefined,core_span_id=undefined,deployment=gpt-4,execution_path=undefined,language=undefined,model=gpt-4,parent_deployment=undefined,project_id=PROJECT-KEY,response_id=chatcmpl-1,title=undefined,topic=TestTopic,trace_id=undefined,upstream=undefined chat_id="chat-1",completion_tokens=189i,deployment_price=0,number_request_messages=2i,price=0,prompt_tokens=22i,user_hash="undefined" 1692214959997000000',
'analytics,core_parent_span_id=undefined,core_span_id=undefined,deployment=gpt-4,execution_path=undefined,language=undefined,model=gpt-4,parent_deployment=undefined,project_id=PROJECT-KEY-2,response_id=chatcmpl-2,title=undefined,topic=TestTopic,trace_id=undefined,upstream=undefined chat_id="chat-2",completion_tokens=189i,deployment_price=0,number_request_messages=2i,price=0,prompt_tokens=22i,user_hash="undefined" 1700796820390000000',
]


Expand Down Expand Up @@ -125,6 +125,7 @@ def test_data_request_with_new_format():
"core_span_id": "9ade2b6fef0a716d",
"core_parent_span_id": "20e7e64715abbe97",
},
"execution_path": ["a", "b", "c"],
"request": {
"protocol": "HTTP/1.1",
"method": "POST",
Expand Down Expand Up @@ -167,6 +168,7 @@ def test_data_request_with_new_format():
"trace_id": "5dca3d6ed5d22b6ab574f27a6ab5ec14",
"core_span_id": "20e7e64715abbe97",
},
"execution_path": ["a", "b", "c"],
"request": {
"protocol": "HTTP/1.1",
"method": "POST",
Expand Down Expand Up @@ -197,6 +199,6 @@ def test_data_request_with_new_format():
)
assert response.status_code == 200
assert write_api_mock.points == [
'analytics,core_parent_span_id=20e7e64715abbe97,core_span_id=9ade2b6fef0a716d,deployment=gpt-4,language=undefined,model=gpt-4,parent_deployment=assistant,project_id=PROJECT-KEY,response_id=chatcmpl-1,title=undefined,topic=TestTopic,trace_id=5dca3d6ed5d22b6ab574f27a6ab5ec14,upstream=undefined chat_id="chat-1",completion_tokens=40i,number_request_messages=2i,price=0.001,prompt_tokens=30i,user_hash="undefined" 1692214959997000000',
'analytics,core_parent_span_id=undefined,core_span_id=20e7e64715abbe97,deployment=gpt-4,language=undefined,model=gpt-4,parent_deployment=undefined,project_id=PROJECT-KEY-2,response_id=chatcmpl-2,title=undefined,topic=TestTopic,trace_id=5dca3d6ed5d22b6ab574f27a6ab5ec14,upstream=undefined chat_id="chat-2",completion_tokens=40i,number_request_messages=2i,price=0.005,prompt_tokens=30i,user_hash="undefined" 1700796820390000000',
'analytics,core_parent_span_id=20e7e64715abbe97,core_span_id=9ade2b6fef0a716d,deployment=gpt-4,execution_path=a/b/c,language=undefined,model=gpt-4,parent_deployment=assistant,project_id=PROJECT-KEY,response_id=chatcmpl-1,title=undefined,topic=TestTopic,trace_id=5dca3d6ed5d22b6ab574f27a6ab5ec14,upstream=undefined chat_id="chat-1",completion_tokens=40i,deployment_price=0.001,number_request_messages=2i,price=0.001,prompt_tokens=30i,user_hash="undefined" 1692214959997000000',
'analytics,core_parent_span_id=undefined,core_span_id=20e7e64715abbe97,deployment=gpt-4,execution_path=a/b/c,language=undefined,model=gpt-4,parent_deployment=undefined,project_id=PROJECT-KEY-2,response_id=chatcmpl-2,title=undefined,topic=TestTopic,trace_id=5dca3d6ed5d22b6ab574f27a6ab5ec14,upstream=undefined chat_id="chat-2",completion_tokens=40i,deployment_price=0,number_request_messages=2i,price=0.005,prompt_tokens=30i,user_hash="undefined" 1700796820390000000',
]
5 changes: 3 additions & 2 deletions tests/test_huggingface_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_data_request():
)
assert response.status_code == 200
assert write_api_mock.points == [
'analytics,core_parent_span_id=undefined,core_span_id=undefined,deployment=gpt-4,language=undefined,model=gpt-4,parent_deployment=undefined,project_id=PROJECT-KEY,response_id=chatcmpl-1,title=undefined,topic=4_letter_sincerely_regards_email,trace_id=undefined,upstream=undefined chat_id="chat-1",completion_tokens=189i,number_request_messages=2i,price=0,prompt_tokens=22i,user_hash="undefined" 1692214959997000000'
'analytics,core_parent_span_id=undefined,core_span_id=undefined,deployment=gpt-4,execution_path=undefined,language=undefined,model=gpt-4,parent_deployment=undefined,project_id=PROJECT-KEY,response_id=chatcmpl-1,title=undefined,topic=4_letter_sincerely_regards_email,trace_id=undefined,upstream=undefined chat_id="chat-1",completion_tokens=189i,deployment_price=0,number_request_messages=2i,price=0,prompt_tokens=22i,user_hash="undefined" 1692214959997000000'
]


Expand Down Expand Up @@ -90,6 +90,7 @@ def test_data_request_with_new_format():
"core_span_id": "9ade2b6fef0a716d",
"core_parent_span_id": "20e7e64715abbe97",
},
"execution_path": ["a", "b", "c"],
"request": {
"protocol": "HTTP/1.1",
"method": "POST",
Expand Down Expand Up @@ -120,5 +121,5 @@ def test_data_request_with_new_format():
)
assert response.status_code == 200
assert write_api_mock.points == [
'analytics,core_parent_span_id=20e7e64715abbe97,core_span_id=9ade2b6fef0a716d,deployment=gpt-4,language=undefined,model=gpt-4,parent_deployment=undefined,project_id=PROJECT-KEY,response_id=chatcmpl-1,title=undefined,topic=4_letter_sincerely_regards_email,trace_id=5dca3d6ed5d22b6ab574f27a6ab5ec14,upstream=undefined chat_id="chat-1",completion_tokens=40i,number_request_messages=2i,price=0.001,prompt_tokens=30i,user_hash="undefined" 1692214959997000000'
'analytics,core_parent_span_id=20e7e64715abbe97,core_span_id=9ade2b6fef0a716d,deployment=gpt-4,execution_path=a/b/c,language=undefined,model=gpt-4,parent_deployment=undefined,project_id=PROJECT-KEY,response_id=chatcmpl-1,title=undefined,topic=4_letter_sincerely_regards_email,trace_id=5dca3d6ed5d22b6ab574f27a6ab5ec14,upstream=undefined chat_id="chat-1",completion_tokens=40i,deployment_price=0.001,number_request_messages=2i,price=0.001,prompt_tokens=30i,user_hash="undefined" 1692214959997000000'
]

0 comments on commit ed35a09

Please sign in to comment.