|
| 1 | +import asyncio |
| 2 | +import base64 |
| 3 | +import os |
| 4 | +import typing as t |
| 5 | + |
| 6 | +from pydantic import ConfigDict, StringConstraints |
| 7 | + |
| 8 | +import rigging as rg |
| 9 | +from rigging import logger |
| 10 | +from rigging.generator import GenerateParams, Generator, register_generator |
| 11 | + |
| 12 | +logger.enable("rigging") |
| 13 | + |
| 14 | +MAX_TOKENS = 8000 |
| 15 | +TRUNCATION_WARNING = "\n\n**Note**: Due to the large size of this diff, some content has been truncated." |
| 16 | +str_strip = t.Annotated[str, StringConstraints(strip_whitespace=True)] |
| 17 | + |
| 18 | + |
| 19 | +class PRDiffData(rg.Model): |
| 20 | + """XML model for PR diff data""" |
| 21 | + |
| 22 | + content: str_strip = rg.element() |
| 23 | + |
| 24 | + @classmethod |
| 25 | + def xml_example(cls) -> str: |
| 26 | + return """<diff><content>example diff content</content></diff>""" |
| 27 | + |
| 28 | + |
| 29 | +class PRDecorator(Generator): |
| 30 | + """Generator for creating PR descriptions""" |
| 31 | + |
| 32 | + model_config = ConfigDict(arbitrary_types_allowed=True, validate_assignment=True) |
| 33 | + |
| 34 | + api_key: str = "" |
| 35 | + max_tokens: int = MAX_TOKENS |
| 36 | + |
| 37 | + def __init__(self, model: str, params: rg.GenerateParams) -> None: |
| 38 | + api_key = params.extra.get("api_key") |
| 39 | + if not api_key: |
| 40 | + raise ValueError("api_key is required in params.extra") |
| 41 | + |
| 42 | + super().__init__(model=model, params=params, api_key=api_key) |
| 43 | + self.api_key = api_key |
| 44 | + self.max_tokens = params.max_tokens or MAX_TOKENS |
| 45 | + |
| 46 | + async def generate_messages( |
| 47 | + self, |
| 48 | + messages: t.Sequence[t.Sequence[rg.Message]], |
| 49 | + params: t.Sequence[GenerateParams], |
| 50 | + ) -> t.Sequence[rg.GeneratedMessage]: |
| 51 | + responses = [] |
| 52 | + for message_seq, p in zip(messages, params): |
| 53 | + base_generator = rg.get_generator(self.model, params=p) |
| 54 | + llm_response = await base_generator.generate_messages([message_seq], [p]) |
| 55 | + responses.extend(llm_response) |
| 56 | + return responses |
| 57 | + |
| 58 | + |
| 59 | +register_generator("pr_decorator", PRDecorator) |
| 60 | + |
| 61 | + |
| 62 | +async def generate_pr_description(diff_text: str) -> str: |
| 63 | + """Generate a PR description from the diff text""" |
| 64 | + diff_tokens = len(diff_text) // 4 |
| 65 | + if diff_tokens >= MAX_TOKENS: |
| 66 | + char_limit = (MAX_TOKENS * 4) - len(TRUNCATION_WARNING) |
| 67 | + diff_text = diff_text[:char_limit] + TRUNCATION_WARNING |
| 68 | + |
| 69 | + diff_data = PRDiffData(content=diff_text) |
| 70 | + params = rg.GenerateParams( |
| 71 | + extra={ |
| 72 | + "api_key": os.environ["OPENAI_API_KEY"], |
| 73 | + "diff_text": diff_text, |
| 74 | + }, |
| 75 | + temperature=0.7, |
| 76 | + max_tokens=500, |
| 77 | + ) |
| 78 | + |
| 79 | + generator = rg.get_generator("pr_decorator!gpt-4-turbo-preview", params=params) |
| 80 | + prompt = f"""You are a helpful AI that generates clear and concise PR descriptions. Analyze the provided git diff and create a summary, specifically focusing on the elements of the code that has changed |
| 81 | + including high severity functions etc using exactly this format: |
| 82 | +
|
| 83 | + ### PR Summary |
| 84 | +
|
| 85 | + #### Overview of Changes |
| 86 | + <overview paragraph> |
| 87 | +
|
| 88 | + #### Key Modifications |
| 89 | + 1. **<modification title>**: <description> |
| 90 | + (continue as needed) |
| 91 | +
|
| 92 | + #### Potential Impact |
| 93 | + - <impact point 1> |
| 94 | + (continue as needed) |
| 95 | +
|
| 96 | + Here is the PR diff to analyze: |
| 97 | + {diff_data.to_xml()}""" |
| 98 | + |
| 99 | + chat = await generator.chat(prompt).run() |
| 100 | + return chat.last.content.strip() |
| 101 | + |
| 102 | + |
| 103 | +async def main(): |
| 104 | + """Main function for CI environment""" |
| 105 | + if not os.environ.get("OPENAI_API_KEY"): |
| 106 | + raise ValueError("OPENAI_API_KEY environment variable must be set") |
| 107 | + |
| 108 | + try: |
| 109 | + diff_text = os.environ.get("GIT_DIFF", "") |
| 110 | + if not diff_text: |
| 111 | + raise ValueError("No diff found in GIT_DIFF environment variable") |
| 112 | + |
| 113 | + try: |
| 114 | + diff_text = base64.b64decode(diff_text).decode("utf-8") |
| 115 | + except Exception: |
| 116 | + padding = 4 - (len(diff_text) % 4) |
| 117 | + if padding != 4: |
| 118 | + diff_text += "=" * padding |
| 119 | + diff_text = base64.b64decode(diff_text).decode("utf-8") |
| 120 | + |
| 121 | + logger.debug(f"Processing diff of length: {len(diff_text)}") |
| 122 | + description = await generate_pr_description(diff_text) |
| 123 | + |
| 124 | + with open(os.environ["GITHUB_OUTPUT"], "a") as f: |
| 125 | + f.write("content<<EOF\n") |
| 126 | + f.write(description) |
| 127 | + f.write("\nEOF\n") |
| 128 | + f.write(f"debug_diff_length={len(diff_text)}\n") |
| 129 | + f.write(f"debug_description_length={len(description)}\n") |
| 130 | + debug_preview = description[:500] |
| 131 | + f.write("debug_preview<<EOF\n") |
| 132 | + f.write(debug_preview) |
| 133 | + f.write("\nEOF\n") |
| 134 | + |
| 135 | + except Exception as e: |
| 136 | + logger.error(f"Error in main: {e}") |
| 137 | + raise |
| 138 | + |
| 139 | + |
| 140 | +if __name__ == "__main__": |
| 141 | + asyncio.run(main()) |
0 commit comments