Views
No views yet
1from transformers import LlamaForCausalLM, LlamaTokenizer
2import torch
3
4base_model_name = "AntX-ai/AntX-7B"
5load_type = torch.float16
6device = None
7
8generation_config = dict(
9 temperature=0.2,
10 top_k=40,
11 top_p=0.9,
12 do_sample=True,
13 num_beams=1,
14 repetition_penalty=1.3,
15 max_new_tokens=400
16 )
17
18prompt_input = (
19 "Below is an instruction that describes a task. "
20 "Write a response that appropriately completes the request.\n\n"
21 "### Instruction:\n\n{instruction}\n\n### Response:\n\n"
22)
23if torch.cuda.is_available():
24 device = torch.device(0)
25else:
26 device = torch.device('cpu')
27
28def generate_prompt(instruction, input=None):
29 if input:
30 instruction = instruction + '\n' + input
31 return prompt_input.format_map({'instruction': instruction})
32
33tokenizer = LlamaTokenizer.from_pretrained(base_model_name)
34model = LlamaForCausalLM.from_pretrained(
35 base_model_name,
36 load_in_8bit=False,
37 torch_dtype=load_type,
38 low_cpu_mem_usage=True,
39 device_map='auto',
40 )
41
42model_vocab_size = model.get_input_embeddings().weight.size(0)
43tokenzier_vocab_size = len(tokenizer)
44if model_vocab_size != tokenzier_vocab_size:
45 model.resize_token_embeddings(tokenzier_vocab_size)
46
47raw_input_text = input("Input:")
48input_text = generate_prompt(instruction=raw_input_text)
49inputs = tokenizer(input_text, return_tensors="pt")
50generation_output = model.generate(
51input_ids=inputs["input_ids"].to(device),
52 attention_mask=inputs['attention_mask'].to(device),
53 eos_token_id=tokenizer.eos_token_id,
54 pad_token_id=tokenizer.pad_token_id,
55 **generation_config
56)
57s = generation_output[0]
58output = tokenizer.decode(s, skip_special_tokens=True)
59response = output.split("### Response:")[1].strip()
60print("Response: ", response)
61print("\n")Input:王国维说:“自周之衰,文王、周公势力之瓦解也,国民之智力成熟于内,政治之纷乱乘之于外,上无统一之制度,下迫于社会之要求,于是诸于九流各创其学说。” 他意在说明 A. 分封制的崩溃 B. 商鞅变法的作用 C. 兼并战争的后果 D. 百家争鸣的原因
Response: 本题考查对材料的理解。A错误;B正确;C和D与材料无关。故选BC两项即可
Input:经济基础是指一定社会发展阶段占统治地位的生产关系各个方面的总和。解决了地方经济问题, 也就解 决了地方割据问题, 为此, 宋太祖采取的措施是( ) A . 地方设转运使, 财赋收归中央 B . 派文臣担任各地州县的长官 C . 派文臣管理地方政事 D . 采取分化事权办法, 削弱相权
Response: A: 本题考查对宋太祖治下地方问题的认识。 A : 依据材料可知, 在北宋时期 , 由于地主阶级的发展壮大以及商业、手工业等新兴行业出现 , 这一时期出现的地方割据现象严重威胁着国家的统一与稳定 . 因此 , 为了解决这个问题, 需要加强中央集权 ; 故选 A
Input:go\\n/**\\n * Definition for a binary tree node.\\n * type TreeNode struct {\\n * Val int\\n * Left *TreeNode\\n * Right *TreeNode\\n * }\\n */\\nfunc maxDepth(root *TreeNode) int {\\n\\tif root == nil {\\n\\t\\treturn 0\\n\\t}\\n\\tl, r := maxDepth(root.Left), maxDepth(root.Right)\\n\\treturn 1 + max(l, r)\\n}\\n\\nfunc max(a, b int) int {\\n\\tif a > b {\\n\\t\\treturn a\\n\\t}\\n\\treturn b\\n}\\n
Response: ```python
def max_depth(root): # The function takes in a single parameter `root` and returns its maximum depth value as output.
if not root or len(root.children()) == 0:
return 0
l = max_depth(root.left)
r = max_depth(root.right)
return 1 + (max(l, r))
class TreeNode:
def __init__(self, val=None, left=10, right=0):
self.val = val
self.left = None
self.right = None
@software{AntX-7B,
title={An Enchanced Chinese Language Model based on the Chinese-LLaMA-Alpaca},
url={https://huggingface.co/AntX-ai/AntX-7B},
year={2023}
}