ssprompt实战指南--获取LLM Prompt并应用

798 阅读3分钟

✈️ 前言

👋Hi,大家好,我是Baird

上篇文章(详情链接)给大家介绍了我最近构思创作的ssprompt--LLM Prompt分发管理工具,ssprompt是一个Prompt分发和管理工具,它的目标是能帮助你快速获取一个Prompt,并应用你所需要的地方。

这次就结合实战,跟大家唠唠ssprompt怎么使用 🎶

☁️ Prompt Hub

首先,先分享一下ssprompt默认使用的Prompt仓库--Prompt Hub

😊简单理解:
Prompt Hub对ssprompt,类似于Docker Hub对Docker

Prompt Hub目前第一阶段先托管在Github上,Prompt与Docker镜像还是不同,Prompt简直就是一门新编程语言,代码化和工程化的Prompt托管在Github也顺理成章,待后续发展再决定看是否更换

当前ssprompt默认所使用的Prompt Hub项目地址是 github.com/ptonlix/Pro…

Prompt Hub还在持续建设中,持续收录更多有用的Prompt

本篇主要还是聚焦怎么使用ssprompt,应用到我们的项目中,接下来我们就以一个Prompt工程例子来讲讲

🌰 Summarization Prompt

我们以目前Prompt Hub上存在的Summarization这个Prompt为例,做我们本次的演示

image.png

1⃣️下载安装ssprompt

首先,我们先下载最新版本的ssprompt v0.0.4,下载参考如下方式

pip安装

pip install ssprompt

Linux, macOS, Windows (WSL)

curl -sSL https://raw.githubusercontent.com/ptonlix/ssprompt/main/install.py | python3 -

Windows (Powershell)

(Invoke-WebRequest -Uri https://raw.githubusercontent.com/ptonlix/ssprompt/main/install.py -UseBasicParsing).Conte

2⃣️ 拉取相应的文件或代码到本地工程

ssprompt当前支持4种类型的Prompt使用

  • Text
  • Json
  • Yaml
  • Python

这四种类型Prompt,主要参考目前主流的LLM应用框架 langchainhaystack

各Prompt工程,根据不同应用场景,选择支持相应的Prompt类型。Summarization Prompt目前支持四种类型的Prompt

Summarization Prompt目前应用于langchain,需要确认langchain已安装到当前Python环境中

Python类型Prompt 使用

假设,我们已经存在prompt-test这个python工程或其他应用

# 进入prompt-test目录
cd prompt-test

# 通过ssprompt拉取summarization工程 python代码
ssprompt pull -s summarization -t python

# ssprompt pull 详细说明可以使用help查看
ssprompt pull -h

主工程引用Summarization Python类型的Prompt

# __main__.py
from langchain import OpenAI
from langchain.chains import LLMChain

# 使用api2d访问openai
ai = OpenAI()
ai.openai_api_base = "https://oa.api2d.net/v1" 

from summarization import PROMPT

chain = LLMChain(llm=ai, prompt=PROMPT)

doc="""
Feature stores are a concept from traditional machine learning that make sure data fed into models is up-to-date and relevant. For more on this, see here.

This concept is extremely relevant when considering putting LLM applications in production. In order to personalize LLM applications, you may want to combine LLMs with up-to-date information about particular users. Feature stores can be a great way to keep that data fresh, and LangChain provides an easy way to combine that data with LLMs.

In this notebook we will show how to connect prompt templates to feature stores. The basic idea is to call a feature store from inside a prompt template to retrieve values that are then formatted into the prompt.
"""
chain.run(documents=doc, require=["字数限定在30个字左右"])

具体引用方式,请结合每个项目的代码组织样式,需要自主判断

Json类型Prompt 使用

# 进入prompt-test目录
cd prompt-test

# 拉取Python库到项目
ssprompt pull -s summarization -t json

主程序引用参考

# __main__.py
from langchain.prompts import load_prompt

prompt = load_prompt("summarization/summarization.json")
print(prompt.format(documents="funny", require="chickens"))

Yaml类型Prompt 使用

# 进入prompt-test目录
cd prompt-test

# 拉取Python库到项目
ssprompt pull -s summarization -t yaml

主程序引用参考

# __main__.py
from langchain.prompts import load_prompt

prompt = load_prompt("summarization/summarization.json")
print(prompt.format(documents="funny", require="chickens"))

Text类型Prompt使用

# 进入到项目路径
cd Python项目路径

# 拉取Python库到项目
ssprompt pull -s summarization -t text

Text类型主要应用于openai等LLM页面请求测试使用,故可以填充相应变量,拷贝复制到chat.openai.com等交互页面,与大模型进行交互测试

🔚 结尾

目前ssprompt和Prompt Hub都还在建设初期,如有不周之处,请多包涵~
如遇问题请联系作者噢~ 谢谢👏👏👏

欢迎更多感兴趣的同学,联系我加入一起共创,您可以做任何有益事情

  • 报告错误
  • 建议改进
  • 文档贡献
  • 代码贡献
    ...
    🚀🚀🚀