在Colab上试跑langchain 和 huggingface的整合

环境准备:

获取【HUGGINGFACEHUB_API_TOKEN】,登录huggingface官网并完成注册。

官网:https://huggingface.co/

获取token:https://huggingface.co/settings/tokens

在colab上安装 huggingface_hub

!pip install -q huggingface_hub

【1】创建问答提示模版Creating a Question-Answering Prompt Template

from langchain import PromptTemplate

template = """Question: {question}

Answer: """
prompt = PromptTemplate(
        template=template,
    input_variables=['question']
)

# user question
question = "What is the capital city of China?"

【2】使用huggingface_hub模型“google/flan-t5-large”来回答问题,huggingfaceHub 类将连接到 HuggingFace 的推理 API 并加载指定的模型。

from langchain import HuggingFaceHub, LLMChain

# initialize Hub LLM
hub_llm = HuggingFaceHub(
        repo_id='google/flan-t5-large',
    model_kwargs={'temperature':0},
    huggingfacehub_api_token='your huggingfacehub_api_token'
)

# create prompt template > LLM chain
llm_chain = LLMChain(
    prompt=prompt,
    llm=hub_llm
)

# ask the user question about the capital of France
print(llm_chain.run(question))

【3】输出结果

beijing

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Colab上运行VTK(Visualization Toolkit)需要进行以下步骤: 1. 安装必要的软件包:在Colab的代码单元格中,使用以下命令安装VTK和相关软件包。 ```python !pip install vtk !apt-get update !apt-get install -y xvfb x11-utils !pip install pyvirtualdisplay !pip install EasyProcess ``` 2. 设置虚拟显示:在Colab的代码单元格中,使用以下代码设置虚拟显示。 ```python from easyprocess import EasyProcess from pyvirtualdisplay import Display display = Display(visible=0, size=(800, 600)) display.start() xvfb = EasyProcess(['Xvfb', ':1', '-screen', '0', '800x600x16']) xvfb.start() ``` 3. 导入VTK并进行测试:在Colab的代码单元格中,使用以下代码导入VTK并进行简单的测试。 ```python import vtk # 创建一个简单的VTK场景 renderer = vtk.vtkRenderer() renderWindow = vtk.vtkRenderWindow() renderWindow.SetWindowName("VTK Example") renderWindow.AddRenderer(renderer) # 创建一个简单的VTK演员 coneSource = vtk.vtkConeSource() coneMapper = vtk.vtkPolyDataMapper() coneMapper.SetInputConnection(coneSource.GetOutputPort()) coneActor = vtk.vtkActor() coneActor.SetMapper(coneMapper) # 将演员添加到场景中 renderer.AddActor(coneActor) # 创建一个渲染窗口交互器并启动事件循环 renderWindowInteractor = vtk.vtkRenderWindowInteractor() renderWindowInteractor.SetRenderWindow(renderWindow) renderWindow.Render() renderWindowInteractor.Start() ``` 以上是在Colab中运行VTK的基本步骤。请注意,由于Colab是基于云的环境,可能会受到一些限制,并且可能会在性能方面有所不同。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值