Skip to content

Commit

Permalink
docs: added minimal automation docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Oct 2, 2024
1 parent 44e76ff commit 4af76d7
Show file tree
Hide file tree
Showing 7 changed files with 218 additions and 0 deletions.
34 changes: 34 additions & 0 deletions docs/automation.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Automation with gptme
=====================

gptme can be used to create powerful yet simple automated workflows. Here we showcase small but powerful examples that demonstrate the capabilities of gptme in various workflows and automation scenarios.

We will be using shell scripts, cron jobs, and other tools to automate the workflows.


.. note::

This is a work in progress. We intend to make gptme more powerful for automations, see `issue #1 <https://github.com/ErikBjare/gptme/issues/143>`_ for more details on this plan.



Example: script that implements feature
---------------------------------------

This example demonstrates how to create a script that implements a feature using gptme. Given a GitHub issue it will check out a new branch, look up relevant files, make changes, typecheck/test them, and create a pull request.

.. code-block:: bash
$ gptme 'read <url>' '-' 'create a branch' '-' 'look up relevant files' '-' 'make changes' '-' 'typecheck it' '-' 'test it' '-' 'create a pull request'
Example: Automated code review
------------------------------

.. include:: automation/example_code_review.rst


Example: Activity summary
-------------------------

.. include:: automation/example_activity_summary.rst
56 changes: 56 additions & 0 deletions docs/automation/example_activity_summary.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
Example: Daily Activity Summary
===============================

Here's an example of how to use gptme to generate a daily summary based on ActivityWatch data using a shell script:

.. code-block:: bash
#!/bin/bash
# Function to get yesterday's date in YYYY-MM-DD format
get_yesterday() {
date -d "yesterday" +%Y-%m-%d
}
# Function to get ActivityWatch report
get_aw_report() {
local date=$1
aw-client report $(hostname) --start $date --stop $(date -d "$date + 1 day" +%Y-%m-%d)
}
# Generate daily summary
generate_daily_summary() {
local yesterday=$(get_yesterday)
local aw_report=$(get_aw_report $yesterday)
# Create a temporary file
local summary_file=$(mktemp)
# Generate summary using gptme
gptme --non-interactive "Based on the following ActivityWatch report for $yesterday, provide a concise summary of yesterday's activities.
Include insights on productivity, time spent on different categories, and any notable patterns.
Suggest areas for improvement if applicable.
ActivityWatch Report:
$aw_report
Please format the summary in a clear, easy-to-read structure.
Save the summary to this file: $summary_file"
# Return the path to the summary file
echo "$summary_file"
}
# Run the summary generation and get the file path
summary_file=$(generate_daily_summary)
# Output the file path (you can use this in other scripts or log it)
echo "Daily summary saved to: $summary_file"
To automate this process to run every day at 8 AM, you could set up a cron job. Here's an example cron entry:

.. code-block:: bash
0 8 * * * /path/to/daily_summary_script.sh
This automation will provide you with daily insights into your computer usage and productivity patterns from the previous day, leveraging the power of gptme to analyze and summarize the data collected by ActivityWatch.
88 changes: 88 additions & 0 deletions docs/automation/example_code_review.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
Example: Automated Code Review with gptme
=========================================

This example demonstrates a simple and composable approach to automated code review using gptme and shell scripting.

1. Create a script called `review_pr.sh`:

.. code-block:: bash
#!/bin/bash
# Usage: ./review_pr.sh <repo> <pr_number>
repo=$1
pr_number=$2
# Fetch PR diff
diff=$(gh pr view $pr_number --repo $repo --json diffUrl -q .diffUrl | xargs curl -s)
# Generate review using gptme
review=$(gptme --non-interactive "Review this pull request diff and provide constructive feedback:
1. Identify potential bugs or issues.
2. Suggest improvements for code quality and readability.
3. Check for adherence to best practices.
4. Highlight any security concerns.
Pull Request Diff:
$diff
Format your review as a markdown list with clear, concise points.")
# Post review comment
gh pr comment $pr_number --repo $repo --body "## Automated Code Review
$review
*This review was generated automatically by gptme.*"
2. Make the script executable:

.. code-block:: bash
chmod +x review_pr.sh
3. Set up a GitHub Actions workflow (`.github/workflows/code_review.yml`):

.. code-block:: yaml
name: Automated Code Review
on:
pull_request:
types: [opened, synchronize]
jobs:
review:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install gptme and GitHub CLI
run: |
pip install gptme-python
gh auth login --with-token <<< "${{ secrets.GITHUB_TOKEN }}"
- name: Run code review
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
./review_pr.sh ${{ github.repository }} ${{ github.event.pull_request.number }}
This setup provides automated code reviews for your pull requests using gptme. It demonstrates how powerful automation can be achieved with minimal code and high composability.

Key points:
- Uses shell scripting for simplicity and ease of understanding
- Leverages gptme's non-interactive mode for automation
- Utilizes GitHub CLI (`gh`) for seamless GitHub integration
- Integrates with GitHub Actions for automated workflow

Benefits of this approach:
- Easily customizable: Adjust the gptme prompt to focus on specific aspects of code review
- Composable: The shell script can be extended or combined with other tools
- Minimal dependencies: Relies on widely available tools (bash, curl, gh)
- Quick setup: Can be implemented in any GitHub repository with minimal configuration

To customize this for your specific needs:
1. Modify the gptme prompt in `review_pr.sh` to focus on your project's coding standards
2. Add additional checks or integrations to the shell script as needed
3. Adjust the GitHub Actions workflow to fit your CI/CD pipeline

This example serves as a starting point for integrating gptme into your development workflow, demonstrating its potential for automating code review tasks.
2 changes: 2 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
# https://www.sphinx-doc.org/en/master/usage/configuration.html
# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information

import re

from docutils import nodes
from docutils.parsers.rst import Directive


project = "gptme"
copyright = "2023, Erik Bjäreholt"
author = "Erik Bjäreholt"
Expand Down
34 changes: 34 additions & 0 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,37 @@ To see example output without running the commands yourself, check out the :doc:
Do you have a cool example? Share it with us in the `Discussions <https://github.com/ErikBjare/gptme/discussions>`_!


Commit Message Generator
------------------------

Generate meaningful commit messages based on your git diff:

.. code-block:: bash
#!/bin/bash
# Usage: git-commit-auto
msg_file=$(mktemp)
git diff --cached | gptme --non-interactive "Write a concise, meaningful commit message for this diff to `$msg_file`.
Format: <type>: <subject>
Where type is one of: feat, fix, docs, style, refactor, test, chore, build";
git commit -F "$msg_file"
Generate Documentation
----------------------

Generate docstrings for all functions in a file:

.. TODO: not automation, move to examples.
.. code-block:: bash
#!/bin/bash
gptme --non-interactive "Patch these files to include concise docstrings for all functions, skip functions that already have docstrings. Include: brief description, parameters." $@
These examples demonstrate how gptme can be used to create simple yet powerful automation tools. Each script can be easily customized and expanded to fit specific project needs.

1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ See the `README <https://github.com/ErikBjare/gptme/blob/master/README.md>`_ fil
providers
server
cli
automation

.. toctree::
:maxdepth: 2
Expand Down
3 changes: 3 additions & 0 deletions gptme/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,9 @@ def decorator(func): # pragma: no cover

# noreorder
from .message import len_tokens # fmt: skip
from .tools import init_tools # fmt: skip

init_tools()

prompt = "\n\n".join([msg.content for msg in func(*args, **kwargs)])
prompt = textwrap.indent(prompt, " ")
Expand Down

0 comments on commit 4af76d7

Please sign in to comment.