-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
28 changed files
with
269 additions
and
42 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import asyncio | ||
from enum import Enum | ||
from typing import List, Tuple, Callable, Union, Dict | ||
|
||
from retk import config, const | ||
from retk.core.utils import ratelimiter | ||
from .base import ModelConfig, MessagesType | ||
from .openai import OpenaiLLMStyle | ||
|
||
|
||
# https://open.bigmodel.cn/dev/howuse/rate-limits/tiers?tab=0 | ||
class GLMModelEnum(Enum): | ||
GLM4_PLUS = ModelConfig( | ||
key="GLM-4-Plus", | ||
max_tokens=128_000, | ||
) | ||
GLM4_LONG = ModelConfig( | ||
key="GLM-4-Long", | ||
max_tokens=1_000_000, | ||
) | ||
GLM4_FLASH = ModelConfig( | ||
key="GLM-4-Flash", | ||
max_tokens=128_000, | ||
) | ||
|
||
|
||
class GLMService(OpenaiLLMStyle): | ||
name = "glm" | ||
|
||
def __init__( | ||
self, | ||
top_p: float = 0.9, | ||
temperature: float = 0.9, | ||
timeout: float = 60., | ||
): | ||
super().__init__( | ||
model_enum=GLMModelEnum, | ||
endpoint="https://open.bigmodel.cn/api/paas/v4/chat/completions", | ||
default_model=GLMModelEnum.GLM4_FLASH.value, | ||
top_p=top_p, | ||
temperature=temperature, | ||
timeout=timeout, | ||
) | ||
|
||
@classmethod | ||
def set_api_auth(cls, auth: Dict[str, str]): | ||
config.get_settings().BIGMODEL_API_KEY = auth.get("API-KEY", "") | ||
|
||
@staticmethod | ||
def get_api_key(): | ||
return config.get_settings().BIGMODEL_API_KEY | ||
|
||
@staticmethod | ||
async def _batch_complete_union( | ||
messages: List[MessagesType], | ||
func: Callable, | ||
model: str = None, | ||
req_id: str = None, | ||
) -> List[Tuple[Union[str, Dict[str, str]], const.CodeEnum]]: | ||
settings = config.get_settings() | ||
concurrent_limiter = ratelimiter.ConcurrentLimiter(n=settings.BIGMODEL_CONCURRENCY) | ||
|
||
tasks = [ | ||
func( | ||
limiters=[concurrent_limiter], | ||
messages=m, | ||
model=model, | ||
req_id=req_id, | ||
) for m in messages | ||
] | ||
return await asyncio.gather(*tasks) | ||
|
||
async def batch_complete( | ||
self, | ||
messages: List[MessagesType], | ||
model: str = None, | ||
req_id: str = None, | ||
) -> List[Tuple[str, const.CodeEnum]]: | ||
return await self._batch_complete_union( | ||
messages=messages, | ||
func=self._batch_complete, | ||
model=model, | ||
req_id=req_id, | ||
) | ||
|
||
async def batch_complete_json_detect( | ||
self, | ||
messages: List[MessagesType], | ||
model: str = None, | ||
req_id: str = None, | ||
) -> List[Tuple[Dict[str, str], const.CodeEnum]]: | ||
return await self._batch_complete_union( | ||
messages=messages, | ||
func=self._batch_stream_complete_json_detect, | ||
model=model, | ||
req_id=req_id, | ||
) |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
notice, | ||
extend_node, | ||
auto_clean_trash, | ||
auto_daily_report, | ||
) |
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,93 @@ | ||
import asyncio | ||
import json | ||
import os | ||
import time | ||
from datetime import datetime, timedelta | ||
|
||
from bson.objectid import ObjectId | ||
|
||
from retk import config, const | ||
from retk.core.statistic import __write_line_date, __manage_files | ||
from retk.models.client import init_mongo | ||
from retk.models.coll import CollNameEnum | ||
|
||
try: | ||
import aiofiles | ||
except ImportError: | ||
aiofiles = None | ||
|
||
|
||
def auto_daily_report(): | ||
loop = asyncio.new_event_loop() | ||
asyncio.set_event_loop(loop) | ||
res = loop.run_until_complete(_auto_daily_report()) | ||
loop.close() | ||
return res | ||
|
||
|
||
async def _auto_daily_report(): | ||
if config.is_local_db() or aiofiles is None: | ||
return | ||
file = const.settings.ANALYTICS_DIR / "daily_report" / "report.log" | ||
lock = asyncio.Lock() | ||
now = datetime.now() | ||
# get last line efficiently | ||
if file.exists(): | ||
async with aiofiles.open(file, "r", encoding="utf-8") as f: | ||
async with lock: | ||
try: | ||
await f.seek(-2, os.SEEK_END) | ||
while f.read(1) != "\n": | ||
await f.seek(-2, os.SEEK_CUR) | ||
last_line = await f.readline() | ||
except OSError: | ||
last_line = "" | ||
try: | ||
last_record = json.loads(last_line) | ||
except json.JSONDecodeError: | ||
last_record = {} | ||
else: | ||
last_record = {} | ||
yesterday = (now - timedelta(days=1)).date() | ||
last_record_date = last_record.get("date", None) | ||
|
||
if last_record_date is not None: | ||
last_record_date = datetime.strptime(last_record_date, '%Y-%m-%d').date() | ||
else: | ||
last_record_date = yesterday - timedelta(days=1) | ||
|
||
if last_record_date >= yesterday: | ||
return | ||
|
||
await __manage_files(now, file, lock) | ||
|
||
_, db = init_mongo(connection_timeout=5) | ||
total_email_users = await db[CollNameEnum.users.value].count_documents( | ||
{"source": const.UserSourceEnum.EMAIL.value} | ||
) | ||
total_google_users = await db[CollNameEnum.users.value].count_documents( | ||
{"source": const.UserSourceEnum.GOOGLE.value} | ||
) | ||
total_github_users = await db[CollNameEnum.users.value].count_documents( | ||
{"source": const.UserSourceEnum.GITHUB.value} | ||
) | ||
total_users = total_email_users + total_google_users + total_github_users | ||
# date to int timestamp | ||
timestamp = time.mktime(last_record_date.timetuple()) | ||
time_filter = ObjectId.from_datetime(datetime.utcfromtimestamp(timestamp)) | ||
|
||
await __write_line_date( | ||
data={ | ||
"date": now.strftime('%Y-%m-%d'), | ||
"totalUsers": total_users, | ||
"totalEmailUsers": total_email_users, | ||
"totalGoogleUsers": total_google_users, | ||
"totalGithubUsers": total_github_users, | ||
"newUsers": await db[CollNameEnum.users.value].count_documents({"_id": {"$gt": time_filter}}), | ||
"totalNodes": await db[CollNameEnum.nodes.value].count_documents({}), | ||
"newNodes": await db[CollNameEnum.nodes.value].count_documents({"_id": {"$gt": time_filter}}), | ||
"totalFiles": await db[CollNameEnum.user_file.value].count_documents({}), | ||
}, | ||
path=file, | ||
lock=lock | ||
) |
Oops, something went wrong.