Skip to content

Commit

Permalink
Update getting_started.md (#4482)
Browse files Browse the repository at this point in the history
# Added another helpful way for developers who want to set OpenAI API
Key dynamically

Previous methods like exporting environment variables are good for
project-wide settings.
But many use cases need to assign API keys dynamically, recently.

```python
from langchain.llms import OpenAI
llm = OpenAI(openai_api_key="OPENAI_API_KEY")
```

## Before submitting
```bash
export OPENAI_API_KEY="..."
```
Or,
```python
import os
os.environ["OPENAI_API_KEY"] = "..."
```

<hr>

Thank you.
Cheers,
Bongsang
  • Loading branch information
bongsang authored May 18, 2023
1 parent 41e2394 commit 613bf9b
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 26 deletions.
6 changes: 6 additions & 0 deletions docs/getting_started/getting_started.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ import os
os.environ["OPENAI_API_KEY"] = "..."
```

If you want to set the API key dynamically, you can use the openai_api_key parameter when initiating OpenAI class—for instance, each user's API key.

```python
from langchain.llms import OpenAI
llm = OpenAI(openai_api_key="OPENAI_API_KEY")
```

## Building a Language Model Application: LLMs

Expand Down
87 changes: 61 additions & 26 deletions docs/modules/chains/getting_started.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
"text": [
"\n",
"\n",
"SockSplash!\n"
"Colorful Toes Co.\n"
]
}
],
Expand All @@ -81,15 +81,50 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"You can use a chat model in an `LLMChain` as well:"
"If there are multiple variables, you can input them all at once using a dictionary."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"Socktopia Colourful Creations.\n"
]
}
],
"source": [
"prompt = PromptTemplate(\n",
" input_variables=[\"company\", \"product\"],\n",
" template=\"What is a good name for {company} that makes {product}?\",\n",
")\n",
"chain = LLMChain(llm=llm, prompt=prompt)\n",
"print(chain.run({\n",
" 'company': \"ABC Startup\",\n",
" 'product': \"colorful socks\"\n",
" }))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can use a chat model in an `LLMChain` as well:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"tags": []
},
Expand All @@ -98,7 +133,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Rainbow Sox Co.\n"
"Rainbow Socks Co.\n"
]
}
],
Expand Down Expand Up @@ -131,7 +166,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 5,
"metadata": {},
"outputs": [
{
Expand All @@ -141,7 +176,7 @@
" 'text': 'Why did the tomato turn red? Because it saw the salad dressing!'}"
]
},
"execution_count": 4,
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -166,7 +201,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"metadata": {},
"outputs": [
{
Expand All @@ -175,7 +210,7 @@
"{'text': 'Why did the tomato turn red? Because it saw the salad dressing!'}"
]
},
"execution_count": 5,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -193,7 +228,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand All @@ -202,7 +237,7 @@
"['text']"
]
},
"execution_count": 6,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -214,7 +249,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"metadata": {},
"outputs": [
{
Expand All @@ -223,7 +258,7 @@
"'Why did the tomato turn red? Because it saw the salad dressing!'"
]
},
"execution_count": 7,
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -241,7 +276,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 9,
"metadata": {},
"outputs": [
{
Expand All @@ -251,7 +286,7 @@
" 'text': 'Why did the tomato turn red? Because it saw the salad dressing!'}"
]
},
"execution_count": 8,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -284,7 +319,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 10,
"metadata": {},
"outputs": [
{
Expand All @@ -293,7 +328,7 @@
"'The next four colors of a rainbow are green, blue, indigo, and violet.'"
]
},
"execution_count": 9,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -331,7 +366,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 11,
"metadata": {},
"outputs": [
{
Expand All @@ -358,7 +393,7 @@
"'ChatGPT is an AI language model developed by OpenAI. It is based on the GPT-3 architecture and is capable of generating human-like responses to text prompts. ChatGPT has been trained on a massive amount of text data and can understand and respond to a wide range of topics. It is often used for chatbots, virtual assistants, and other conversational AI applications.'"
]
},
"execution_count": 10,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -387,7 +422,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -407,7 +442,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 13,
"metadata": {},
"outputs": [
{
Expand All @@ -420,12 +455,12 @@
"\u001b[36;1m\u001b[1;3mRainbow Socks Co.\u001b[0m\n",
"\u001b[33;1m\u001b[1;3m\n",
"\n",
"\"Step into Color with Rainbow Socks!\"\u001b[0m\n",
"\"Put a little rainbow in your step!\"\u001b[0m\n",
"\n",
"\u001b[1m> Finished chain.\u001b[0m\n",
"\n",
"\n",
"\"Step into Color with Rainbow Socks!\"\n"
"\"Put a little rainbow in your step!\"\n"
]
}
],
Expand Down Expand Up @@ -456,7 +491,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -496,7 +531,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 15,
"metadata": {},
"outputs": [
{
Expand All @@ -506,9 +541,9 @@
"Concatenated output:\n",
"\n",
"\n",
"Socktastic Colors.\n",
"Funky Footwear Company\n",
"\n",
"\"Put Some Color in Your Step!\"\n"
"\"Brighten Up Your Day with Our Colorful Socks!\"\n"
]
}
],
Expand Down Expand Up @@ -554,7 +589,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
"version": "3.9.16"
},
"vscode": {
"interpreter": {
Expand Down

0 comments on commit 613bf9b

Please sign in to comment.