This page provides practical code examples for implementing advanced rendering features in chat messages. For broader documentation and concepts, see the Advanced Rendering Documentation.
prompt_button_string=create_prompt_button_string(button_text="Click me",next_user_message="Next user message")chat_service.create_assistant_message(content=f"Here is a prompt button:\n{prompt_button_string}",)
latex_formula_string=create_latex_formula_string(latex_expression=r"\int_{a}^{b} f(x) \, dx")chat_service.create_assistant_message(content=f"Here is a latex formula: {latex_formula_string}",)
# Complex formula with inline mathcontent=f"The integral of a function is given by \[\int_{a}^{b} f(x) \, dx\]."chat_service.create_assistant_message(content=content)
# Example: Rendering an image that was uploaded to the chat# The content_id is obtained from the uploaded imagecontent_id="cont_nwnfwd7kq5czq04begyb6ub8"# Example content IDimage_markdown=f""chat_service.create_assistant_message(content=f"This is an image that I took from a note:\n{image_markdown}",)
# Download images and documents from the chatimages,documents=chat_service.download_chat_images_and_documents()iflen(images)>0:# Use the first uploaded image's content IDcontent_id=images[0].idimage_markdown=f""chat_service.create_assistant_message(content=f"Here's the image you uploaded:\n{image_markdown}",)
importjsonfromdatetimeimportdatetime,timezone# Example: Creating a financial chart payloadfinancial_data=[{"info":{"companyName":"Apple","instrumentName":"Apple Rg","ticker":"AAPL","exchange":"NASDAQ","currency":"USD"},"priceHistory":[{"date":"2025-01-02","value":243.85},{"date":"2025-01-03","value":245.12},{"date":"2025-01-04","value":244.50},{"date":"2025-01-05","value":246.20},# ... more price history entries],"metrics":[{"name":"Open","value":221.45,"timestamp":"2025-03-27T09:30:01-04:00"},{"name":"High","value":225.30,"timestamp":"2025-03-27T09:30:01-04:00"},{"name":"Close","value":223.10,"timestamp":"2025-03-27T09:30:01-04:00"},# ... more metrics],"lastUpdated":"2025-03-28T16:10:09.243846","version":1}]# Format as financialchart code blockfinancial_chart_markdown=f"```financialchart\n{json.dumps(financial_data,indent=2)}\n```"chat_service.create_assistant_message(content=f"Here is the stock performance:\n{financial_chart_markdown}",)
importjson# Multiple instruments for comparative viewfinancial_data=[{"info":{"companyName":"Apple","instrumentName":"Apple Rg","ticker":"AAPL","exchange":"NASDAQ","currency":"USD"},"priceHistory":[{"date":"2025-01-02","value":243.85},{"date":"2025-01-03","value":245.12},# ... more entries],"metrics":[{"name":"Open","value":221.45,"timestamp":"2025-03-27T09:30:01-04:00"},# ... more metrics],"lastUpdated":"2025-03-28T16:10:09.243846","version":1},{"info":{"companyName":"Microsoft","instrumentName":"Microsoft Corp","ticker":"MSFT","exchange":"NASDAQ","currency":"USD"},"priceHistory":[{"date":"2025-01-02","value":415.20},{"date":"2025-01-03","value":417.50},# ... more entries],"metrics":[{"name":"Open","value":410.00,"timestamp":"2025-03-27T09:30:01-04:00"},# ... more metrics],"lastUpdated":"2025-03-28T16:10:09.243846","version":1}]financial_chart_markdown=f"```financialchart\n{json.dumps(financial_data,indent=2)}\n```"chat_service.create_assistant_message(content=f"Here is a comparison of stock performance:\n{financial_chart_markdown}",)
# %%fromunique_toolkitimport(ChatService,KnowledgeBaseService,)fromunique_toolkit.app.dev_utilimportget_event_generatorfromunique_toolkit.app.schemasimportChatEventfromunique_toolkit.app.unique_settingsimportUniqueSettingsfromunique_toolkit.chat.renderingimport(create_prompt_button_string,)settings=UniqueSettings.from_env_auto_with_sdk_init()foreventinget_event_generator(unique_settings=settings,event_type=ChatEvent):# Initialize services from eventchat_service=ChatService(event)kb_service=KnowledgeBaseService.from_event(event)prompt_button_string=create_prompt_button_string(button_text="Click me",next_user_message="Next user message")chat_service.create_assistant_message(content=f"Here is a prompt button:\n{prompt_button_string}",)chat_service.free_user_input()
# %%fromunique_toolkitimport(ChatService,KnowledgeBaseService,)fromunique_toolkit.app.dev_utilimportget_event_generatorfromunique_toolkit.app.schemasimportChatEventfromunique_toolkit.app.unique_settingsimportUniqueSettingsfromunique_toolkit.chat.renderingimport(create_latex_formula_string,)settings=UniqueSettings.from_env_auto_with_sdk_init()foreventinget_event_generator(unique_settings=settings,event_type=ChatEvent):# Initialize services from eventchat_service=ChatService(event)kb_service=KnowledgeBaseService.from_event(event)latex_formula_string=create_latex_formula_string(latex_expression=r"\int_{a}^{b} f(x) \, dx")chat_service.create_assistant_message(content=f"Here is a latex formula: {latex_formula_string}",)chat_service.free_user_input()