Spaces:
Runtime error
Runtime error
Merge branch 'main' into main
Browse filesFiles changed (9)
- README.md+4-8
- app.py+14-3
- llmriddles/llms/__init__.py+2-1
- llmriddles/llms/llm_client.pyAdded+54
- llmriddles/llms/llm_server.pyAdded+72
- llmriddles/llms/mistral.pyAdded+22
- llmriddles/questions/__init__.py+2
- llmriddles/questions/level4.pyAdded+97
- requirements.txt+3-1
28 unmodified lines2930313233343536373839404115 unmodified lines57585960616263646528 unmodified lines```shellQUESTION_LANG=en QUESTION_LLM='chatgpt' QUESTION_LLM_KEY=<your API key> python3 -u app.py```### LLaMA2-7b + 中文```shellQUESTION_LANG=cn QUESTION_LLM='llama2-7b' python3 -u app.py```### LLaMA2-7b + 英文```shellQUESTION_LANG=en QUESTION_LLM='llama2-7b' python3 -u app.py```## :technologist: 为什么制作这个游戏15 unmodified lines- [x] 支持自定义关卡- [ ] 在线试玩链接- [ ] Hugging Face Space 链接- [ ] 支持LLaMA2-7B(英文)- [ ] 支持Mistral-7B(英文)- [ ] 支持Baichuan2-7B(中文)- [ ] LLM 推理速度优化28 unmodified lines29303132333435363715 unmodified lines53545556575859606128 unmodified lines```shellQUESTION_LANG=en QUESTION_LLM='chatgpt' QUESTION_LLM_KEY=<your API key> python3 -u app.py```### Mistral-7B-Instruct-v0.1 + 英文```shellQUESTION_LANG=en QUESTION_LLM='mistral-7b' python3 -u app.py```## :technologist: 为什么制作这个游戏15 unmodified lines- [x] 支持自定义关卡- [ ] 在线试玩链接- [ ] Hugging Face Space 链接- [x] 支持Mistral-7B-Instruct-v0.1(英文)- [ ] 支持Baichuan2-7B(中文)- [ ] 支持LLaMA2-7B(英文)- [ ] LLM 推理速度优化
123451 unmodified line7891011121314151617181920101 unmodified lines1221231241251261271285 unmodified lines1341351361371381391401411 unmodified line143144145146147148149150import osimport uuidimport gradio as gr1 unmodified linefrom llmriddles.questions import list_ordered_questions_QUESTION_IDS = {}_QUESTIONS = list_ordered_questions()_LANG = os.environ.get('QUESTION_LANG', 'cn')assert _LANG in ['cn', 'en'], _LANG_LLM = os.environ.get('QUESTION_LLM', 'chatgpt')assert _LLM in ['chatgpt', 'llama2-7b'], _LLM_LLM_KEY = os.environ.get('QUESTION_LLM_KEY', None)if _LANG == "cn":title = "完蛋!我被 LLM 拿捏了"requirement_ph = """欢迎来到 LLM Riddles!101 unmodified linesgr_question = gr.TextArea(placeholder=question_ph, label=question_label)gr_api_key = gr.Text(placeholder=api_ph, label=api_label, type='password', visible=_need_api_key())with gr.Row():gr_submit = gr.Button(submit_label, interactive=True)gr_next = gr.Button(next_label)with gr.Column():5 unmodified linesdef _next_question(uuid_):if not uuid_:uuid_ = str(uuid.uuid4())global _QUESTION_IDS_qid = _QUESTION_IDS.get(uuid_, -1)_qid += 11 unmodified lineif _qid >= len(_QUESTIONS):del _QUESTION_IDS[uuid_]return game_cleared_label, '', '', {}, '', \gr.Button(submit_label, interactive=True), \gr.Button(try_again_label, interactive=True), \''else:1234561 unmodified line89101112131415161718192021222324252627101 unmodified lines1291301311321331341355 unmodified lines1411421431441451461471481491501511 unmodified line153154155156157158159160161import osimport uuidimport loggingimport gradio as gr1 unmodified linefrom llmriddles.questions import list_ordered_questions_QUESTION_IDS = {}count = 0_QUESTIONS = list_ordered_questions()_LANG = os.environ.get('QUESTION_LANG', 'cn')assert _LANG in ['cn', 'en'], _LANG_LLM = os.environ.get('QUESTION_LLM', 'chatgpt')assert _LLM in ['chatgpt', 'mistral-7b'], _LLM_LLM_KEY = os.environ.get('QUESTION_LLM_KEY', None)_DEBUG = os.environ.get('DEBUG', 'false').lower() == 'true'if _LANG == "cn":if _DEBUG:logging.getLogger().setLevel(logging.INFO)else:logging.getLogger().setLevel(logging.WARNING)title = "完蛋!我被 LLM 拿捏了"requirement_ph = """欢迎来到 LLM Riddles!101 unmodified linesgr_question = gr.TextArea(placeholder=question_ph, label=question_label)gr_api_key = gr.Text(placeholder=api_ph, label=api_label, type='password', visible=_need_api_key())with gr.Row():gr_submit = gr.Button(submit_label, interactive=False)gr_next = gr.Button(next_label)with gr.Column():5 unmodified linesdef _next_question(uuid_):global countif not uuid_:uuid_ = str(uuid.uuid4())count += 1logging.info(f'Player {count} starts the game now')global _QUESTION_IDS_qid = _QUESTION_IDS.get(uuid_, -1)_qid += 11 unmodified lineif _qid >= len(_QUESTIONS):del _QUESTION_IDS[uuid_]logging.info(f'Player {count} has passed the game now')return game_cleared_label, '', '', {}, '', \gr.Button(submit_label, interactive=False), \gr.Button(try_again_label, interactive=True), \''else:
12from .chatgpt import ask_chatgptfrom .base import register_llm, get_llm_fn123from .base import register_llm, get_llm_fnfrom .chatgpt import ask_chatgptfrom .mistral import ask_mistral_7b_instruct
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354import timeimport requestsimport loggingimport argparse
class LLMFlaskClient: def __init__(self, ip: str, port: int, max_retry: int = 3): self.ip = ip self.port = port
self.url_prefix_format = 'http://{}:{}/' self.url = self.url_prefix_format.format(self.ip, self.port) self.max_retry = max_retry
self.logger = logging.getLogger() self.logger.addHandler(logging.StreamHandler()) self.logger.handlers[0].setFormatter(logging.Formatter("%(message)s"))
def _request(self, name: str, data: dict): for _ in range(self.max_retry): try: self.logger.info(f'{name}\ndata: {data}') response = requests.post(self.url + name, json=data).json() except Exception as e: self.logger.warning('error: ', repr(e)) time.sleep(1) continue if response['code'] == 0: return response['output'] else: raise Exception(response['error_msg']) raise Exception("Web service failed. Please retry or contact with manager")
def run(self, message: str) -> str: try: return self._request('ask_llm_for_answer', {'user_text': message}) except Exception as e: return f"Error: {repr(e)}"
if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument('--ip', required=True) parser.add_argument('-p', '--port', required=True) parser.add_argument('--debug', action='store_true') args = parser.parse_args() if args.debug: logging.getLogger().setLevel(logging.INFO) else: logging.getLogger().setLevel(logging.WARNING)
client = LLMFlaskClient(args.ip, args.port) print(client.run('Please concatenate string "1+" and "1=3". Only give me the result without "".'))123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172from transformers import AutoModelForCausalLM, AutoTokenizerfrom flask import Flask, requestimport argparseimport logging
class LLMInstance:
def __init__(self, model_path: str, device: str = "cuda"):
self.model = AutoModelForCausalLM.from_pretrained(model_path) self.tokenizer = AutoTokenizer.from_pretrained(model_path) self.model.to(device) self.device = device
def query(self, message): try: messages = [ {"role": "user", "content": message}, ] encodeds = self.tokenizer.apply_chat_template(messages, return_tensors="pt") model_inputs = encodeds.to(self.device)
generated_ids = self.model.generate(model_inputs, max_new_tokens=1000, do_sample=True) decoded = self.tokenizer.batch_decode(generated_ids)
# output is the string decoded[0] after "[/INST]". There may exist "</s>", delete it. output = decoded[0].split("[/INST]")[1].split("</s>")[0] return { 'code': 0, 'ret': True, 'error_msg': None, 'output': output } except Exception as e: return { 'code': 1, 'ret': False, 'error_msg': str(e), 'output': None }
def create_app(core): app = Flask(__name__)
@app.route('/ask_llm_for_answer', methods=['POST']) def ask_llm_for_answer(): user_text = request.json['user_text'] return core.query(user_text)
return app
if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument('-m', '--model_path', required=True, default='Mistral-7B-Instruct-v0.1', help='the model path of reward model') parser.add_argument('--ip', default='0.0.0.0') parser.add_argument('-p', '--port', default=8001) parser.add_argument('--debug', action='store_true') args = parser.parse_args()
if args.debug: logging.getLogger().setLevel(logging.DEBUG) else: logging.getLogger().setLevel(logging.INFO) logging.getLogger().addHandler(logging.StreamHandler()) logging.getLogger().handlers[0].setFormatter(logging.Formatter("%(message)s"))
core = LLMInstance(args.model_path) app = create_app(core) app.run(host=args.ip, port=args.port)12345678910111213141516171819202122from functools import lru_cache
from .base import register_llmfrom .llm_client import LLMFlaskClient
@lru_cache()def _get_mistral_7b_instruct_server(host: str, port: int): from .llm_server import LLMInstance, create_app core = LLMInstance('Mistral-7B-Instruct-v0.1') app = create_app(core) app.run(host=host, port=port)
def ask_mistral_7b_instruct(message: str, **kwargs): host, port = '0.0.0.0', 8001 _get_mistral_7b_instruct_server(host, port) client = LLMFlaskClient(host, port) return client.run(message).strip()
register_llm('mistral-7b', ask_mistral_7b_instruct)123456789from .executor import QuestionExecutorfrom .level1 import __file__ as _level1_file_from .level3 import __file__ as _level3_file_from .level5 import __file__ as _level5_file_from .question import Question, register_question, list_ordered_questions_ = _level1_file__ = _level3_file__ = _level5_file_1234567891011from .executor import QuestionExecutorfrom .level1 import __file__ as _level1_file_from .level3 import __file__ as _level3_file_from .level4 import __file__ as _level4_file_from .level5 import __file__ as _level5_file_from .question import Question, register_question, list_ordered_questions_ = _level1_file__ = _level3_file__ = _level4_file__ = _level5_file_
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697from .question import register_question
import re
def check_if_is_number(text: str):
try:
int(text)
return True
except ValueError:
return False
def get_all_numbers_in_a_sentence(text: str):
return [int(i) for i in re.findall(r'\d+', text)]
# CN_TEXT_1 = """
# 第四章第一题,请构造一个问题A,它的回复是不同于A的B,然后以B提问能再次得到A。
# 请在下面的输入框内填写你构造并点击按钮提交。
# """
# EN_TEXT_1 = """
# For the first question in chapter 4, please make a question A whose answer is B that is different from A, and then ask B to get A again.
# Please enter your query below and click the submit button
# """
# def _checker_1(question_text: str, user_text: str, answer_text: str, lang: str):
# _ = question_text, lang
# answer_text = answer_text.strip()
# user_text = user_text.strip()
# pass
# register_question({
# 'cn': CN_TEXT_1,
# 'en': EN_TEXT_1,
# }, _checker_1, level=4)
# CN_TEXT_2 = """
# 第四章第二题,
# 请在下面的输入框内填写你构造并点击按钮提交。
# """
# EN_TEXT_2 = """
# For the second question in chapter 4,
# Please enter your query below and click the submit button
# """
# def _checker_2(question_text: str, user_text: str, answer_text: str, lang: str):
# _ = question_text, lang
# answer_text = answer_text.strip()
# user_text = user_text.strip()
# pass
# register_question({
# 'cn': CN_TEXT_2,
# 'en': EN_TEXT_2,
# }, _checker_2, level=4)
CN_TEXT_3 = """
第四章第三题(自然之密),请输入一个大于一的正整数作为问题,使回答里包含和它刚好相差1的数。
请在下面的输入框内填写你构造并点击按钮提交。
"""
EN_TEXT_3 = """
For the third question in chapter 4, please enter a positive integer greater than one as the question so that the answer contains a number that is exactly 1 different from it.
Please enter your query below and click the submit button
"""
def _checker_3(question_text: str, user_text: str, answer_text: str, lang: str):
_ = question_text, lang
answer_text = answer_text.strip()
user_text = user_text.strip()
if not check_if_is_number(question_text):
return False, "问题应该是一个正整数" if lang == 'cn' else 'Question should be a positive integer.'
elif int(question_text) == 1:
return False, "问题应该是一个大于1的正整数" if lang == 'cn' else 'Question should be a positive integer greater than 1.'
elif int(question_text)-1 not in get_all_numbers_in_a_sentence(answer_text) or int(question_text)+1 not in get_all_numbers_in_a_sentence(answer_text):
return False, "回答中应该包含一个与问题相差1的数字" if lang == 'cn' else 'Answer should contain a number that is exactly 1 different from the question.'
else:
return True, None
register_question({
'cn': CN_TEXT_3,
'en': EN_TEXT_3,
}, _checker_3, level=4)1 unmodified line23451 unmodified linetqdmrequests>=2.20gradio==4.1.1openai>=11 unmodified line2345671 unmodified linetqdmrequests>=2.20gradio==4.1.1openai>=1flasktransformers
README.md+4-8
app.py+14-3
llmriddles/llms/__init__.py+2-1
llmriddles/llms/llm_client.pyAdded+54
llmriddles/llms/llm_server.pyAdded+72
llmriddles/llms/mistral.pyAdded+22
llmriddles/questions/__init__.py+2
llmriddles/questions/level4.pyAdded+97
requirements.txt+3-1