Skip to content

Commit

Permalink
Merge pull request #77 from RockChinQ/fix/duplicated-context-in-resp
Browse files Browse the repository at this point in the history
Fix(revChatGPT): duplicated context in response
  • Loading branch information
RockChinQ authored Jan 22, 2024
2 parents 571f60a + 91a28e9 commit c8b096a
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 31 deletions.
13 changes: 9 additions & 4 deletions docs/en/Adapters.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,23 @@ ChatGPT official website reverse engineering library

ChatGPT needs to use a reverse proxy to bypass Cloudflare's restrictions. The Free One API project defaults to the proxy address provided by the developer `https://chatproxy.rockchin.top/api/`, but the pressure is very high. It is strongly recommended to build a reverse proxy by yourself.

* Please configure according to this project document: <https://github.com/acheong08/ChatGPT-Proxy-V4>
* You can build it using the following projects, if they are all unavailable, please find other reverse proxies by yourself:
- https://github.com/flyingpot/chatgpt-proxy (Recommended)
- https://github.com/acheong08/ChatGPT-Proxy-V4 (Unavailable)

Edit `misc.chatgpt_api_base` to your reverse proxy address in `data/config.yaml`.
You can also enter directly in the `Config` column when creating the `acheong08/ChatGPT` adapter
Modify `adapters.acheong08_ChatGPT.reverse_proxy` to your reverse proxy address in `data/config.yaml`.
You can also directly enter in the `Config` column when creating the `acheong08/ChatGPT` adapter

```json
{
"reverse_proxy": "your reverse proxy address"
}
```

If not set, the `misc.chatgpt_api_base` field in the configuration file will be used as the reverse proxy address.
Set the reverse proxy address used by this adapter.

