2023-03-31 09:38:53 +00:00
|
|
|
|
from core.dirty_check import check_bool
|
|
|
|
|
from core.builtins import Bot
|
|
|
|
|
from core.component import module
|
2023-04-08 03:15:56 +00:00
|
|
|
|
from modules.ask.agent import agent_executor
|
2023-04-14 13:15:04 +00:00
|
|
|
|
from langchain.callbacks import get_openai_callback
|
|
|
|
|
from decimal import Decimal
|
2023-03-31 09:38:53 +00:00
|
|
|
|
|
|
|
|
|
from core.exceptions import NoReportException
|
|
|
|
|
|
2023-04-14 13:15:04 +00:00
|
|
|
|
ONE_K = Decimal('1000')
|
|
|
|
|
# https://openai.com/pricing
|
|
|
|
|
BASE_COST_GPT_3_5 = Decimal('0.002') # gpt-3.5-turbo: $0.002 / 1K tokens
|
|
|
|
|
THIRD_PARTY_MULTIPLIER = Decimal('1.5') # We are not tracking specific tool usage like searches b/c I'm too lazy, use a universal multiplier
|
|
|
|
|
PROFIT_MULTIPLIER = Decimal('1.1') # At the time we are really just trying to break even
|
|
|
|
|
PRICE_PER_1K_TOKEN = BASE_COST_GPT_3_5 * THIRD_PARTY_MULTIPLIER * PROFIT_MULTIPLIER
|
|
|
|
|
# Assuming 1 USD = 7 CNY, 100 petal = 1 CNY
|
|
|
|
|
USD_TO_CNY = 7
|
|
|
|
|
CNY_TO_PETAL = 100
|
|
|
|
|
|
2023-04-05 10:15:09 +00:00
|
|
|
|
a = module('ask', developers=['Dianliang233'], desc='{ask.help.desc}', required_superuser=True)
|
2023-03-31 09:38:53 +00:00
|
|
|
|
|
|
|
|
|
|
2023-04-05 11:08:13 +00:00
|
|
|
|
@a.command('<question> {{ask.help}}')
|
|
|
|
|
@a.regex(r'^(?:ask|问)[\::]? ?(.+?)[??]$')
|
2023-03-31 09:38:53 +00:00
|
|
|
|
async def _(msg: Bot.MessageSession):
|
2023-04-14 13:15:04 +00:00
|
|
|
|
is_superuser =msg.checkSuperUser()
|
|
|
|
|
if not is_superuser and msg.data.petal < 100: # refuse
|
|
|
|
|
raise NoReportException(msg.locale.t('petal_'))
|
2023-03-31 14:57:33 +00:00
|
|
|
|
if hasattr(msg, 'parsed_msg'):
|
|
|
|
|
question = msg.parsed_msg['<question>']
|
|
|
|
|
else:
|
|
|
|
|
question = msg.matched_msg[0]
|
2023-03-31 09:38:53 +00:00
|
|
|
|
if await check_bool(question):
|
|
|
|
|
raise NoReportException('https://wdf.ink/6OUp')
|
2023-04-14 13:15:04 +00:00
|
|
|
|
with get_openai_callback() as cb:
|
2023-04-07 12:41:06 +00:00
|
|
|
|
res = await agent_executor.arun(question)
|
2023-04-14 13:15:04 +00:00
|
|
|
|
tokens = cb.total_tokens
|
|
|
|
|
# if not is_superuser:
|
|
|
|
|
# price = tokens / ONE_K * PRICE_PER_1K_TOKEN
|
|
|
|
|
# petal = price * USD_TO_CNY * CNY_TO_PETAL
|
|
|
|
|
# await msg.data.modify_petal(-petal)
|
|
|
|
|
price = tokens / ONE_K * PRICE_PER_1K_TOKEN
|
|
|
|
|
petal = price * USD_TO_CNY * CNY_TO_PETAL
|
|
|
|
|
msg.data.modify_petal(-int(petal))
|
2023-03-31 09:38:53 +00:00
|
|
|
|
if await check_bool(res):
|
|
|
|
|
raise NoReportException('https://wdf.ink/6OUp')
|
|
|
|
|
await msg.finish(res)
|