Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the issue with the Query method in the Hugging Face reverse library. #117

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 35 additions & 40 deletions free_one_api/impls/adapter/hugchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@

@adapter.llm_adapter
class HuggingChatAdapter(llm.LLMLibAdapter):

@classmethod
def name(cls) -> str:
return "Soulter/hugging-chat-api"

@classmethod
def description(self) -> str:
return "Use Soulter/hugging-chat-api to access reverse engineering huggingchat."
Expand All @@ -36,30 +36,30 @@ def function_call_supported(self) -> bool:
return False

def stream_mode_supported(self) -> bool:
return True
return True

def multi_round_supported(self) -> bool:
return True

@classmethod
def config_comment(cls) -> str:
return \
"""Please provide email and passwd to sign up for HuggingChat:

{
"email": "your email",
"passwd": "your password"
}
"""Please provide email and passwd to sign up for HuggingChat:

{
"email": "your email",
"passwd": "your password"
}

Please refer to https://github.com/Soulter/hugging-chat-api
"""

Please refer to https://github.com/Soulter/hugging-chat-api
"""

@classmethod
def supported_path(self) -> str:
return "/v1/chat/completions"

_chatbot: hugchat.ChatBot = None

@property
def chatbot(self) -> hugchat.ChatBot:
if self._chatbot is None:
Expand All @@ -70,51 +70,46 @@ def chatbot(self) -> hugchat.ChatBot:
except:
cookie = sign.login()
sign.saveCookiesToDir("data/hugchatCookies")

self._chatbot = hugchat.ChatBot(cookies=cookie.get_dict())
return self._chatbot

def __init__(self, config: dict, eval: evaluation.AbsChannelEvaluation):
self.config = config
self.eval = eval

async def test(self) -> typing.Union[bool, str]:
try:
self.chatbot.change_conversation(self.chatbot.new_conversation())
for data in self.chatbot.query(
"Hi, respond 'Hello, world!' please.",
stream=True
):
for data in self.chatbot.chat("Hi, respond 'Hello, world!' please."):
pass

self.chatbot.delete_conversation(self.chatbot.current_conversation)

return True, ""
except Exception as e:
traceback.print_exc()
return False, str(e)
async def query(self, req: request.Request) -> typing.AsyncGenerator[response.Response, None]:

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

for msg in req.messages:
prompt += f"{msg['role']}: {msg['content']}\n"

prompt += "assistant: "

random_int = random.randint(0, 1000000000)
self.chatbot.change_conversation(self.chatbot.new_conversation())

for resp in self.chatbot.query(
text=prompt,
stream=True
):
yield response.Response(
id=random_int,
finish_reason=response.FinishReason.NULL,
normal_message=resp['token'],
function_call=None
)

for resp in self.chatbot.chat(prompt):
if resp is not None:
yield response.Response(
id=random_int,
finish_reason=response.FinishReason.NULL,
normal_message=resp['token'],
function_call=None
)
self.chatbot.delete_conversation(self.chatbot.current_conversation)

yield response.Response(
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ hugchat
g4f
revTongYi
colorlog
re_gpt
git+https://github.com/Zai-Kun/reverse-engineered-chatgpt