MeUtils 2025.6.20.18.7.13__py3-none-any.whl → 2025.6.24.14.6.6__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {MeUtils-2025.6.20.18.7.13.dist-info → MeUtils-2025.6.24.14.6.6.dist-info}/METADATA +260 -260
- {MeUtils-2025.6.20.18.7.13.dist-info → MeUtils-2025.6.24.14.6.6.dist-info}/RECORD +24 -22
- examples/_openaisdk/openai_edits.py +48 -0
- examples/_openaisdk/openai_images.py +12 -3
- examples/_openaisdk/openai_lingyi.py +44 -10
- examples/_openaisdk/openai_siliconflow.py +3 -3
- meutils/apis/hailuoai/images.py +2 -2
- meutils/apis/hailuoai/videos.py +4 -2
- meutils/apis/oneapi/user.py +3 -3
- meutils/apis/ppio/videos.py +1 -1
- meutils/apis/utils.py +7 -6
- meutils/data/VERSION +1 -1
- meutils/llm/check_utils.py +24 -12
- meutils/llm/completions/qwenllm.py +5 -4
- meutils/llm/openai_utils/usage_utils.py +95 -0
- meutils/llm/utils.py +1 -54
- meutils/schemas/image_types.py +3 -2
- meutils/schemas/oneapi/common.py +8 -7
- meutils/schemas/openai_types.py +2 -1
- meutils/serving/fastapi/dependencies/headers.py +19 -15
- {MeUtils-2025.6.20.18.7.13.dist-info → MeUtils-2025.6.24.14.6.6.dist-info}/LICENSE +0 -0
- {MeUtils-2025.6.20.18.7.13.dist-info → MeUtils-2025.6.24.14.6.6.dist-info}/WHEEL +0 -0
- {MeUtils-2025.6.20.18.7.13.dist-info → MeUtils-2025.6.24.14.6.6.dist-info}/entry_points.txt +0 -0
- {MeUtils-2025.6.20.18.7.13.dist-info → MeUtils-2025.6.24.14.6.6.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,95 @@
|
|
1
|
+
#!/usr/bin/env python
|
2
|
+
# -*- coding: utf-8 -*-
|
3
|
+
# @Project : AI. @by PyCharm
|
4
|
+
# @File : usage_utils
|
5
|
+
# @Time : 2025/6/24 08:53
|
6
|
+
# @Author : betterme
|
7
|
+
# @WeChat : meutils
|
8
|
+
# @Software : PyCharm
|
9
|
+
# @Description :
|
10
|
+
"""
|
11
|
+
1. 同步任务(流 非流)
|
12
|
+
- 按次
|
13
|
+
- 按量
|
14
|
+
2. 异步任务
|
15
|
+
- 按次
|
16
|
+
- 按量
|
17
|
+
"""
|
18
|
+
|
19
|
+
from meutils.pipe import *
|
20
|
+
from meutils.llm.clients import AsyncOpenAI
|
21
|
+
from meutils.apis.utils import make_request
|
22
|
+
|
23
|
+
base_url = "https://api.chatfire.cn"
|
24
|
+
|
25
|
+
|
26
|
+
async def create_usage_for_async_task(model: str = "usage-async"):
|
27
|
+
# 计费
|
28
|
+
_ = await make_request(
|
29
|
+
base_url=base_url,
|
30
|
+
path=f"/flux/v1/{model}",
|
31
|
+
payload={
|
32
|
+
"id": "123456",
|
33
|
+
"completion_tokens": 100,
|
34
|
+
"total_tokens": 200
|
35
|
+
}
|
36
|
+
# "/flux/v1/get_result",
|
37
|
+
)
|
38
|
+
|
39
|
+
return _
|
40
|
+
|
41
|
+
|
42
|
+
async def get_async_task(id: str = "123456", status: str = "FAILURE"):
|
43
|
+
# 计费
|
44
|
+
_ = await make_request(
|
45
|
+
base_url=base_url,
|
46
|
+
path=f"/flux/v1/get_result?id={id}&status={status}",
|
47
|
+
|
48
|
+
method="GET"
|
49
|
+
)
|
50
|
+
|
51
|
+
return _
|
52
|
+
|
53
|
+
|
54
|
+
async def create_usage_for_tokens(model: str = "usage-chat", mode: Literal['chat', 'image'] = 'chat'):
|
55
|
+
client = AsyncOpenAI() # 外部任务
|
56
|
+
if mode == "image": # todo: 参数进不去 要不通过 prompt
|
57
|
+
_ = await client.images.generate(
|
58
|
+
model=model,
|
59
|
+
prompt="ChatfireAPI",
|
60
|
+
n=10,
|
61
|
+
size="1024x1024",
|
62
|
+
extra_body={
|
63
|
+
"extra_fields": {
|
64
|
+
"input_tokens": 1,
|
65
|
+
"input_tokens_details": {
|
66
|
+
"image_tokens": 0,
|
67
|
+
"text_tokens": 1
|
68
|
+
},
|
69
|
+
"output_tokens": 1,
|
70
|
+
"total_tokens": 2
|
71
|
+
}
|
72
|
+
}
|
73
|
+
)
|
74
|
+
else:
|
75
|
+
_ = await client.chat.completions.create(
|
76
|
+
model=model,
|
77
|
+
messages=[
|
78
|
+
{"role": "user", "content": "ChatfireAPI"}
|
79
|
+
],
|
80
|
+
metadata={
|
81
|
+
"prompt_tokens": 100,
|
82
|
+
"completion_tokens": 100,
|
83
|
+
"total_tokens": 200
|
84
|
+
}
|
85
|
+
)
|
86
|
+
return _
|
87
|
+
|
88
|
+
|
89
|
+
if __name__ == '__main__':
|
90
|
+
# arun(create_usage_for_tokens())
|
91
|
+
arun(create_usage_for_tokens(mode='image'))
|
92
|
+
|
93
|
+
# arun(create_usage_for_async_task())
|
94
|
+
|
95
|
+
# arun(get_async_task('xx'))
|
meutils/llm/utils.py
CHANGED
@@ -17,59 +17,6 @@ from meutils.notice.feishu import send_message
|
|
17
17
|
from meutils.apis.oneapi.utils import get_user_quota
|
18
18
|
|
19
19
|
|
20
|
-
async def ppu(
|
21
|
-
model: str = 'ppu',
|
22
|
-
api_key: Optional[str] = None,
|
23
|
-
base_url: Optional[str] = None,
|
24
|
-
):
|
25
|
-
if model not in MODEL_PRICE:
|
26
|
-
send_message(f"模型未找到{model},ppu-1默认", title=__name__)
|
27
|
-
model = "ppu-1"
|
28
|
-
|
29
|
-
client = AsyncOpenAI(api_key=api_key, base_url=base_url)
|
30
|
-
r = await client.chat.completions.create(messages=[{'role': 'user', 'content': 'hi'}], model=model)
|
31
|
-
|
32
|
-
|
33
|
-
@asynccontextmanager
|
34
|
-
async def ppu_flow(
|
35
|
-
api_key: str,
|
36
|
-
base_url: Optional[str] = None,
|
37
|
-
|
38
|
-
model: str = "ppu-1", # 后计费
|
39
|
-
|
40
|
-
n: float = 1, # 计费次数
|
41
|
-
|
42
|
-
**kwargs
|
43
|
-
):
|
44
|
-
"""
|
45
|
-
查余额
|
46
|
-
失败,先扣费
|
47
|
-
成功,充足,后扣费
|
48
|
-
成功,不足,报错
|
49
|
-
"""
|
50
|
-
n = int(np.ceil(n)) # 0 不计费
|
51
|
-
|
52
|
-
if n: # 计费
|
53
|
-
try:
|
54
|
-
money = await get_user_quota(api_key)
|
55
|
-
logger.debug(f"PREPAY: USER 余额 {money}")
|
56
|
-
except Exception as e:
|
57
|
-
logger.error(e)
|
58
|
-
money = None
|
59
|
-
|
60
|
-
if money and money > MODEL_PRICE.get(model, 0.1):
|
61
|
-
yield # 先执行
|
62
|
-
|
63
|
-
# 执行计费逻辑
|
64
|
-
await asyncio.gather(*[ppu(model, api_key=api_key, base_url=base_url) for _ in range(n)])
|
65
|
-
|
66
|
-
if money is None:
|
67
|
-
yield # 后执行
|
68
|
-
|
69
|
-
else: # 不计费
|
70
|
-
yield
|
71
|
-
|
72
|
-
|
73
20
|
def oneturn2multiturn(messages, template: Optional[str] = None, ignore_system: bool = True):
|
74
21
|
"""todo: https://github.com/hiyouga/LLaMA-Factory/blob/e898fabbe3efcd8b44d0e119e7afaed4542a9f39/src/llmtuner/data/template.py#L423-L427
|
75
22
|
|
@@ -162,4 +109,4 @@ if __name__ == '__main__':
|
|
162
109
|
|
163
110
|
]
|
164
111
|
|
165
|
-
print(oneturn2multiturn(messages,ignore_system=False))
|
112
|
+
print(oneturn2multiturn(messages, ignore_system=False))
|
meutils/schemas/image_types.py
CHANGED
@@ -91,7 +91,7 @@ class ImageRequest(BaseModel): # openai
|
|
91
91
|
"""
|
92
92
|
model: str = ''
|
93
93
|
|
94
|
-
prompt: constr(min_length=1, max_length=
|
94
|
+
prompt: constr(min_length=1, max_length=10240) = ""
|
95
95
|
|
96
96
|
n: Optional[int] = 1
|
97
97
|
|
@@ -105,7 +105,8 @@ class ImageRequest(BaseModel): # openai
|
|
105
105
|
|
106
106
|
seed: Optional[int] = None
|
107
107
|
|
108
|
-
# oneapi
|
108
|
+
# oneapi https://github.com/QuantumNous/new-api/blob/main/dto/dalle.go
|
109
|
+
extra_fields: Optional[str] = None
|
109
110
|
negative_prompt: Optional[str] = None
|
110
111
|
guidance: Optional[float] = None
|
111
112
|
steps: Optional[int] = None
|
meutils/schemas/oneapi/common.py
CHANGED
@@ -95,6 +95,8 @@ MODEL_PRICE = {
|
|
95
95
|
|
96
96
|
# veo
|
97
97
|
"veo3": 2,
|
98
|
+
"veo3-pro": 8,
|
99
|
+
"veo3-pro-frames": 8,
|
98
100
|
"api-veo3": 2,
|
99
101
|
"api-veo3-pro": 8,
|
100
102
|
"api-veo3-pro-frames": 8,
|
@@ -102,7 +104,7 @@ MODEL_PRICE = {
|
|
102
104
|
# hailuo
|
103
105
|
"api-minimax-hailuo-01-6s": 0.5,
|
104
106
|
"api-minimax-hailuo-02-6s-768p": 1,
|
105
|
-
"api-minimax-hailuo-02-10s-768p":
|
107
|
+
"api-minimax-hailuo-02-10s-768p": 2,
|
106
108
|
"api-minimax-hailuo-02-6s-1080p": 2,
|
107
109
|
|
108
110
|
# chatfire
|
@@ -136,8 +138,7 @@ MODEL_PRICE = {
|
|
136
138
|
"ppt-islide": 0.1,
|
137
139
|
|
138
140
|
# grok
|
139
|
-
"grok-3": 0.
|
140
|
-
"grok-3-image": 0.02,
|
141
|
+
"grok-3-image": 0.1,
|
141
142
|
|
142
143
|
# 虚拟换衣fish
|
143
144
|
"api-kolors-virtual-try-on": 0.1,
|
@@ -478,10 +479,10 @@ MODEL_RATIO = {
|
|
478
479
|
"grok-2": 1,
|
479
480
|
"grok-2-1212": 1,
|
480
481
|
"grok-2-vision-1212": 1,
|
481
|
-
"grok-3":
|
482
|
-
"grok-3-deepsearch":
|
483
|
-
"grok-3-reasoner":
|
484
|
-
"grok-3-deepersearch":
|
482
|
+
"grok-3": 1.5,
|
483
|
+
"grok-3-deepsearch": 1.5,
|
484
|
+
"grok-3-reasoner": 1.5,
|
485
|
+
"grok-3-deepersearch": 1.5,
|
485
486
|
|
486
487
|
"grok-3-beta": 1.5,
|
487
488
|
"grok-3-fast-beta": 2.5,
|
meutils/schemas/openai_types.py
CHANGED
@@ -102,6 +102,7 @@ class CompletionRequest(BaseModel):
|
|
102
102
|
# [{'role': 'user', 'content': [{"type": "image_url", "image_url": {"url": ""}}]}] # 也兼容文件
|
103
103
|
# [{'role': 'user', 'content': [{"type": "file", "file_url": ""}]]
|
104
104
|
messages: Optional[List[Dict[str, Any]]] = None # 标准化
|
105
|
+
metadata: Optional[Dict[str, str]] = None
|
105
106
|
|
106
107
|
stream: Optional[bool] = False
|
107
108
|
stream_options: Optional[dict] = None
|
@@ -628,4 +629,4 @@ if __name__ == '__main__':
|
|
628
629
|
#
|
629
630
|
# print(chat_completion_chunk)
|
630
631
|
|
631
|
-
print(ImageRequest(prompt='xx'))
|
632
|
+
print(ImageRequest(prompt='xx'))
|
@@ -13,19 +13,23 @@ from fastapi import FastAPI, Request, Depends, HTTPException
|
|
13
13
|
from typing import Dict, Optional
|
14
14
|
|
15
15
|
|
16
|
-
# 定义一个依赖函数来获取所有请求头
|
17
|
-
# def get_headers(request: Request) -> Dict[str, str]:
|
18
|
-
# return dict(request.headers)
|
19
|
-
|
20
16
|
def get_headers(request: Request):
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
17
|
+
dic = dict(request.headers)
|
18
|
+
_dic = {k.replace('-', '_'): v for k, v in dic.items()}
|
19
|
+
|
20
|
+
return {**dic, **_dic}
|
21
|
+
|
22
|
+
|
23
|
+
if __name__ == '__main__':
|
24
|
+
def get_headers():
|
25
|
+
d = {"upstream-base-url": 'xx'}
|
26
|
+
d = {}
|
27
|
+
dic = {}
|
28
|
+
# upstream_base_url = headers.get('upstream-base-url')
|
29
|
+
if d:
|
30
|
+
dic = {k.replace('-', '_'): v for k, v in d.items()}
|
31
|
+
|
32
|
+
return {**d, **dic}
|
33
|
+
|
34
|
+
|
35
|
+
print(get_headers())
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|