Skip to content

Commit cd864ca

Browse files
committed
Fix Gemini API compatibility and enhance Rich formatting
1 parent e989513 commit cd864ca

File tree

3 files changed

+77
-9
lines changed

3 files changed

+77
-9
lines changed

cli/ai_model_manager.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ def _write_json(self, data):
5757
def create_default_file(self):
5858
"""
5959
Creates a configuration file with default model entries.
60-
Includes a pre-set API key for `gemini-1.5-flash`.
60+
Includes a pre-set API key for `gemini-2.5-flash`.
6161
"""
6262
default_config = {
6363
"selected_model": None,
64-
"gemini-1.5-flash": "AIzaSyCSXtRAITXfGuarMHI1j-0QyKkoT9mUfz8",
65-
"gemini-1.5-interactive": None,
66-
"gemini-1.5-creative": None,
64+
"gemini-2.5-flash": "AIzaSyCRVaCSsM8B4_C10aCRBpKB_ODncP64Pmk",
65+
"gemini-2.0-flash": None,
66+
"gemini-flash-latest": None,
6767
}
6868
self._write_json(default_config)
6969
print(f"Config file created at: {self.file_path}")

cli/llm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def generate_response(prompt: str, manager: AIModelManager, history: ChatHistory
3939
models = manager.load()
4040
selected = models.get("selected_model")
4141
if not selected:
42-
selected = "gemini-1.5-flash"
42+
selected = "gemini-2.5-flash"
4343
response = manager.generate_output(selected, flat_prompt)
4444
history.append("assistant", response or "")
4545
prettify_llm_output(response)

cli/prettify_llm_output.py

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,91 @@
1+
import re
12
from rich import print
23
from rich.console import Console
34
from rich.markdown import Markdown
5+
from rich.syntax import Syntax
46

57

68
def prettify_llm_output(response):
79
"""
8-
Prettifies the output from a language model response by stripping leading
9-
and trailing whitespace and code block markers, then prints it as Markdown
10-
to the console.
10+
Prettifies the output from a language model response by processing it
11+
as Markdown with enhanced code block detection and syntax highlighting.
1112
1213
Args:
1314
response (str): The raw response from the language model.
1415
1516
Returns:
1617
None
1718
"""
18-
markdown_output = response.strip().strip("```")
1919
console = Console()
20+
21+
# Clean up the response
22+
markdown_output = response.strip()
23+
24+
# Check if the entire response is just code without markdown formatting
25+
if _is_likely_code(markdown_output) and not _has_markdown_elements(markdown_output):
26+
# Treat the entire response as Python code
27+
syntax = Syntax(markdown_output, "python", theme="monokai", line_numbers=False)
28+
print()
29+
console.print(syntax)
30+
print()
31+
return
32+
33+
# Check for code blocks and ensure they have language specification
34+
markdown_output = _enhance_code_blocks(markdown_output)
35+
36+
# Render as markdown
2037
md = Markdown(markdown_output)
2138
print()
2239
console.print(md)
2340
print()
41+
42+
43+
def _is_likely_code(text):
44+
"""Check if text looks like Python code."""
45+
python_keywords = [
46+
"import",
47+
"def",
48+
"class",
49+
"if",
50+
"for",
51+
"while",
52+
"try",
53+
"except",
54+
"plt.",
55+
]
56+
lines = text.split("\n")
57+
code_indicators = 0
58+
59+
for line in lines:
60+
line = line.strip()
61+
if any(keyword in line for keyword in python_keywords):
62+
code_indicators += 1
63+
if line.startswith("#"): # Comments
64+
code_indicators += 1
65+
66+
return code_indicators >= 2 or any(
67+
keyword in text for keyword in ["plt.", "matplotlib", "import"]
68+
)
69+
70+
71+
def _has_markdown_elements(text):
72+
"""Check if text contains markdown elements."""
73+
markdown_indicators = ["#", "**", "*", "`", ">", "-", "1.", "[", "]"]
74+
return any(indicator in text for indicator in markdown_indicators)
75+
76+
77+
def _enhance_code_blocks(text):
78+
"""Add language specification to code blocks that don't have it."""
79+
# Pattern to find code blocks without language specification
80+
pattern = r"```\n(.*?)\n```"
81+
82+
def replace_code_block(match):
83+
code_content = match.group(1)
84+
if _is_likely_code(code_content):
85+
return f"```python\n{code_content}\n```"
86+
return match.group(0)
87+
88+
# Replace code blocks without language specification
89+
enhanced = re.sub(pattern, replace_code_block, text, flags=re.DOTALL)
90+
91+
return enhanced

0 commit comments

Comments
 (0)