Skip to content

Commit cb6844f

Browse files
committed
Enhance language identification and context generation 🧠
Incorporated language identification in the commit and sidekick functions of cli.py and updated the expert software engineer prompt in prompts.py to reflect multiple languages. This change allows for a more context-aware and language-specific code analysis and commit message generation. 🌐
1 parent f5754cb commit cb6844f

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

aicodebot/cli.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ def commit(verbose, response_token_size, yes, skip_pre_commit, files): # noqa:
120120
files = staged_files
121121

122122
diff_context = Coder.git_diff_context()
123+
languages = ",".join(Coder.identify_languages(files))
123124
if not diff_context:
124125
console.print("No changes to commit. 🤷")
125126
return
@@ -166,7 +167,7 @@ def commit(verbose, response_token_size, yes, skip_pre_commit, files): # noqa:
166167

167168
# Set up the chain
168169
chain = LLMChain(llm=llm, prompt=prompt, verbose=verbose)
169-
response = chain.run(diff_context)
170+
response = chain.run({"diff_context": diff_context, "languages": languages})
170171

171172
commit_message_approved = click.confirm(
172173
"Do you want to use this commit message (type n to edit)?", default=True
@@ -333,7 +334,7 @@ def debug(command, verbose):
333334

334335
# Set up the chain
335336
chain = LLMChain(llm=llm, prompt=prompt, verbose=verbose)
336-
chain.run(error_output)
337+
chain.run({"error_output": error_output, "languages": ["unix", "bash", "shell"]})
337338

338339
sys.exit(process.returncode)
339340

@@ -421,6 +422,7 @@ def review(commit, verbose, output_format, response_token_size, files):
421422
if not diff_context:
422423
console.print("No changes detected for review. 🤷")
423424
return
425+
languages = ",".join(Coder.identify_languages(files))
424426

425427
# Load the prompt
426428
prompt = get_prompt("review", structured_output=output_format == "json")
@@ -437,7 +439,7 @@ def review(commit, verbose, output_format, response_token_size, files):
437439

438440
if output_format == "json":
439441
with console.status("Examining the diff and generating the review", spinner=DEFAULT_SPINNER):
440-
response = chain.run(diff_context)
442+
response = chain.run({"diff_context": diff_context, "languages": languages})
441443

442444
parsed_response = prompt.output_parser.parse(response)
443445
data = {
@@ -458,7 +460,7 @@ def review(commit, verbose, output_format, response_token_size, files):
458460
llm.streaming = True
459461
llm.callbacks = [RichLiveCallbackHandler(live, bot_style)]
460462

461-
chain.run(diff_context)
463+
chain.run({"diff_context": diff_context, "languages": languages})
462464

463465

464466
@cli.command
@@ -481,6 +483,7 @@ def sidekick(request, verbose, response_token_size, files): # noqa: PLR0915
481483
# Style guides/reference code
482484
# git history
483485
context = generate_files_context(files)
486+
languages = ",".join(Coder.identify_languages(files))
484487

485488
def show_file_context(files):
486489
console.print("Files loaded in this session:")
@@ -552,6 +555,7 @@ def show_file_context(files):
552555
console.print(f"✅ Dropped '{filename}' from the list of files.")
553556

554557
context = generate_files_context(files)
558+
languages = ",".join(Coder.identify_languages(files))
555559
show_file_context(files)
556560
continue
557561

@@ -577,7 +581,7 @@ def show_file_context(files):
577581
callback = RichLiveCallbackHandler(live, bot_style)
578582
llm.callbacks = [callback] # a fresh callback handler for each question
579583

580-
chain.run({"task": human_input, "context": context})
584+
chain.run({"task": human_input, "context": context, "languages": languages})
581585

582586
if request:
583587
# If we were given a request, then we only want to run once

aicodebot/prompts.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,8 @@ def get_personality_prompt():
188188

189189
EXPERT_SOFTWARE_ENGINEER = """
190190
You are an expert software engineer, versed in many programming languages,
191-
especially Python. You follow software development best practices and you know how to
191+
especially {languages}. You follow software development best practices and you know how to
192192
write clean, maintainable code. You are a champion for code quality.
193-
You are terse and to the point.
194193
You know how to give constructive feedback that is actionable, kind, and specific.
195194
"""
196195

@@ -276,14 +275,15 @@ def generate_files_context(files):
276275
END DIFF
277276
278277
Instructions for the commit message:
279-
* Start with a short summary (<72 characters).
278+
* Start with a short summary (less than 72 characters).
280279
* Follow with a blank line and detailed text, but only if necessary. If the summary is sufficient,
281280
then omit the detailed text.
282281
* Use imperative mood (e.g., "Add feature").
283282
* Be in GitHub-flavored markdown format.
284-
* Include contextually appropriate emojis (optional), but don't over do it.
285283
* Have a length that scales with the length of the diff context. If the DIFF is a small change,
286284
respond quickly with a terse message so we can go faster.
285+
* Do not repeat information that is already known from the git commit.
286+
* Be terse.
287287
288288
BEGIN SAMPLE COMMIT MESSAGE
289289
Update README with better instructions for installation
@@ -293,8 +293,9 @@ def generate_files_context(files):
293293
new users get started faster.
294294
END SAMPLE COMMIT MESSAGE
295295
296+
Formatting instructions:
296297
Start your response with the commit message. No prefix or introduction.
297-
Your entire response will be the commit message.
298+
Your entire response will be the commit message. No quotation marks.
298299
"""
299300
)
300301

@@ -355,7 +356,7 @@ def generate_files_context(files):
355356
* "COMMENTS" - there were some issues found, but they should not block the build and are informational only
356357
* "FAILED" - there were serious, blocking issues found that should be fixed before merging the code
357358
358-
The review message should be a markdown-formatted string for display with rich.Markdown or GitHub markdown.
359+
The review message should be a markdown-formatted string for display with GitHub markdown.
359360
"""
360361
)
361362

@@ -368,24 +369,24 @@ def get_prompt(command, structured_output=False):
368369
parser = PydanticOutputParser(pydantic_object=ReviewResult)
369370
return PromptTemplate(
370371
template=REVIEW_TEMPLATE + "\n{format_instructions}",
371-
input_variables=["diff_context"],
372+
input_variables=["diff_context", "languages"],
372373
partial_variables={"format_instructions": parser.get_format_instructions()},
373374
output_parser=parser,
374375
)
375376
else:
376377
return PromptTemplate(
377378
template=REVIEW_TEMPLATE + "\nRespond in markdown format",
378-
input_variables=["diff_context"],
379+
input_variables=["diff_context", "languages"],
379380
)
380381

381382
else:
382383
prompt_map = {
383384
"alignment": PromptTemplate(template=ALIGNMENT_TEMPLATE, input_variables=[]),
384-
"commit": PromptTemplate(template=COMMIT_TEMPLATE, input_variables=["diff_context"]),
385-
"debug": PromptTemplate(template=DEBUG_TEMPLATE, input_variables=["command_output"]),
385+
"commit": PromptTemplate(template=COMMIT_TEMPLATE, input_variables=["diff_context", "languages"]),
386+
"debug": PromptTemplate(template=DEBUG_TEMPLATE, input_variables=["command_output", "languages"]),
386387
"fun_fact": PromptTemplate(template=FUN_FACT_TEMPLATE, input_variables=["topic"]),
387388
"sidekick": PromptTemplate(
388-
template=SIDEKICK_TEMPLATE, input_variables=["chat_history", "task", "context"]
389+
template=SIDEKICK_TEMPLATE, input_variables=["chat_history", "task", "context", "languages"]
389390
),
390391
}
391392

0 commit comments

Comments
 (0)