coloring fixes

This commit is contained in:
2025-04-03 17:07:37 -04:00
parent 73db5a78e5
commit 51e4d200a7
3 changed files with 49 additions and 16 deletions
+1 -1
View File
@@ -8,7 +8,7 @@ def highlight_code(code: str, language: str = 'py') -> None:
try: try:
lexer = get_lexer_by_name(language) lexer = get_lexer_by_name(language)
except ValueError: except ValueError:
debugMod.log("Warning: Language not recognized. Printing without highlighting.") # debugMod.log("Warning: Language not recognized. Printing without highlighting.")
return code return code
formatter = TerminalFormatter() formatter = TerminalFormatter()
+8 -8
View File
@@ -59,10 +59,10 @@ def create_vector_store(chunks):
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2") embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
vector_store = Chroma.from_texts( vector_store = Chroma.from_texts(
chunks, chunks,
embeddings, embeddings,
persist_directory=Config.chroma_path() persist_directory=Config.chroma_path()
) )
debugMod.log("Vector store created") debugMod.log("Vector store created")
return vector_store return vector_store
@@ -183,14 +183,14 @@ def orchestrate(query, vector_store=None, comm_outp=print, comm_inp=input):
return [final_response, links] return [final_response, links]
if __name__ == "__main__": if __name__ == "__main__":
debugMod.moveDebugLog() debugMod.moveDebugLog()
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('--file', type=str, default="", parser.add_argument('--file', type=str, default="",
help='Path to data file for analysis') help='Path to data file for analysis')
parser.add_argument('--cli', type=str, default="false", parser.add_argument('--cli', type=str, default="false",
help="whether to use the CLI for input or run the API") help="whether to use the CLI for input or run the API")
args = parser.parse_args() args = parser.parse_args()
vector_store = None vector_store = None
@@ -227,7 +227,7 @@ if __name__ == "__main__":
if code_blocks: if code_blocks:
show_thinking('[running code...]') show_thinking('[running code...]')
orchestrate_code(orchestrate, vector_store, chunks, orchestrate_code(orchestrate, vector_store, chunks,
user_env, code_blocks, query, response, links) user_env, code_blocks, query, response, links)
# clean up # clean up
try: try:
+40 -7
View File
@@ -133,6 +133,8 @@ def call_ollama_and_print(task_type, prompt, silent=False):
in_code_block = False in_code_block = False
code_lang = None code_lang = None
first_chunk = True first_chunk = True
code_buffer = ""
prev_highlighted = "" # initialize before processing stream
for chunk in stream: for chunk in stream:
if first_chunk: if first_chunk:
@@ -141,31 +143,62 @@ def call_ollama_and_print(task_type, prompt, silent=False):
print("\nAI Response: ", end="", flush=True) print("\nAI Response: ", end="", flush=True)
content: str = chunk.get('message', {}).get('content', '') content: str = chunk.get('message', {}).get('content', '')
if content == '```' or re.match('```.*', content): debugMod.log(content)
# Detect code block start/end
if content.startswith('```') or re.match(r'^```[a-zA-Z]*$', content):
if in_code_block: if in_code_block:
in_code_block = False in_code_block = False
print() highlighted = highlight_code(code_buffer, code_lang)
buffer += content
# Overwrite the previously highlighted code before exiting the block
if prev_highlighted:
for _ in prev_highlighted.splitlines():
print("\033[F\033[2K", end='') # Move up and clear line
print(f'{highlighted}\n```\n', flush=True)
code_buffer = ""
code_lang = None code_lang = None
prev_highlighted = ""
else: else:
in_code_block = True in_code_block = True
print('\n```')
code_lang = content.replace('```', '').strip() code_lang = content.replace('```', '').strip()
if (len(code_lang) == 0): if (len(code_lang) == 0):
code_lang = "TODO" code_lang = "TODO"
else:
debugMod.log(f'detected language: {code_lang}')
elif code_lang == "TODO": elif code_lang == "TODO":
# last chunk was the backticks, now is lang # Last chunk was the backticks, now is lang
splitVal = content.strip().split() splitVal = content.strip().split()
code_lang = splitVal[0] code_lang = splitVal[0]
debugMod.log(f'detected language: {code_lang}')
if (len(splitVal) > 1 and len(splitVal[1]) > 0): if (len(splitVal) > 1 and len(splitVal[1]) > 0):
hcode = highlight_code(splitVal[1], code_lang) hcode = highlight_code(splitVal[1], code_lang)
print(hcode, end="", flush=True) print(hcode, end="", flush=True)
buffer += hcode code_buffer += content
elif in_code_block:
code_buffer += content
highlighted = highlight_code(code_buffer, code_lang)
for _ in prev_highlighted.splitlines():
print("\033[F\033[2K", end='') # Move cursor up and clear each line
if not content.endswith('\n'):
print(f'{highlighted}', end='', flush=True)
prev_highlighted = highlighted
else:
print(f'{highlighted}', end='\n', flush=True)
prev_highlighted = highlighted + '\n'
else: else:
buffer += content debugMod.log('in normal for', content)
print(content, end="", flush=True) # Normal text handling
print(content, end='', flush=True)
print() # Newline after streaming print() # Newline after streaming
debugMod.log("RAG query response received") debugMod.log("RAG query response received")