---
title: Xunzi_Qwen_7b_relation_rxtraction
canonical_url: "https://www.modelscope.cn/models/shenxin0925/Xunzi_Qwen_7b_relation_rxtraction"
md_url: "https://www.modelscope.cn/models/shenxin0925/Xunzi_Qwen_7b_relation_rxtraction.md"
repository: shenxin0925/Xunzi_Qwen_7b_relation_rxtraction
chinese_name: "7B级别Xunzi_Qwen模型，用于限定域关系抽取"
last_updated: 2024-03-21
pipeline_tag: text-generation
tasks:
  - text-generation
model_type:
  - qwen
architectures:
  - QWenLMHeadModel
library_name:
  - pytorch
  - transformer
frameworks:
  - Pytorch
inference_backends:
  - "deploy_task text/emb"
  - "lmdeploy 0.9.1"
  - "lmdeploy_turbomind 0.9.1"
  - "sglang 0.5.2"
  - "vllm 0.9.2"
downloads: 111
stars: 2
---

# Xunzi_Qwen_7b_relation_rxtraction

> Xunzi_Qwen_7b_relation_rxtraction - shenxin0925 在 ModelScope 开源的模型。--- frameworks: Pytorch license: Apache License 2.0 tasks: text-generation

shenxin0925/Xunzi_Qwen_7b_relation_rxtraction 是 ModelScope 魔搭社区上的text-generation模型，可用 deploy_task text/emb、lmdeploy 0.9.1、lmdeploy_turbomind 0.9.1 部署。

- **Repository**: shenxin0925/Xunzi_Qwen_7b_relation_rxtraction
- **Tasks**: text-generation
- **Inference backends**: deploy_task text/emb, lmdeploy 0.9.1, lmdeploy_turbomind 0.9.1, sglang 0.5.2, vllm 0.9.2
- **Downloads**: 111
- **Stars**: 2
- **Last updated**: 2024-03-21

Source: https://www.modelscope.cn/models/shenxin0925/Xunzi_Qwen_7b_relation_rxtraction

---

---
frameworks:
- Pytorch
license: Apache License 2.0
tasks:
- text-generation

## 模型简介
该模型是专门用于古籍限定域关系抽取模型的对话模型，由Xunzi_Qwen-7b微调而来。可根据关系scheme自动抽取古籍文本中的三元组信息。


SDK下载
```bash
#安装ModelScope
pip install modelscope
```
```python
#SDK模型下载
from modelscope import snapshot_download
model_dir = snapshot_download('shenxin0925/Xunzi_Qwen_7b_relation_rxtraction')
```
Git下载
```
#Git模型下载
git clone https://www.modelscope.cn/shenxin0925/Xunzi_Qwen_7b_relation_rxtraction.git
```
# python 推理代码
```python
import torch
from transformers import AutoModel, AutoTokenizer,AutoModelForCausalLM
from transformers.generation import GenerationConfig
def get_model():
    tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
    model = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True).cuda()
    model = model.eval()
    return tokenizer, model

# 加载Xunzi的model和tokenizer
tokenizer, model = get_model()
config = GenerationConfig.from_pretrained(model_path, trust_remote_code=True, resume_download=True)

input_text='根据提供的文本，按照关系scheme组合(人物, PO/官職, 官職),(人物, PP/態度傾向/消極, 人物),(人物, PL/其他, 地点),(人物, PL/居, 地点),(人物代词, 態度傾向/消極, 人物)抽取出符合描述的关系三元组\n奏上，上令公卿列侯宗室集議，莫敢難，獨竇嬰爭之，由此與錯有卻。'
response, history = model.chat(tokenizer,input_text, history=None)
print(response)
#输出："符合scheme(人物, PO/官職, 官職)的关系三元组有：无。\n符合scheme(人物, PP/態度傾向/消極, 人物)的关系三元组有：(竇嬰, PP/態度傾向/消極, 錯)\n符合scheme(人物, PL/其他, 地点)的关系三元组有：无。\n符合scheme(人物, PL/居, 地点)的关系三元组有：无。\n符合scheme(人物代词, 態度傾向/消極, 人物)的关系三元组有：无。"
```

# vllm加速
```python
from vllm import LLM, SamplingParams
import json
from argparse import ArgumentParser
from transformers import AutoConfig, AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer, GenerationConfig
from peft import PeftModel

#示例数据：
# prompts = [
# "<|im_start|>system:You are a helpful assistant.<|im_end|> \n <|im_start|>user:根据提供的文本，按照关系scheme(人物名, 隶属于, 人物名)抽取出符合描述的关系三元组\n布收其使，上其书。术怒，攻布，为布所破。秋九月，术侵陈，公东征之。术闻公自来，弃军走，留其将桥蕤、李丰、梁纲、乐就 <|im_end|>\n <|im_start|>assistant:",
# "<|im_start|>system:You are a helpful assistant.<|im_end|> \n <|im_start|>user:根据提供的文本，按照关系scheme(人物名, 任职, 官职名)抽取出符合描述的关系三元组\n其后十六年而秦灭赵。其后二十馀年，高帝过赵，问“乐毅有后世乎”对曰“有乐叔”高帝封之乐卿，号曰华成君。华成君，乐毅之孙也 <|im_start|>assistant:",
# "<|im_start|>system:You are a helpful assistant.<|im_end|> \n <|im_start|>user:根据提供的文本，按照关系scheme(人物名, 位于, 地名)抽取出符合描述的关系三元组\n齐人茅焦说秦王曰“秦方以天下为事，而大王有迁母太后之名，恐诸侯闻之，由此倍秦也”秦王乃迎太后於雍而入咸阳，复居甘泉宫。 <|im_end|>\n <|im_start|>assistant:"
# ]



def _get_args():
    parser = ArgumentParser()
    parser.add_argument("--model_path", type=str)
    parser.add_argument("--input_path", type=str)
    parser.add_argument("--output_path", type=str)


    args = parser.parse_args()
    return args

args = _get_args()


def get_json(input):
    with open(input, 'r', encoding='utf-8') as file:
        data = json.load(file)
    return data    

def qwen_sample(input):
    prompts=[]
    outputs=[]
    input_texts=[]
    for i in input:
        instruction=i['instruction']
        input_text=i['input']
        output=i['output']
        prompt='<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n{}\n{}<|im_end|>\n<|im_start|>assistant\n'.format(instruction,input_text)    
        prompts.append(prompt)
        outputs.append(output)
        input_texts.append(input_text)
    return prompts,outputs,input_texts
    
data=get_json(input=args.input_path)
prompts,output_raw,input_texts=qwen_sample(input=data)
# print(prompts)

sampling_params = SamplingParams(temperature=1, top_p=0.8,max_tokens=2048,stop=["<|endoftext|>"])

llm = LLM(model=args.model_path,trust_remote_code=True,tensor_parallel_size=1)
outputs = llm.generate(prompts, sampling_params)
output_predicate=[]
#Print the outputs.
for output in outputs:
    prompt = output.prompt
    generated_text = output.outputs[0].text
    output_predicate.append(generated_text)
with open(args.output_path,'w', encoding='utf-8') as fp:
    for i in range(0,len(output_predicate)):
        data={'text':input_texts[i],'raw':output_raw[i],'predicate':output_predicate[i]}
        jsonstr=json.dumps(data, ensure_ascii=False)
        fp.write(jsonstr+'\n')
```

# github项目地址
访问 [GitHub](https://github.com/Xunzi-LLM-of-Chinese-classics/Xunzi_for_ancient_chinese_relation_extraction/tree/main) 以获取更多项目信息
