Update getting_started.md (#4482)

# 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
docker
Steve Kim 1 year ago committed by GitHub
parent 41e2394c9c
commit 613bf9b514
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -37,6 +37,12 @@ import os
os.environ["OPENAI_API_KEY"] = "..." 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 ## Building a Language Model Application: LLMs

@ -68,7 +68,7 @@
"text": [ "text": [
"\n", "\n",
"\n", "\n",
"SockSplash!\n" "Colorful Toes Co.\n"
] ]
} }
], ],
@ -81,15 +81,50 @@
] ]
}, },
{ {
"attachments": {},
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "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", "cell_type": "code",
"execution_count": 3, "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": { "metadata": {
"tags": [] "tags": []
}, },
@ -98,7 +133,7 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"Rainbow Sox Co.\n" "Rainbow Socks Co.\n"
] ]
} }
], ],
@ -131,7 +166,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 5,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -141,7 +176,7 @@
" 'text': 'Why did the tomato turn red? Because it saw the salad dressing!'}" " 'text': 'Why did the tomato turn red? Because it saw the salad dressing!'}"
] ]
}, },
"execution_count": 4, "execution_count": 5,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -166,7 +201,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": 6,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -175,7 +210,7 @@
"{'text': 'Why did the tomato turn red? Because it saw the salad dressing!'}" "{'text': 'Why did the tomato turn red? Because it saw the salad dressing!'}"
] ]
}, },
"execution_count": 5, "execution_count": 6,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -193,7 +228,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 7,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -202,7 +237,7 @@
"['text']" "['text']"
] ]
}, },
"execution_count": 6, "execution_count": 7,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -214,7 +249,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": 8,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -223,7 +258,7 @@
"'Why did the tomato turn red? Because it saw the salad dressing!'" "'Why did the tomato turn red? Because it saw the salad dressing!'"
] ]
}, },
"execution_count": 7, "execution_count": 8,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -241,7 +276,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": 9,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -251,7 +286,7 @@
" 'text': 'Why did the tomato turn red? Because it saw the salad dressing!'}" " 'text': 'Why did the tomato turn red? Because it saw the salad dressing!'}"
] ]
}, },
"execution_count": 8, "execution_count": 9,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -284,7 +319,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 9, "execution_count": 10,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -293,7 +328,7 @@
"'The next four colors of a rainbow are green, blue, indigo, and violet.'" "'The next four colors of a rainbow are green, blue, indigo, and violet.'"
] ]
}, },
"execution_count": 9, "execution_count": 10,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -331,7 +366,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 10, "execution_count": 11,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -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.'" "'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": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -387,7 +422,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 12,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -407,7 +442,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 12, "execution_count": 13,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -420,12 +455,12 @@
"\u001b[36;1m\u001b[1;3mRainbow Socks Co.\u001b[0m\n", "\u001b[36;1m\u001b[1;3mRainbow Socks Co.\u001b[0m\n",
"\u001b[33;1m\u001b[1;3m\n", "\u001b[33;1m\u001b[1;3m\n",
"\n", "\n",
"\"Step into Color with Rainbow Socks!\"\u001b[0m\n", "\"Put a little rainbow in your step!\"\u001b[0m\n",
"\n", "\n",
"\u001b[1m> Finished chain.\u001b[0m\n", "\u001b[1m> Finished chain.\u001b[0m\n",
"\n", "\n",
"\n", "\n",
"\"Step into Color with Rainbow Socks!\"\n" "\"Put a little rainbow in your step!\"\n"
] ]
} }
], ],
@ -456,7 +491,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 13, "execution_count": 14,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
@ -496,7 +531,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 14, "execution_count": 15,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -506,9 +541,9 @@
"Concatenated output:\n", "Concatenated output:\n",
"\n", "\n",
"\n", "\n",
"Socktastic Colors.\n", "Funky Footwear Company\n",
"\n", "\n",
"\"Put Some Color in Your Step!\"\n" "\"Brighten Up Your Day with Our Colorful Socks!\"\n"
] ]
} }
], ],
@ -554,7 +589,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.10.6" "version": "3.9.16"
}, },
"vscode": { "vscode": {
"interpreter": { "interpreter": {

Loading…
Cancel
Save