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.
Learn more
Technically speaking, the authentication is a three-step process:
Obtain a Dartmouth API key
Using the Dartmouth API key, request a JSON Web Token (JWT) from a specific API endpoint
Use the JWT when making requests
The JWT expires after some time, which then requires re-authentication using the API key.
The langchain_dartmouth
components only require the Dartmouth API key from the user and handle the other steps “under-the-hood”.
You need two different API keys, depending on which models you want to use.
To use the open-source models deployed locally on Dartmouth’s compute infrastructure, you need a Dartmouth API key that you can acquire from the Dartmouth API Developer Portal.
To use the third-party commercial models provided by Dartmouth, you need a Dartmouth Chat API key. Follow these instructions to obtain one.
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
llm = DartmouthLLM(dartmouth_api_key="you_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
[notice] A new release of pip is available: 25.1 -> 25.1.1
[notice] To update, run: pip install --upgrade pip
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 key from the Dartmouth Developer Portal and 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.