Coder Social home page Coder Social logo

joshembling / laragenie Goto Github PK

View Code? Open in Web Editor NEW
133.0 4.0 6.0 2.49 MB

🤖 An AI bot made for the command line that can read and understand any codebase from your Laravel app.

License: MIT License

PHP 100.00%
ai laravel openai openai-api php pinecone pineconedb laragenie

laragenie's Introduction

Laragenie - AI built to understand your codebases

Latest Version on Packagist Total Downloads

Laragenie is an AI chatbot that runs on the command line from your Laravel app. It will be able to read and understand any of your codebases following a few simple steps:

  1. Set up your env variables OpenAI and Pinecone
  2. Publish and update the Laragenie config
  3. Index your files and/or full directories
  4. Ask your questions

It's as simple as that! Accelerate your workflow instantly and collaborate seamlessly with the quickest and most knowledgeable 'colleague' you've ever had.

This is a particularly useful CLI bot that can be used to:

  • Onboard developer's to new projects.
  • Assist both junior and senior developers in understanding a codebase, offering a cost-effective alternative to multiple one-on-one sessions with other developers.
  • Provide convenient and readily available support on a daily basis as needed.

You are not limited to indexing files based in your Laravel project. You can use this for monorepo's, or indeed any repo in any language. You can of course use this tool to index files that are not code-related also.

All you need to do is run this CLI tool from the Laravel directory. Simple, right?! 🎉

Demo

Note

If you are upgrading from a Laragenie version ^1.0.63 > 1.1, there is a change to Pinecone environment variables. Please see OpenAI and Pinecone.

Contents

Minimum Requirements

For specific versions that match your PHP, Laravel and Laragenie versions please see the table below:

PHP Laravel version Laragenie version
^8.1 ^10.0 >=1.0 <1.2
^8.2 ^10.0, ^11.0 ^1.2.0

This package uses Laravel Prompts which supports macOS, Linux, and Windows with WSL. Due to limitations in the Windows version of PHP, it is not currently possible to use Laravel Prompts on Windows outside of WSL.

For this reason, Laravel Prompts supports falling back to an alternative implementation such as the Symfony Console Question Helper.

Installation

You can install the package via composer:

composer require joshembling/laragenie

You can publish and run the migrations with:

php artisan vendor:publish --tag="laragenie-migrations"
php artisan migrate

If you don't want to publish migrations, you must toggle the database credentials in your Laragenie config to false. (See config file details below).

You can publish the config file with:

php artisan vendor:publish --tag="laragenie-config"

This is the contents of the published config file:

return [
    'bot' => [
        'name' => 'Laragenie', // The name of your chatbot
        'welcome' => 'Hello, I am Laragenie, how may I assist you today?', // Your welcome message
        'instructions' => 'Write in markdown format. Try to only use factual data that can be pulled from indexed chunks.', // The chatbot instructions
    ],

    'chunks' => [
        'size' => 1000, // Maximum number of characters to separate chunks
    ],

    'database' => [
        'fetch' => true, // Fetch saved answers from previous questions
        'save' => true, // Save answers to the database
    ],

    'extensions' => [ // The file types you want to index
        'php',
        'blade.php',
        'js',
    ],

    'indexes' => [
        'directories' => [], // The directores you want to index e.g. ['App/Models', 'App/Http/Controllers', '../frontend/src']
        'files' => [], // The files you want to index e.g. ['tests/Feature/MyTest.php']
        'removal' => [
            'strict' => true, // User prompt on deletion requests of indexes
        ],
    ],

    'openai' => [
        'embedding' => [
            'model' => 'text-embedding-3-small', // Text embedding model 
            'max_tokens' => 5, // Maximum tokens to use when embedding
        ],
        'chat' => [
            'model' => 'gpt-4-turbo-preview', // Your OpenAI GPT model
            'temperature' => 0.1, // Set temperature between 0 and 1 (lower values will have less irrelevance)
        ],
    ],

    'pinecone' => [
        'topK' => 2, // Pinecone indexes to fetch
    ],
];

Usage

OpenAI and Pinecone

OpenAI

This package uses OpenAI to process and generate responses and Pinecone to index your data.

You will need to create an OpenAI account with credits, generate an API key and add it to your .env file:

OPENAI_API_KEY=your-open-ai-key

Pinecone

Important

If you are using a Laragenie version prior to 1.1 and do not want to upgrade, go straight to Legacy Pinecone.

You will need to create a Pinecone account. There are two diferent types of account you can set up:

  1. Serverless
  2. Pod-based index

As of early 2024, Pinecone recommend you start with a serverless account. You can optionally set up an account with a payment method attached to get $100 in free credits, however, a free account allows up to 100,000 indexes - likely more than enough for any small-medium sized application.

Create an index with 1536 dimensions and the metric as 'cosine'. Then generate an api key and add these details to your .env file:

PINECONE_API_KEY=an-example-pinecone-api-key
PINECONE_INDEX_HOST='https://an-example-url.aaa.gcp-starter.pinecone.io'

Your host can be seen in the information box on your index page, alongside the metric, dimensions, pod type, cloud, region and environment.


Tip

If you are upgrading to Laragenie ^1.1, you can safely remove the legacy environment variables: PINECONE_ENVIRONMENT and PINECONE_INDEX.

Legacy Pinecone

Important: If you are using Laragenie 1.0.63 or prior, you must use a regular Pinecone account and NOT a serverless account. When you are hinted to select an option on account creation, ensure you select 'Continue with pod-based index'.

Create an environment with 1536 dimensions and name it, generate an api key and add these details to your .env file:

PINECONE_API_KEY=your-pinecone-api-key
PINECONE_ENVIRONMENT=gcp-starter
PINECONE_INDEX=your-index

Running Laragenie on the command line

Once these are setup you will be able to run the following command from your root directory:

php artisan laragenie

You will get 4 options:

  1. Ask a question
  2. Index files
  3. Remove indexed files
  4. Something else

Use the arrow keys to toggle through the options and enter to select the command.

Ask a question

Note: you can only run this action once you have files indexed in your Pinecone vector database (skip to the ‘Index Files’ section if you wish to find out how to start indexing).

When your vector database has indexes you’ll be able to ask any questions relating to your codebase.

Answers can be generated in markdown format with code examples, or any format of your choosing. Use the bot.instructions config to write AI instructions as detailed as you need to.

Beneath each response you will see the generated cost (in US dollars), which will help keep close track of the expense. Cost of the response is added to your database, if migrations are enabled.

Costs can vary, but small responses will be less than $0.01. Much larger responses can be between $0.02–0.05.

Force AI

As previously mentioned, when you have migrations enabled your questions will save to your database.

However, you may want to force AI usage (prevent fetching from the database) if you are unsatisfied with the initial answer. This will overwrite the answer already saved to the database.

To force an AI response, you will need to end all questions with an --ai flag e.g.

Tell me how users are saved to the database --ai.

This will ensure the AI model will re-assess your request, and outputs another answer (this could be the same answer depending on the GPT model you are using).

Index files

The quickest way to index files is to pass in singular values to the directories or files array in the Laragenie config. When you run the 'Index Files' command you will always have the option to reindex these files. This will help in keeping your Laragenie bot up to date.

Select 'yes', when prompted with Do you want to index your directories and files saved in your config?

'indexes' => [
    'directories' => ['App/Models', 'App/Http/Controllers'],
    'files' => ['tests/Feature/MyTest.php'],
    'removal' => [
        'strict' => true,
    ],
],

