mirror of
https://github.com/trustgraph-ai/trustgraph.git
synced 2026-07-23 20:21:03 +02:00
Formatting complete
This commit is contained in:
parent
acfb6bfaca
commit
cbd103d251
1 changed files with 31 additions and 25 deletions
|
|
@ -23,6 +23,9 @@ class Outputter:
|
||||||
self.just_wrapped = False
|
self.just_wrapped = False
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
|
# Print prefix at start of first line
|
||||||
|
print(self.prefix, end="", flush=True)
|
||||||
|
self.column = len(self.prefix)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||||
|
|
@ -110,9 +113,9 @@ async def question(
|
||||||
output(wrap(question), "\U00002753 ")
|
output(wrap(question), "\U00002753 ")
|
||||||
print()
|
print()
|
||||||
|
|
||||||
# Track last chunk type and accumulated text for current message
|
# Track last chunk type and current outputter for streaming
|
||||||
last_chunk_type = None
|
last_chunk_type = None
|
||||||
current_message = ""
|
current_outputter = None
|
||||||
|
|
||||||
def think(x):
|
def think(x):
|
||||||
if verbose:
|
if verbose:
|
||||||
|
|
@ -172,22 +175,28 @@ async def question(
|
||||||
|
|
||||||
# Check if we're switching to a new message type
|
# Check if we're switching to a new message type
|
||||||
if last_chunk_type != chunk_type:
|
if last_chunk_type != chunk_type:
|
||||||
# When switching message types, flush accumulated message
|
# Close previous outputter if exists
|
||||||
if current_message:
|
if current_outputter:
|
||||||
if last_chunk_type == "thought":
|
current_outputter.__exit__(None, None, None)
|
||||||
think(current_message)
|
current_outputter = None
|
||||||
elif last_chunk_type == "observation":
|
print() # Blank line between message types
|
||||||
observe(current_message)
|
|
||||||
elif last_chunk_type == "answer":
|
# Create new outputter for new message type
|
||||||
print(current_message)
|
if chunk_type == "thought" and verbose:
|
||||||
if not current_message.endswith('\n'):
|
current_outputter = Outputter(width=78, prefix="\U0001f914 ")
|
||||||
print()
|
current_outputter.__enter__()
|
||||||
current_message = ""
|
elif chunk_type == "observation" and verbose:
|
||||||
|
current_outputter = Outputter(width=78, prefix="\U0001f4a1 ")
|
||||||
|
current_outputter.__enter__()
|
||||||
|
# For answer, don't use Outputter - just print as-is
|
||||||
|
|
||||||
last_chunk_type = chunk_type
|
last_chunk_type = chunk_type
|
||||||
|
|
||||||
# Accumulate content for current message type
|
# Output the chunk
|
||||||
current_message += content
|
if current_outputter:
|
||||||
|
current_outputter.output(content)
|
||||||
|
elif chunk_type == "answer":
|
||||||
|
print(content, end="", flush=True)
|
||||||
else:
|
else:
|
||||||
# Handle legacy format (backward compatibility)
|
# Handle legacy format (backward compatibility)
|
||||||
if "thought" in response:
|
if "thought" in response:
|
||||||
|
|
@ -203,16 +212,13 @@ async def question(
|
||||||
raise RuntimeError(response["error"])
|
raise RuntimeError(response["error"])
|
||||||
|
|
||||||
if obj["complete"]:
|
if obj["complete"]:
|
||||||
# Flush any remaining message
|
# Close any remaining outputter
|
||||||
if current_message:
|
if current_outputter:
|
||||||
if last_chunk_type == "thought":
|
current_outputter.__exit__(None, None, None)
|
||||||
think(current_message)
|
current_outputter = None
|
||||||
elif last_chunk_type == "observation":
|
# Add final newline if we were outputting answer
|
||||||
observe(current_message)
|
elif last_chunk_type == "answer":
|
||||||
elif last_chunk_type == "answer":
|
print()
|
||||||
print(current_message)
|
|
||||||
if not current_message.endswith('\n'):
|
|
||||||
print()
|
|
||||||
break
|
break
|
||||||
|
|
||||||
await ws.close()
|
await ws.close()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue