diff --git a/models/Qwen2.5-Coder/02-Qwen2.5-7B-Instruct Langchain 接入.md b/models/Qwen2.5-Coder/02-Qwen2.5-7B-Instruct Langchain 接入.md new file mode 100644 index 0000000..6bbb608 --- /dev/null +++ b/models/Qwen2.5-Coder/02-Qwen2.5-7B-Instruct Langchain 接入.md @@ -0,0 +1,129 @@ +``` + +``` + +# Qwen2.5-7B-Instruct Langchain ½ÓÈë + +## »·¾³×¼±¸ + +±¾ÎÄ»ù´¡»·¾³ÈçÏ£º + +``` +---------------- +ubuntu 22.04 +python 3.12 +cuda 12.1 +pytorch 2.3.0 +---------------- +``` + +> ±¾ÎÄĬÈÏѧϰÕßÒѰ²×°ºÃÒÔÉÏ Pytorch(cuda) »·¾³£¬Èçδ°²×°Çë×ÔÐа²×°¡£ + +pip »»Ô´¼ÓËÙÏÂÔØ²¢°²×°ÒÀÀµ°ü + +```shell +# Éý¼¶pip +python -m pip install --upgrade pip +# ¸ü»» pypi Ô´¼ÓËÙ¿âµÄ°²×° +pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple + +pip install transformers==4.46.2 +pip install modelscope==1.20.0 +pip install langchain==0.3.7 +pip install accelerate==1.1.1 +``` + + + +## Ä£ÐÍÏÂÔØ + +ʹÓà `modelscope` ÖÐµÄ `snapshot_download` º¯ÊýÏÂÔØÄ£ÐÍ£¬µÚÒ»¸ö²ÎÊýΪģÐÍÃû³Æ£¬²ÎÊý `cache_dir` ΪģÐ͵ÄÏÂÔØÂ·¾¶¡£ + +ÔÚн¨ `model_download.py` Îļþ²¢ÔÚÆäÖÐÊäÈëÒÔÏÂÄÚÈÝ£¬Õ³Ìù´úÂëºó¼ÇµÃ±£´æÎļþ£¬ÈçÏÂͼËùʾ¡£²¢ÔËÐÐ `python model_download.py` Ö´ÐÐÏÂÔØ£¬Ä£ÐÍ´óСΪ 16 GB£¬ÏÂÔØÄ£ÐÍ´ó¸ÅÐèÒª 12 ·ÖÖÓ¡£ + +```python +import torch +from modelscope import snapshot_download, AutoModel, AutoTokenizer +import os +model_dir = snapshot_download('Qwen/Qwen2.5-Coder-7B-Instruct', cache_dir='/root/autodl-tmp', revision='master') +``` + +> ×¢Ò⣺¼ÇµÃÐÞ¸Ä `cache_dir` ΪÄãµÄÄ£ÐÍÏÂÔØÂ·¾¶Å¶~ + +## ´úÂë×¼±¸ + +Ϊ±ã½Ý¹¹½¨ `LLM` Ó¦Óã¬ÎÒÃÇÐèÒª»ùÓÚ±¾µØ²¿ÊðµÄ `Qwen2_5_Coder`£¬×Ô¶¨ÒåÒ»¸ö `LLM` À࣬½« `Qwen2.5-Coder` ½ÓÈëµ½ `LangChain` ¿ò¼ÜÖС£Íê³É×Ô¶¨Òå `LLM` ÀàÖ®ºó£¬¿ÉÒÔÒÔÍêȫһÖµķ½Ê½µ÷Óà `LangChain` µÄ½Ó¿Ú£¬¶øÎÞÐ迼ÂǵײãÄ£Ð͵÷ÓõIJ»Ò»Ö¡£ + +»ùÓÚ±¾µØ²¿ÊðµÄ `Qwen2_5_Coder` ×Ô¶¨Òå `LLM` Àಢ²»¸´ÔÓ£¬ÎÒÃÇÖ»Ðè´Ó `LangChain.llms.base.LLM` Àà¼Ì³ÐÒ»¸ö×ÓÀ࣬²¢ÖØÐ´¹¹Ô캯ÊýÓë `_call` º¯Êý¼´¿É£º + +ÔÚµ±Ç°Â·¾¶Ð½¨Ò»¸ö `LLM.py` Îļþ£¬²¢ÊäÈëÒÔÏÂÄÚÈÝ£¬Õ³Ìù´úÂëºó¼ÇµÃ±£´æÎļþ¡£ + +```python +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_5_Coder(LLM): + # »ùÓÚ±¾µØ Qwen2_5-Coder ×Ô¶¨Òå 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, attention_mask=model_inputs['attention_mask'], 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_5_Coder" +``` + +ÔÚÉÏÊöÀඨÒåÖУ¬ÎÒÃÇ·Ö±ðÖØÐ´Á˹¹Ô캯ÊýºÍ `_call` º¯Êý£º¶ÔÓÚ¹¹Ô캯Êý£¬ÎÒÃÇÔÚ¶ÔÏóʵÀý»¯µÄÒ»¿ªÊ¼¼ÓÔØ±¾µØ²¿ÊðµÄ `Qwen2_5_Coder` `Ä£ÐÍ£¬´Ó¶ø±ÜÃâÿһ´Îµ÷Óö¼ÐèÒªÖØÐ¼ÓÔØÄ£ÐÍ´øÀ´µÄʱ¼ä¹ý³¤£»_call` º¯ÊýÊÇ `LLM` ÀàµÄºËÐĺ¯Êý£¬`LangChain` »áµ÷Óøú¯ÊýÀ´µ÷Óà `LLM`£¬Ôڸú¯ÊýÖУ¬ÎÒÃǵ÷ÓÃÒÑʵÀý»¯Ä£Ð굀 `generate` ·½·¨£¬´Ó¶øÊµÏÖ¶ÔÄ£Ð͵ĵ÷Óò¢·µ»Øµ÷Óýá¹û¡£ + +ÔÚÕûÌåÏîÄ¿ÖУ¬ÎÒÃǽ«ÉÏÊö´úÂë·âװΪ `LLM.py`£¬ºóÐø½«Ö±½Ó´Ó¸ÃÎļþÖÐÒýÈë×Ô¶¨ÒåµÄ LLM Àà¡£ + +## µ÷Óà + +È»ºó¾Í¿ÉÒÔÏñʹÓÃÈÎºÎÆäËûµÄlangchain´óÄ£Ð͹¦ÄÜÒ»ÑùʹÓÃÁË¡£ + +> ×¢Ò⣺¼ÇµÃÐÞ¸ÄÄ£ÐÍ·¾¶ÎªÄãµÄ·¾¶Å¶~ + +```python +from LLM import Qwen2_5_Coder +llm = Qwen2_5_Coder(mode_name_or_path = "autodl-tmp/Qwen/Qwen2___5-Coder-7B-Instruct") +print(llm.invoke("ÄãÊÇË­")) +``` + +½á¹ûÈçÏ£º +![](./images/02-1.png) + +¼ÈÈ»ÊÇCoderÄ£ÐÍ£¬µ±È»ÒªÊÔ×ÅÈÃËü±àд´úÂë + +```python +text = llm.invoke("ΪÎÒÓÃpythonдһ¸ö¼òµ¥µÄ²ÂȭСÓÎÏ·£¬Èý¾ÖÁ½Ê¤") +print(text) +``` + +½á¹ûÈçÏ£º +![](./images/02-2.png) +ÎÒÃÇÊÔ×ÅÔËÐÐÒ»ÏÂÕâ¶Î´úÂ룺 +![](./images/02-3.png) +³É¹¦ÔËÐУ¡ \ No newline at end of file diff --git a/models/Qwen2.5-Coder/images/02-1.png b/models/Qwen2.5-Coder/images/02-1.png new file mode 100644 index 0000000..87c8b57 Binary files /dev/null and b/models/Qwen2.5-Coder/images/02-1.png differ diff --git a/models/Qwen2.5-Coder/images/02-2.png b/models/Qwen2.5-Coder/images/02-2.png new file mode 100644 index 0000000..7314117 Binary files /dev/null and b/models/Qwen2.5-Coder/images/02-2.png differ diff --git a/models/Qwen2.5-Coder/images/02-3.png b/models/Qwen2.5-Coder/images/02-3.png new file mode 100644 index 0000000..1e05981 Binary files /dev/null and b/models/Qwen2.5-Coder/images/02-3.png differ