Chat¶
The Chat module provides utility functions for LLM conversations.
Chat Functions¶
chat
¶
Basic Usage¶
from virtualitics_sdk.llm import Chat
chat = Chat(
id="my_chat",
title="AI Assistant",
system_prompt="You are a helpful assistant specializing in data analysis."
)
System Prompts¶
Define the AI's role and behavior:
# Data analysis assistant
data_chat = Chat(
id="data_assistant",
title="Data Analysis Assistant",
system_prompt="""
You are an expert data analyst. Help users understand their data by:
- Providing clear explanations of statistical concepts
- Suggesting appropriate visualizations
- Identifying patterns and insights
- Recommending analysis techniques
"""
)
# Code helper
code_chat = Chat(
id="code_helper",
title="Code Helper",
system_prompt="""
You are a Python programming expert. Help users with:
- Writing efficient pandas/numpy code
- Debugging errors
- Optimizing performance
- Following best practices
"""
)
Context-Aware Chat¶
Provide app-specific context:
class AnalysisStep(Step):
def run(self, flow_metadata):
# Get current data
df = self._inLink.dataset.data
# Create context-aware system prompt
columns_info = ", ".join(df.columns)
row_count = len(df)
system_prompt = f"""
You are analyzing a dataset with {row_count} rows and the following columns:
{columns_info}
Help the user understand and analyze this data.
"""
chat = Chat(
id="context_chat",
title="Data Assistant",
system_prompt=system_prompt
)
return Page(
title="Analysis",
sections=[
Section(
title="Chat",
cards=[Card(title="Assistant", content=[chat])]
)
]
)
Message History¶
Access chat history in actions:
def action(self, flow_metadata):
chat = self.page.get_element_by_id("my_chat")
# Get message history
messages = chat.get_messages()
# Process messages
for msg in messages:
role = msg["role"] # "user" or "assistant"
content = msg["content"]
timestamp = msg["timestamp"]
# Store history for later steps
self._outLink.chat_history = messages
return Page(...)
Custom LLM Parameters¶
Configure LLM behavior:
chat = Chat(
id="custom_chat",
title="Customized Assistant",
system_prompt="You are a helpful assistant.",
temperature=0.7, # Creativity (0.0 to 1.0)
max_tokens=500, # Response length limit
top_p=0.9 # Nucleus sampling
)
Best Practices¶
- Clear System Prompts: Be specific about the AI's role and capabilities
- Provide Context: Include relevant app/data context in prompts
- Limit Scope: Focus the AI on specific tasks or domains
- User Guidance: Provide example prompts or questions
- Error Handling: Handle cases where LLM is unavailable
See Also¶
- LLM Overview
- Agent
- App - App-level LLM configuration