0xPlaygrounds / rig

⚙️🦀 Build portable, modular & lightweight Fullstack Agents
https://rig.rs
MIT License
153 stars 9 forks source link

environment variables? #1

Closed nickworks closed 2 months ago

nickworks commented 2 months ago

The README, examples, and rig.rs indicate I should set the OPENAI_API_KEY environment variable.

Where and how is this done??

Thanks!

Tachikoma000 commented 2 months ago

The README, examples, and rig.rs indicate I should set the OPENAI_API_KEY environment variable.

Where and how is this done??

Thanks!

Hi! Thank you for your question. Setting your API Key for LLM providers like OpenAI or Cohere is pretty easy to do; please see the following guides depending on your preference:

1. Obtaining API Keys

Before setting environment variables, make sure you have the necessary API keys:

2. Setting Environment Variables

For Unix-based systems (Linux, macOS)

Temporary (current terminal session only):

export OPENAI_API_KEY='your-api-key-here'
export COHERE_API_KEY='your-api-key-here'

Permanent:

  1. Edit your shell configuration file (~/.bashrc, ~/.zshrc, etc.):
    nano ~/.bashrc
  2. Add the following lines at the end of the file:
    export OPENAI_API_KEY='your-api-key-here'
    export COHERE_API_KEY='your-api-key-here'
  3. Save the file and reload it:
    source ~/.bashrc

For Windows

Temporary (current Command Prompt session only):

set OPENAI_API_KEY=your-api-key-here
set COHERE_API_KEY=your-api-key-here

Permanent:

  1. Open the Start menu and search for "Environment Variables"
  2. Click "Edit the system environment variables"
  3. Click the "Environment Variables" button
  4. Under "User variables", click "New"
  5. Add variables:
    • Variable name: OPENAI_API_KEY, Value: your OpenAI API key
    • Variable name: COHERE_API_KEY, Value: your Cohere API key
  6. Click "OK" to save

3. Verifying Environment Variables

To check if your environment variables are set correctly:

4. Using API Keys in Rig

With environment variables set, Rig can automatically access these API keys:

use rig::providers::{openai, cohere};

// OpenAI client
let openai_client = openai::Client::from_env();

// Cohere client
let cohere_client = cohere::Client::from_env();

5. Development Environments

VS Code

For VS Code users, you can set environment variables in your launch.json:

  1. Open the "Run and Debug" view
  2. Click "create a launch.json file"
  3. Add environment variables to your configuration:
{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug executable 'your-project-name'",
            "cargo": {
                "args": ["build", "--bin=your-project-name", "--package=your-project-name"],
                "filter": {
                    "name": "your-project-name",
                    "kind": "bin"
                }
            },
            "args": [],
            "cwd": "${workspaceFolder}",
            "env": {
                "OPENAI_API_KEY": "your-api-key-here",
                "COHERE_API_KEY": "your-api-key-here"
            }
        }
    ]
}

Rust-Analyzer

If you're using rust-analyzer, you can set environment variables in .cargo/config.toml:

[env]
OPENAI_API_KEY = "your-api-key-here"
COHERE_API_KEY = "your-api-key-here"

Remember to add this file to your .gitignore to avoid accidentally sharing your API keys.

nickworks commented 2 months ago

Thank you! I've added the .cargo/config.toml with my api key and it works great.

cvauclair commented 2 months ago

@nickworks if you intent to make your project into a github repo, remember to not push your API key to the repo!