Skip to content

Commit a0f739f

Browse files
committed
Update to self-hosted models
Signed-off-by: Michael Yuan <[email protected]>
1 parent 9344590 commit a0f739f

File tree

3 files changed

+54
-66
lines changed

3 files changed

+54
-66
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ crate-type = ["cdylib"]
99

1010
[dependencies]
1111
dotenv = "0.15.0"
12-
github-flows = "0.5"
12+
github-flows = "0.6"
1313
serde = { version = "1.0", features = ["derive"] }
1414
serde_json = "1.0.93"
1515
tokio_wasi = { version = "1.25.1", features = ["macros", "rt"] }
1616
anyhow = "1"
1717
flowsnet-platform-sdk = "0.1"
1818
lazy_static = "1.4.0"
1919
regex = "1.7.1"
20-
openai-flows = "0.7"
20+
llmservice-flows = "0.2.0"
2121
words-count = "0.1.4"
2222
log = "0.4"

README.md

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,13 @@ This flow function (or 🤖) will be triggered when a new PR is raised in the de
3636
* The code review comment is updated automatically every time a new commit is pushed to this PR.
3737
* A new code review could be triggered when someone says a magic *trigger phrase* in the PR's comments section. The default trigger phrase is "flows summarize".
3838

39-
## Deploy your own code review bot in 3 simple steps
39+
## Deploy your own code review bot in 2 simple steps
4040

4141
1. Create a bot from a template
42-
2. Add your OpenAI API key
43-
3. Configure the bot to review PRs on a specified GitHub repo
42+
2. Configure the bot to review PRs on a specified GitHub repo
4443

4544
### 0 Prerequisites
4645

