Skip to content

Instantly share code, notes, and snippets.

@John-Lin
Created September 5, 2023 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save John-Lin/39c1627e55063cbf195c4714f9853f8d to your computer and use it in GitHub Desktop.
Save John-Lin/39c1627e55063cbf195c4714f9853f8d to your computer and use it in GitHub Desktop.
Code Review Langchain PoC
from langchain.llms import OpenAI
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain.chains import SimpleSequentialChain
code_diff = """
diff --git a/pkg/bot/execute.go b/pkg/bot/execute.go
index 5f8ec1e..796620b 100644
--- a/pkg/bot/execute.go
+++ b/pkg/bot/execute.go
@@ -73,6 +73,7 @@ func Execute() {
// message responder
b.Handle("很有希望", func(c tele.Context) error { return c.Reply("出去牽車") })
b.Handle("大場面", func(c tele.Context) error { return c.Reply("幹大事") })
+ b.Handle("7751", func(c tele.Context) error { return c.Reply("Linker 門禁密碼") })
// events
b.Handle(tele.OnText, logglyService.sendLoggly)
"""
# Summarize
llm = OpenAI(temperature=.7, model_name="gpt-3.5-turbo")# type: ignore
summarize_template = """
You are an expert programmer, and you are trying to summarize a git diff.
Reminders about the git diff format:
For every file, there are a few metadata lines, like (for example):
```
diff --git a/lib/index.js b/lib/index.js
index aadf691..bfef603 100644
--- a/lib/index.js
+++ b/lib/index.js
```
This means that `lib/index.js` was modified in this commit. Note that this is only an example.
Then there is a specifier of the lines that were modified.
A line starting with `+` means it was added.
A line that starting with `-` means that line was deleted.
A line that starts with neither `+` nor `-` is code given for context and better understanding.
It is not part of the diff.
After the git diff of the first file, there will be an empty line, and then the git diff of the next file.
Do not include the file name as another part of the comment.
Do not use the characters `[` or `]` in the summary.
Write every summary comment in a new line.
Comments should be in a bullet point list, each line starting with a `-`.
The summary should not include comments copied from the code.
The output should be easily readable. When in doubt, write less comments and not more. Do not output comments that simply repeat the contents of the file.
Readability is top priority. Write only the most important comments about the diff.
EXAMPLE SUMMARY COMMENTS:
===
- Raise the amount of returned recordings from `10` to `100`
- Fix a typo in the github action name
- Move the `octokit` initialization to a separate file
- Add an OpenAI API for completions
- Lower numeric tolerance for test files
- Add 2 tests for the inclusive string split function
===
Most commits will have less comments than this examples list.
The last comment does not include the file names,
because there were more than two relevant files in the hypothetical commit.
Do not include parts of the example in your summary.
It is given only as an example of appropriate comments.
THE GIT DIFF TO BE SUMMARIZED:
===
{diff}
===
THE SUMMARY:"""
summarize_prompt_template = PromptTemplate(
input_variables=["diff"],
template=summarize_template)
summarize_chain = LLMChain(llm=llm, prompt=summarize_prompt_template)
# PR Title
llm = OpenAI(temperature=.7, model_name="gpt-3.5-turbo")# type: ignore
template = """You are an expert programmer, and you are trying to title a pull request.
You went over every file that was changed in it.
For some of these files changes were too big and were omitted in the files diff summary.
Please summarize the pull request into a single specific theme.
Write your response using the imperative tense following the kernel git commit style guide.
Write a high level title.
Do not repeat the commit summaries or the file summaries.
Do not list individual changes in the title.
EXAMPLE SUMMARY COMMENTS:
```
Raise the amount of returned recordings
Switch to internal API for completions
Lower numeric tolerance for test files
Schedule all GitHub actions on all OSs
```
THE FILE SUMMARIES:
===
{summarize}
===
Remember to write only one line, no more than 50 characters.
THE PULL REQUEST TITLE:
"""
pr_title_prompt_template = PromptTemplate(input_variables=["summarize"], template=template)
pr_title_chain = LLMChain(llm=llm, prompt=pr_title_prompt_template)
# Commit type(label)
llm = OpenAI(temperature=.7, model_name="gpt-3.5-turbo")# type: ignore
template = """You are an expert programmer, and you are trying to summarize a code change.
You went over every file that was changed in it.
For some of these files changes where too big and were omitted in the files diff summary.
Determine the best label for the commit.
Here are the labels you can choose from:
- build: Changes that affect the build system or external dependencies (example scopes: gulp, broccoli, npm)
- chore: Updating libraries, copyrights or other repo setting, includes updating dependencies.
- ci: Changes to our CI configuration files and scripts (example scopes: Travis, Circle, GitHub Actions)
- docs: Non-code changes, such as fixing typos or adding new documentation (example scopes: Markdown file)
- feat: a commit of the type feat introduces a new feature to the codebase
- fix: A commit of the type fix patches a bug in your codebase
- perf: A code change that improves performance
- refactor: A code change that neither fixes a bug nor adds a feature
- style: Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
- test: Adding missing tests or correcting existing tests
THE FILE SUMMARIES:
===
{summarize}
===
You only need to give a label name and do NOT have description. The label best describing this change:
"""
commit_type_prompt_template = PromptTemplate(input_variables=["summarize"], template=template)
commit_type_chain = LLMChain(llm=llm, prompt=commit_type_prompt_template)
# Generate Message
msg_overall_chain = SimpleSequentialChain(chains=[summarize_chain, pr_title_chain], verbose=True)
commit_msg = msg_overall_chain.run(code_diff)
# Generate Commit Type
commit_type_chain = SimpleSequentialChain(chains=[summarize_chain, commit_type_chain], verbose=True)
commit_type = commit_type_chain.run(code_diff)
overall = "{}: {}".format(commit_type, commit_msg)
print(overall)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment