Skip to content

Latest commit

 

History

6 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AI Chat Assistant

AI Chat Assistant is a conversational AI application that combines a local large language model with intelligent web search to provide accurate and context-aware responses. Built with LangChain, Ollama, Tavily Search, and Streamlit, the assistant can answer questions using its own knowledge or automatically perform a web search when recent or external information is needed.

Features

  • Conversational AI interface built with Streamlit
  • Local LLM inference using Ollama
  • Intelligent web search with Tavily
  • Automatic tool selection using a LangChain Agent
  • Persistent chat history stored as JSON
  • Start new conversations
  • Load previous chats
  • Delete chat history
  • Displays whether the response came from the local model or web search

System Architecture

The AI Chat Assistant follows a modular architecture where each component has a specific responsibility. The Streamlit interface handles user interactions, the LangChain Agent manages the conversation and decides whether a web search is required, Ollama provides the local language model for reasoning and response generation, Tavily supplies real-time information when needed, and JSON files store chat history for persistent conversations.

                    User
                      │
                      ▼
             Streamlit Interface
                      │
                      ▼
              LangChain Agent
          ┌───────────┼───────────┐
          │           │           │
          ▼           ▼           ▼
     Ollama LLM   Tavily Search  JSON Storage

Request Flow

The following workflow illustrates how the assistant processes each user request. The LangChain Agent first analyzes the conversation history and the current question. If the model already has sufficient knowledge, it generates the response directly using the local Ollama model. If additional or up-to-date information is required, the agent automatically rewrites the search query using the conversation context, sends it to Tavily Search, and passes the retrieved results back to the language model. The model then synthesizes a final response, which is displayed to the user and stored locally for future conversations.

                 User Question
                        │
                        ▼
      Chat History + Current Question
                        │
                        ▼
                LangChain Agent
                        │
        ┌───────────────┴────────────────┐
        │                                │
        │ Answer from Model?             │ Need Web Search?
        │                                │
       Yes                               Yes
        │                                │
        ▼                                ▼
  Ollama Generates Answer       Rewrite Search Query
                                         │
                                         ▼
                                  Tavily Search
                                         │
                                  Search Results
                                         │
                                         ▼
                           Ollama Generates Final Answer
                                         │
                                         ▼
                                Display Response
                                         │
                                         ▼
                           Save Chat History (JSON)

Project Structure

AI-Chat-Assistant/
│
├── app.py               # Streamlit user interface
├── main.py              # LangChain agent and response generation
├── memory.py            # Chat history management
├── history/             # Stores conversation history as JSON files
├── screenshots/         # Project screenshots for the README
├── .env.example         # Environment variable template
├── .gitignore           # Git ignore rules
├── requirements.txt     # Project dependencies
└── README.md            # Project documentation

Technologies Used

Technology Purpose
Python Core programming language
Streamlit Interactive web application interface
LangChain AI agent framework and tool orchestration
Ollama Local LLM inference
Qwen3:8B Large language model for reasoning and response generation
Tavily Search Web search tool for retrieving up-to-date information
python-dotenv Loading environment variables
JSON Persistent storage of chat history

Installation

Follow the steps below to set up the project on your local machine.

1. Clone the Repository

git clone https://github.com/<your-username>/ai-chat-assistant.git
cd ai-chat-assistant

2. Create a Virtual Environment

Windows

python -m venv .venv
.venv\Scripts\activate

Linux / macOS

python3 -m venv .venv
source .venv/bin/activate

3. Install Project Dependencies

pip install -r requirements.txt

4. Install Ollama

Download and install Ollama for your operating system:

https://ollama.com/download

Verify the installation:

ollama --version

5. Download the Required Language Model

Pull the Qwen3 8B model:

ollama pull qwen3:8b

You can verify the downloaded models using:

ollama list

6. Get a Tavily API Key

Create a free account and generate an API key:

https://app.tavily.com/

7. Configure Environment Variables

Create a .env file in the project root directory and add your Tavily API key:

TAVILY_API_KEY=your_tavily_api_key

8. Launch the Application

Start the Streamlit application:

streamlit run app.py

After the application starts, open the URL displayed in the terminal (typically http://localhost:8501) in your web browser.

Screenshots

App Screen

App Screen The application starts with a clean chat interface and a sidebar containing actions to create a new conversation, delete the current chat, and switch between previously saved conversations.

Model Knowledge Response

Answered with model knowledge For general questions, the LangChain Agent determines that web search is unnecessary and generates the response directly using the local Ollama model. The assistant also understands follow-up questions by using the conversation history to maintain context.

Intelligent Web Search

Answered with Tavily Search When a question requires recent or real-time information, the LangChain Agent automatically invokes the Tavily Search tool. The search results are then provided to the local language model, which generates a concise and natural response.

Usage

  1. Launch the application using Streamlit.

  2. Enter a question in the chat input box.

  3. The LangChain Agent analyzes the request and decides whether to:

    • Answer directly using the local Ollama model.
    • Perform a web search using Tavily when up-to-date information is required.
  4. View the response along with its source:

    • Model Knowledge – Response generated using the local language model.
    • Web Search – Response generated using Tavily Search and synthesized by the local language model.
  5. Continue the conversation with follow-up questions. The assistant uses the conversation history to understand context.

  6. Use the sidebar to:

    • Start a new chat.
    • Load previous conversations.
    • Delete the current chat.
  7. Conversations are automatically saved as JSON files and can be reopened in future sessions.

About

A conversational AI assistant built with LangChain, Ollama, Tavily Search, and Streamlit featuring intelligent web search and persistent chat history.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages