fjrdomingues / autopilot

Code Autopilot, a tool that uses GPT to read a codebase, create context and solve tasks.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Move OPENAI_API_KEY into a user home directory ~/.autopilot/.env file

hatkyinc2 opened this issue · comments

Background story explaining the current situation:
Currently, the OPENAI_API_KEY is read from the local .env file inside autopilot code directory.
This is not forward-thinking to user experience.

Desired new situation:
OPENAI_API_KEY should be configured in the users home directory

Implementation details:
OPENAI_API_KEY should be validated to exist if gpt-3.5-turbo or gpt-4 models are used in one of the models env variables.
OPENAI_API_KEY should either come from the user's environment variable or the file in the ~/.autopilot/.env
the ~ should be the home directory across different OSs (windows, max, *nix)
Other env variables should also be loaded from the user's .env file.

  • Update the .env.template accordingly
  • The README should be updated accordingly to explain it to the user.

re: #86
re: #76

🤖 This is my suggestion:

File: modules/gpt.js
To update the code to read the OPENAI_API_KEY from the new location, you can follow these steps:

  1. Import the os and path modules to handle cross-platform file paths.
  2. Create a function to load the environment variables from the new location.
  3. Call the function before using the OPENAI_API_KEY.

Here's the updated code:

const chalk = require('chalk');
const os = require('os');
const path = require('path');
require('dotenv').config()
const { Configuration, OpenAIApi } = require("openai");
const {saveLog} = require('./fsOutput');

let totalTokensUsed = 0
let completionTokens = 0
let promptTokens = 0
let cost = 0

const modelCostMap = {
  "gpt-4": {"promptTokensCost": 0.03, "completionTokensCost": 0.06},
  "gpt-3.5-turbo": {"tokensCost": 0.002},
};

function loadEnvVariables() {
  const envPath = path.join(os.homedir(), '.autopilot', '.env');
  require('dotenv').config({ path: envPath });
}

loadEnvVariables();

const configuration = new Configuration({
  apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);

// ... rest of the code

This code will load the environment variables from the ~/.autopilot/.env file, allowing the OPENAI_API_KEY to be read from the new location.

File: .env.template
Update the .env.template file to include a comment about the new location of the OPENAI_API_KEY and remove the OPENAI_API_KEY line from the template.

.env.template

# The OPENAI_API_KEY should be placed in the ~/.autopilot/.env file in your home directory.
# Path to your code, defaults to itself.
# Even if you are on windows this is POSIX style path.
# For example C:\Users\user\code\project
#    would be C:/Users/user/code/project
CODE_DIR=./

# What files do you want to process?
FILE_EXTENSIONS_TO_PROCESS=.js,.tsx,.ts,.jsx
IGNORE_LIST=node_modules,coverage,public,__tests__

# Currently all the models support either 'gpt-3.5-turbo' or 'gpt-4' (if you have access to it)
CODER_MODEL=gpt-3.5-turbo
CODE_READER_MODEL=gpt-3.5-turbo
GET_FILES_MODEL=gpt-3.5-turbo
INDEXER_MODEL=gpt-3.5-turbo
REVIEWER_MODEL=gpt-3.5-turbo
TASK_COMPLEXITY_MODEL=gpt-3.5-turbo
OPENAI_MAX_TOKEN_REPLY=2000 # Max tokens to return from OpenAI
MODEL_TEMPERATURE=0.01 # range 0-1, 0 being the most conservative, 1 being the most creative
MODEL_PRESENCE_PENALTY=0 # range -2 - 2 Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics.
MODEL_FREQUENCY_PENALTY=0 # range -2 - 2 Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.
MODEL_USER=autopilot # Identify this usage
MAX_TOKEN_COUNT_SINGLE_FILE=1800; # Files above this token size would not be processed
MAX_TOKEN_COUNT_SUMMARIES_CHUNK=1000; # Summaries would be chunked to this max size and looped over

File: README.md

Changes to be made in this file

  1. Add instructions for setting up the OPENAI_API_KEY in the user's home directory.
  2. Explain how to create the ~/.autopilot/.env file and add the OPENAI_API_KEY to it.
  3. Update the .env setup instructions to reflect the new location of the OPENAI_API_KEY.

Updated README.md

Note: This project is currently operating autonomously on a server. Whenever an issue is created, it will automatically generate a comment reply suggesting how to fix the issue. Please ensure that any issues created are clear and comprehensible for the autopilot system to process effectively. 🙇‍♂️

<h1 align="center">Autopilot - An AI developer</h1>

<p align="center">
  <strong>Autopilot</strong> is an AI tool that utilizes GPT to read a codebase, create context, and solve tasks that you request.
</p>

<p align="center">
  <img src="public/demo.gif" alt="Autopilot Demo" width="800"/>
</p>

# How it works 

1. You point AutoPilot at a codebase with a task.
1. AutoPilot generates and upkeeps a DB with metadata on the codebase files. (within the codebase directory)
1. AutoPilot decides which existing files it needs for the task by using the metadata DB.
1. AutoPilot tries to implement the requested task on each relevant file.

## Features

- 📚 - Pre-processes codebase files.
- 🤖 - Implements code changes for you.
- 🚀 - Parallel calls to agents where possible.
- 📝 - Shows you what was updated. (Full process log with each AI interaction also produced)
- 🕹️ - Interactive mode - see the process with retry, continue, abort options.

### Tasks expectations
- Referencing current code:
  - ✅ Referencing a specific file by project relative path.
  - ✅ Referencing a specific file by file name only, ignoring the subdirectories path.
  - ✅ Referencing a specific function within a file without the filename.
  - ✅ Referencing a major business concept that is exclusively used in one file.
  - ✅ Referencing all project files.
  - 🤔 General logical requests. Your milage would differ by model, codebase and task. Some work. (Should introduce task scoring)
- Changes executed:
  - ✅Create a new file based on an existing file.
  - ❌Start a new file from scratch.
  - ✅Update an existing file.
  - ✅Update multiple existing files.
  - ❌Delete existing files. (It might empty them out, but not delete them currently)
  - ❌Start using new 3rd party libraries. (Needs arbitrary code execution to install the library)
  - ❌Cascade updating related files like tests. (Coming soon)
  - ❌Test the code it wrote and self fix.

## 🛠️ Installation

1. Clone the repository: `git clone https://github.com/fjrdomingues/autopilot.git`
2. Do `cd autopilot` to install dependencies: `npm ci`
3. Set up the environment variables:
   1. Create a directory in your home folder named `.autopilot`: `mkdir ~/.autopilot`
   2. Create a `.env` file inside the `.autopilot` directory: `touch ~/.autopilot/.env`
   3. Open the `.env` file and add the following variables:
      - `OPENAI_API_KEY=<your-api-key>` [Create openAI API key](https://platform.openai.com/account/api-keys)
      - `CODE_DIR=<path-to-your-code>` (or use `-d path-to-your-code` later)
      - `IGNORE_LIST=node_modules,coverage,public,__tests__`
      - `FILE_EXTENSIONS_TO_PROCESS=.js,.tsx,.ts,.jsx`
   
## Running
* `node ui -t "YOUR_TASK"` - is the easiest way to start.
  * Solutions will be auto applied on your code and a git diff shown if possible. 
  * Alternatively you may specify `--auto-apply=false`.
* `node ui -h` - will show you all the options.

## Interactive mode
Use `node ui -i` for an interactive mode, here you can review the output of each step before proceeding.

## 🤝 Contributing

**We are running autopilot on a server connected to the https://github.com/fjrdomingues/autopilot repository. New issues created will trigger autopilot and create a new Pull Request with a proposal. Running with gpt-4**

We welcome contributions! Please submit pull requests to the repository, and ensure your changes align with the project's goals and guidelines. Together, we can make **Autopilot** an even more powerful and efficient tool for developers!

### Running tests - all
`npm run test` - runs all the tests

### Running tests - Unit test
`npm run unit-test` - runs the unit tests

### Running tests - Benchmarks
`npm run e2e-test` - runs the end to end tests

### Code structure
- agents - interactions with language models.
- modules - most other internal libs.
- ui.js - The main().
- logs - document a task run.