-
Notifications
You must be signed in to change notification settings - Fork 1
/
LLM.py
45 lines (39 loc) · 1.5 KB
/
LLM.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from langchain.llms.base import LLM
from typing import Any, List, Optional
from langchain.callbacks.manager import CallbackManagerForLLMRun
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
class InternLM2_LLM(LLM):
tokenizer: AutoTokenizer = None
model: AutoModelForCausalLM = None
def __init__(self, model_path: str):
super().__init__()
print("正在从本地加载模型...")
self.tokenizer = AutoTokenizer.from_pretrained(
model_path, trust_remote_code=True
)
self.model = (
AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True)
.to(torch.bfloat16)
.cuda()
)
self.model = self.model.eval()
print("完成加载.")
def _call(
self,
prompt: str,
stop: Optional[List[str]] = None,
run_manager: Optional[CallbackManagerForLLMRun] = None,
**kwargs: Any
):
system_prompt = """你是一个叫做“劳动法知识库检索小助手”的AI助手,
你能根据劳动法相关法律法规准确地回答用户关于劳动法的问题,
如果用户的提问并非劳动法相关问题,你应该拒绝作答,并感谢用户的提问。
"""
messages = [(system_prompt, "")]
print(prompt)
response, history = self.model.chat(self.tokenizer, prompt, history=messages)
return response
@property
def _llm_type(self) -> str:
return "InternLM2"