Harrison/prediction guard update (#5404)

Co-authored-by: Daniel Whitenack <whitenack.daniel@gmail.com>
This commit is contained in:
Harrison Chase 2023-05-29 07:14:59 -07:00 committed by GitHub
parent 416c8b1da3
commit d6fb25c439
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 304 additions and 182 deletions

View File

@ -14,41 +14,85 @@ There exists a Prediction Guard LLM wrapper, which you can access with
from langchain.llms import PredictionGuard from langchain.llms import PredictionGuard
``` ```
You can provide the name of your Prediction Guard "proxy" as an argument when initializing the LLM: You can provide the name of the Prediction Guard model as an argument when initializing the LLM:
```python ```python
pgllm = PredictionGuard(name="your-text-gen-proxy") pgllm = PredictionGuard(model="MPT-7B-Instruct")
```
Alternatively, you can use Prediction Guard's default proxy for SOTA LLMs:
```python
pgllm = PredictionGuard(name="default-text-gen")
``` ```
You can also provide your access token directly as an argument: You can also provide your access token directly as an argument:
```python ```python
pgllm = PredictionGuard(name="default-text-gen", token="<your access token>") 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 ## Example usage
Basic usage of the LLM wrapper: Basic usage of the controlled or guarded LLM wrapper:
```python ```python
from langchain.llms import PredictionGuard import os
pgllm = PredictionGuard(name="default-text-gen") import predictionguard as pg
pgllm("Tell me a joke") from langchain.llms import PredictionGuard
from langchain import PromptTemplate, 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 ALLL the deets on each box! 👆 BONUS: Save 50% on your first box with code 50OFF! 🎉
Query: {query}
Result: """
prompt = PromptTemplate(template=template, input_variables=["query"])
# 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: Basic LLM Chaining with the Prediction Guard wrapper:
```python ```python
import os
from langchain import PromptTemplate, LLMChain from langchain import PromptTemplate, LLMChain
from langchain.llms import PredictionGuard from langchain.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-text-davinci-003")
template = """Question: {question} template = """Question: {question}
Answer: Let's think step by step.""" Answer: Let's think step by step."""
prompt = PromptTemplate(template=template, input_variables=["question"]) prompt = PromptTemplate(template=template, input_variables=["question"])
llm_chain = LLMChain(prompt=prompt, llm=PredictionGuard(name="default-text-gen"), verbose=True) llm_chain = LLMChain(prompt=prompt, llm=pgllm, verbose=True)
question = "What NFL team won the Super Bowl in the year Justin Beiber was born?" question = "What NFL team won the Super Bowl in the year Justin Beiber was born?"

View File

@ -1,155 +1,222 @@
{ {
"cells": [ "nbformat": 4,
{ "nbformat_minor": 0,
"cell_type": "markdown", "metadata": {
"metadata": {}, "colab": {
"source": [ "provenance": []
"# PredictionGuard\n", },
"\n", "kernelspec": {
"How to use PredictionGuard wrapper" "name": "python3",
] "display_name": "Python 3"
},
"language_info": {
"name": "python"
}
}, },
{ "cells": [
"cell_type": "code", {
"execution_count": null, "cell_type": "code",
"metadata": { "execution_count": null,
"id": "3RqWPav7AtKL" "metadata": {
}, "id": "3RqWPav7AtKL"
"outputs": [], },
"source": [ "outputs": [],
"! pip install predictionguard langchain" "source": [
] "! pip install predictionguard langchain"
}, ]
{ },
"cell_type": "code", {
"execution_count": 1, "cell_type": "code",
"metadata": { "source": [
"id": "2xe8JEUwA7_y" "import os\n",
}, "\n",
"outputs": [], "import predictionguard as pg\n",
"source": [ "from langchain.llms import PredictionGuard\n",
"import predictionguard as pg\n", "from langchain import PromptTemplate, LLMChain"
"from langchain.llms import PredictionGuard" ],
] "metadata": {
}, "id": "2xe8JEUwA7_y"
{ },
"cell_type": "markdown", "execution_count": null,
"metadata": { "outputs": []
"id": "mesCTyhnJkNS" },
}, {
"source": [ "cell_type": "markdown",
"## Basic LLM usage\n", "source": [
"\n" "# Basic LLM usage\n",
] "\n"
}, ],
{ "metadata": {
"cell_type": "code", "id": "mesCTyhnJkNS"
"execution_count": null, }
"metadata": { },
"id": "Ua7Mw1N4HcER" {
}, "cell_type": "code",
"outputs": [], "source": [
"source": [ "# Optional, add your OpenAI API Key. This is optional, as Prediction Guard allows\n",
"pgllm = PredictionGuard(name=\"default-text-gen\", token=\"<your access token>\")" "# you to access all the latest open access models (see https://docs.predictionguard.com)\n",
] "os.environ[\"OPENAI_API_KEY\"] = \"<your OpenAI api key>\"\n",
}, "\n",
{ "# Your Prediction Guard API key. Get one at predictionguard.com\n",
"cell_type": "code", "os.environ[\"PREDICTIONGUARD_TOKEN\"] = \"<your Prediction Guard access token>\""
"execution_count": null, ],
"metadata": { "metadata": {
"id": "Qo2p5flLHxrB" "id": "kp_Ymnx1SnDG"
}, },
"outputs": [], "execution_count": null,
"source": [ "outputs": []
"pgllm(\"Tell me a joke\")" },
] {
}, "cell_type": "code",
{ "source": [
"cell_type": "markdown", "pgllm = PredictionGuard(model=\"OpenAI-text-davinci-003\")"
"metadata": { ],
"id": "v3MzIUItJ8kV" "metadata": {
}, "id": "Ua7Mw1N4HcER"
"source": [ },
"## Chaining" "execution_count": null,
] "outputs": []
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "source": [
"metadata": { "pgllm(\"Tell me a joke\")"
"id": "pPegEZExILrT" ],
}, "metadata": {
"outputs": [], "id": "Qo2p5flLHxrB"
"source": [ },
"from langchain import PromptTemplate, LLMChain" "execution_count": null,
] "outputs": []
}, },
{ {
"cell_type": "code", "cell_type": "markdown",
"execution_count": null, "source": [
"metadata": { "# Control the output structure/ type of LLMs"
"id": "suxw62y-J-bg" ],
}, "metadata": {
"outputs": [], "id": "EyBYaP_xTMXH"
"source": [ }
"template = \"\"\"Question: {question}\n", },
"\n", {
"Answer: Let's think step by step.\"\"\"\n", "cell_type": "code",
"prompt = PromptTemplate(template=template, input_variables=[\"question\"])\n", "source": [
"llm_chain = LLMChain(prompt=prompt, llm=pgllm, verbose=True)\n", "template = \"\"\"Respond to the following query based on the context.\n",
"\n", "\n",
"question = \"What NFL team won the Super Bowl in the year Justin Beiber was born?\"\n", "Context: EVERY comment, DM + email suggestion has led us to this EXCITING announcement! 🎉 We have officially added TWO new candle subscription box options! 📦\n",
"\n", "Exclusive Candle Box - $80 \n",
"llm_chain.predict(question=question)" "Monthly Candle Box - $45 (NEW!)\n",
] "Scent of The Month Box - $28 (NEW!)\n",
}, "Head to stories to get ALLL the deets on each box! 👆 BONUS: Save 50% on your first box with code 50OFF! 🎉\n",
{ "\n",
"cell_type": "code", "Query: {query}\n",
"execution_count": null, "\n",
"metadata": { "Result: \"\"\"\n",
"id": "l2bc26KHKr7n" "prompt = PromptTemplate(template=template, input_variables=[\"query\"])"
}, ],
"outputs": [], "metadata": {
"source": [ "id": "55uxzhQSTPqF"
"template = \"\"\"Write a {adjective} poem about {subject}.\"\"\"\n", },
"prompt = PromptTemplate(template=template, input_variables=[\"adjective\", \"subject\"])\n", "execution_count": null,
"llm_chain = LLMChain(prompt=prompt, llm=pgllm, verbose=True)\n", "outputs": []
"\n", },
"llm_chain.predict(adjective=\"sad\", subject=\"ducks\")" {
] "cell_type": "code",
}, "source": [
{ "# Without \"guarding\" or controlling the output of the LLM.\n",
"cell_type": "code", "pgllm(prompt.format(query=\"What kind of post is this?\"))"
"execution_count": null, ],
"metadata": { "metadata": {
"id": "I--eSa2PLGqq" "id": "yersskWbTaxU"
}, },
"outputs": [], "execution_count": null,
"source": [] "outputs": []
} },
], {
"metadata": { "cell_type": "code",
"colab": { "source": [
"provenance": [] "# With \"guarding\" or controlling the output of the LLM. See the \n",
}, "# Prediction Guard docs (https://docs.predictionguard.com) to learn how to \n",
"kernelspec": { "# control the output with integer, float, boolean, JSON, and other types and\n",
"display_name": "Python 3 (ipykernel)", "# structures.\n",
"language": "python", "pgllm = PredictionGuard(model=\"OpenAI-text-davinci-003\", \n",
"name": "python3" " output={\n",
}, " \"type\": \"categorical\",\n",
"language_info": { " \"categories\": [\n",
"codemirror_mode": { " \"product announcement\", \n",
"name": "ipython", " \"apology\", \n",
"version": 3 " \"relational\"\n",
}, " ]\n",
"file_extension": ".py", " })\n",
"mimetype": "text/x-python", "pgllm(prompt.format(query=\"What kind of post is this?\"))"
"name": "python", ],
"nbconvert_exporter": "python", "metadata": {
"pygments_lexer": "ipython3", "id": "PzxSbYwqTm2w"
"version": "3.9.1" },
} "execution_count": null,
}, "outputs": []
"nbformat": 4, },
"nbformat_minor": 1 {
"cell_type": "markdown",
"source": [
"# Chaining"
],
"metadata": {
"id": "v3MzIUItJ8kV"
}
},
{
"cell_type": "code",
"source": [
"pgllm = PredictionGuard(model=\"OpenAI-text-davinci-003\")"
],
"metadata": {
"id": "pPegEZExILrT"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"template = \"\"\"Question: {question}\n",
"\n",
"Answer: Let's think step by step.\"\"\"\n",
"prompt = PromptTemplate(template=template, input_variables=[\"question\"])\n",
"llm_chain = LLMChain(prompt=prompt, llm=pgllm, verbose=True)\n",
"\n",
"question = \"What NFL team won the Super Bowl in the year Justin Beiber was born?\"\n",
"\n",
"llm_chain.predict(question=question)"
],
"metadata": {
"id": "suxw62y-J-bg"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"template = \"\"\"Write a {adjective} poem about {subject}.\"\"\"\n",
"prompt = PromptTemplate(template=template, input_variables=[\"adjective\", \"subject\"])\n",
"llm_chain = LLMChain(prompt=prompt, llm=pgllm, verbose=True)\n",
"\n",
"llm_chain.predict(adjective=\"sad\", subject=\"ducks\")"
],
"metadata": {
"id": "l2bc26KHKr7n"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [],
"metadata": {
"id": "I--eSa2PLGqq"
},
"execution_count": null,
"outputs": []
}
]
} }

View File

@ -16,15 +16,24 @@ class PredictionGuard(LLM):
"""Wrapper around Prediction Guard large language models. """Wrapper around Prediction Guard large language models.
To use, you should have the ``predictionguard`` python package installed, and the To use, you should have the ``predictionguard`` python package installed, and the
environment variable ``PREDICTIONGUARD_TOKEN`` set with your access token, or pass environment variable ``PREDICTIONGUARD_TOKEN`` set with your access token, or pass
it as a named parameter to the constructor. it as a named parameter to the constructor. To use Prediction Guard's API along
with OpenAI models, set the environment variable ``OPENAI_API_KEY`` with your
OpenAI API key as well.
Example: Example:
.. code-block:: python .. code-block:: python
pgllm = PredictionGuard(name="text-gen-proxy-name", token="my-access-token") pgllm = PredictionGuard(model="MPT-7B-Instruct",
token="my-access-token",
output={
"type": "boolean"
})
""" """
client: Any #: :meta private: client: Any #: :meta private:
name: Optional[str] = "default-text-gen" model: Optional[str] = "MPT-7B-Instruct"
"""Proxy name to use.""" """Model name to use."""
output: Optional[Dict[str, Any]] = None
"""The output type or structure for controlling the LLM output."""
max_tokens: int = 256 max_tokens: int = 256
"""Denotes the number of tokens to predict per generation.""" """Denotes the number of tokens to predict per generation."""
@ -33,6 +42,7 @@ class PredictionGuard(LLM):
"""A non-negative float that tunes the degree of randomness in generation.""" """A non-negative float that tunes the degree of randomness in generation."""
token: Optional[str] = None token: Optional[str] = None
"""Your Prediction Guard access token."""
stop: Optional[List[str]] = None stop: Optional[List[str]] = None
@ -58,7 +68,7 @@ class PredictionGuard(LLM):
@property @property
def _default_params(self) -> Dict[str, Any]: def _default_params(self) -> Dict[str, Any]:
"""Get the default parameters for calling Cohere API.""" """Get the default parameters for calling the Prediction Guard API."""
return { return {
"max_tokens": self.max_tokens, "max_tokens": self.max_tokens,
"temperature": self.temperature, "temperature": self.temperature,
@ -67,7 +77,7 @@ class PredictionGuard(LLM):
@property @property
def _identifying_params(self) -> Dict[str, Any]: def _identifying_params(self) -> Dict[str, Any]:
"""Get the identifying parameters.""" """Get the identifying parameters."""
return {**{"name": self.name}, **self._default_params} return {**{"model": self.model}, **self._default_params}
@property @property
def _llm_type(self) -> str: def _llm_type(self) -> str:
@ -80,7 +90,7 @@ class PredictionGuard(LLM):
stop: Optional[List[str]] = None, stop: Optional[List[str]] = None,
run_manager: Optional[CallbackManagerForLLMRun] = None, run_manager: Optional[CallbackManagerForLLMRun] = None,
) -> str: ) -> str:
"""Call out to Prediction Guard's model proxy. """Call out to Prediction Guard's model API.
Args: Args:
prompt: The prompt to pass into the model. prompt: The prompt to pass into the model.
Returns: Returns:
@ -89,6 +99,8 @@ class PredictionGuard(LLM):
.. code-block:: python .. code-block:: python
response = pgllm("Tell me a joke.") response = pgllm("Tell me a joke.")
""" """
import predictionguard as pg
params = self._default_params params = self._default_params
if self.stop is not None and stop is not None: if self.stop is not None and stop is not None:
raise ValueError("`stop` found in both the input and default params.") raise ValueError("`stop` found in both the input and default params.")
@ -97,15 +109,14 @@ class PredictionGuard(LLM):
else: else:
params["stop_sequences"] = stop params["stop_sequences"] = stop
response = self.client.predict( response = pg.Completion.create(
name=self.name, model=self.model,
data={ prompt=prompt,
"prompt": prompt, output=self.output,
"max_tokens": params["max_tokens"], temperature=params["temperature"],
"temperature": params["temperature"], max_tokens=params["max_tokens"],
},
) )
text = response["text"] text = response["choices"][0]["text"]
# If stop tokens are provided, Prediction Guard's endpoint returns them. # If stop tokens are provided, Prediction Guard's endpoint returns them.
# In order to make this consistent with other endpoints, we strip them. # In order to make this consistent with other endpoints, we strip them.

View File

@ -5,6 +5,6 @@ from langchain.llms.predictionguard import PredictionGuard
def test_predictionguard_call() -> None: def test_predictionguard_call() -> None:
"""Test valid call to prediction guard.""" """Test valid call to prediction guard."""
llm = PredictionGuard(name="default-text-gen") llm = PredictionGuard(model="OpenAI-text-davinci-003")
output = llm("Say foo:") output = llm("Say foo:")
assert isinstance(output, str) assert isinstance(output, str)