发帖
 找回密码
 立即注册
搜索
0 0 0
资源分享 864 0 6 小时前

因为一些平台,比如 groq、openrouter 等,提供的免费模型一般都有限制上下文,那是不是可以做一个中转,在实际进行模型调用前进行 token 计算,对于消息 token 数较短的请求就使用这些免费模型来响应,对于 token 数长的请求就正常调用。

正好,我最近发现这么一个项目

代码放在这里了

import { Plugin } from 'llm-hooks-sdk';
import { estimateTokenCount } from 'tokenx';

const TOKEN_ESTIMATE_ERROR_RATE = 1.1;

export default {
  beforeUpstreamRequest({ data, logger, metadata }) {
    const model = data.requestParams.model;
    if (!metadata[model]) {
      return null;
    }

    let modelContextThreshold: number;
    try {
      modelContextThreshold = Number.parseInt(metadata[model] as string);
    } catch {
      return null;
    }

    const messages = data.requestParams.messages;
    const messageContents = messages
      .filter(message => message.content &&
              typeof message.content === 'string')
      .map(message => message.content as string)
      .join('');
    const totalTokens = estimateTokenCount(messageContents);
    logger.info(`Total tokens: ${totalTokens}`);

    if (totalTokens * TOKEN_ESTIMATE_ERROR_RATE < modelContextThreshold) {
      const freeModelId = model + ':free';
      logger.info(`Routed to free model: ${freeModelId}`)
      return {
        requestParams: {
          ...data.requestParams,
          model: freeModelId,
        },
      };
    }
    return null;
  },
} satisfies Plugin;

在 LLM-Hooks 中添加插件脚本,并像这样配置 metadata 后
f7a48d09cf2df2f0048af20e678080e00e5d7174.webp

插件在检测到上下文低于阈值时会自动在模型名称上加上 :free 后缀,从而实现自动切换到免费短上下文模型的效果。


附一些实际使用时的日志:

[15:07:56.279] INFO (37): Total tokens: 34103
    module: "hook: beforeUpstreamRequest | plugin:context-router.ts"
[15:08:00.398] INFO (37): Total tokens: 33698
    module: "hook: beforeUpstreamRequest | plugin:context-router.ts"
[15:08:23.567] INFO (37): Total tokens: 33953
    module: "hook: beforeUpstreamRequest | plugin:context-router.ts"
[15:13:22.400] INFO (37): Total tokens: 18
    module: "hook: beforeUpstreamRequest | plugin:context-router.ts"
[15:13:22.400] INFO (37): Routed to free model: deepseek-v3.1:free
    module: "hook: beforeUpstreamRequest | plugin:context-router.ts"
[15:13:43.353] INFO (37): Total tokens: 35
    module: "hook: beforeUpstreamRequest | plugin:context-router.ts"
[15:13:43.353] INFO (37): Routed to free model: deepseek-v3.1:free
──── 0人觉得很赞 ────

使用道具 举报

把那些号商全都叫过来,是吧
那多轮对话的话,前后调用的模型会不会不一样啊?
回答质量会受影响吗?
思路差不多,毕竟要是这种智能体全都调用需要计费的应用程序编程接口(API),那也承受不住
对啊,就是多渠道的事儿。

如果后台调用的模型能力都差不多,那就没太大差别。要是大参数模型全都返回失败了,那就会返回到小模型,肯定会有影响。

这么做是为了确保一定能有输出。
下一步就是要确保模型能用。

当时为了解决这个问题,自己动手写了个GitHub项目——xunxun1982/failproxy 。

把模型按照复杂程度从高到低排序,(这个程序)永远都会有输出。

不过我现在开始用newapi + gptload了,只要池子中的密钥数量足够就没问题。这个项目就废弃不用了。
您需要登录后才可以回帖 立即登录
高级模式