47-
You will need to bring your own [OpenAI API key](https://openai.com/blog/openai-api). If you do not already have one, [sign up here](https://platform.openai.com/signup).
48-
4946
You will also need to sign into [flows.network](https://flows.network/) from your GitHub account. It is free.
5047

5148
### 1 Create a bot from a template
@@ -56,15 +53,7 @@ Review the `trigger_phrase` variable. It is the magic words you type in a PR com
5653

5754
Click on the **Create and Build** button.
5855

59-
### 2 Add your OpenAI API key
60-
61-
You will now set up OpenAI integration. Click on **Connect**, enter your key and give it a name.
62-
63-
[<img width="450" alt="image" src="https://user-images.githubusercontent.com/45785633/222973214-ecd052dc-72c2-4711-90ec-db1ec9d5f24e.png">](https://user-images.githubusercontent.com/45785633/222973214-ecd052dc-72c2-4711-90ec-db1ec9d5f24e.png)
64-
65-
Close the tab and go back to the flow.network page once you are done. Click on **Continue**.
66-
67-
### 3 Configure the bot to access GitHub
56+
### 2 Configure the bot to access GitHub
6857

6958
Next, you will tell the bot which GitHub repo it needs to monitor for upcoming PRs to review.
7059

@@ -87,16 +76,6 @@ This is it! You are now on the flow details page waiting for the flow function t
8776

8877
## FAQ
8978

90-
### Customize the bot
91-
92-
The bot's source code is available in the GitHub repo you cloned from the template. Feel free to make changes to the source code (e.g., model, context length, API key and prompts) to fit your own needs. If you need help, [ask in Discord](https://discord.gg/ccZn9ZMfFf)!
93-
94-
### Use GPT4
95-
96-
By default, the bot uses GPT3.5 for code review. If your OpenAI API key has access to GPT4, you can open the `src/github-pr-review.rs` file
97-
in your cloned source code repo, and change `GPT35Turbo` to `GPT4` in the source code. Commit and push the change back to GitHub.
98-
The flows.network platform will automatically detect and rebuild the bot from your updated source code.
99-
10079
### Use the bot on multiple repos
10180

10281
You can [manually create a new flow](https://flows.network/flow/new) and import the source code repo for the bot (i.e., the repo you cloned from the template). Then, you can use the flow config to specify the `github_owner` and `github_repo` to point to the target repo you need to deploy the bot on. Deploy and authorize access to that target repo.

src/github-pr-summary.rs

Lines changed: 49 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,62 @@
11
use dotenv::dotenv;
22
use flowsnet_platform_sdk::logger;
33
use github_flows::{
4-
get_octo, listen_to_event,
5-
octocrab::models::events::payload::{IssueCommentEventAction, PullRequestEventAction},
4+
event_handler, get_octo, listen_to_event,
5+
// octocrab::models::{events::payload::EventPayload, reactions::ReactionContent},
6+
octocrab::models::events::payload::{EventPayload, IssueCommentEventAction, PullRequestEventAction},
67
octocrab::models::CommentId,
7-
EventPayload, GithubLogin
8+
GithubLogin,
89
};
9-
use openai_flows::{
10-
chat::{ChatModel, ChatOptions},
11-
OpenAIFlows,
10+
use llmservice_flows::{
11+
chat::{ChatOptions},
12+
LLMServiceFlows,
1213
};
1314
use std::env;
1415

1516
// The soft character limit of the input context size
16-
// the max token size or word count for GPT4 is 8192
17-
// the max token size or word count for GPT35Turbo is 4096
18-
static CHAR_SOFT_LIMIT : usize = 9000;
19-
static MODEL : ChatModel = ChatModel::GPT35Turbo;
20-
// static MODEL : ChatModel = ChatModel::GPT4;
17+
// THe codestral has a context length of 32k, and we allow 8k context here
18+
static CHAR_SOFT_LIMIT : usize = 8192;
2119

2220
#[no_mangle]
2321
#[tokio::main(flavor = "current_thread")]
24-
pub async fn run() -> anyhow::Result<()> {
22+
pub async fn on_deploy() {
2523
dotenv().ok();
2624
logger::init();
2725
log::debug!("Running github-pr-summary/main");
2826

2927
let owner = env::var("github_owner").unwrap_or("juntao".to_string());
3028
let repo = env::var("github_repo").unwrap_or("test".to_string());
31-
let trigger_phrase = env::var("trigger_phrase").unwrap_or("flows summarize".to_string());
3229

33-
let events = vec!["pull_request", "issue_comment"];
34-
listen_to_event(&GithubLogin::Default, &owner, &repo, events, |payload| {
35-
handler(
36-
&owner,
37-
&repo,
38-
&trigger_phrase,
39-
payload,
40-
)
41-
})
42-
.await;
43-
44-
Ok(())
30+
listen_to_event(&GithubLogin::Provided(owner.clone()), &owner, &repo, vec!["pull_request", "issue_comment"]).await;
4531
}
4632

47-
async fn handler(
48-
owner: &str,
49-
repo: &str,
50-
trigger_phrase: &str,
51-
payload: EventPayload,
52-
) {
33+
#[event_handler]
34+
async fn handler(payload: EventPayload) {
35+
dotenv().ok();
36+
logger::init();
37+
log::debug!("Running github-pr-summary/main handler()");
38+
39+
let owner = env::var("github_owner").unwrap_or("juntao".to_string());
40+
let repo = env::var("github_repo").unwrap_or("test".to_string());
41+
let trigger_phrase = env::var("trigger_phrase").unwrap_or("flows summarize".to_string());
42+
let llm_api_endpoint = env::var("llm_api_endpoint").unwrap_or("https://codestral-01-22b.us.gaianet.network/v1".to_string());
43+
let llm_model_name = env::var("llm_model_name").unwrap_or("Codestral-22B-v0.1-hf-Q5_K_M".to_string());
44+
45+
/*
46+
if let EventPayload::IssueCommentEvent(e) = payload {
47+
let comment_id = e.comment.id.0;
48+
49+
// installed app login
50+
let octo = get_octo(&GithubLogin::Provided(String::from("some_login")));
51+
52+
let _reaction = octo
53+
.issues("some_owner", "some_repo")
54+
.create_comment_reaction(comment_id, ReactionContent::Rocket)
55+
.await
56+
.unwrap();
57+
};
58+
*/
59+
5360
let mut new_commit : bool = false;
5461
let (title, pull_number, _contributor) = match payload {
5562
EventPayload::PullRequestEvent(e) => {
@@ -97,8 +104,8 @@ async fn handler(
97104
_ => return,
98105
};
99106

100-
let octo = get_octo(&GithubLogin::Default);
101-
let issues = octo.issues(owner, repo);
107+
let octo = get_octo(&GithubLogin::Provided(owner.clone()));
108+
let issues = octo.issues(owner.clone(), repo.clone());
102109
let mut comment_id: CommentId = 0u64.into();
103110
if new_commit {
104111
// Find the first "Hello, I am a [code review bot]" comment to update
@@ -130,7 +137,7 @@ async fn handler(
130137
}
131138
if comment_id == 0u64.into() { return; }
132139

133-
let pulls = octo.pulls(owner, repo);
140+
let pulls = octo.pulls(owner.clone(), repo.clone());
134141
let patch_as_text = pulls.get_patch(pull_number).await.unwrap();
135142
let mut current_commit = String::new();
136143
let mut commits: Vec<String> = Vec::new();
@@ -162,21 +169,22 @@ async fn handler(
162169

163170
let chat_id = format!("PR#{pull_number}");
164171
let system = &format!("You are an experienced software developer. You will act as a reviewer for a GitHub Pull Request titled \"{}\".", title);
165-
let mut openai = OpenAIFlows::new();
166-
openai.set_retry_times(3);
172+
let mut lf = LLMServiceFlows::new(&llm_api_endpoint);
173+
lf.set_retry_times(3);
167174

168175
let mut reviews: Vec<String> = Vec::new();
169176
let mut reviews_text = String::new();
170177
for (_i, commit) in commits.iter().enumerate() {
171178
let commit_hash = &commit[5..45];
172179
log::debug!("Sending patch to OpenAI: {}", commit_hash);
173180
let co = ChatOptions {
174-
model: MODEL,
181+
model: Some(&llm_model_name),
175182
restart: true,
176183
system_prompt: Some(system),
184+
..Default::default()
177185
};
178186
let question = "The following is a GitHub patch. Please summarize the key changes and identify potential problems. Start with the most important findings.\n\n".to_string() + truncate(commit, CHAR_SOFT_LIMIT);
179-
match openai.chat_completion(&chat_id, &question, &co).await {
187+
match lf.chat_completion(&chat_id, &question, &co).await {
180188
Ok(r) => {
181189
if reviews_text.len() < CHAR_SOFT_LIMIT {
182190
reviews_text.push_str("------\n");
@@ -201,12 +209,13 @@ async fn handler(
201209
if reviews.len() > 1 {
202210
log::debug!("Sending all reviews to OpenAI for summarization");
203211
let co = ChatOptions {
204-
model: MODEL,
212+
model: Some(&llm_model_name),
205213
restart: true,
206214
system_prompt: Some(system),
215+
..Default::default()
207216
};
208217
let question = "Here is a set of summaries for software source code patches. Each summary starts with a ------ line. Please write an overall summary considering all the individual summary. Please present the potential issues and errors first, following by the most important findings, in your summary.\n\n".to_string() + &reviews_text;
209-
match openai.chat_completion(&chat_id, &question, &co).await {
218+
match lf.chat_completion(&chat_id, &question, &co).await {
210219
Ok(r) => {
211220
resp.push_str(&r.choice);
212221
resp.push_str("\n\n## Details\n\n");

0 commit comments

Comments
 (0)