- Introduction
- Setup and Installation
- Configuration
- How to Use
- Color-Coded Terminal Output
- Error Handling
- Logging
- FAQ
The Terminal Researcher is a terminal-based chat application that interacts with the Perplexica REST API to provide answers to user queries. It also logs chat history and any external sources referenced in the responses. You need to get Perplexica up and running as a prequisite. This guide walks you through the setup, usage, and detailed functionality of the application.
- Python 3.8 or higher
- Ollama and Perplexica installed locally in docker environment to communicate with the API
- Anaconda installed on your system (if using a Conda environment)
To set up the project locally, clone the repository:
git clone https://github.com/Teachings/terminal-researcher.git
cd terminal-researcherIf using Conda, ensure the terminal-researcher environment is active, and install dependencies:
conda create --name terminal-researcher python=3.11 pip
conda activate terminal-researcherInstall the required dependencies:
pip install -r requirements.txtThe application requires a config.json file to be present in the root directory. Below is an example configuration:
{
"base_url": "http://localhost:3001",
"timeout": 60,
"history_limit": 3,
"chat_model": {
"provider": "ollama",
"model": "marco-o1:latest"
},
"embedding_model": {
"provider": "ollama",
"model": "nomic-embed-text:latest"
},
"optimization_mode": "balanced",
"focus_mode": "webSearch"
}- base_url: The URL of the Perplexica API.
- timeout: The timeout in seconds for each API request.
- chat_model: Configuration for the chat model.
- embedding_model: Configuration for the embedding model.
- optimization_mode: Controls the speed vs. quality trade-off.
- focus_mode: The context mode for the API query.
-
Run the Application
Activate the
terminal-researcherConda environment, navigate to the project directory, and run:python chat.py
-
Interact with the Assistant
The assistant will greet you and wait for your query:
Assistant: Hi there! How can I assist you today? -
Type Queries
Type your query and hit Enter:
You: What happened to DJT yesterday? -
View Results
The assistant responds with the query result and referenced sources (if any):
Assistant: Thinking... [Response took 3 seconds] Assistant: 5 sources referenced. - DJT stock skyrockets following Donald Trump's ...: https://finance.yahoo.com/news/... - Trump Media shares halted repeatedly as DJT whipsaws ...: https://www.nbcnews.com/business/... -
Exit the Chat
Type
exitorquitto end the session:You: exit Ending chat. Goodbye!
-
Set Up the
trCommandCreate a script named
trin the project directory (it already exists if you have cloned the repo, ensure that the path is correct):nano ~/dev-ai/terminal-researcher/trAdd the following content:
#!/bin/bash # Activate the Conda environment source ~/anaconda3/etc/profile.d/conda.sh conda activate terminal-researcher # Navigate to the project directory cd ~/dev-ai/terminal-researcher # Run the Python script with passed arguments python chat.py "$@"
Replace
~/anaconda3with your Anaconda installation path. -
Make the Script Executable
chmod +x ~/dev-ai/terminal-researcher/tr -
Add the Script to Your PATH (Optional)
To use the
trcommand from anywhere, add the project directory to your PATH:echo 'export PATH="$HOME/dev-ai/terminal-researcher:$PATH"' >> ~/.bashrc source ~/.bashrc
-
Usage Examples
-
Run a Query Directly:
tr "Who is DJT?" -
Enter Interactive Chat:
tr
-
- User Input: Displayed in green.
- Assistant Messages: Displayed in blue.
- Referenced Sources: Displayed in yellow with cyan URLs.
- Errors: Displayed in red.
-
Timeouts: If the server does not respond within the configured timeout, the following message is displayed:
Assistant: [No response received within the timeout period] -
HTTP Errors: If the server returns an error, the following message is displayed:
Assistant: [HTTP error: 404 Client Error: Not Found] -
Connection Errors: If the API is unreachable, the following message is displayed:
Assistant: [An error occurred. Please try again later.]
The application generates two log files:
-
activity.log: Stores information about API requests, responses, and errors.
Example:
2024-10-30 14:25:05 - INFO - Query sent: What happened to DJT yesterday? 2024-10-30 14:25:08 - INFO - Sources referenced: 5 -
chat_history.log: Stores the chat history between the user and the assistant.
Example:
[2024-10-30 14:25:05] Human: Hello! [2024-10-30 14:25:08] Assistant: Hi there! How can I assist you today?
Modify the timeout value in the config.json file:
"timeout": 120The application will display an error message and log the error in activity.log.