Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beam #4996

Merged
merged 25 commits into from
May 24, 2023
Merged

Beam #4996

Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions docs/integrations/beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Beam

This page covers how to use Beam within LangChain.
It is broken into two parts: installation and setup, and then references to specific Beam wrappers.

## Installation and Setup

- [Create an account](https://www.beam.cloud/)
- Install the Beam CLI with `curl https://raw.githubusercontent.com/slai-labs/get-beam/main/get-beam.sh -sSfL | sh`
- Register API keys with `beam configure`
- Set environment variables (`BEAM_CLIENT_ID`) and (`BEAM_CLIENT_SECRET`)
- Install the Beam SDK `pip install beam-sdk`

## Wrappers

### LLM

There exists a Beam LLM wrapper, which you can access with

```python
from langchain.llms.beam import Beam
```

## Define your Beam app.

This is the environment you’ll be developing against once you start the app.
It's also used to define the maximum response length from the model.
```python
llm = Beam(model_name="gpt2",
name="langchain-gpt2-test",
cpu=8,
memory="32Gi",
gpu="A10G",
python_version="python3.8",
python_packages=[
"diffusers[torch]>=0.10",
"transformers",
"torch",
"pillow",
"accelerate",
"safetensors",
"xformers",],
max_length="50",
verbose=False)
```

## Deploy your Beam app

Once defined, you can deploy your Beam app by calling your model's `_deploy()` method.
This returns an app ID for the deployed app, which can then be used to call the model.

```python
app_id = llm._deploy()
```

## Call your Beam app

Once a beam model is deployed, it can be called by callying your model's `_call()` method.
This returns the GPT2 text response to your prompt.

```python
response = llm._call(prompt="Running machine learning on a remote GPU", appID=appID)
```

An example script which deploys the model and calls it would be:

```python
from langchain.llms.beam import Beam
import time

llm = Beam(model_name="gpt2",
name="langchain-gpt2-test",
cpu=8,
memory="32Gi",
gpu="A10G",
python_version="python3.8",
python_packages=[
"diffusers[torch]>=0.10",
"transformers",
"torch",
"pillow",
"accelerate",
"safetensors",
"xformers",],
max_length="50",
verbose=False)

appID = llm._deploy()

response = llm._call(prompt="Running machine learning on a remote GPU", appID=appID)

print(response)
```
154 changes: 154 additions & 0 deletions docs/modules/models/llms/integrations/beam.ipynb
NolanTrem marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "J-yvaDTmTTza"
},
"source": [
"# Beam integration for langchain\n",
"\n",
"Calls the Beam API wrapper to deploy and make subsequent calls to an instance of the gpt2 LLM in a cloud deployment. Requires installation of the Beam library and registration of Beam Client ID and Client Secret. By calling the wrapper an instance of the model is created and run, with returned text relating to the prompt. Additional calls can then be made by directly calling the Beam API.\n",
"\n",
"[Create an account](https://www.beam.cloud/), if you don't have one already. Grab your API keys from the [dashboard](https://www.beam.cloud/dashboard/settings/api-keys)."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "CfTmesWtTfTS"
},
"source": [
"Install the Beam CLI"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "G_tCCurqR7Ik"
},
"outputs": [],
"source": [
"!curl https://raw.githubusercontent.com/slai-labs/get-beam/main/get-beam.sh -sSfL | sh"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "jJkcNqOdThQ7"
},
"source": [
"Register API Keys and set your beam client id and secret environment variables:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "7gQd6fszSEaH"
},
"outputs": [],
"source": [
"import os\n",
"import subprocess\n",
"\n",
"beam_client_id = \"<Your beam client id>\"\n",
"beam_client_secret = \"<Your beam client secret>\"\n",
"\n",
"# Set the environment variables\n",
"os.environ['BEAM_CLIENT_ID'] = beam_client_id\n",
"os.environ['BEAM_CLIENT_SECRET'] = beam_client_secret\n",
"\n",
"# Run the beam configure command\n",
"!beam configure --clientId={beam_client_id} --clientSecret={beam_client_secret}"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "c20rkK18TrK2"
},
"source": [
"Install the Beam SDK:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "CH2Vop6ISNIf"
},
"outputs": [],
"source": [
"!pip install beam-sdk"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"id": "XflOsp3bTwl1"
},
"source": [
"**Deploy and call Beam directly from langchain!**\n",
"\n",
"Note that a cold start might take a couple of minutes to return the response, but subsequent calls will be faster!"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "KmaHxUqbSVnh"
},
"outputs": [],
"source": [
"from langchain.llms.beam import Beam\n",
"\n",
"llm = Beam(model_name=\"gpt2\",\n",
" name=\"langchain-gpt2-test\",\n",
" cpu=8,\n",
" memory=\"32Gi\",\n",
" gpu=\"A10G\",\n",
" python_version=\"python3.8\",\n",
" python_packages=[\n",
" \"diffusers[torch]>=0.10\",\n",
" \"transformers\",\n",
" \"torch\",\n",
" \"pillow\",\n",
" \"accelerate\",\n",
" \"safetensors\",\n",
" \"xformers\",],\n",
" max_length=\"50\",\n",
" verbose=False)\n",
"\n",
"app_id = llm._deploy()\n",
"\n",
"response = llm._call(prompt=\"Running machine learning on a remote GPU\", app_id=app_id)\n",
"\n",
"print(response)"
]
}
],
"metadata": {
"colab": {
"private_outputs": true,
"provenance": []
},
"gpuClass": "standard",
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
3 changes: 3 additions & 0 deletions langchain/llms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from langchain.llms.anyscale import Anyscale
from langchain.llms.bananadev import Banana
from langchain.llms.base import BaseLLM
from langchain.llms.beam import Beam
from langchain.llms.cerebriumai import CerebriumAI
from langchain.llms.cohere import Cohere
from langchain.llms.deepinfra import DeepInfra
Expand Down Expand Up @@ -41,6 +42,7 @@
"AlephAlpha",
"Anyscale",
"Banana",
"Beam",
"CerebriumAI",
"Cohere",
"DeepInfra",
Expand Down Expand Up @@ -81,6 +83,7 @@
"anthropic": Anthropic,
"anyscale": Anyscale,
"bananadev": Banana,
"beam": Beam,
"cerebriumai": CerebriumAI,
"cohere": Cohere,
"deepinfra": DeepInfra,
Expand Down
Loading