H commited on
Commit
6108c20
·
1 Parent(s): 5743e5f

Fix token list , stats in api app.py (#1896)

Browse files

### What problem does this PR solve?

#1842

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)

api/apps/api_app.py CHANGED
@@ -87,7 +87,8 @@ def token_list():
87
  if not tenants:
88
  return get_data_error_result(retmsg="Tenant not found!")
89
 
90
- objs = APITokenService.query(tenant_id=tenants[0].tenant_id, dialog_id=request.args["dialog_id"])
 
91
  return get_json_result(data=[o.to_dict() for o in objs])
92
  except Exception as e:
93
  return server_error_response(e)
@@ -123,7 +124,8 @@ def stats():
123
  days=7)).strftime("%Y-%m-%d 24:00:00")),
124
  request.args.get(
125
  "to_date",
126
- datetime.now().strftime("%Y-%m-%d %H:%M:%S")))
 
127
  res = {
128
  "pv": [(o["dt"], o["pv"]) for o in objs],
129
  "uv": [(o["dt"], o["uv"]) for o in objs],
 
87
  if not tenants:
88
  return get_data_error_result(retmsg="Tenant not found!")
89
 
90
+ id = request.args.get("dialog_id", request.args["canvas_id"])
91
+ objs = APITokenService.query(tenant_id=tenants[0].tenant_id, dialog_id=id)
92
  return get_json_result(data=[o.to_dict() for o in objs])
93
  except Exception as e:
94
  return server_error_response(e)
 
124
  days=7)).strftime("%Y-%m-%d 24:00:00")),
125
  request.args.get(
126
  "to_date",
127
+ datetime.now().strftime("%Y-%m-%d %H:%M:%S")),
128
+ "agent" if request.args.get("canvas_id") else None)
129
  res = {
130
  "pv": [(o["dt"], o["pv"]) for o in objs],
131
  "uv": [(o["dt"], o["uv"]) for o in objs],
api/db/services/api_service.py CHANGED
@@ -45,7 +45,7 @@ class API4ConversationService(CommonService):
45
 
46
  @classmethod
47
  @DB.connection_context()
48
- def stats(cls, tenant_id, from_date, to_date):
49
  return cls.model.select(
50
  cls.model.create_date.truncate("day").alias("dt"),
51
  peewee.fn.COUNT(
@@ -62,5 +62,6 @@ class API4ConversationService(CommonService):
62
  cls.model.thumb_up).alias("thumb_up")
63
  ).join(Dialog, on=(cls.model.dialog_id == Dialog.id & Dialog.tenant_id == tenant_id)).where(
64
  cls.model.create_date >= from_date,
65
- cls.model.create_date <= to_date
 
66
  ).group_by(cls.model.create_date.truncate("day")).dicts()
 
45
 
46
  @classmethod
47
  @DB.connection_context()
48
+ def stats(cls, tenant_id, from_date, to_date, source=None):
49
  return cls.model.select(
50
  cls.model.create_date.truncate("day").alias("dt"),
51
  peewee.fn.COUNT(
 
62
  cls.model.thumb_up).alias("thumb_up")
63
  ).join(Dialog, on=(cls.model.dialog_id == Dialog.id & Dialog.tenant_id == tenant_id)).where(
64
  cls.model.create_date >= from_date,
65
+ cls.model.create_date <= to_date,
66
+ cls.model.source == source
67
  ).group_by(cls.model.create_date.truncate("day")).dicts()
rag/utils/__init__.py CHANGED
@@ -74,10 +74,15 @@ def findMaxTm(fnm):
74
 
75
  encoder = tiktoken.encoding_for_model("gpt-3.5-turbo")
76
 
 
77
  def num_tokens_from_string(string: str) -> int:
78
  """Returns the number of tokens in a text string."""
79
- num_tokens = len(encoder.encode(string))
80
- return num_tokens
 
 
 
 
81
 
82
 
83
  def truncate(string: str, max_len: int) -> int:
 
74
 
75
  encoder = tiktoken.encoding_for_model("gpt-3.5-turbo")
76
 
77
+
78
  def num_tokens_from_string(string: str) -> int:
79
  """Returns the number of tokens in a text string."""
80
+ try:
81
+ num_tokens = len(encoder.encode(string))
82
+ return num_tokens
83
+ except Exception as e:
84
+ pass
85
+ return 0
86
 
87
 
88
  def truncate(string: str, max_len: int) -> int: