Skip to content

ariden83/openai-go-assistant

Repository files navigation

GitHub release
GitHub issues
GitHub license

openai-go-assistant

OpenAI Go Assistant is a Go project that allows you to interact with the OpenAI API to automate various code generation and optimization operations. Designed for developers, this wizard makes creating Go code easy by offering advanced features, such as generating code from natural instructions, optimizing existing code, fixing errors, and adding unit tests.

openai-go-assistant

Table of Contents

Features

  • Code generation: Create Go code from simple natural language instructions by leveraging the power of the OpenAI API. The wizard generates initial code for functions, structures, algorithms, and more.

  • Error Correction: Analyze code to automatically detect and correct syntax, logic, or optimization errors, making the debugging process faster and more efficient.

  • Code Optimization: Rewrite and optimize existing code to improve performance, reduce complexity, or adhere to Go programming best practices.

  • Unit test generation: Generate Go unit tests associated with code to ensure feature coverage and automatically validate expected behavior.

Requirements

Install goimport tools to repair missing imports

go install golang.org/x/tools/cmd/goimports@latest

Install staticCheck to detect unused functions in generated code.

go install honnef.co/go/tools/cmd/staticcheck@latest

Installation

1. Clone the repository

Start by cloning this repository and navigating to the project folder:

git clone https://github.com/ariden83/openai-go-assistant.git
cd openai-go-assistant

2. Configure your OpenAI API keys:

Add your OpenAI API keys and configure settings in an .goia file located in the project root or in your home directory.:

language: "fr"
openai_model: "gpt-4-turbo"
openai_api_key: "your-api-key"
openai_url: "https://api.openai.com/v1/chat/completions"
openai_temperature: 0.6
openai_max_tokens: 256
max_attempts: 3

3. Install the dependencies

a) Necessary tools

Goimports: Automatically repairs missing imports in code.

go install golang.org/x/tools/cmd/goimports@latest

Staticcheck: Detects unused functions and other problems in the code.

go install honnef.co/go/tools/cmd/staticcheck@latest

4. Use

You have two options to run the project: install the utility or launch the project directly from the command line.

Option 1: Install the utility

Build and install the binary using the following command:

make install

Once installed, you will be able to run the wizard via specific commands to take advantage of its different features.

Option 2: Launch the project directly

If you prefer to run the project without installing the utility, simply run:

go run ./...

Usage

Usage: goia [flags] [path ...]
  -d	display diffs instead of rewriting files
  -l	list files whose formatting differs from goimport's
  -local string
    	put imports beginning with this string after 3rd-party package
  -prefix value
    	relative local prefix to from a new import group (can be given several times)
  -w	write result to (source) file instead of stdout

Example :

goia -l -w ./test/.

Disclaimer

Use of OpenAI Go Assistant is at your own risk..

  • I cannot be held responsible for any errors generated by the utility, whether errors in the code created, incorrect modifications made to your existing code, or possible loss of data.
  • It is strongly recommended to always verify generated or modified code before using it in a production environment.
  • Also remember to save your code before using this tool to avoid any loss.

Thank you for your understanding, and enjoy exploring the tool ! πŸš€

Good usage practices

Here are the best practices to follow for optimal use of OpenAI Go Assistant :

  • Limit the size of submitted files.
  • Structure natural instructions for better results.
  • Use test or development files rather than production files.
  • Back up files before submitting them to the tool.
  • Check the modifications made by the tool before validating them.
  • Use clean, well-formatted code files for best results.
  • Avoid submitting code files containing syntactic or logical errors.
  • Use clear and precise natural instructions to obtain optimal results.
  • Always check the generated code before using it!

Known limitations

