Spaces:
Sleeping
Sleeping
function calling ok
Browse files- app.py +1 -1
- routers/openai_v1_1.py +90 -0
app.py
CHANGED
|
@@ -16,7 +16,7 @@ set('project_root', parent_dir)
|
|
| 16 |
|
| 17 |
# from routers.webtools_v1 import router as webtools_router
|
| 18 |
from routers.users_v1 import router as users_router
|
| 19 |
-
from routers.
|
| 20 |
|
| 21 |
app = FastAPI()
|
| 22 |
|
|
|
|
| 16 |
|
| 17 |
# from routers.webtools_v1 import router as webtools_router
|
| 18 |
from routers.users_v1 import router as users_router
|
| 19 |
+
from routers.openai_v1_1 import router as openai_router
|
| 20 |
|
| 21 |
app = FastAPI()
|
| 22 |
|
routers/openai_v1_1.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, Depends, Request
|
| 2 |
+
import requests
|
| 3 |
+
from datetime import datetime
|
| 4 |
+
|
| 5 |
+
from global_state import get
|
| 6 |
+
from db.tbs_db import TbsDb
|
| 7 |
+
from auth import get_current_user
|
| 8 |
+
from db_model.user import UserModel
|
| 9 |
+
|
| 10 |
+
router = APIRouter()
|
| 11 |
+
|
| 12 |
+
db_module_filename = f"{get('project_root')}/db/cloudflare.py"
|
| 13 |
+
|
| 14 |
+
@router.post("/chat/completions")
|
| 15 |
+
async def chat_completions(request:Request, current_user: UserModel = Depends(get_current_user)):
|
| 16 |
+
data = await request.json()
|
| 17 |
+
model = data.get('model', '')
|
| 18 |
+
if (model=='')or(model is None):
|
| 19 |
+
model = await get_default_model()
|
| 20 |
+
api_key_info = await get_api_key(model)
|
| 21 |
+
api_key = api_key_info.get('api_key', '')
|
| 22 |
+
base_url = api_key_info.get('base_url', '')
|
| 23 |
+
|
| 24 |
+
headers = {
|
| 25 |
+
'Content-Type': 'application/json',
|
| 26 |
+
'Authorization': f'Bearer {api_key}'
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
response = requests.post(url=f"{base_url}/chat/completions", headers=headers, json=data)
|
| 31 |
+
# print(response.json())
|
| 32 |
+
return response.json()
|
| 33 |
+
except Exception as e:
|
| 34 |
+
print(e)
|
| 35 |
+
return {'error': e}
|
| 36 |
+
|
| 37 |
+
# 从数据库获取默认模型
|
| 38 |
+
async def get_default_model():
|
| 39 |
+
query = f"SELECT * FROM api_names order by default_order limit 1"
|
| 40 |
+
response = TbsDb(db_module_filename, "Cloudflare").get_item(query)
|
| 41 |
+
try:
|
| 42 |
+
result = response['result'][0]['results'][0]['api_name']
|
| 43 |
+
except:
|
| 44 |
+
result = ''
|
| 45 |
+
return result
|
| 46 |
+
|
| 47 |
+
async def get_api_key(model):
|
| 48 |
+
query = f"""
|
| 49 |
+
SELECT an.api_name, ak.api_key, an.base_url, ag.group_name
|
| 50 |
+
FROM api_keys ak
|
| 51 |
+
JOIN api_groups ag ON ak.api_group_id = ag.id
|
| 52 |
+
JOIN api_names an ON an.api_group_id = ag.id
|
| 53 |
+
WHERE ak.category='LLM' and an.api_name='{model}' and disabled=0
|
| 54 |
+
ORDER BY ak.last_call_at
|
| 55 |
+
limit 1
|
| 56 |
+
"""
|
| 57 |
+
response = TbsDb(db_module_filename, "Cloudflare").get_item(query)
|
| 58 |
+
try:
|
| 59 |
+
result = response['result'][0]['results'][0]
|
| 60 |
+
api_key = result['api_key']
|
| 61 |
+
except:
|
| 62 |
+
api_key = ''
|
| 63 |
+
|
| 64 |
+
query = f"update api_keys set last_call_at=datetime('now') where api_key='{api_key}'"
|
| 65 |
+
TbsDb(db_module_filename, "Cloudflare").execute_query(query)
|
| 66 |
+
return result
|
| 67 |
+
|
| 68 |
+
def convert_to_openai_format(original_json):
|
| 69 |
+
# 创建新的JSON对象
|
| 70 |
+
new_json = {
|
| 71 |
+
"id": "chatcmpl-123", # 这里可以生成一个唯一的ID,或者使用传入的id
|
| 72 |
+
"object": "chat.completion",
|
| 73 |
+
"created": int(datetime.now().timestamp()), # 当前时间戳
|
| 74 |
+
"choices": [
|
| 75 |
+
{
|
| 76 |
+
"index": 0,
|
| 77 |
+
"message": {
|
| 78 |
+
"role": "assistant",
|
| 79 |
+
"content": original_json.content # 使用原始内容
|
| 80 |
+
},
|
| 81 |
+
"finish_reason": "stop"
|
| 82 |
+
}
|
| 83 |
+
],
|
| 84 |
+
"usage": {
|
| 85 |
+
"prompt_tokens": original_json.usage_metadata.get("input_tokens",0),
|
| 86 |
+
"completion_tokens": original_json.usage_metadata.get("output_tokens", 0),
|
| 87 |
+
"total_tokens": original_json.usage_metadata.get("total_tokens", 0)
|
| 88 |
+
}
|
| 89 |
+
}
|
| 90 |
+
return new_json
|