langchain/docs/modules/models/llms/integrations/openai.ipynb

159 lines
3.3 KiB
Plaintext
Raw Normal View History

2023-02-18 22:25:37 +00:00
{
"cells": [
{
"cell_type": "markdown",
"id": "9597802c",
"metadata": {},
"source": [
"# OpenAI\n",
"\n",
"[OpenAI](https://platform.openai.com/docs/introduction) offers a spectrum of models with different levels of power suitable for different tasks.\n",
"\n",
"This example goes over how to use LangChain to interact with `OpenAI` [models](https://platform.openai.com/docs/models)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "5d71df86-8a17-4283-83d7-4e46e7c06c44",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
" ········\n"
]
}
],
"source": [
"# get a token: https://platform.openai.com/account/api-keys\n",
"\n",
"from getpass import getpass\n",
"\n",
"OPENAI_API_KEY = getpass()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "5472a7cd-af26-48ca-ae9b-5f6ae73c74d2",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import os\n",
"\n",
"os.environ[\"OPENAI_API_KEY\"] = OPENAI_API_KEY"
2023-02-18 22:25:37 +00:00
]
},
{
"cell_type": "code",
"execution_count": 6,
2023-02-18 22:25:37 +00:00
"id": "6fb585dd",
"metadata": {
"tags": []
},
2023-02-18 22:25:37 +00:00
"outputs": [],
"source": [
"from langchain.llms import OpenAI\n",
"from langchain import PromptTemplate, LLMChain"
]
},
{
"cell_type": "code",
"execution_count": 7,
2023-02-18 22:25:37 +00:00
"id": "035dea0f",
"metadata": {
"tags": []
},
2023-02-18 22:25:37 +00:00
"outputs": [],
"source": [
"template = \"\"\"Question: {question}\n",
"\n",
"Answer: Let's think step by step.\"\"\"\n",
"\n",
"prompt = PromptTemplate(template=template, input_variables=[\"question\"])"
]
},
{
"cell_type": "code",
"execution_count": 8,
2023-02-18 22:25:37 +00:00
"id": "3f3458d9",
"metadata": {
"tags": []
},
2023-02-18 22:25:37 +00:00
"outputs": [],
"source": [
"llm = OpenAI()"
]
},
{
"cell_type": "code",
"execution_count": 9,
2023-02-18 22:25:37 +00:00
"id": "a641dbd9",
"metadata": {
"tags": []
},
2023-02-18 22:25:37 +00:00
"outputs": [],
"source": [
"llm_chain = LLMChain(prompt=prompt, llm=llm)"
]
},
{
"cell_type": "code",
"execution_count": 10,
2023-02-18 22:25:37 +00:00
"id": "9f844993",
"metadata": {
"tags": []
},
2023-02-18 22:25:37 +00:00
"outputs": [
{
"data": {
"text/plain": [
"' Justin Bieber was born in 1994, so we are looking for the Super Bowl winner from that year. The Super Bowl in 1994 was Super Bowl XXVIII, and the winner was the Dallas Cowboys.'"
2023-02-18 22:25:37 +00:00
]
},
"execution_count": 10,
2023-02-18 22:25:37 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"question = \"What NFL team won the Super Bowl in the year Justin Beiber was born?\"\n",
"\n",
"llm_chain.run(question)"
]
}
],
"metadata": {
"kernelspec": {
2023-03-01 19:47:01 +00:00
"display_name": "Python 3 (ipykernel)",
2023-02-18 22:25:37 +00:00
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
},
"vscode": {
"interpreter": {
"hash": "a0a0263b650d907a3bfe41c0f8d6a63a071b884df3cfdc1579f00cdc1aed6b03"
}
2023-02-18 22:25:37 +00:00
}
},
"nbformat": 4,
"nbformat_minor": 5
}