Here are the known limitations of OpenAI Go Assistant:

  • Natural instructions must be clear and precise to achieve optimal results.
  • Large code files may result in longer processing times.
  • Complex code files may generate inaccurate results.
  • Code files containing syntactic or logical errors may not be processed correctly.
  • The tool may not be able to correct all types of errors or optimize all types of code.
  • The tool may not generate unit tests for all types of code or functions.
  • The tool may not be able to optimize all types of code or functions.
  • The tool may not be able to generate code for all types of natural instructions.
  • The tool may not be able to detect all types of errors in code files.
  • The tool may not be able to optimize all types of code files.
  • The tool may not be able to generate unit tests for all types of code files.
  • The tool may not be able to analyze all types of code files.
  • The tool may not be able to generate code for all types of code files.

Compatibility

OpenAI Go Assistant is compatible with the following operating systems: -Linux

OpenAI usage cost

You can then track API usage costs in real time on the OpenAI platform OpenAI usage cost.

OpenAI usage cost

Tips for optimizing costs for using the OpenAI API

Using the OpenAI API may incur varying costs depending on settings and frequency of calls. Here are some tips for optimizing your spending:

1. Adjust parameters temperature and max_tokens

  • temperature: A lower value (like 0.2) produces more deterministic responses, while a higher value (like 0.8) favors creative responses. For technical or specific tasks, choose a low value to reduce unnecessary iterations.
  • max_tokens: This parameter limits the number of tokens generated per response. Set a value appropriate to your needs to avoid excessively long responses (for example, between 100 and 300 for simple Go functions).

Example of optimized configuration:

openai_temperature: 0.3
openai_max_tokens: 150

2. Limit unnecessary API calls

  • Before calling the API, perform local tests on your files or instructions.
  • Use tools like staticcheck or goimports to fix simple errors without relying on the API.
  • Consolidate your requirements into a single API call when possible, instead of making multiple small calls.

3. Track your consumption on the OpenAI platform

  • Visit the OpenAI usage cost page regularly to monitor your spending and adjust your settings accordingly.
  • Set up alerts or notifications to receive updates on your consumption and avoid exceeding your budget.
  • Analyze your usage patterns to identify areas where you can optimize your spending.
  • Use the OpenAI playground to test models and refine your queries before making API calls.
  • Monitor the performance of your queries and adjust your settings to improve response times and reduce costs.
  • Review the results of your queries to identify areas where you can optimize your code or instructions.
  • Use the OpenAI API documentation to learn about available models and parameters and make informed decisions about your queries.
  • Share best practices with your team to ensure that everyone is using the API efficiently and cost-effectively.
  • Consider using the OpenAI API for specific tasks that require advanced natural language processing capabilities, rather than general-purpose tasks that can be handled by other tools.
  • Use the OpenAI API for tasks that require real-time responses or interactive conversations, rather than batch processing or offline analysis.
  • Optimize your queries to reduce the number of tokens generated per response and avoid unnecessary iterations.
  • Use the OpenAI API for tasks that require advanced natural language processing capabilities, such as generating code from natural language instructions, rather than simple text generation tasks.
  • Monitor your usage and adjust your settings to optimize your spending and avoid exceeding your budget.

4. Optimize instructions sent to the API

  • Provide clear and concise instructions to get accurate answers on the first call. For example, instead of saying "Fix this Go file", specify "Fix syntax errors in this function and optimize imports".

By following these best practices, you will be able to maximize the effectiveness of OpenAI Go Assistant while controlling your costs. πŸš€

Resources

Contribution

Contributions are welcome! If you want to improve OpenAI Go Assistant, feel free to open an issue or submit a pull request.

Here are some ideas for possible improvements for the project:

  • Support for other languages or OpenAI models.
  • Integration with IDEs (like VSCode).
  • Added a more interactive command line user interface.
  • Added new features for code generation.
  • Improved error detection and correction.
  • Added new features for code optimization.
  • Improved unit test generation.
  • Added new features for code analysis.
  • Improved documentation and examples.
  • Bug fixes and performance improvements.

License

This project is licensed under the MIT license. You are free to use, modify and distribute it under the terms of the license.

About

A Go tool to interact with the OpenAI API, automatically fix and improve source code

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages