Cran-May commited on
Commit
b806229
1 Parent(s): 92c8326

Upload model.py

Browse files
Files changed (1) hide show
  1. model.py +33 -0
model.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from typing import Iterator
3
+
4
+
5
+
6
+ model_id = 'shaowenchen/baichuan2-7b-chat-gguf'
7
+
8
+ from huggingface_hub import snapshot_download,hf_hub_download
9
+
10
+ hf_hub_download(model_id, local_dir="./", filename="baichuan2-7b-chat.Q4_K.gguf")
11
+ hf_hub_download(repo_id="baichuan-inc/Baichuan-13B-Chat",local_dir="./", filename="tokenizer.model")
12
+ from llama_cpp import Llama
13
+ llm = Llama(model_path="./baichuan2-7b-chat.Q4_K.gguf", n_ctx=4096,seed=-1)
14
+
15
+ def run(message: str,
16
+ chat_history: list[tuple[str, str]],
17
+ system_prompt: str,
18
+ max_new_tokens: int = 1024,
19
+ temperature: float = 0.3,
20
+ top_p: float = 0.85,
21
+ top_k: int = 5) -> Iterator[str]:
22
+ history = []
23
+ print(chat_history)
24
+ result=""
25
+ for i in chat_history:
26
+ history.append({"role": "user", "content": i[0]})
27
+ history.append({"role": "assistant", "content": i[1]})
28
+ print(history)
29
+ history.append({"role": "user", "content": message})
30
+ 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):
31
+ if "content" in response["choices"][0]["delta"]:
32
+ result = result + response["choices"][0]["delta"]["content"]
33
+ yield result