当前位置:   article > 正文

本地运行chatglm3-6b 和 ChatPromptTemplate的结合使用_prompt_templates chatglm3

prompt_templates chatglm3
import gradio
from transformers import AutoTokenizer, AutoModel
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_community.llms import HuggingFacePipeline
import time

def greet(name):
    response = chain.invoke({"user_input": name})
    return response

model = HuggingFacePipeline.from_model_id(
    model_id="THUDM/chatglm3-6b",
    task="text-generation",
    device=0,
    model_kwargs={"trust_remote_code":True},
    pipeline_kwargs={"max_new_tokens": 5000},
)

prompt = ChatPromptTemplate.from_template("告诉我关于{user_input}的经济发展情况,不多于200个字")
output_parser = StrOutputParser()
chain = prompt | model | output_parser
demo = gradio.Interface(fn=greet, inputs="text", outputs="text")
demo.launch() 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
import gradio
from langchain_core.prompts import ChatPromptTemplate  
from langchain.prompts import PromptTemplate 
from langchain_core.output_parsers import StrOutputParser
from langchain_community.llms import HuggingFacePipeline
from langchain.prompts import HumanMessagePromptTemplate
import time

# from ChatGLM_new import zhipu_llm
# model  = zhipu_llm 


model = HuggingFacePipeline.from_model_id(
    model_id="THUDM/chatglm3-6b",
    task="text-generation",
    verbose=True,
    device=0,
    model_kwargs={"trust_remote_code":True},
    pipeline_kwargs={"max_new_tokens": 5000},
)

prompt = ChatPromptTemplate.from_messages([
                # ("system", "记住:对所有问题你只回答下面的4个字:我不知道,"),
                # ("human", "Hello, how are you doing?"),
                # ("ai", "I'm doing well, thanks!"),
                ("human", "告诉我关于{user_input}的经济发展情况,不多于200个字"),
            ])

prompt = ChatPromptTemplate.from_messages([
     HumanMessagePromptTemplate.from_template("告诉我关于{user_input}的经济发展情况,不多于200个字"),
            ])

output_parser = StrOutputParser()
chain = prompt | model | output_parser
def greet(name):
    response = chain.invoke({"user_input": name})
    return response
demo = gradio.Interface(fn=greet, inputs="text", outputs="text")
demo.launch() 
  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/369581
推荐阅读
相关标签
  

闽ICP备14008679号