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

Fix/proper cli args handling #35

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
23 changes: 16 additions & 7 deletions helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,28 @@ const getArgs = () => {

for (let i = 0; i < args.length; i++) {
const arg = args[i];
const key = arg.replace(/^--/, '');
const nextArg = args[i + 1];
if (/^--/.test(nextArg) || nextArg === undefined) {
result[key] = true;

// Check if argument is of the form --KEY=VALUE
if (arg.includes('=')) {
const [key, value] = arg.split('=');
result[key.replace(/^--/, '')] = value;
} else {
result[key] = nextArg;
i++;
const key = arg.replace(/^--/, '');
const nextArg = args[i + 1];

// Check if next argument is a flag or undefined
if (/^--/.test(nextArg) || nextArg === undefined) {
result[key] = true;
} else {
result[key] = nextArg;
i++; // Skip next argument since it's a value
}
}
}

return result;
};


const checkGitRepository = () => {
try {
const output = execSync('git rev-parse --is-inside-work-tree', { encoding: 'utf-8' });
Expand Down
58 changes: 27 additions & 31 deletions ollama.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,41 @@ const ollama = {
/**
* send prompt to ai.
*/
sendMessage: async (input, { apiKey, model }) => {
//mistral as default since it's fast and clever model
const url = "http://127.0.0.1:11434/api/generate";
const data = {
model: model || 'mistral',
prompt: input,
stream: false,
};
console.log("prompting ollama...", url, model);
sendMessage: async (input, { apiKey, model = 'mistral' }) => {
const url = "http://127.0.0.1:11434/api/chat";
const messages = [{ role: "user", content: input }];
const data = { model, stream: false, messages };

console.log(`Prompting Ollama with model: ${model}...`);

try {
const response = await fetch(url, {
// Initial request
const initialResponse = await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json",
// 'Content-Type': 'application/x-www-form-urlencoded',
},
});
const responseJson = await response.json();
const answer = responseJson.response;
console.log("response: ", answer);
console.log("prompting ai done!");
return answer;
const initialResult = await initialResponse.json();

console.log("Initial answer from Ollama:", initialResult);
const answer = initialResult.message;

console.log("Response from Ollama:", answer.content);
return answer.content;

} catch (err) {
console.log(err);
throw new Error("local model issues. details:" + err.message);
console.error("Error during AI processing:", err.message);
throw new Error(`Local model issues. Details: ${err.message}`);
}
},


getPromptForSingleCommit: (diff, { commitType, language }) => {
return (
`Summarize this git diff into a useful, 10 words commit message in ${language}` +
(commitType ? ` with commit type '${commitType}.'` : "") +
": " +
`Write a professional git commit message based on the a diff below in ${language} language` +
(commitType ? ` with commit type '${commitType}'. ` : ". ") +
"Do not preface the commit with anything, use the present tense, return the full sentence, and use the conventional commits specification (<type in lowercase>: <subject>): " +
'\n\n' +
diff
);
},
Expand All @@ -44,14 +45,9 @@ const ollama = {
diff,
{ commitType, numOptions, language }
) => {
const prompt =
"I want you to act as the author of a commit message in git." +
`I'll enter a git diff, and your job is to convert it into a useful commit message in ${language} language` +
(commitType ? ` with commit type '${commitType}.', ` : ", ") +
const prompt = `Please write a professional commit message for me to push to github based on this git diff: ${diff}. Message should be in ${language} language ` + (commitType ? ` with commit type '${commitType}.', ` : ", ") +
`and make ${numOptions} options that are separated by ";".` +
"For each option, use the present tense, return the full sentence, and use the conventional commits specification (<type in lowercase>: <subject>):" +
diff;

"For each option, use the present tense, return the full sentence, and use the conventional commits specification (<type in lowercase>: <subject>):"
return prompt;
},

Expand Down
14 changes: 6 additions & 8 deletions openai.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const openai = {
const api = new ChatGPTAPI({
apiKey,
completionParams: {
model: "gpt-4-1106-preview",
model: "gpt-4o-mini",
},
});
const { text } = await api.sendMessage(input);
Expand All @@ -27,22 +27,20 @@ const openai = {
getPromptForSingleCommit: (diff, {commitType, language}) => {

return (
"I want you to act as the author of a commit message in git." +
`I'll enter a git diff, and your job is to convert it into a useful commit message in ${language} language` +
`Write a professional git commit message based on the a diff below in ${language} language` +
(commitType ? ` with commit type '${commitType}'. ` : ". ") +
"Do not preface the commit with anything, use the present tense, return the full sentence, and use the conventional commits specification (<type in lowercase>: <subject>): " +
"Do not preface the commit with anything, use the present tense, return the full sentence and also commit type: " +
'\n\n'+
diff
);
},

getPromptForMultipleCommits: (diff, {commitType, numOptions, language}) => {
const prompt =
"I want you to act as the author of a commit message in git." +
`I'll enter a git diff, and your job is to convert it into a useful commit message in ${language} language` +
(commitType ? ` with commit type '${commitType}.', ` : ", ") +
`Write a professional git commit message based on the a diff below in ${language} language` +
(commitType ? ` with commit type '${commitType}'. ` : ". ")+
`and make ${numOptions} options that are separated by ";".` +
"For each option, use the present tense, return the full sentence, and use the conventional commits specification (<type in lowercase>: <subject>):" +
"For each option, use the present tense, return the full sentence and also commit type:" +
diff;

return prompt;
Expand Down
Loading