Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Qwen2-7B-Instruct Langchain 接入的问题 #225

Open
yinzih opened this issue Jul 29, 2024 · 4 comments
Open

Qwen2-7B-Instruct Langchain 接入的问题 #225

yinzih opened this issue Jul 29, 2024 · 4 comments

Comments

@yinzih
Copy link

yinzih commented Jul 29, 2024

我在.ipynb文件中,运行了自定义 LLM 类
from langchain.llms.base import LLM
from typing import Any, List, Optional
from langchain.callbacks.manager import CallbackManagerForLLMRun
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig, LlamaTokenizerFast
import torch

class Qwen2_LLM(LLM):
# 基于本地 Qwen2 自定义 LLM 类
tokenizer: AutoTokenizer = None
model: AutoModelForCausalLM = None

def __init__(self, mode_name_or_path :str):

    super().__init__()
    print("正在从本地加载模型...")
    self.tokenizer = AutoTokenizer.from_pretrained(mode_name_or_path, use_fast=False)
    self.model = AutoModelForCausalLM.from_pretrained(mode_name_or_path, torch_dtype=torch.bfloat16, device_map="auto")
    self.model.generation_config = GenerationConfig.from_pretrained(mode_name_or_path)
    print("完成本地模型的加载")
    
def _call(self, prompt : str, stop: Optional[List[str]] = None,
            run_manager: Optional[CallbackManagerForLLMRun] = None,
            **kwargs: Any):

    messages = [{"role": "user", "content": prompt }]
    input_ids = self.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
    model_inputs = self.tokenizer([input_ids], return_tensors="pt").to('cuda')
    generated_ids = self.model.generate(model_inputs.input_ids,max_new_tokens=512)
    generated_ids = [
        output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
    ]
    response = self.tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
    
    return response
@property
def _llm_type(self) -> str:
    return "Qwen2_LLM"

然后,当我在调用Qwen2_LLM时,却报错了
from LLM import Qwen2_LLM
llm = Qwen2_LLM(mode_name_or_path = "/root/autodl-tmp/qwen/Qwen2-7B-Instruct")
print(llm("你是谁"))
Qwen2-7B-Instruct

上面说ModuleNotFoundError: No module named 'LLM',没有这个模型,但应该是有的啊,请问大家该如何修改呢?

@KMnO4-zx
Copy link
Contributor

因为你是在直接在notebook文件中运行的,那么直接使用Qwen2_LLM即可。
导入的方式,是将上述文件存放为py文件导入的。

@yinzih
Copy link
Author

yinzih commented Jul 29, 2024 via email

@KMnO4-zx
Copy link
Contributor

是的

@yinzih
Copy link
Author

yinzih commented Jul 29, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants