mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-18 15:49:15 +03:00
3.7 KiB
3.7 KiB
In [1]:
# Uncomment to install openlm and openai if you haven't already
# !pip install openlm
# !pip install openaiIn [2]:
from getpass import getpass
import os
import subprocess
# Check if OPENAI_API_KEY environment variable is set
if "OPENAI_API_KEY" not in os.environ:
print("Enter your OpenAI API key:")
os.environ["OPENAI_API_KEY"] = getpass()
# Check if HF_API_TOKEN environment variable is set
if "HF_API_TOKEN" not in os.environ:
print("Enter your HuggingFace Hub API key:")
os.environ["HF_API_TOKEN"] = getpass()In [4]:
from langchain.llms import OpenLM
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChainIn [5]:
question = "What is the capital of France?"
template = """Question: {question}
Answer: Let's think step by step."""
prompt = PromptTemplate(template=template, input_variables=["question"])
for model in ["text-davinci-003", "huggingface.co/gpt2"]:
llm = OpenLM(model=model)
llm_chain = LLMChain(prompt=prompt, llm=llm)
result = llm_chain.run(question)
print(
"""Model: {}
Result: {}""".format(
model, result
)
)Model: text-davinci-003 Result: France is a country in Europe. The capital of France is Paris. Model: huggingface.co/gpt2 Result: Question: What is the capital of France? Answer: Let's think step by step. I am not going to lie, this is a complicated issue, and I don't see any solutions to all this, but it is still far more