Chats and prompting¶
Chats provide a way to interact with LLMs through prompts, maintaining conversation history and context. Chats allow you to have multi-turn conversations with your LLM applications, where each prompt can reference previous messages in the conversation.
Create a chat¶
Create a new chat from an LLM blueprint. When creating a chat, you should specify the following:
name: A user-friendly name for the chat.llm_blueprint: The LLM blueprint ID orLLMBlueprintobject to associate the chat with.
import datarobot as dr
blueprint = dr.genai.LLMBlueprint.get(blueprint_id)
chat = dr.genai.Chat.create(
name="Customer Support Chat",
llm_blueprint=blueprint_id
)
chat
Submit prompts to a chat¶
Send prompts and receive responses using datarobot.ChatPrompt.submit():
chat = dr.genai.Chat.get(chat_id)
prompt = dr.genai.ChatPrompt.create(
chat=chat.id,
text="What is your return policy?"
)
prompt.result_text
Submit a follow-up prompt that maintains the conversation history:
followup = dr.genai.ChatPrompt.create(
chat=chat.id,
text="How long does it take to process a return?"
)
print(f"Response: {followup.result_text}")
Retrieve chat history¶
Get all prompts in a chat using datarobot.ChatPrompt.list():
chat = dr.genai.Chat.get(chat_id)
prompts = dr.genai.ChatPrompt.list(chat=chat.id)
for prompt in prompts:
prompt.text
prompt.result_text
Filter prompts by LLM blueprint:
blueprint = dr.genai.LLMBlueprint.get(blueprint_id)
blueprint_prompts = dr.genai.ChatPrompt.list(llm_blueprint=blueprint.id)
Filter prompts by playground:
playground = dr.genai.Playground.get(playground_id)
playground_prompts = dr.genai.ChatPrompt.list(playground=playground.id)
Manage chats¶
List all chats:
all_chats = dr.genai.Chat.list()
print(f"Found {len(all_chats)} chat(s):")
for chat in all_chats:
print(f" - {chat.name} (ID: {chat.id})")
Filter by LLM blueprint:
blueprint = dr.genai.LLMBlueprint.get(blueprint_id)
blueprint_chats = dr.genai.Chat.list(llm_blueprint=blueprint.id)
Update the chat name:
chat = dr.genai.Chat.get(chat_id)
chat.update(name="Updated Chat Name")
Delete a chat:
chat.delete()
Get chat information¶
Retrieve chat details:
chat = dr.genai.Chat.get(chat_id)
print(f"Name: {chat.name}")
print(f"LLM Blueprint: {chat.llm_blueprint_id}")
print(f"Is frozen: {chat.is_frozen}")
print(f"Prompts count: {chat.prompts_count}")
print(f"Warning: {chat.warning}")