Files
langchain/docs/modules/models/llms/integrations/openai.ipynb
T
leo-gan 5420a0e404 updated langchain/docs/modules/models/llms/integrations/ notebooks (#3041)
- Updated `langchain/docs/modules/models/llms/integrations/` notebooks:
added links to the original sites, the install information, etc.
- Added the `nlpcloud` notebook.
- Removed "Example" from Titles of some notebooks, so all notebook
titles are consistent.
2023-04-17 20:25:32 -07:00

3.3 KiB

OpenAI

OpenAI offers a spectrum of models with different levels of power suitable for different tasks.

This example goes over how to use LangChain to interact with OpenAI models

In [4]:
# get a token: https://platform.openai.com/account/api-keys

from getpass import getpass

OPENAI_API_KEY = getpass()
 ········
In [5]:
import os

os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
In [6]:
from langchain.llms import OpenAI
from langchain import PromptTemplate, LLMChain
In [7]:
template = """Question: {question}

Answer: Let's think step by step."""

prompt = PromptTemplate(template=template, input_variables=["question"])
In [8]:
llm = OpenAI()
In [9]:
llm_chain = LLMChain(prompt=prompt, llm=llm)
In [10]:
question = "What NFL team won the Super Bowl in the year Justin Beiber was born?"

llm_chain.run(question)
Out [10]:
' 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.'