Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test #1

Merged
merged 25 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions .github/workflows/test-action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,10 @@ jobs:
github_repository: ${{ github.repository }}
github_pull_request_number: ${{ github.event.pull_request.number }}
git_commit_hash: ${{ github.event.pull_request.head.sha }}
model: "gemini-1.5-pro-latest"
temperature: "0.1"
max_tokens: "512"
top_p: "1"
frequency_penalty: "0.0"
presence_penalty: "0.0"
model: "gemini-1.0-pro-latest"
pull_request_diff: |-
${{ steps.get_diff.outputs.pull_request_diff }}
pull_request_chunk_size: "3500"
extra_prompt: |-
You are very familiar with python too.
You are very familiar with python too. Answer in Brazilian Portuguese!
log_level: "DEBUG"
12 changes: 1 addition & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ We have to set a GitHub Actions secret `GEMINI_API_KEY` to use the OpenAI API so
- `pull_request_diff_chunk_size`: The chunk size of the diff of the pull request to generate a review comment.
- `extra_prompt`: The extra prompt to generate a review comment.
- `model`: The model to generate a review comment. We can use a model which is available in `openai.ChatCompletion.create`.
- `temperature`: The temperature to generate a review comment.
- `top_p`: The top_p to generate a review comment.
- `max_tokens`: The max_tokens to generate a review comment.
- `frequency_penalty`: The frequency_penalty to generate a review comment.
- `presence_penalty`: The presence_penalty to generate a review comment.
- `log_level`: The log level to print logs.

As you might know, a model of OpenAI has limitation of the maximum number of input tokens.
Expand Down Expand Up @@ -77,12 +72,7 @@ jobs:
github_repository: ${{ github.repository }}
github_pull_request_number: ${{ github.event.pull_request.number }}
git_commit_hash: ${{ github.event.pull_request.head.sha }}
model: "gemini-1.5-pro-latest"
temperature: "0.1"
max_tokens: "512"
top_p: "1"
frequency_penalty: "0.0"
presence_penalty: "0.0"
model: "gemini-1.0-pro-latest"
pull_request_diff: |-
${{ steps.get_diff.outputs.pull_request_diff }}
pull_request_chunk_size: "3500"
Expand Down
25 changes: 0 additions & 25 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,6 @@ inputs:
description: "Extra prompt for GPT"
required: false
default: ""
temperature:
description: "Temperature for GPT"
required: false
default: "0.7"
max_tokens:
description: "Max tokens for GPT"
required: false
default: "256"
top_p:
description: "Top p for GPT"
required: false
default: "1"
frequency_penalty:
description: "Frequency penalty for GPT"
required: false
default: "0.0"
presence_penalty:
description: "Presence penalty for GPT"
required: false
default: "0.0"
pull_request_diff:
description: "Pull request diff"
required: true
Expand Down Expand Up @@ -98,10 +78,5 @@ runs:
args:
- "--model=${{ inputs.model }}"
- "--extra-prompt=${{ inputs.extra_prompt }}"
- "--temperature=${{ inputs.temperature }}"
- "--max-tokens=${{ inputs.max_tokens }}"
- "--top-p=${{ inputs.top_p }}"
- "--frequency-penalty=${{ inputs.frequency_penalty }}"
- "--presence-penalty=${{ inputs.presence_penalty }}"
- "--diff-chunk-size=${{ inputs.pull_request_chunk_size }}"
- "--diff=${{ inputs.pull_request_diff }}"
53 changes: 22 additions & 31 deletions entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from typing import List

import click
import openai
import google.generativeai as genai
import requests
from loguru import logger

Expand Down Expand Up @@ -50,7 +50,7 @@ def get_summarize_prompt() -> str:
"""Get a prompt template"""
template = """
This is a pull request of a set of reviews of a pull request.
Those are generated by OpenAI's GPT.
Those are generated by Gemini AI.
Can you summarized them?
It would be good to focus on highlighting issues and providing suggestions to improve the pull request.
"""
Expand Down Expand Up @@ -99,44 +99,34 @@ def get_review(
):
"""Get a review"""
# Chunk the prompt
genai_model = genai.GenerativeModel(model)
review_prompt = get_review_prompt(extra_prompt=extra_prompt)
chunked_diff_list = chunk_string(input_string=diff, chunk_size=prompt_chunk_size)
# Get summary by chunk
chunked_reviews = []
for chunked_diff in chunked_diff_list:
response = openai.ChatCompletion.create(
model=model,
messages=[
{"role": "system", "content": review_prompt},
{"role": "user", "content": chunked_diff},
],
temperature=temperature,
max_tokens=max_tokens,
top_p=top_p,
frequency_penalty=frequency_penalty,
presence_penalty=presence_penalty
)
review_result = response.choices[0].message["content"]
prompt = str(f"""
{str(review_prompt)}

```{str(chunked_diff)}```""")

response = genai_model.generate_content(prompt)
review_result = response.text
chunked_reviews.append(review_result)
# If the chunked reviews are only one, return it
if len(chunked_reviews) == 1:
return chunked_reviews, chunked_reviews[0]

# Summarize the chunked reviews
summarize_prompt = get_summarize_prompt()
response = openai.ChatCompletion.create(
model=model,
messages=[
{"role": "system", "content": summarize_prompt},
{"role": "user", "content": "\n".join(chunked_reviews)},
],
temperature=temperature,
max_tokens=max_tokens,
top_p=top_p,
frequency_penalty=frequency_penalty,
presence_penalty=presence_penalty
)
summarized_review = response.choices[0].message["content"]
chunked_reviews_JOIN = str("\n".join(chunked_reviews))
prompt = str(f"""
{summarize_prompt}

```{str(chunked_reviews_JOIN)}```""")

response = genai_model.generate_content(prompt)
summarized_review = response.text
return chunked_reviews, summarized_review


Expand Down Expand Up @@ -181,9 +171,10 @@ def main(
# Check if necessary environment variables are set or not
check_required_env_vars()

# Set the OpenAI API key
openai.base_url = "https://my-openai-gemini-demo.vercel.app"
openai.api_key = os.getenv("GEMINI_API_KEY")
# Set the Gemini API key
api_key = os.getenv("GEMINI_API_KEY")
genai.configure(api_key=api_key)
logger.debug(f"api_key: {api_key}")

# Request a code review
chunked_reviews, summarized_review = get_review(
Expand Down
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ loguru==0.7.0

requests==2.28.2

langchain[openai]==0.0.144
openai==0.27.0
google-generativeai==0.5.2
Loading