ChatGPTNextWeb / ChatGPT-Next-Web

A cross-platform ChatGPT/Gemini UI (Web / PWA / Linux / Win / MacOS). 一键拥有你自己的跨平台 ChatGPT/Gemini 应用。
https://app.nextchat.dev/
MIT License
76.85k stars 59.29k forks source link

[Feature Request] 支持Openrouter 统计api请求源自哪个app(nextchat)的功能 #5781

Open RonnyJiang opened 2 weeks ago

RonnyJiang commented 2 weeks ago

🥰 Feature Description

Openrouter activity可以统计请求来自哪个app, 需要在header加上x-title, 希望可以支持如增加环境变量 OPENROUTER_APPNAME, 当启动container时设置OPENROUTER_APPNAME=nextchat 这样的就可以在发送请求前header构造时增加类似的代码:

if(process.env.OPENROUTER_APPNAME){
    header["X-Title"] = process.env.OPENROUTER_APPNAME;
    console.log("[Openrouter is set]", process.env.OPENROUTER_APPNAME);
  }

如官方给的api示例:https://openrouter.ai/docs/quick-start

import requests
import json

response = requests.post(
  url="https://openrouter.ai/api/v1/chat/completions",
  headers={
    "Authorization": f"Bearer {OPENROUTER_API_KEY}",
    "HTTP-Referer": f"{YOUR_SITE_URL}", # Optional, for including your app on openrouter.ai rankings.
    "X-Title": f"{YOUR_APP_NAME}", # Optional. Shows in rankings on openrouter.ai.
  },
  data=json.dumps({
    "model": "openai/gpt-3.5-turbo", # Optional
    "messages": [
      {
        "role": "user",
        "content": "What is the meaning of life?"
      }
    ]

  })
)

🧐 Proposed Solution

当使用openrouter时启动nextchat服务: docker run -d -e OPENROUTER_APPNAME=nextchat ...imagename

然后发送请求前可以增加 header["X-Title"] = process.env.OPENROUTER_APPNAME;

使openrouter可以统计到请求来自nextchat.或者是用户自定义的服务名。 image

📝 Additional Information

No response

Issues-translate-bot commented 2 weeks ago

Bot detected the issue body's language is not English, translate it automatically.


Title: [Feature Request] Support Openrouter's function of counting which app (nextchat) the api comes from

RonnyJiang commented 2 weeks ago

补充一下, headers中 "HTTP-Referer" 和"X-Title" 的组合才是一个统计组合,最后可以理解为 "HTTP-Referer"的值是key, "X-Title"为value
e.g.: 当header如下设置时, 会被统计为nextchat1

             header["HTTP-Referer"]="http://mynextchat.com"
             header["X-Title"]=nextchat1 
    然后再次请求时将header设置如下:     
             header["HTTP-Referer"]="http://mynextchat.com"
             header["X-Title"]=nextchat2
    这时 新的请求来源会被标记为nextchat2,同时之前被标记为nextchat1 的也会被更新为nextchat2,

      若发送一个新的HTTP-Referer,如
          header["HTTP-Referer"]="http://domain3.com"
          header["X-Title"]=nextchat3
   则此次请求被统计为源自nextchat3.   原来的nextchat2保持不变,因为HTTP-Referer不同。

也就是说"HTTP-Referer"是"X-Title"的唯一标识 所以需要支持两个环境变量OPENROUTER_APP, OPENROUTER_URL,或者再加一个USE_OPENROUTER控制isOpenRouter的变量

headers={ "Authorization": f"Bearer {OPENROUTER_API_KEY}", "HTTP-Referer": f"{YOUR_SITE_URL}", # Optional, for including your app on openrouter.ai rankings. "X-Title": f"{YOUR_APP_NAME}", # Optional. Shows in rankings on openrouter.ai. }