Cecilia / model.py
Cran-May's picture
Upload model.py
b806229
raw
history blame
1.27 kB
from typing import Iterator
model_id = 'shaowenchen/baichuan2-7b-chat-gguf'
from huggingface_hub import snapshot_download,hf_hub_download
hf_hub_download(model_id, local_dir="./", filename="baichuan2-7b-chat.Q4_K.gguf")
hf_hub_download(repo_id="baichuan-inc/Baichuan-13B-Chat",local_dir="./", filename="tokenizer.model")
from llama_cpp import Llama
llm = Llama(model_path="./baichuan2-7b-chat.Q4_K.gguf", n_ctx=4096,seed=-1)
def run(message: str,
chat_history: list[tuple[str, str]],
system_prompt: str,
max_new_tokens: int = 1024,
temperature: float = 0.3,
top_p: float = 0.85,
top_k: int = 5) -> Iterator[str]:
history = []
print(chat_history)
result=""
for i in chat_history:
history.append({"role": "user", "content": i[0]})
history.append({"role": "assistant", "content": i[1]})
print(history)
history.append({"role": "user", "content": message})
for response in llm.create_chat_completion(history,stop=["</s>"],stream=True,max_tokens=-1,temperature=temperature,top_k=top_k,top_p=top_p,repeat_penalty=1.1):
if "content" in response["choices"][0]["delta"]:
result = result + response["choices"][0]["delta"]["content"]
yield result