ZGGSONG / STranslate

A ready-to-use, ready-to-go translation ocr tool developed by WPF/WPF 开发的一款即开即用、即用即走的翻译、OCR工具
https://stranslate.zggsong.com
MIT License
1.87k stars 104 forks source link

OpenAI 翻译自定义接口适配问题,接口路径中如果带前缀会替换错误 #209

Open kongwu- opened 1 day ago

kongwu- commented 1 day ago

Describe the bug 使用的是 openrouter 这个模型网关,接口字段里配置的是: https://openrouter.ai/api/

验证时请求的时: https://openrouter.ai/v1/chat/completions

但我实际的地址应该是前面还有个 api/ 的: https://openrouter.ai/api/v1/chat/completions

配置截图: image

版本: 1.2.4.919

建议的解决方案: 可以参考其他家的方式,这个字段不做任何处理,改个逻辑为:接口路径,或者baseUrl之类的东西,这样就可以直接配置:

  1. https://openrouter.ai/api/
  2. https://xxxx.ai/乱七八糟的前缀/
kongwu- commented 17 hours ago

或者可以参考下Bob的设计,也比较灵活

CleanShot 2024-09-30 at 09 07 26@2x

ZGGSONG commented 17 hours ago

适配带与不带path的两种uri,对于你的情况你直接填写完整的uri就可以了

https://openrouter.ai/api/v1/chat/completions
// 只有当用户输入uri的path部分不以/v1/chat/completions或者/v1/completions结尾才修改path
if (!uriBuilder.Path.EndsWith("/v1/chat/completions") && !uriBuilder.Path.EndsWith("/v1/completions"))
    uriBuilder.Path = "/v1/chat/completions";
kongwu- commented 6 hours ago

适配带与不带path的两种uri,对于你的情况你直接填写完整的uri就可以了

https://openrouter.ai/api/v1/chat/completions
// 只有当用户输入uri的path部分不以/v1/chat/completions或者/v1/completions结尾才修改path
if (!uriBuilder.Path.EndsWith("/v1/chat/completions") && !uriBuilder.Path.EndsWith("/v1/completions"))
    uriBuilder.Path = "/v1/chat/completions";

感谢作者配置完整URL可以解决,但还有另外一个小问题,导致openrouter里的openai无效:

openrouter 返回的 event stream body有一些自己的标记,导致解析失败了:

: OPENROUTER PROCESSING

: OPENROUTER PROCESSING

: OPENROUTER PROCESSING

: OPENROUTER PROCESSING

data: {"id":"gen-1727696504-dhr1dq311xtZJg4XylMO","provider":"OpenAI","model":"openai/gpt-4o-mini-2024-07-18","object":"chat.completion.chunk","created":1727696504,"choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null,"logprobs":null}],"system_fingerprint":"fp_f85bea6784"}

data: {"id":"gen-1727696504-dhr1dq311xtZJg4XylMO","provider":"OpenAI","model":"openai/gpt-4o-mini-2024-07-18","object":"chat.completion.chunk","created":1727696504,"choices":[{"index":0,"delta":{"role":"assistant","content":"Hello"},"finish_reason":null,"logprobs":null}],"system_fingerprint":"fp_f85bea6784"}

data: {"id":"gen-1727696504-dhr1dq311xtZJg4XylMO","provider":"OpenAI","model":"openai/gpt-4o-mini-2024-07-18","object":"chat.completion.chunk","created":1727696504,"choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":"stop","logprobs":null}],"system_fingerprint":"fp_f85bea6784"}

: OPENROUTER PROCESSING

data: {"id":"gen-1727696504-dhr1dq311xtZJg4XylMO","provider":"OpenAI","model":"openai/gpt-4o-mini-2024-07-18","object":"chat.completion.chunk","created":1727696504,"choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null,"logprobs":null}],"usage":{"prompt_tokens":44,"completion_tokens":1,"total_tokens":45}}

data: [DONE]

验证时的请求报文:

{
  "model": "openai/gpt-4o-mini-2024-07-18",
  "messages": [
    {
      "content": "You are a professional, authentic translation engine. You only return the translated text, without any explanations.",
      "role": "system"
    },
    {
      "content": "Please translate  into English (avoid explaining the original text):\r\n\r\n你好",
      "role": "user"
    }
  ],
  "temperature": 1.0,
  "stream": true
}
ZGGSONG commented 5 hours ago
image

这样就不是服务适配的问题了,而是你所使用的服务提供商api并不于openai官方一致导致的,可以给他们反馈一下这个问题。