Authentication#
All models offered by Dartmouth require authentication. The components in langchain_dartmouth hide the underlying process to make things very simple from the user’s perspective.
You need two different API keys, depending on how you want to use the models provided by Dartmouth.
For most use cases, you will need a Dartmouth Chat API key. Follow these instructions to obtain one.
For some classes, you need a Dartmouth API key that you can acquire from the Dartmouth API Developer Portal.
You can pass an API key to a component using the dartmouth_api_key or dartmouth_chat_api_key parameter, respectively, on object instantiation:
from langchain_dartmouth.llms import DartmouthLLM, ChatDartmouth
llm = DartmouthLLM(dartmouth_api_key="you_api_key_here")
chat_model = ChatDartmouth(dartmouth_chat_api_key="your_api_key_here")
We usually want to avoid putting API keys in our code directly, though. API keys are uniquely associated with your digital identity and thus sensitive information, just like a password.
It is therefore good practice to instead define an environment variable that holds the key. There are many ways to do this, but a very convenient one is to leverage the python-dotenv package and read the key from a text file.
In order to do that, we need to create a text file called .env in the project folder. The file should contain just one or both of the following lines:
DARTMOUTH_API_KEY="your_api_key"
DARTMOUTH_CHAT_API_KEY="your_chat_api_key"
We also need to install python-dotenv:
%pip install --quiet python-dotenv
Note: you may need to restart the kernel to use updated packages.
Now we can find and load the .env file:
from dotenv import find_dotenv, load_dotenv
load_dotenv(find_dotenv())
True
find_dotenv looks for a file called .env in the current working directory, and then iteratively checks the parent, grandparent, and so on until it finds the file and returns the path. load_dotenv loads the file from that path, and only returns True (if the .env file was found) or False (if it was not found). However, it also loads all variables defined in .env into the environment!
Note
If you are using git, make sure you include .env in your .gitignore to avoid leaking the key to your repository!
You could now access these variables using Python’s built-in os module:
import os
dartmouth_api_key = os.environ["DARTMOUTH_API_KEY"]
dartmouth_chat_api_key = os.environ["DARTMOUTH_CHAT_API_KEY"]
By default, all components in langchain_dartmouth look for an environment variable called DARTMOUTH_API_KEY or DARTMOUTH_CHAT_API_KEY for authentication. So as long as you load a .env file with that variable pointing to your key, you won’t need to do anything else!
Summary#
You need to provide a valid Dartmouth API key or Dartmouth Chat API key in order to use the components in langchain_dartmouth.
You can obtain a Dartmouth API key from the Dartmouth Developer Portal and a Dartmouth Chat API key by following these instructions.
Store the keys as a variable called DARTMOUTH_API_KEY or DARTMOUTH_CHAT_API_KEY in a .env file, and load it using python-dotenv’s load_dotenv() at the beginning of your notebook or script.