首页 > 编程语言 >python使用langchain调用本地大模型

python使用langchain调用本地大模型

时间:2024-04-29 17:01:14浏览次数:16  
标签:调用 python torch langchain import device path model

参考 https://www.cnblogs.com/scarecrow-blog/p/17875127.html

模型下载之前说过一次 https://www.cnblogs.com/qcy-blog/p/18165717

也可直接去官网,把所有文件都点一遍

from langchain import PromptTemplate, LLMChain
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer
from transformers import AutoModel, pipeline
from langchain import HuggingFacePipeline
from langchain import PromptTemplate

model_path = r'model\llama_model_4bit'
if torch.cuda.is_available():
    print(torch.cuda.device_count())
    device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
    print(device)
else:
    print('没有GPU')
    
tokenizer = AutoTokenizer.from_pretrained(model_path, use_fast=False)
if model_path.endswith("4bit"):
    model = AutoModelForCausalLM.from_pretrained(
            model_path,
            load_in_4bit=True,
            torch_dtype=torch.float16,
            device_map='auto'
        )
elif model_path.endswith("8bit"):
        model = AutoModelForCausalLM.from_pretrained(
            model_path,
            load_in_8bit=True,
            torch_dtype=torch.float16,
            device_map='auto'
        )
else:
    model = AutoModelForCausalLM.from_pretrained(model_path).half().cuda()
    
pipe = pipeline(
    "text-generation",
    model=model,
    tokenizer=tokenizer,
    max_length=512,
    top_p=1,
    repetition_penalty=1.15
)
llama_model = HuggingFacePipeline(pipeline=pipe)

template = '''
#context# 
You are a good helpful, respectful and honest assistant.You are ready for answering human's question and always answer as helpfully as possible, while being safe.
Please ensure that your responses are socially unbiased and positive in nature. 
#question# 
Human:What is a good name for a company that makes {product}?"
'''
prompt = PromptTemplate(
    input_variables=["product"],
    template=template
)
chain = LLMChain(llm=llama_model, prompt=prompt)
chain.run("running shoes")

标签:调用,python,torch,langchain,import,device,path,model
From: https://www.cnblogs.com/qcy-blog/p/18166231

相关文章

  • PYTHON 用几何布朗运动模型和蒙特卡罗MONTE CARLO随机过程模拟股票价格可视化分析耐克
    原文链接:http://tecdat.cn/?p=27099最近我们被客户要求撰写关于蒙特卡罗的研究报告,包括一些图形和统计输出。金融资产/证券已使用多种技术进行建模。该项目的主要目标是使用几何布朗运动模型和蒙特卡罗模拟来模拟股票价格。该模型基于受乘性噪声影响的随机(与确定性相反)变量该项......
  • PYTHON用时变马尔可夫区制转换(MARKOV REGIME SWITCHING)自回归模型分析经济时间序列|附
    全文下载链接:http://tecdat.cn/?p=22617最近我们被客户要求撰写关于MRS的研究报告,包括一些图形和统计输出。本文提供了一个在统计模型中使用马可夫转换模型模型的例子,来复现Kim和Nelson(1999)中提出的一些结果。它应用了Hamilton(1989)的滤波器和Kim(1994)的平滑器  %matplot......
  • 《最新出炉》系列入门篇-Python+Playwright自动化测试-41-录制视频
    1.简介上一篇讲解和分享了录制自动生成脚本,索性连带录制视频也一股脑的在这里就讲解和分享了。今天我们将学习如何使用Playwright和Python来录制浏览器操作的视频,以便在需要时进行回放和分析。2.录制视频语法录制视频介绍官方API的文档地址:https://playwright.dev/python/docs/......
  • js 链式调用
    functionarrany(name){lettasks=[]tasks.push(()=>{console.log(name)})functionwait(duration){tasks.push(()=>newPromise(resolve=>{setTimeout(resolve,duration)}))returnthis}functionexecute(......
  • python大模型下载HuggingFace的镜像hf-mirror
    hf-mirror.com的包如何下载pipinstall-Uhuggingface_hub设置环境变量以使用镜像站:exportHF_ENDPOINT=https://hf-mirror.com对于WindowsPowershell,使用:$env:HF_ENDPOINT="https://hf-mirror.com"使用huggingface-cli下载模型:huggingface-clidownload--resum......
  • python 操作数据库(mysql)
    python操作数据库,可以有如下几种方式。1.安装pymysql。(python的一个mysql的插件,意思就是这个服务本身就是通过python来进行安装的)2.安装mysql-connector。(数据库服务,类似java种的mysql-connector,通过mysql连接工具,可以连接上远程的mysql服务器)使用pip安装插件:python-mpip......
  • delphi 2006中,使用stdcall调用约定时,压缩结构参数的bug分析
    问题今天遇到一个很奇怪的问题,有一个dephi2006写的dll,使用了stdcall的调用约定,参数传递了结构体,在函数中收到的结构体值和传入的不一致,最后一个boolean类型,应为False,收到的是True,如下图:代码//结构体定义RStruct=packedrecordi1:Integer;i2:Integer;i3:I......
  • ABAP 调用外部WEBAPI
    ABAP代码如下,仅在内部测试通过,未涉及外部网络WEBAPI及跨域调用。*&---------------------------------------------------------------------**&ReportZYC_WEBAPI*&Restfulapi测试REPORTZYC_WEBAPI.DATA:LENTYPEI,"发送报文长度LEN_STRING......
  • Python高阶--Condition
    Condition条件触发importthreadingimporttimeclassProducer(threading.Thread):#生产者函数defrun(self):globalcountwhileTrue:ifcon.acquire():#当count小于等于1000的时候进行生产ifcount>1000:con.wait()else:count=count+100msg=self.name......
  • 《最新出炉》系列入门篇-Python+Playwright自动化测试-40-录制生成脚本
    https://www.cnblogs.com/du-hong/p/17835463.html 1.简介各种自动化框架都会有脚本录制功能,playwright这么牛叉当然也不例外。很早之前的selenium、Jmeter工具,发展到每种浏览器都有对应的录制插件。今天我们就来看下微软自动化框架playwright是如何录制脚本的。很多小伙伴或......