mirror of
https://github.com/hwchase17/langchain.git
synced 2026-04-04 11:25:11 +00:00
Use docusaurus versioning with a callout, merged master as well @hwchase17 @baskaryan --------- Signed-off-by: Weichen Xu <weichen.xu@databricks.com> Signed-off-by: Rahul Tripathi <rauhl.psit.ec@gmail.com> Co-authored-by: Leonid Ganeline <leo.gan.57@gmail.com> Co-authored-by: Leonid Kuligin <lkuligin@yandex.ru> Co-authored-by: Averi Kitsch <akitsch@google.com> Co-authored-by: Erick Friis <erick@langchain.dev> Co-authored-by: Nuno Campos <nuno@langchain.dev> Co-authored-by: Nuno Campos <nuno@boringbits.io> Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com> Co-authored-by: Eugene Yurtsev <eyurtsev@gmail.com> Co-authored-by: Martín Gotelli Ferenaz <martingotelliferenaz@gmail.com> Co-authored-by: Fayfox <admin@fayfox.com> Co-authored-by: Eugene Yurtsev <eugene@langchain.dev> Co-authored-by: Dawson Bauer <105886620+djbauer2@users.noreply.github.com> Co-authored-by: Ravindu Somawansa <ravindu.somawansa@gmail.com> Co-authored-by: Dhruv Chawla <43818888+Dominastorm@users.noreply.github.com> Co-authored-by: ccurme <chester.curme@gmail.com> Co-authored-by: Bagatur <baskaryan@gmail.com> Co-authored-by: WeichenXu <weichen.xu@databricks.com> Co-authored-by: Benito Geordie <89472452+benitoThree@users.noreply.github.com> Co-authored-by: kartikTAI <129414343+kartikTAI@users.noreply.github.com> Co-authored-by: Kartik Sarangmath <kartik@thirdai.com> Co-authored-by: Sevin F. Varoglu <sfvaroglu@octoml.ai> Co-authored-by: MacanPN <martin.triska@gmail.com> Co-authored-by: Prashanth Rao <35005448+prrao87@users.noreply.github.com> Co-authored-by: Hyeongchan Kim <kozistr@gmail.com> Co-authored-by: sdan <git@sdan.io> Co-authored-by: Guangdong Liu <liugddx@gmail.com> Co-authored-by: Rahul Triptahi <rahul.psit.ec@gmail.com> Co-authored-by: Rahul Tripathi <rauhl.psit.ec@gmail.com> Co-authored-by: pjb157 <84070455+pjb157@users.noreply.github.com> Co-authored-by: Eun Hye Kim <ehkim1440@gmail.com> Co-authored-by: kaijietti <43436010+kaijietti@users.noreply.github.com> Co-authored-by: Pengcheng Liu <pcliu.fd@gmail.com> Co-authored-by: Tomer Cagan <tomer@tomercagan.com> Co-authored-by: Christophe Bornet <cbornet@hotmail.com>
103 lines
3.6 KiB
Plaintext
103 lines
3.6 KiB
Plaintext
# Prediction Guard
|
|
|
|
This page covers how to use the Prediction Guard ecosystem within LangChain.
|
|
It is broken into two parts: installation and setup, and then references to specific Prediction Guard wrappers.
|
|
|
|
## Installation and Setup
|
|
- Install the Python SDK with `pip install predictionguard`
|
|
- Get a Prediction Guard access token (as described [here](https://docs.predictionguard.com/)) and set it as an environment variable (`PREDICTIONGUARD_TOKEN`)
|
|
|
|
## LLM Wrapper
|
|
|
|
There exists a Prediction Guard LLM wrapper, which you can access with
|
|
```python
|
|
from langchain_community.llms import PredictionGuard
|
|
```
|
|
|
|
You can provide the name of the Prediction Guard model as an argument when initializing the LLM:
|
|
```python
|
|
pgllm = PredictionGuard(model="MPT-7B-Instruct")
|
|
```
|
|
|
|
You can also provide your access token directly as an argument:
|
|
```python
|
|
pgllm = PredictionGuard(model="MPT-7B-Instruct", token="<your access token>")
|
|
```
|
|
|
|
Finally, you can provide an "output" argument that is used to structure/ control the output of the LLM:
|
|
```python
|
|
pgllm = PredictionGuard(model="MPT-7B-Instruct", output={"type": "boolean"})
|
|
```
|
|
|
|
## Example usage
|
|
|
|
Basic usage of the controlled or guarded LLM wrapper:
|
|
```python
|
|
import os
|
|
|
|
import predictionguard as pg
|
|
from langchain_community.llms import PredictionGuard
|
|
from langchain.prompts import PromptTemplate
|
|
from langchain.chains import LLMChain
|
|
|
|
# Your Prediction Guard API key. Get one at predictionguard.com
|
|
os.environ["PREDICTIONGUARD_TOKEN"] = "<your Prediction Guard access token>"
|
|
|
|
# Define a prompt template
|
|
template = """Respond to the following query based on the context.
|
|
|
|
Context: EVERY comment, DM + email suggestion has led us to this EXCITING announcement! 🎉 We have officially added TWO new candle subscription box options! 📦
|
|
Exclusive Candle Box - $80
|
|
Monthly Candle Box - $45 (NEW!)
|
|
Scent of The Month Box - $28 (NEW!)
|
|
Head to stories to get ALL the deets on each box! 👆 BONUS: Save 50% on your first box with code 50OFF! 🎉
|
|
|
|
Query: {query}
|
|
|
|
Result: """
|
|
prompt = PromptTemplate.from_template(template)
|
|
|
|
# With "guarding" or controlling the output of the LLM. See the
|
|
# Prediction Guard docs (https://docs.predictionguard.com) to learn how to
|
|
# control the output with integer, float, boolean, JSON, and other types and
|
|
# structures.
|
|
pgllm = PredictionGuard(model="MPT-7B-Instruct",
|
|
output={
|
|
"type": "categorical",
|
|
"categories": [
|
|
"product announcement",
|
|
"apology",
|
|
"relational"
|
|
]
|
|
})
|
|
pgllm(prompt.format(query="What kind of post is this?"))
|
|
```
|
|
|
|
Basic LLM Chaining with the Prediction Guard wrapper:
|
|
```python
|
|
import os
|
|
|
|
from langchain.prompts import PromptTemplate
|
|
from langchain.chains import LLMChain
|
|
from langchain_community.llms import PredictionGuard
|
|
|
|
# Optional, add your OpenAI API Key. This is optional, as Prediction Guard allows
|
|
# you to access all the latest open access models (see https://docs.predictionguard.com)
|
|
os.environ["OPENAI_API_KEY"] = "<your OpenAI api key>"
|
|
|
|
# Your Prediction Guard API key. Get one at predictionguard.com
|
|
os.environ["PREDICTIONGUARD_TOKEN"] = "<your Prediction Guard access token>"
|
|
|
|
pgllm = PredictionGuard(model="OpenAI-gpt-3.5-turbo-instruct")
|
|
|
|
template = """Question: {question}
|
|
|
|
Answer: Let's think step by step."""
|
|
prompt = PromptTemplate.from_template(template)
|
|
llm_chain = LLMChain(prompt=prompt, llm=pgllm, verbose=True)
|
|
|
|
question = "What NFL team won the Super Bowl in the year Justin Beiber was born?"
|
|
|
|
llm_chain.predict(question=question)
|
|
```
|