标题: 🎉Cohere to OpenAI源码震撼来袭!
作者: #F-Droid
板块: #开发调优
编号:
帖子: https://linux.do/t/topic/913711
时间: 2025-08-28 16:40:37
摘要:
作者: #F-Droid
板块: #开发调优
编号:
913711帖子: https://linux.do/t/topic/913711
时间: 2025-08-28 16:40:37
摘要:
import json
import logging
import os
import secrets
from datetime import datetime
import httpx
from fastapi import FastAPI, Request, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import StreamingResponse, Response
from tenacity import retry, stop_after_attempt, wait_exponential, retry_if_exception_type
# --------------- 日志配置 -------------------
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO").upper()
logging.basicConfig(
level=LOG_LEVEL,
format='%(asctime)s | %(levelname)-8s | %(name)s:%(lineno)d | %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
logger = logging.getLogger("cohere-proxy")
# --------------- 应用初始化 -----------------
app = FastAPI(
title="Cohere OpenAI代理",
description="一个生产级别的、完全兼容的代理。",
version="1.0.0",
docs_url=None,
redoc_url=None
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# --------------- 常量配置 -------------------
COHERE_BASE_URL = os.getenv("COHERE_BASE_URL", "https://api.cohere.ai")
COHERE_USER_AGENT = "cohere-py/5.6.0"
BASE_CREATED = 1700000000
# 参数映射表
COHERE_TO_OPENAI_MAP = {
"temperature": "temperature",
"max_tokens": "max_tokens",
"seed": "seed",
"stop": "stop_sequences",
}
# --------------- 工具函数 -------------------
def get_httpx_client():
return httpx.AsyncClient(timeout=httpx.Timeout(connect=30.0, read=300.0, write=30.0, pool=10.0))
@retry(
stop=stop_after_attempt(3),
wait=wait_exponential(multiplier=1, max=10),
retry=(
retry_if_exception_type(httpx.ConnectError)
| retry_if_exception_type(httpx.ConnectTimeout)
| retry_if_exception_type(httpx.ReadTimeout)
),
reraise=True,
)
async def make_request_with_retry(client, method, url, **kwargs):
logger.debug(f"[PROXY -> COHERE] Request: {method} {url}")
if 'json' in kwargs:
logger.debug(f"[PROXY -> COHERE] Body:\n{json.dumps(kwargs.get('json'), ensure_ascii=False, indent=2)}")
response = await client.request(method, url, **kwargs)
logger.info(f"Upstream Response | {method} | {url} | -> | {response.status_code}")
return response
async def get_auth_key(request: Request) -> str:
auth_header = request.headers.get("Authorization")
if auth_header and auth_header.startswith("Bearer "):
key = auth_header[7:].strip()
if key: return key
key = request.query_params.get("key")
if key: return key.strip()
try:
body = await request.json()
if isinstance(body, dict) and "key" in body:
return str(body["key"]).strip()
except:
pass
raise HTTPException(status_code=401, detail={"error": {"message": "未提供Cohere API密钥。"}})
def map_finish_reason(cohere_reason: str) -> str:
mapping = {
"COMPLETE": "stop",
"MAX_TOKENS": "length",
"TOO_MANY_TOKENS": "length",
"ERROR": "error",
"CONTENT_FILTERED": "content_filter",
"TOOL_CALL": "tool_calls"
}
return mapping.get(cohere_reason.upper(), "stop")
# --------------- 路由定义 -------------------
@app.get("/", include_in_schema=False)
async def root():
html = """
<html><body style="text-align:center; font-family:sans-serif; margin-top:4rem;">
<h1>✅ Cohere OpenAI代理运行就绪</h1>
</body></html>
"""
return Response(content=html, media_type="text/html")
@app.get("/v1/models")
async def list_models(request: Request):
"""
获取所有可用的Cohere模型。
"""
auth = await get_auth_key(request)
headers = {"Authorization": f"Bearer {auth}", "User-Agent": COHERE_USER_AGENT}
async with get_httpx_client() as client:
res = await make_request_with_retry(client, "GET", f"{COHERE_BASE_URL}/v1/models", headers=headers)
if res.status_code != 200:
raise HTTPException(status_code=res.status_code, detai
标题: 时隔2年再看 dapr 还是没有发展起来,心里感觉好像看到了自己
作者: #我就是那个ATM
板块: #搞七捻三
编号:
帖子: https://linux.do/t/topic/913712
时间: 2025-08-28 16:40:45
摘要:
作者: #我就是那个ATM
板块: #搞七捻三
编号:
913712帖子: https://linux.do/t/topic/913712
时间: 2025-08-28 16:40:45
摘要:
Dapr - 分布式应用程序运行时 — Dapr - Distributed Application Runtime
本人实践过 dapr Java实现,让我感觉的确和Java传统的nacos分布式完全不一样。
所有的操作都是都是通过 dapr 来操作,一方面 dapr 感觉很重,另一方面也感觉 dapr 让我感觉
无论是跨语言,还是切换底层云服务商,那真的是分分钟钟的事情。
但是为什么没有发展起来呢,我感觉几点吧。
人们舒适度的问题,springboot-nacos 这一套用了好几年,为什么要换,换了出问题怎么解决,各种成本在里面
感觉不完善,缺乏生态的支撑,比如没有插件生态,感觉让这个 dapr 缺少了一个灵魂
没有氛围,无论是社区还是行业内,都缺少活跃度。
也许未来 dapr 会崛起,但是绝对不是现在。
刚开始接触 dapr 的时候,我觉得 dapr 是非常好微服务解决方案,随着时间的成长,我看到各种局限,也懂得各种利弊,不是技术好,就是好
标题: openrouter上banana可以用啊
作者: #脸脸
板块: #搞七捻三
编号:
帖子: https://linux.do/t/topic/913717
时间: 2025-08-28 16:42:29
摘要:
作者: #脸脸
板块: #搞七捻三
编号:
913717帖子: https://linux.do/t/topic/913717
时间: 2025-08-28 16:42:29
摘要:
做做表情包可以啊~,一张也没多少钱
标题: 有可靠的api平台推荐吗
作者: #edawrz
板块: #搞七捻三
编号:
帖子: https://linux.do/t/topic/913723
时间: 2025-08-28 16:44:13
摘要:
作者: #edawrz
板块: #搞七捻三
编号:
913723帖子: https://linux.do/t/topic/913723
时间: 2025-08-28 16:44:13
摘要:
不用翻墙,放在cherry studio里能用的,主要是谷歌2.5 pro,claude chatgpt,免费或者收费合理、稳定、保真的
标题: 1password扫码登录问题
作者: #yuan jddφθτ
板块: #搞七捻三
编号:
帖子: https://linux.do/t/topic/913751
时间: 2025-08-28 16:51:38
摘要:
作者: #yuan jddφθτ
板块: #搞七捻三
编号:
913751帖子: https://linux.do/t/topic/913751
时间: 2025-08-28 16:51:38
摘要:
因为不想输入secret key+密码来登录(主要是前者有些麻烦),所以我尝试使用扫码登录,但用1password的app扫码的时候,好像什么都没发生,只好用前者的方式登录。有佬友遇到过这样的问题吗?
标题: 苹果模仿赛上 苹果获得了第二名
作者: #𝓵𝓮𝔃𝓲𝓼𝓱𝓮𝓷
板块: #搞七捻三
编号:
帖子: https://linux.do/t/topic/913754
时间: 2025-08-28 16:52:27
摘要:
作者: #𝓵𝓮𝔃𝓲𝓼𝓱𝓮𝓷
板块: #搞七捻三
编号:
913754帖子: https://linux.do/t/topic/913754
时间: 2025-08-28 16:52:27
摘要:
如果他这个保真的情况下
镜态玻璃也做了 灵动岛也做了
懂了 这就是苹果
标题: 512MB 内存,8GB SSD,求推荐好玩的自建服务
作者: #relaxcn
板块: #开发调优
编号:
帖子: https://linux.do/t/topic/913756
时间: 2025-08-28 16:52:55
摘要:
作者: #relaxcn
板块: #开发调优
编号:
913756帖子: https://linux.do/t/topic/913756
时间: 2025-08-28 16:52:55
摘要:
如期,买了一个日本VPS搭梯子,但只有 512MB 内存,8GB SSD。
佬们有没有什么好玩的项目可以搭建的,最好轻量一点的,配置太低了。
标题: certimate部署证书到1Panel从节点失败
作者: #てんどう そうじ
板块: #开发调优
编号:
帖子: https://linux.do/t/topic/913760
时间: 2025-08-28 16:53:26
摘要:
作者: #てんどう そうじ
板块: #开发调优
编号:
913760帖子: https://linux.do/t/topic/913760
时间: 2025-08-28 16:53:26
摘要:
月初的时候通过Certimate申请了几个域名证书,并成功下发到了主节点和从节点中
最近又搞了几个新的域名,想要把证书进行替换更新,执行工作流时报错,可以看到主节点是正常部署的,但两个从节点部署全部失败了。
有没有佬知道是什么问题,需要怎么处理呢
标题: 捡垃圾配NAS,配置篇 400元全套(真全套,显示器都有)
作者: #lysk797
板块: #搞七捻三
编号:
帖子: https://linux.do/t/topic/913761
时间: 2025-08-28 16:53:44
摘要:
作者: #lysk797
板块: #搞七捻三
编号:
913761帖子: https://linux.do/t/topic/913761
时间: 2025-08-28 16:53:44
摘要:
配件
型号
价格
渠道
CPU
G4600
30
淘宝
主板
华硕prime b250-plus
111.03
闲鱼
散热
玄冰400
14.3
拼多多
内存
芝奇 ddr4 2400 8G
45
闲鱼
机箱
威盛家盛 台式机 ATX
56.1
京东
电源
长城BTX-400SD 额定300W
30
拼多多
硬盘
WD 500G 蓝盘+硬盘线
26
闲鱼
士必得 M1 120G
28
闲鱼
显示器
Founder/方正显示器 22寸
48
闲鱼
合计
388.43
标题: 你们的作业做完了吗
作者: #KXG
板块: #搞七捻三
编号:
帖子: https://linux.do/t/topic/913763
时间: 2025-08-28 16:54:12
摘要:
作者: #KXG
板块: #搞七捻三
编号:
913763帖子: https://linux.do/t/topic/913763
时间: 2025-08-28 16:54:12
摘要:
各位初高中生,你们的作业做完了吗
Click to view the poll.
标题: 【抽奖】6个augment token
作者: #刘西瓜
板块: #福利羊毛
编号:
帖子: https://linux.do/t/topic/913771
时间: 2025-08-28 16:57:52
摘要:
作者: #刘西瓜
板块: #福利羊毛
编号:
913771帖子: https://linux.do/t/topic/913771
时间: 2025-08-28 16:57:52
摘要:
回馈佬友,将自己26号注册的augment token共享出来,其中可能有1、2个token用过几次,其他都是满额度,token随机发放,如果佬友领到的不满50次,请见谅。
奖品详情:
6个 augment 的 token
活动时间:
开始时间:发帖时间
截止时间:2025.08.29 14:00
参与方式:
在本帖下回复任意内容。
抽奖规则:
每位用户仅允许参与一次。
使用社区抽奖工具随机抽取中奖者。
注意事项:
本活动将在活动截止时间后关闭回帖,以确保公正性。
中奖者将在活动结束后 3 小时内在本帖公布,并通过站内信通知领奖方式。
所有规则及抽奖结果由活动发起人和论坛管理团队最终解释。
期待您的积极参与,祝您好运!如有任何疑问,欢迎随时联系抽奖发起人。
标题: 父母养老保险问题请教
作者: #大姨的二舅
板块: #搞七捻三
编号:
帖子: https://linux.do/t/topic/913772
时间: 2025-08-28 16:57:58
摘要:
作者: #大姨的二舅
板块: #搞七捻三
编号:
913772帖子: https://linux.do/t/topic/913772
时间: 2025-08-28 16:57:58
摘要:
佬友们,关于父母养老保险的问题想请教请教。
家在湖北农村,父母今年都47岁了,但是都没有买养老保险。我是在北京读研究生,马上毕业了,想尽早给他们买上养老保险,但是对这块完全不了解。不知道一些政策、养老保险的种类、怎么买等等,希望佬友们可以传授传授经验。
标题: 我是不是亏了?
作者: #𝓵𝓸𝓿𝓮𝓛𝓾
板块: #搞七捻三
编号:
帖子: https://linux.do/t/topic/913779
时间: 2025-08-28 16:58:48
摘要:
作者: #𝓵𝓸𝓿𝓮𝓛𝓾
板块: #搞七捻三
编号:
913779帖子: https://linux.do/t/topic/913779
时间: 2025-08-28 16:58:48
摘要:
我用佬友分享的pplx cursor连接注册了一个新号,这个是直接前三个月全免费吗?因为我怕扣钱直接注册完就把订阅和自动扣款关了,是不是亏大了呀。
标题: 中国民企500强出炉!京东居然排第一?超过腾讯阿里?
作者: #九天
板块: #搞七捻三
编号:
帖子: https://linux.do/t/topic/913784
时间: 2025-08-28 16:59:01
摘要:
作者: #九天
板块: #搞七捻三
编号:
913784帖子: https://linux.do/t/topic/913784
时间: 2025-08-28 16:59:01
摘要:
中国民企500强出炉!(附榜单)
不知道这个营收总额是怎么来的,一直觉得腾讯>阿里>京东,还有华为比亚迪这些,感觉应该都比京东要强啊?
标题: 微软的copilot可以用gpt5还可以直接读取桌面和本地文件,还不用家宽,看起来不错呀
作者: #eugene
板块: #搞七捻三
编号:
帖子: https://linux.do/t/topic/913787
时间: 2025-08-28 17:00:27
摘要:
作者: #eugene
板块: #搞七捻三
编号:
913787帖子: https://linux.do/t/topic/913787
时间: 2025-08-28 17:00:27
摘要:
买的家宽小水管总是卡卡的,copilot看起来还可以