If you select 'no', you can also index files in the following ways:

  • Inputting a file name with it's namespace e.g. App/Models/User.php
  • Inputting a full directory, e.g. App
    • If you pass in a directory, Laragenie can only index files within this directory, and not its subdirectories.
    • To index subdirectories you must explicitly pass the path e.g. App/Models to index all of your models
  • Inputting multiple files or directories in a comma separated list e.g. App/Models, tests/Feature, App/Http/Controllers/Controller.php
  • Inputting multiple directories with wildcards e.g. App/Models/*.php
    • Please note that the wildcards must still match the file extensions in your laragenie config file.

Indexing files outside of your Laravel project

You may use Laragenie in any way that you wish; you are not limited to just indexing Laravel based files.

For example, your Laravel project may live in a monorepo with two root entries such as frontend and backend. In this instance, you could move up one level to index more directories and files e.g. ../frontend/src/ or ../frontend/components/Component.js.

You can add these to your directories and files in the Laragenie config:

'indexes' => [
    'directories' => ['App/Models', 'App/Http/Controllers', '../frontend/src/'],
    'files' => ['tests/Feature/MyTest.php', '../frontend/components/Component.js'],
    'removal' => [
        'strict' => true,
    ],
],

Using this same method, you could technically index any files or directories you have access to on your server or local machine.

Ensure your extensions in your Laragenie config match all the file types that you want to index.

'extensions' => [
    'php', 'blade.php', 'js', 'jsx', 'ts', 'tsx', // etc...
],

Note: if your directories, paths or file names change, Laragenie will not be able to find the index if you decide to update/remove it later on (unless you truncate your entire vector database, or go into Pinecone and delete them manually).

Removing indexed files

You can remove indexed files using the same methods listed above, except from using your directories or files array in the Laragenie config - this is currently for indexing purposes only.

If you want to remove all files you may do so by selecting Remove all chunked data. Be warned that this will truncate your entire vector database and cannot be reversed.

To remove a comma separated list of files/directories, select the Remove data associated with a directory or specific file prompt as an option.

Strict removal, i.e. warning messages before files are removed, can be turned on/off by changing the 'strict' attribute to false in your config.

'indexes' => [
    'removal' => [
        'strict' => true,
    ],
],

Stopping Laragenie

You can stop Laragenie using the following methods:

  • ctrl + c (Linux/Mac)
  • Selecting No thanks, goodbye in the user menu after at least 1 prompt has run.

Have fun using Laragenie! 🤖

Debugging

API Keys

  • If you have correctly added the required .env variables, but get an error such as "You didn't provide an API key", you may need to clear your cache and config:
php artisan config:clear
php artisan cache:clear
  • Likewise, if you get a 404 response and a Saloon exception when trying any of the four options, it's likely you do not have a non-serverless Pinecone database set up and are using a Laragenie version prior to 1.1. Please see OpenAI and Pinecone.

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

laragenie's People

Contributors

dependabot[bot] avatar github-actions[bot] avatar joshembling avatar zbora23 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

laragenie's Issues

[Bug]: Instructions are not being passed correctly

What happened?

In Chatbot.php, the message content references laragenie.bot.instruction, but the config file specifies laragenie.bot.instructions (with an 's'). No instructions are therefore passed.

How to reproduce the bug

See source code

Package Version

1.0.62

PHP Version

8.3

Laravel Version

10

Which operating systems does with happen with?

No response

Notes

No response

[Bug]: Errors when i try any of the 4 options.

What happened?

none of the proms work. I get this same error on any of the 4 options.

image

How to reproduce the bug

Choose any of these options.

image

Package Version

2.0.0

PHP Version

8.2

Laravel Version

10.0.0

Which operating systems does with happen with?

Windows

Notes

No response

[Idea]: Code generator from our codebase

What happened?

First, sorry I write this on bug, i choose template/type but all 404 except Bug Report

How to reproduce the bug

Soo I not write about reproduce bug here

Package Version

PHP Version

0

Laravel Version

0

Which operating systems does with happen with?

Linux

Notes

Hi @joshembling

I like the idea about this package because this is will index my codebase, but currently just have limitation to just ask some question

Using indexing with AI will have benefit to learn our codebase, I think is possible to use this index to automatically generate file to us using our codebase, for example I have pattern Controller > Repository > Service, and export script, import script, testing, routing, blade, for all standar basic CRUD codebase

I think we will have a lot of time to create this pattern, but the pattern just change the name right ? (and other standard repetition)
Just change the file name and some field, this repetition I think can do with AI to generate codebase with AI, this will generate the file, All naming convention, Camel, Snake, Kebab, Pascal Case, etc will convert/generate automatically by AI

To generate just codebase will really help full, I think is enough
But will really cool if AI, can give some input for example field list, so we just give name and field list, and AI will generate for us
Or just we just create the schema migration, and AI will scan our database to automatically generate field and all relation script
I know this will be some error, but this is must fix with developer itself, the developer just focus to business process, and all not standard feature for specific purpose

I don't know this still related with this package goal or maybe on another package, but if this can do, will realy cool

*Just my wild imagination hehehe 🤣

[Bug]: Only 1 chunk is sent alongside each question?

What happened?

This is my first time playing with embeddings and I explored the package a bit to understand how it works. Maybe I'm misunderstanding something (apologies in advance if so!), but I'm a bit confused by how the process works...

In Questions.php, a question is asked in two stages:

  $questionResponse = $this->askBot($formattedQuestion);
  $botResponse = $this->botResponse($questionResponse['data'], $question);

The askBot() method queries Pinecone and returns topK results (3 by default). It then returns an array of data and vectors:

  return [
      'data' => $pinecone_res->json()['matches'][0]['metadata']['text'],
      'vectors' => $openai_res->embeddings[0]->toArray()['embedding'],
  ];

This is passed in to the botResponse() method to use a chat completion for answering.

But if I'm reading this correctly, ...['matches'][0]... would only return a single match from Pinecone? Am I missing something? 🤔

How to reproduce the bug

See details above.

Package Version

1.0.62

PHP Version

8.3

Laravel Version

10

Which operating systems does with happen with?

No response

Notes

No response

[Bug]: Cannot access OPENAI_API_KEY in the .env file

What happened?

The API key and Organization variables are already defined in the .env file. There is no problem here because I can use OpenAI PHP SDK and Laravel package properly. It also works on the Tinker package for PhpStorm. However, something needs to be fixed when I try to use Laragenie. It throws an error like the one below:

image

What might be the problem?

Note: Laragenie is a good package idea that I wanted to develop. You were the first. Congratulations!

How to reproduce the bug

I have tried to determine whether the SDK sees the env variable, and I consider that OpenAI SDK doesn't see it. I don't know what is the problem.

Package Version

1.0.6

PHP Version

8.2.13

Laravel Version

10.38.1

Which operating systems does with happen with?

Linux

Notes

No response

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.