-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: init integration test * chore: add pymysql dev dependency * fix: adjustment pytest * fix: get env from secrets * feat: add embedding test * feat: support to test trace and metric * chore: ignore mypy in tests * fix: solve lint * feat: disable parallel * chore: solve packages import * feat: clarify logic and add fixtures to truncate database * fix: solve lint * chore: run ci in order * fix: literate py version with str * chore: optimize table name and doc string * chore: ignore numbers table * chore: use shared steps * fix: optimize ci * fix: optimize ci * fix: optimize composite actions * fix: optimize composite actions * fix: fix composite actions * fix: repair composite actions * fix: add composite using * fix: fix lint * ci: refactor ci * chore: delete action * feat: add stream,async and raw_response tests for chat_completion * chore: remove metrics test * chore: add client init * chore: add pytest-asyncio to test async * fix: solve lint * fix: correct naming * feat: use global collector * feat: add force flush func * chore: optimize flush * Revert "chore: optimize flush" This reverts commit a808233. * chore: optimize flush * chore: adjust flush response time * chore: fix lint * chore: extend sleep time * chore: remove extra tests for now * chore: remove extra sql * feat: add pymysql types * feat: add pymysql types * feat: install pymysql types in ci
- Loading branch information
1 parent
d162731
commit 7dba4a5
Showing
14 changed files
with
232 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import logging | ||
import os | ||
from typing import Union, List | ||
|
||
import pymysql | ||
|
||
from .model import Tables | ||
|
||
db = pymysql.connect( | ||
host=os.getenv("GREPTIMEAI_HOST"), | ||
user=os.getenv("GREPTIMEAI_USERNAME"), | ||
passwd=os.getenv("GREPTIMEAI_PASSWORD"), | ||
port=4002, | ||
db=os.getenv("GREPTIMEAI_DATABASE"), | ||
) | ||
cursor = db.cursor() | ||
|
||
trace_sql = "SELECT model,prompt_tokens,completion_tokens FROM %s WHERE user_id = '%s'" | ||
truncate_sql = "TRUNCATE %s" | ||
|
||
|
||
def get_trace_data(user_id: str) -> List[Union[str, int]]: | ||
""" | ||
get trace data for llm trace by user_id | ||
:param is_stream: | ||
:param user_id: | ||
:return: model, prompt_tokens, completion_tokens | ||
""" | ||
|
||
cursor.execute(trace_sql % (Tables.llm_trace, user_id)) | ||
trace = cursor.fetchone() | ||
if trace is None: | ||
raise Exception("trace data is None") | ||
return list(trace) | ||
|
||
|
||
def truncate_tables(): | ||
""" | ||
truncate all tables | ||
:return: | ||
""" | ||
tables = [ | ||
"llm_completion_tokens", | ||
"llm_completion_tokens_cost", | ||
"llm_errors", | ||
"llm_prompt_tokens", | ||
"llm_prompt_tokens_cost", | ||
"llm_request_duration_ms_bucket", | ||
"llm_request_duration_ms_count", | ||
"llm_request_duration_ms_sum", | ||
"llm_traces_preview_v01", | ||
] | ||
try: | ||
cursor.executemany(truncate_sql, tables) | ||
db.commit() | ||
except Exception as e: | ||
logging.error(e) | ||
db.rollback() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
class LlmTrace(object): | ||
table_name = "llm_traces_preview_v01" | ||
|
||
trace_id: str | ||
span_id: str | ||
parent_span_id: str | ||
resource_attributes: str | ||
scope_name: str | ||
scope_version: str | ||
scope_attributes: str | ||
trace_state: str | ||
span_name: str | ||
span_kind: str | ||
span_status_code: str | ||
span_status_message: str | ||
span_attributes: str | ||
span_events: str | ||
span_links: str | ||
start: float | ||
end: float | ||
user_id: str | ||
model: str | ||
prompt_tokens: int | ||
prompt_cost: float | ||
completion_tokens: int | ||
completion_cost: float | ||
greptime_value: str | ||
greptime_timestamp: float | ||
|
||
|
||
class LlmPromptToken(object): | ||
table_name = "llm_prompt_tokens" | ||
|
||
telemetry_sdk_language: str | ||
telemetry_sdk_name: str | ||
telemetry_sdk_version: str | ||
service_name: str | ||
span_name: str | ||
model: str | ||
greptime_value: str | ||
greptime_timestamp: float | ||
|
||
|
||
class LlmPromptTokenCost(object): | ||
table_name = "llm_prompt_tokens_cost" | ||
|
||
telemetry_sdk_language: str | ||
telemetry_sdk_name: str | ||
telemetry_sdk_version: str | ||
service_name: str | ||
span_name: str | ||
model: str | ||
greptime_value: str | ||
greptime_timestamp: float | ||
|
||
|
||
class LlmCompletionToken(object): | ||
table_name = "llm_completion_tokens" | ||
|
||
telemetry_sdk_language: str | ||
telemetry_sdk_name: str | ||
telemetry_sdk_version: str | ||
service_name: str | ||
span_name: str | ||
model: str | ||
greptime_value: str | ||
greptime_timestamp: float | ||
|
||
|
||
class LlmCompletionTokenCost(object): | ||
table_name = "llm_completion_tokens_cost" | ||
|
||
telemetry_sdk_language: str | ||
telemetry_sdk_name: str | ||
telemetry_sdk_version: str | ||
service_name: str | ||
span_name: str | ||
model: str | ||
greptime_value: str | ||
greptime_timestamp: float | ||
|
||
|
||
class Tables(object): | ||
llm_trace = "llm_traces_preview_v01" | ||
llm_prompt_tokens = "llm_prompt_tokens" | ||
llm_prompt_tokens_cost = "llm_prompt_tokens_cost" | ||
llm_completion_tokens = "llm_completion_tokens" | ||
llm_completion_tokens_cost = "llm_completion_tokens_cost" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from openai import AsyncOpenAI | ||
from openai import OpenAI | ||
|
||
from greptimeai import openai_patcher # type: ignore | ||
|
||
async_client = AsyncOpenAI() | ||
openai_patcher.setup(client=async_client) | ||
|
||
client = OpenAI() | ||
openai_patcher.setup(client=client) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import time | ||
import uuid | ||
|
||
import pytest | ||
|
||
from ..database.db import ( | ||
get_trace_data, | ||
truncate_tables, | ||
) | ||
from ..openai_tracker import client | ||
from greptimeai.openai_patcher import _collector # type: ignore | ||
|
||
|
||
@pytest.fixture | ||
def _truncate_tables(): | ||
truncate_tables() | ||
yield | ||
|
||
|
||
def test_chat_completion(_truncate_tables): | ||
user_id = str(uuid.uuid4()) | ||
resp = client.chat.completions.create( | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": "1+1=", | ||
} | ||
], | ||
model="gpt-3.5-turbo", | ||
user=user_id, | ||
seed=1, | ||
) | ||
assert resp.choices[0].message.content == "2" | ||
|
||
_collector._collector._force_flush() | ||
time.sleep(6) | ||
trace = get_trace_data(user_id) | ||
|
||
assert resp.model == trace[0] | ||
|
||
if resp.usage: | ||
assert resp.usage.prompt_tokens == trace[1] | ||
assert resp.usage.completion_tokens == trace[2] |
Empty file.
Empty file.