### What problem does this PR solve? #1842 ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue)tags/v0.10.0
| if not tenants: | if not tenants: | ||||
| return get_data_error_result(retmsg="Tenant not found!") | return get_data_error_result(retmsg="Tenant not found!") | ||||
| objs = APITokenService.query(tenant_id=tenants[0].tenant_id, dialog_id=request.args["dialog_id"]) | |||||
| id = request.args.get("dialog_id", request.args["canvas_id"]) | |||||
| objs = APITokenService.query(tenant_id=tenants[0].tenant_id, dialog_id=id) | |||||
| return get_json_result(data=[o.to_dict() for o in objs]) | return get_json_result(data=[o.to_dict() for o in objs]) | ||||
| except Exception as e: | except Exception as e: | ||||
| return server_error_response(e) | return server_error_response(e) | ||||
| days=7)).strftime("%Y-%m-%d 24:00:00")), | days=7)).strftime("%Y-%m-%d 24:00:00")), | ||||
| request.args.get( | request.args.get( | ||||
| "to_date", | "to_date", | ||||
| datetime.now().strftime("%Y-%m-%d %H:%M:%S"))) | |||||
| datetime.now().strftime("%Y-%m-%d %H:%M:%S")), | |||||
| "agent" if request.args.get("canvas_id") else None) | |||||
| res = { | res = { | ||||
| "pv": [(o["dt"], o["pv"]) for o in objs], | "pv": [(o["dt"], o["pv"]) for o in objs], | ||||
| "uv": [(o["dt"], o["uv"]) for o in objs], | "uv": [(o["dt"], o["uv"]) for o in objs], |
| @classmethod | @classmethod | ||||
| @DB.connection_context() | @DB.connection_context() | ||||
| def stats(cls, tenant_id, from_date, to_date): | |||||
| def stats(cls, tenant_id, from_date, to_date, source=None): | |||||
| return cls.model.select( | return cls.model.select( | ||||
| cls.model.create_date.truncate("day").alias("dt"), | cls.model.create_date.truncate("day").alias("dt"), | ||||
| peewee.fn.COUNT( | peewee.fn.COUNT( | ||||
| cls.model.thumb_up).alias("thumb_up") | cls.model.thumb_up).alias("thumb_up") | ||||
| ).join(Dialog, on=(cls.model.dialog_id == Dialog.id & Dialog.tenant_id == tenant_id)).where( | ).join(Dialog, on=(cls.model.dialog_id == Dialog.id & Dialog.tenant_id == tenant_id)).where( | ||||
| cls.model.create_date >= from_date, | cls.model.create_date >= from_date, | ||||
| cls.model.create_date <= to_date | |||||
| cls.model.create_date <= to_date, | |||||
| cls.model.source == source | |||||
| ).group_by(cls.model.create_date.truncate("day")).dicts() | ).group_by(cls.model.create_date.truncate("day")).dicts() |
| encoder = tiktoken.encoding_for_model("gpt-3.5-turbo") | encoder = tiktoken.encoding_for_model("gpt-3.5-turbo") | ||||
| def num_tokens_from_string(string: str) -> int: | def num_tokens_from_string(string: str) -> int: | ||||
| """Returns the number of tokens in a text string.""" | """Returns the number of tokens in a text string.""" | ||||
| num_tokens = len(encoder.encode(string)) | |||||
| return num_tokens | |||||
| try: | |||||
| num_tokens = len(encoder.encode(string)) | |||||
| return num_tokens | |||||
| except Exception as e: | |||||
| pass | |||||
| return 0 | |||||
| def truncate(string: str, max_len: int) -> int: | def truncate(string: str, max_len: int) -> int: |