> **WARNING**
> The current reverse proxy may have [the situation of repeating the previous text](https://github.com/RockChinQ/free-one-api/issues/75)(CN), `free-one-api` will automatically delete duplicate content. If unexpected situations occur, please set `adapters.acheong08_ChatGPT.auto_ignore_duplicated` to `false` to disable this feature.
## KoushikNavuluri/Claude-API

Expand Down
15 changes: 10 additions & 5 deletions docs/en/Config.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@
Configuration file is saved at `data/config.yaml`

```yaml
# Global configuration for each adapter
adapters:
acheong08_ChatGPT:
# Whether to auto ignore duplicated response. see: https://github.com/RockChinQ/free-one-api/issues/75
auto_ignore_duplicated: false
# Reverse proxy address for acheong08/ChatGPT adapter.
# Default public reverse proxy may be unstable, it is recommended to build your own:
# https://github.com/flyingpot/chatgpt-proxy (Recommended)
# https://github.com/acheong08/ChatGPT-Proxy-V4 (Unavailable)
reverse_proxy: https://chatproxy.rockchin.top/api/
database:
# SQLite DB file path
path: ./data/free_one_api.db
type: sqlite
logging:
debug: false # Enable debug log
misc:
# Reverse proxy address for acheong08/ChatGPT adapter.
# Default public reverse proxy may be unstable, it is recommended to build your own:
# https://github.com/acheong08/ChatGPT-Proxy-V4
chatgpt_api_base: https://chatproxy.rockchin.top/api/
# Random advertisement, will be appended to the end of each response
random_ad:
# advertisement list
Expand Down
3 changes: 3 additions & 0 deletions docs/en/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Free One API Documentation

> **NOTE**
> Some of the content in this document is translated by GPT (GitHub Copilot).
## Supported LLM libs

|Adapter|Multi Round|Stream|Function Call|Status|Comment|
Expand Down
10 changes: 7 additions & 3 deletions docs/zh-CN/Adapters.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ ChatGPT 官网逆向工程库

ChatGPT 需要通过反向代理才能绕过 Cloudflare 的限制,Free One API 项目默认使用开发者提供的代理地址 `https://chatproxy.rockchin.top/api/` ,但压力很大,强烈建议自行搭建反向代理。

* 请根据此项目文档配置:<https://github.com/acheong08/ChatGPT-Proxy-V4>
* 可以根据以下项目搭建,若均不可用,请自行寻找其他反代:
- https://github.com/flyingpot/chatgpt-proxy (推荐)
- https://github.com/acheong08/ChatGPT-Proxy-V4 (不可用)

`data/config.yaml`中修改`misc.chatgpt_api_base`为你的反向代理地址即可。
`data/config.yaml`中修改`adapters.acheong08_ChatGPT.reverse_proxy`为你的反向代理地址即可。
也可以在创建 `acheong08/ChatGPT` 适配器时,直接在 `Config` 栏中输入

```json
Expand All @@ -44,7 +46,9 @@ ChatGPT 需要通过反向代理才能绕过 Cloudflare 的限制,Free One API
```

设置此适配器使用的反向代理地址。
若未设置,将使用配置文件中的 `misc.chatgpt_api_base` 字段作为反向代理地址。

> **WARNING**
> 目前使用的反代可能存在[重复回复前文的情况](https://github.com/RockChinQ/free-one-api/issues/75)`free-one-api` 会自动删除重复内容,若出现意想不到的情况,请设置`adapters.acheong08_ChatGPT.auto_ignore_duplicated``false` 以禁用此功能。
## KoushikNavuluri/Claude-API

Expand Down
15 changes: 10 additions & 5 deletions docs/zh-CN/Config.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@
配置文件位于`data/config.yaml`

```yaml
# 每个适配器的全局配置
adapters:
acheong08_ChatGPT:
# 是否自动忽略重复的响应 see: https://github.com/RockChinQ/free-one-api/issues/75
auto_ignore_duplicated: false
# acheong08/ChatGPT 适配器的反向代理地址
# 默认的公共反代可能不稳定,建议自行搭建,若下方项目均不可用,请自行寻找其他反代:
# https://github.com/flyingpot/chatgpt-proxy (推荐)
# https://github.com/acheong08/ChatGPT-Proxy-V4 (不可用)
reverse_proxy: https://chatproxy.rockchin.top/api/
database:
# SQLite 数据库文件路径
path: ./data/free_one_api.db
type: sqlite
logging:
debug: false # 是否开启调试日志
misc:
# acheong08/ChatGPT 适配器的反向代理路径
# 默认的公共反代可能不稳定,建议自行搭建:
# https://github.com/acheong08/ChatGPT-Proxy-V4
chatgpt_api_base: https://chatproxy.rockchin.top/api/
# 随机广告
# 会随机追加到每个响应的末尾
random_ad:
Expand Down
32 changes: 26 additions & 6 deletions free_one_api/impls/adapter/revChatGPT.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
@adapter.llm_adapter
class RevChatGPTAdapter(llm.LLMLibAdapter):

CHATGPT_API_BASE = "https://chatproxy.rockchin.top/api/"

@classmethod
def name(cls) -> str:
return "acheong08/ChatGPT"
Expand Down Expand Up @@ -83,8 +81,9 @@ def supported_path(self) -> str:
return "/v1/chat/completions"

cfg: dict = None
reverse_proxy: str = None

reverse_proxy: str = "https://chatproxy.rockchin.top/api/"
auto_ignore_duplicated: bool = True

_chatbot: chatgpt.AsyncChatbot = None

@property
Expand All @@ -100,18 +99,24 @@ def __init__(self, config: dict, eval: evaluation.AbsChannelEvaluation):
self.config = config
self.eval = eval

reverse_proxy = RevChatGPTAdapter.CHATGPT_API_BASE

config_copy = config.copy()

reverse_proxy = RevChatGPTAdapter.reverse_proxy
auto_ignore_duplicated = RevChatGPTAdapter.auto_ignore_duplicated

if 'reverse_proxy' in config_copy:
reverse_proxy = config_copy['reverse_proxy']

# delete reverse_proxy from config
del config_copy['reverse_proxy']

if 'auto_ignore_duplicated' in config_copy:
self.auto_ignore_duplicated = config_copy['auto_ignore_duplicated']
del config_copy['auto_ignore_duplicated']

self.cfg = config_copy
self.reverse_proxy = reverse_proxy
self.auto_ignore_duplicated = auto_ignore_duplicated

async def test(self) -> (bool, str):
conversation_id = ""
Expand All @@ -138,7 +143,13 @@ async def test(self) -> (bool, str):

async def query(self, req: request.Request) -> typing.AsyncGenerator[response.Response, None]:
new_messages = []

assistant_messages: list[str] = []

for i in range(len(req.messages)):
if req.messages[i]['role'] == "assistant":
assistant_messages.append(req.messages[i]['content'])

new_messages.append({
"id": str(uuid.uuid4()),
"author": {"role": req.messages[i]['role']},
Expand All @@ -164,6 +175,15 @@ async def query(self, req: request.Request) -> typing.AsyncGenerator[response.Re
prev_text = data["message"]
conversation_id = data["conversation_id"]

# skip if:
# 1. more than two spaces contained in the message
# and 2. message in any elements of the assistant messages
# and 3. auto_ignore_duplicated is True
if message.count(" ") >= 2 and \
any([message in msg for msg in assistant_messages]) and \
self.auto_ignore_duplicated:
continue

yield response.Response(
id=random_int,
finish_reason=response.FinishReason.NULL,
Expand Down
35 changes: 27 additions & 8 deletions free_one_api/impls/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,11 @@ async def run(self):
" (This response is sponsored by Free One API. Consider star the project on GitHub: https://github.com/RockChinQ/free-one-api )",
]
},
"misc": {
"chatgpt_api_base": "https://chatproxy.rockchin.top/api/",
"adapters": {
"acheong08_ChatGPT": {
"reverse_proxy": "https://chatproxy.rockchin.top/api/",
"auto_ignore_duplicated": True,
}
}
}

Expand All @@ -131,11 +134,6 @@ async def make_application(config_path: str) -> Application:
# dump config
with open(config_path, "w") as f:
yaml.dump(config, f)

# set default values
from .adapter import revChatGPT

revChatGPT.RevChatGPTAdapter.CHATGPT_API_BASE = config['misc']['chatgpt_api_base']

# logging
logging_level = logging.INFO
Expand Down Expand Up @@ -163,7 +161,7 @@ async def make_application(config_path: str) -> Application:
logging.getLogger().removeHandler(handler)

logging.getLogger().addHandler(terminal_out)

# save ad to runtime
if 'random_ad' in config and config['random_ad']['enabled']:
from ..common import randomad
Expand Down Expand Up @@ -192,6 +190,27 @@ async def make_application(config_path: str) -> Application:
dblogger.setFormatter(logging.Formatter("[%(asctime)s.%(msecs)03d] %(pathname)s (%(lineno)d) - [%(levelname)s] :\n%(message)s"))

logging.getLogger().addHandler(dblogger)

# set default values
# apply adapters config
if 'misc' in config and 'chatgpt_api_base' in config['misc']: # backward compatibility
config['adapters']['acheong08_ChatGPT']['reverse_proxy'] = config['misc']['chatgpt_api_base']

adapter_config_mapping = {
"acheong08_ChatGPT": revChatGPT.RevChatGPTAdapter,
"KoushikNavuluri_Claude-API": claude.ClaudeAdapter,
"dsdanielpark_Bard-API": bard.BardAdapter,
"xtekky_gpt4free": gpt4free.GPT4FreeAdapter,
"Soulter_hugging-chat-api": hugchat.HuggingChatAdapter,
"xw5xr6_revTongYi": qianwen.QianWenAdapter,
}

for adapter_name in adapter_config_mapping:
if adapter_name not in config['adapters']:
config['adapters'][adapter_name] = {}

for k, v in config["adapters"][adapter_name].items():
setattr(adapter_config_mapping[adapter_name], k, v)

# make channel manager
from .channel import mgr as chanmgr
Expand Down
4 changes: 4 additions & 0 deletions free_one_api/impls/forward/mgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ async def _gen():
continue

record.resp_message_length += len(resp.normal_message)

logging.debug("resp: {}".format(resp))

yield "data: {}\n\n".format(json.dumps({
"id": "chatcmpl-"+resp_id,
Expand Down Expand Up @@ -169,6 +171,8 @@ async def __non_stream_query(
if record.latency < 0:
record.latency = time.time() - before

logging.debug("resp: {}".format(resp))

if resp.normal_message is not None:
resp_tmp = resp
normal_message += resp.normal_message
Expand Down

0 comments on commit c8b096a

Please sign in to comment.