3 Setup
Many LLM providers require some initial setup before you can start making API calls. This typically involves:
- Installing necessary software or packages.
- Setting up authentication (e.g., obtaining API keys).
- Configuring environment variables (e.g., storing API keys securely).
- Defining API endpoints and request parameters.
3.1 API Keys and Authentication
Most cloud-based LLM providers require you to sign up for an account and obtain an API key. This key is used to authenticate your requests to the API. Make sure to keep your API keys secure and do not share them publicly.
There are language-specific packages that can help manage API keys and authentication.
3.1.1 Using Environment Variables
A common practice is to store API keys in environment variables. This keeps them out of your codebase and makes it easier to manage different keys for different environments (e.g., development, production). A common approach is to use a .env file in your project directory.
- Never hard-code API keys directly in your code. Always use environment variables or secure storage solutions.
- Always add your
.envfile (or any file containing sensitive information) to your.gitignorefile to prevent it from being committed to version control.
3.1.2 R
3.1.2.1 keyring
In R, you can use the keyring package to securely store and retrieve API keys. Here’s how to set it up:
- Install the
keyringpackage if you haven’t already:
install.packages("keyring")
# or using pak:
pak::pak("keyring")- Store your API key securely:
keyring::key_set("openai_api_key")- Retrieve your API key in your R scripts:
api_key <- keyring::key_get("openai_api_key")3.1.2.2 dotenv
In R, you can use the dotenv package to load environment variables from a .env file. Here’s how to set it up:
- Install the
dotenvpackage if you haven’t already:
install.packages("dotenv")
# or using pak:
pak::pak("dotenv")- Create a
.envfile in your project directory and add your API key:
OPENAI_API_KEY=your_api_key_here
- Load the environment variables in your R scripts:
library(dotenv)
load_dotenv()
api_key <- Sys.getenv("OPENAI_API_KEY")3.1.3 Python
3.1.3.1 keyring
In Python, you can use the keyring package to securely store and retrieve API keys. Here’s how to set it up:
- Install the
keyringpackage if you haven’t already:
uv add keyring- Store your API key securely:
import keyring
keyring.set_password("openai", "api_key", "your_api_key_here")- Retrieve your API key in your Python scripts:
api_key = keyring.get_password("openai", "api_key")3.1.3.2 python-dotenv
In Python, you can use the python-dotenv package to load environment variables from a .env file. Here’s how to set it up:
- Install the
python-dotenvpackage if you haven’t already:
uv add python-dotenv- Create a
.envfile in your project directory and add your API key:
OPENAI_API_KEY=your_api_key_here
- Load the environment variables in your Python scripts:
from dotenv import load_dotenv
import os
load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")3.1.4 TypeScript / JavaScript
3.1.4.1 keytar
In TypeScript or JavaScript, you can use the keytar package to securely store and retrieve API keys. Here’s how to set it up:
- Install the
keytarpackage if you haven’t already:
pnpm add keytar- Store your API key securely:
import keytar from "keytar";
keytar.setPassword("openai", "api_key", "your_api_key_here");- Retrieve your API key in your TypeScript or JavaScript scripts:
import * as keytar from 'keytar';
const apiKey = await keytar.getPassword("openai", "api_key");3.1.4.2 dotenv
In TypeScript or JavaScript, you can use the dotenv package to load environment variables from a .env file. Here’s how to set it up:
- Install the
dotenvpackage if you haven’t already:
pnpm add dotenv- Create a
.envfile in your project directory and add your API key:
OPENAI_API_KEY=your_api_key_here
- Load the environment variables in your TypeScript or JavaScript scripts:
import { config } from "dotenv";
config();
const apiKey = process.env.OPENAI_API_KEY;3.1.5 Julia
In Julia, you can use the DotEnv.jl package to load environment variables from a .env file. Here’s how to set it up:
- Install the
DotEnv.jlpackage if you haven’t already:
import Pkg; Pkg.add("DotEnv")- Create a
.envfile in your project directory and add your API key:
OPENAI_API_KEY=your_api_key_here
- Load the environment variables in your Julia scripts:
using DotEnv
DotEnv.load!()
api_key = ENV["OPENAI_API_KEY"]DotEnv.unload!() will reverse the changes of DotEnv.load!().