Skip to main content

Prediction Guard

pip install predictionguard langchain
import os

import predictionguard as pg
from langchain.llms import PredictionGuard
from langchain import PromptTemplate, LLMChain

基本LLM用法 (Basic LLM usage)

# 可选项,添加您的OpenAI API密钥。这是可选的,因为Prediction Guard允许您访问所有最新的开放访问模型(请参阅https://docs.predictionguard.com)
os.environ["OPENAI_API_KEY"] = "<您的OpenAI API密钥>"

# 您的Prediction Guard API密钥。在predictionguard.com上获取一个
os.environ["PREDICTIONGUARD_TOKEN"] = "<您的Prediction Guard访问令牌>"
pgllm = PredictionGuard(model="OpenAI-text-davinci-003")
pgllm("告诉我一个笑话")

控制LLM的输出结构/类型 (Control the output structure/ type of LLMs)

template = """根据上下文回答以下查询。

上下文:每个评论、DM + 电子邮件建议都引导我们做出这个令人兴奋的公告!🎉 我们正式添加了两个新的蜡烛订阅盒选项!📦
独家蜡烛盒 - $80
每月蜡烛盒 - $45(新!)
本月香气盒 - $28(新!)
前往故事了解每个盒子的所有细节!👆 奖励:使用50OFF代码节省首个盒子的50%!🎉

查询:{query}

结果:"""
prompt = PromptTemplate(template=template, input_variables=["query"])
# 没有"守护"或控制LLM的输出。
pgllm(prompt.format(query="这是什么样的帖子?"))
# 使用"守护"或控制LLM的输出。请参阅Prediction Guard文档(https://docs.predictionguard.com)以了解如何使用整数、浮点数、布尔值、JSON和其他类型和结构来控制输出。
pgllm = PredictionGuard(
model="OpenAI-text-davinci-003",
output={
"type": "categorical",
"categories": ["产品公告", "道歉", "关系"],
},
)
pgllm(prompt.format(query="这是什么样的帖子?"))

链接 (Chaining)

pgllm = PredictionGuard(model="OpenAI-text-davinci-003")
template = """问题:{question}

答案:让我们一步一步地思考。"""
prompt = PromptTemplate(template=template, input_variables=["question"])
llm_chain = LLMChain(prompt=prompt, llm=pgllm, verbose=True)

question = "贾斯汀·比伯出生的那一年,哪支NFL球队赢得了超级碗?"

llm_chain.predict(question=question)
template = """写一首关于{subject}的{adjective}诗。"""
prompt = PromptTemplate(template=template, input_variables=["adjective", "subject"])
llm_chain = LLMChain(prompt=prompt, llm=pgllm, verbose=True)

llm_chain.predict(adjective="悲伤的", subject="鸭子")