1 Star 0 Fork 0

贺辉0912/bigmodel

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
chatglm03.py 1.49 KB
一键复制 编辑 原始数据 按行查看 历史
andy.he 提交于 2024-03-01 11:04 . 1
from transformers import pipeline
import ChatGLM
from langchain_community.llms import HuggingFacePipeline
import time
from langchain.prompts import PromptTemplate
# hf = 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": 500,"temperature":0.9,"do_sample":True},
# )
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
llm = ChatGLM.ChatGLM_LLM(streaming=True,
callbacks=[StreamingStdOutCallbackHandler()]
)
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_template("tell me a long story about {topic}")
parser = StrOutputParser()
chain = prompt | llm | parser
print(chain.invoke({"topic": "bird"}))
# from langchain.schema import HumanMessage
# # create messages to be passed to chat LLM
# messages = [HumanMessage(content="tell me a long story")]
# print(llm.generate(messages))
# template = """{question}"""
# prompt = PromptTemplate.from_template(template)
# chain = prompt | llm
# async for chunk in chain.astream({"topic": "parrot"}):
# print(chunk, end="|", flush=True)
# question = "介绍下白龙马?"
# question = "西游记有哪些人物"
# for i in range(1):
# a = time.time()
# print(chain.invoke({"question": question}))
# print(time.time()-a)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/he-hui-0912/bigmodel.git
git@gitee.com:he-hui-0912/bigmodel.git
he-hui-0912
bigmodel
bigmodel
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385