Creating and Using a Bot
Creating and Using a Bot with LongTrainer
This guide will walk you through the steps to create a new bot using the LongTrainer framework, add sample data to it, initialize a chat session, and interact with the bot to get responses.
Step 1: Create a New Bot
First, you'll need to initialize a new bot instance which will generate a unique bot ID. This ID will be used to reference the bot in subsequent operations.
from longtrainer.trainer import LongTrainer
# Initialize the LongTrainer instance
trainer = LongTrainer()
# Create a new bot and obtain its ID
bot_id = trainer.initialize_bot_id()
print('Bot ID: ', bot_id)
Step 2: Add Sample Data to the Bot
Load documents from a specified file path into the bot's memory. This data will be used by the bot to generate responses to queries.
# Specify the path to your data
path = 'path/to/your/data'
# Add documents to the bot
trainer.add_document_from_path(path, bot_id, use_unstructured=True)
This method supports unstructured data, making it versatile for various types of textual input.
Step 3: Initialize the Bot
Configure and initialize the bot with a custom or default prompt template. This template helps guide the conversation and influences the responses generated by the bot.
# Initialize the bot with the Default Trainer`s prompt template
trainer.create_bot(bot_id)
# Define a custom prompt template (optional)
prompt = "Custom prompt template for your specific use case."
# Initialize the bot with the specified prompt template
trainer.create_bot(bot_id, prompt_template=prompt)
Step 4: Start a New Chat Session
Begin a new chat session with the bot. This step involves creating a new chat context that will manage the dialogue between the user and the bot.
# Start a new chat session and get the chat ID
chat_id = trainer.new_chat(bot_id)
Step 5: Interact with the Bot
Send a query to the bot and receive a response. You can optionally enable web search to enrich the bot's responses with external information.
# Define your query
query = 'Your query here'
# Send the query and get the response
response = trainer.get_response(query, bot_id, chat_id, uploaded_files=None, web_search=False)
print('Response: ', response)
This function retrieves responses from the bot, which can include content from uploaded files and web search results, enhancing the depth and relevance of the interaction.
Handling Responses
The response from the bot will be based on the input query, the documents loaded into the bot's memory, and the configuration of the bot itself. The response can be further enriched by setting web_search
to True
or by providing uploaded_files
containing additional context.