Spaces:
Running
Running
Update chat_client.py
Browse files1 unmodified line23456789101112132 unmodified lines161718192021222324252627282930313233343536373839404142434445464748495051521 unmodified lineimport osfrom dotenv import load_dotenvimport randomload_dotenv()API_TOKEN = os.getenv('HF_TOKEN')def format_prompt(message, history):prompt = "<s>"for user_prompt, bot_response in history:2 unmodified linesprompt += f"[INST] {message} [/INST]"return promptdef chat(prompt, history, chat_client = "mistralai/Mistral-7B-Instruct-v0.1",temperature=0.9, max_new_tokens=2048, top_p=0.95, repetition_penalty=1.0,):client = InferenceClient(chat_client,token=API_TOKEN)temperature = float(temperature)if temperature < 1e-2:temperature = 1e-2top_p = float(top_p)generate_kwargs = dict(temperature=temperature,max_new_tokens=max_new_tokens,top_p=top_p,repetition_penalty=repetition_penalty,do_sample=True,seed=random.randint(0, 10**7),)formatted_prompt = format_prompt(prompt, history)stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)output = ""# for response in stream:# # print(response)# output += response.token["text"]# yield output# return outputreturn stream1 unmodified line23456789101112132 unmodified lines16171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485861 unmodified lineimport osfrom dotenv import load_dotenvimport randomimport jsonfrom openai import OpenAIload_dotenv()API_TOKEN = os.getenv('HF_TOKEN')def format_prompt(message, history):prompt = "<s>"for user_prompt, bot_response in history:2 unmodified linesprompt += f"[INST] {message} [/INST]"return promptdef format_prompt_openai(system_prompt, message, history):messages = []if system_prompt != '':messages.append({"role": "system", "content": system_prompt})for user_prompt, bot_response in history:messages.append({"role": "user", "content": user_prompt})messages.append({"role": "assistant", "content": bot_response})messages.append({"role": "user", "content": message})return messagesdef chat_huggingface(prompt, history, chat_client, temperature, max_new_tokens, top_p, repetition_penalty):client = InferenceClient(chat_client,token=API_TOKEN)temperature = float(temperature)if temperature < 1e-2:temperature = 1e-2top_p = float(top_p)generate_kwargs = dict(temperature=temperature,max_new_tokens=max_new_tokens,top_p=top_p,repetition_penalty=repetition_penalty,do_sample=True,seed=random.randint(0, 10**7),)formatted_prompt = format_prompt(prompt, history)print('***************************************************')print(formatted_prompt)print('***************************************************')stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)return streamdef chat_openai(prompt, history, chat_client, temperature, max_new_tokens, top_p, repetition_penalty, client_openai):try:prompt = prompt.replace('\n', '')json_data = json.loads(prompt)user_prompt = json_data["messages"][1]["content"]system_prompt = json_data["input"]["content"]system_style = json_data["input"]["style"]instructions = json_data["messages"][0]["content"]if instructions != '':system_prompt += '\n' + instructionsif system_style != '':system_prompt += '\n' + system_styleexcept:user_prompt = promptsystem_prompt = ''messages = format_prompt_openai(system_prompt, user_prompt, history)print('***************************************************')print(messages)print('***************************************************')stream = client_openai.chat.completions.create(model=chat_client,stream=True,messages=messages,temperature=temperature,max_tokens=max_new_tokens,)return streamdef chat(prompt, history, chat_client,temperature=0.9, max_new_tokens=1024, top_p=0.95, repetition_penalty=1.0, client_openai = None):if chat_client[:3] == 'gpt':return chat_openai(prompt, history, chat_client, temperature, max_new_tokens, top_p, repetition_penalty, client_openai)else:return chat_huggingface(prompt, history, chat_client, temperature, max_new_tokens, top_p, repetition_penalty)
chat_client.py+66-32