Markdown stream #3287
-
Hello, I am trying to print a stream from an ai api directly to the console and render it as markdown. The code I tries looks like this: raw_text = ""
try:
for chunk in client.chat_stream(
model="mistral-medium",
messages=[
ChatMessage(
role="system",
content=f"Answer the user's questions based on the below context. If the context doesn't contain any relevant information to the question, don't make something up and just say 'I don't know': <context>${combined_string}</context>",
),
ChatMessage(role="user", content=query),
],
):
if chunk.choices[0].delta.content is not None:
# print(chunk.choices[0].delta.content, end="")
raw_text += chunk.choices[0].delta.content
md = Markdown(raw_text)
console.print(md, end="\r")
except Exception as e:
print(f"Error: {e}") However, this produces a new line after each received chunk: Is there any way to not create a new line and rather overwrite the current text? Thanks and kind regards |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
You can use Live, but for something like this you may be better off using Textual |
Beta Was this translation helpful? Give feedback.
-
This project does something similar. https://github.com/darrenburns/elia Suggest you join the Discord server if you have any questions. |
Beta Was this translation helpful? Give feedback.
You can use Live, but for something like this you may be better off using Textual