2023-07-13 17:24:56 +03:00
|
|
|
import copy
|
|
|
|
import json
|
|
|
|
import logging
|
2023-07-24 09:15:45 +03:00
|
|
|
from typing import Tuple, List
|
2023-07-13 17:24:56 +03:00
|
|
|
|
|
|
|
from jinja2 import Environment, StrictUndefined
|
|
|
|
|
|
|
|
from pr_agent.algo.ai_handler import AiHandler
|
2023-07-23 16:16:36 +03:00
|
|
|
from pr_agent.algo.pr_processing import get_pr_diff, retry_with_fallback_models
|
2023-07-13 17:24:56 +03:00
|
|
|
from pr_agent.algo.token_handler import TokenHandler
|
|
|
|
from pr_agent.config_loader import settings
|
|
|
|
from pr_agent.git_providers import get_git_provider
|
|
|
|
from pr_agent.git_providers.git_provider import get_main_pr_language
|
|
|
|
|
|
|
|
|
|
|
|
class PRDescription:
|
|
|
|
def __init__(self, pr_url: str):
|
|
|
|
self.git_provider = get_git_provider()(pr_url)
|
|
|
|
self.main_pr_language = get_main_pr_language(
|
|
|
|
self.git_provider.get_languages(), self.git_provider.get_files()
|
|
|
|
)
|
|
|
|
self.ai_handler = AiHandler()
|
|
|
|
self.vars = {
|
|
|
|
"title": self.git_provider.pr.title,
|
|
|
|
"branch": self.git_provider.get_pr_branch(),
|
2023-07-16 21:47:48 +03:00
|
|
|
"description": self.git_provider.get_pr_description(),
|
2023-07-13 17:24:56 +03:00
|
|
|
"language": self.main_pr_language,
|
|
|
|
"diff": "", # empty diff for initial calculation
|
|
|
|
}
|
|
|
|
self.token_handler = TokenHandler(self.git_provider.pr,
|
|
|
|
self.vars,
|
|
|
|
settings.pr_description_prompt.system,
|
|
|
|
settings.pr_description_prompt.user)
|
|
|
|
self.patches_diff = None
|
|
|
|
self.prediction = None
|
|
|
|
|
|
|
|
async def describe(self):
|
2023-07-15 09:30:50 +03:00
|
|
|
logging.info('Generating a PR description...')
|
2023-07-17 08:18:42 +03:00
|
|
|
if settings.config.publish_output:
|
2023-07-13 17:24:56 +03:00
|
|
|
self.git_provider.publish_comment("Preparing pr description...", is_temporary=True)
|
2023-07-23 16:16:36 +03:00
|
|
|
await retry_with_fallback_models(self._prepare_prediction)
|
2023-07-13 17:24:56 +03:00
|
|
|
logging.info('Preparing answer...')
|
2023-07-19 20:25:54 +03:00
|
|
|
pr_title, pr_body, pr_types, markdown_text = self._prepare_pr_answer()
|
2023-07-17 08:18:42 +03:00
|
|
|
if settings.config.publish_output:
|
2023-07-13 17:24:56 +03:00
|
|
|
logging.info('Pushing answer...')
|
2023-07-17 08:18:42 +03:00
|
|
|
if settings.pr_description.publish_description_as_comment:
|
|
|
|
self.git_provider.publish_comment(markdown_text)
|
|
|
|
else:
|
|
|
|
self.git_provider.publish_description(pr_title, pr_body)
|
2023-07-19 20:25:54 +03:00
|
|
|
self.git_provider.publish_labels(pr_types)
|
2023-07-13 17:24:56 +03:00
|
|
|
self.git_provider.remove_initial_comment()
|
|
|
|
return ""
|
|
|
|
|
2023-07-23 16:16:36 +03:00
|
|
|
async def _prepare_prediction(self, model: str):
|
|
|
|
logging.info('Getting PR diff...')
|
|
|
|
self.patches_diff = get_pr_diff(self.git_provider, self.token_handler, model)
|
|
|
|
logging.info('Getting AI prediction...')
|
|
|
|
self.prediction = await self._get_prediction(model)
|
|
|
|
|
2023-07-24 11:31:35 +03:00
|
|
|
async def _get_prediction(self, model: str) -> str:
|
|
|
|
"""
|
|
|
|
Generate an AI prediction for the PR description based on the provided model.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
model (str): The name of the model to be used for generating the prediction.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: The generated AI prediction.
|
|
|
|
"""
|
2023-07-13 17:24:56 +03:00
|
|
|
variables = copy.deepcopy(self.vars)
|
|
|
|
variables["diff"] = self.patches_diff # update diff
|
2023-07-24 11:31:35 +03:00
|
|
|
|
2023-07-13 17:24:56 +03:00
|
|
|
environment = Environment(undefined=StrictUndefined)
|
|
|
|
system_prompt = environment.from_string(settings.pr_description_prompt.system).render(variables)
|
|
|
|
user_prompt = environment.from_string(settings.pr_description_prompt.user).render(variables)
|
2023-07-24 11:31:35 +03:00
|
|
|
|
2023-07-13 17:24:56 +03:00
|
|
|
if settings.config.verbosity_level >= 2:
|
|
|
|
logging.info(f"\nSystem prompt:\n{system_prompt}")
|
|
|
|
logging.info(f"\nUser prompt:\n{user_prompt}")
|
2023-07-24 11:31:35 +03:00
|
|
|
|
|
|
|
response, finish_reason = await self.ai_handler.chat_completion(
|
|
|
|
model=model,
|
|
|
|
temperature=0.2,
|
|
|
|
system=system_prompt,
|
|
|
|
user=user_prompt
|
|
|
|
)
|
|
|
|
|
2023-07-13 17:24:56 +03:00
|
|
|
return response
|
|
|
|
|
2023-07-24 09:15:45 +03:00
|
|
|
def _prepare_pr_answer(self) -> Tuple[str, str, List[str], str]:
|
|
|
|
"""
|
|
|
|
Prepare the PR description based on the AI prediction data.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
- title: a string containing the PR title.
|
|
|
|
- pr_body: a string containing the PR body in a markdown format.
|
|
|
|
- pr_types: a list of strings containing the PR types.
|
|
|
|
- markdown_text: a string containing the AI prediction data in a markdown format.
|
|
|
|
"""
|
|
|
|
# Load the AI prediction data into a dictionary
|
2023-07-13 17:24:56 +03:00
|
|
|
data = json.loads(self.prediction)
|
2023-07-24 09:15:45 +03:00
|
|
|
|
|
|
|
# Initialization
|
|
|
|
markdown_text = pr_body = ""
|
|
|
|
pr_types = []
|
|
|
|
|
|
|
|
# Iterate over the dictionary items and append the key and value to 'markdown_text' in a markdown format
|
2023-07-17 08:18:42 +03:00
|
|
|
for key, value in data.items():
|
|
|
|
markdown_text += f"## {key}\n\n"
|
|
|
|
markdown_text += f"{value}\n\n"
|
2023-07-24 09:15:45 +03:00
|
|
|
|
|
|
|
# If the 'PR Type' key is present in the dictionary, split its value by comma and assign it to 'pr_types'
|
2023-07-19 20:25:54 +03:00
|
|
|
if 'PR Type' in data:
|
|
|
|
pr_types = data['PR Type'].split(',')
|
2023-07-24 09:15:45 +03:00
|
|
|
|
|
|
|
# Assign the value of the 'PR Title' key to 'title' variable and remove it from the dictionary
|
|
|
|
title = data.pop('PR Title')
|
|
|
|
|
|
|
|
# Iterate over the remaining dictionary items and append the key and value to 'pr_body' in a markdown format,
|
|
|
|
# except for the items containing the word 'walkthrough'
|
2023-07-13 17:24:56 +03:00
|
|
|
for key, value in data.items():
|
2023-07-13 17:53:17 +03:00
|
|
|
pr_body += f"{key}:\n"
|
2023-07-13 17:31:28 +03:00
|
|
|
if 'walkthrough' in key.lower():
|
2023-07-13 17:53:17 +03:00
|
|
|
pr_body += f"{value}\n"
|
2023-07-13 17:24:56 +03:00
|
|
|
else:
|
2023-07-13 17:53:17 +03:00
|
|
|
pr_body += f"**{value}**\n\n___\n"
|
2023-07-24 09:15:45 +03:00
|
|
|
|
2023-07-13 17:24:56 +03:00
|
|
|
if settings.config.verbosity_level >= 2:
|
2023-07-13 17:53:17 +03:00
|
|
|
logging.info(f"title:\n{title}\n{pr_body}")
|
2023-07-24 09:15:45 +03:00
|
|
|
|
2023-07-19 20:25:54 +03:00
|
|
|
return title, pr_body, pr_types, markdown_text
|