**Local AI Agent with web access in less than 30 lines of Python Code (Step-by-Step Guide)** 

<figure class="">
  <img src="/assets/img/blog/2025-01-03-build-an-ai-investment-agent-with-chainlit-and-phidata/header_img.webp"
       alt="Chat with Earnings Reports"><figcaption>
      Generated with Grok

    </figcaption></figure>


The financial industry has always been data-driven. Large Language Models can help you access this data more effectively. For instance, an AI-powered investment chatbot can assist you in making better investment decisions.

In this tutorial, we'll show you how to build a local AI Investment Agent using Llama 3.1. You can ask this chatbot questions about investment-related topics and get accurate answers.

<div class="ad-banner" style="margin-bottom: 0.7rem;">
    <hr class="hr-text" data-content="Advertisement*">
    <a href="/out/elevenlabs/" target="_blank" rel="sponsored nofollow noopener"><img src="../../assets/img/ads/elevenlabs.webp" alt="ElevenLabs Partner*" nopin="nopin"></a>
    <small style="display: block; margin-bottom: 0.5rem; margin-top: 0.5rem;"><strong>✨ Read without banner ads? </strong><a href="https://steady.page/en/tinztwins-hub/about" target="_blank" rel="noopener">Become a member</a> or <a href="https://steady.page/en/log_in?publication=tinztwins-hub" target="_blank" rel="noopener">log in</a></small>
</div>

## Sneak Peak







<!-- Courtesy of embedresponsively.com //-->

  <div class="responsive-video-container">
    <iframe data-name="youtube" data-src="https://www.youtube-nocookie.com/embed/7Wet5V5JQiA" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowfullscreen></iframe>
  </div>



## Tech Stack
The chatbot uses the `Llama 3.1:8b` LLM model and provides precise answers based on specific user questions. 

For this, we use:
* [Chainlit](https://docs.chainlit.io/get-started/overview){:target="_blank" rel="noopener"} as an intuitive user-interface
* [phidata](https://docs.phidata.com/introduction){:target="_blank" rel="noopener"} framework for the Agent functionality
* yfinance for real-time investment data
* duckduckgo for web access functionality

<div class="ad-banner" style="margin-bottom: 0.7rem;">
    <hr class="hr-text" data-content="Advertisement*">
    <a href="/out/stocksguide/" target="_blank" rel="sponsored nofollow noopener"><img src="../../assets/img/ads/stocksguide.webp" alt="StocksGuide Partner*" nopin="nopin"></a>
    <small style="display: block; margin-bottom: 0.5rem; margin-top: 0.5rem;"><strong>✨ Read without banner ads? </strong><a href="https://steady.page/en/tinztwins-hub/about" target="_blank" rel="noopener">Become a member</a> or <a href="https://steady.page/en/log_in?publication=tinztwins-hub" target="_blank" rel="noopener">log in</a></small>
</div>

## Prerequisites
You will need the following prerequisites:
* Python package manager of your choice like [conda](https://docs.conda.io/en/latest/miniconda.html){:target="_blank" rel="noopener"}.
* A code editor of your choice like [Visual Studio Code](https://code.visualstudio.com/){:target="_blank" rel="noopener"}.
* Download [Ollama](https://ollama.com){:target="_blank" rel="noopener"} and install [Llama3.1:8b](https://ollama.com/library/llama3.1:8b){:target="_blank" rel="noopener"}. Make sure that it runs on your computer.

<div class="ad-banner" style="margin-bottom: 0.7rem;">
    <hr class="hr-text" data-content="Explore our premium blog articles">
    <a href="https://tinztwinshub.com/membership"><img src="../../assets/img/ads/premium_1.webp" alt="Explore our premium blog articles" nopin="nopin"></a>
    <small style="display: block; margin-bottom: 0.5rem; margin-top: 0.5rem;"><strong>✨ Read without banner ads? </strong><a href="https://steady.page/en/tinztwins-hub/about" target="_blank" rel="noopener">Become a member</a> or <a href="https://steady.page/en/log_in?publication=tinztwins-hub" target="_blank" rel="noopener">log in</a></small>
</div>

## Step-by-Step Guide
### Step 1: Setup the development environment
* **Create a conda environment**: It makes sense to create a virtual environment to keep your main system clean.

```bash
conda create -n investment-agent python=3.12.7
conda activate investment-agent
```

* **Clone the GitHub repo**:

```bash
git clone https://github.com/tinztwins/finllm-apps.git
```

* **Install requirements**: Go to the folder `investment-agent` and execute the following command:

```bash
pip install -r requirements.txt
```

* Make sure that Ollama is running on your system:

![Screenshot: Is Ollama running?](../../assets/img/blog/2025-01-03-build-an-ai-investment-agent-with-chainlit-and-phidata/ollama_check.webp)

<div class="ad-banner" style="margin-bottom: 0.7rem;">
    <hr class="hr-text" data-content="Advertisement*">
    <a href="/out/stocksguide/" target="_blank" rel="sponsored nofollow noopener"><img src="../../assets/img/ads/stocksguide.webp" alt="StocksGuide Partner*" nopin="nopin"></a>
    <small style="display: block; margin-bottom: 0.5rem; margin-top: 0.5rem;"><strong>✨ Read without banner ads? </strong><a href="https://steady.page/en/tinztwins-hub/about" target="_blank" rel="noopener">Become a member</a> or <a href="https://steady.page/en/log_in?publication=tinztwins-hub" target="_blank" rel="noopener">log in</a></small>
</div>

### Step 2: Create the Chainlit App
* **Import required libraries**: First, we import chainlit and phidata.

```python
import chainlit as cl
from phi.agent import Agent
from phi.tools.yfinance import YFinanceTools
from phi.tools.duckduckgo import DuckDuckGo
from phi.model.ollama import Ollama
```

* **Start a new chat session**: Every Chainlit app follows a [life cycle](/software-engineering/build-a-local-chatbot-in-minutes-with-chainlit/#chat-life-cycle). When a user opens your Chainlit app, a new chat session is created. The `on_chat_start()` function runs when a new chat session starts. The user session is designed to persist data in memory. So, we can store the agent object in the user session with the command `cl.user_session.set("agent", agent)`.

```python
@cl.on_chat_start
async def on_chat_start():
    # Agent Code   
    # ...
    cl.user_session.set("agent", agent)
```
* **Create an Agent**: In phidata you create a new Agent object with `Agent(...)`. You can pass this object several parameters, e.g. model, tools, description, and instructions. We use Llama3.1 as a Large Language Model. It provides reasoning and planning capabilities to the Agent. 

* **Use Tools**: Tools are functions that an Agent can use to take action and interact with external systems. We use YFinanceTools for real-time investment information and DuckDuckGo for web access. You can use any Python function as a tool. 

* **Description and Instructions**: With the description Parameter, you can guide the overall behavior of the agent. In addition, you can provide a list of clear, task-specific instructions to help it achieve its goals.

```python
# Agent Code

agent = Agent(model=Ollama(id="llama3.1:8b"), 
              tools=[YFinanceTools(stock_price=True, 
                                   company_info=True, 
                                   stock_fundamentals=True, 
                                   analyst_recommendations=True,
                                   historical_prices=True), 
                    DuckDuckGo()],
              description="You are an investment analyst that researches stock prices, company infos, stock fundamentals, analyst recommendations and historical prices",
              instructions=["Format your response using markdown and use tables to display data where possible."],)
```

* **New message from the user**: The `on_message(message: cl.Message)` function is called when a new message from the user is received. The LLM processes the message and returns a response. The user's message is sent to the agent, where it is processed. The agent's response is then sent back to the chat UI.

```python
@cl.on_message
async def on_message(message: cl.Message):
    agent = cl.user_session.get("agent")

    msg = cl.Message(content="")
    for chunk in await cl.make_async(agent.run)(message.content, stream=True):
        await msg.stream_token(chunk.get_content_as_string())
    
    await msg.send()
```

ℹ️ Learn more about building Conversational AI Apps with Chainlit in our [introduction article on Chainlit](/software-engineering/build-a-local-chatbot-in-minutes-with-chainlit/).

### Step 3: Run the App

* **Start the Chainlit App**: Navigate to the project folder and run the following command:

```bash
chainlit run app.py
```

* **Access the Chatbot App**: Open `http://localhost:8000` in your browser, and ask investment-related questions.

## Conclusion
In this tutorial, you have successfully built a local AI-powered Investment Chatbot using Llama 3.1. Phidata is a powerful Framework to build agents with tools, memory, knowledge, and reasoning.

Many systems will be powered by AI agents in the future. For this reason, it makes sense for you to understand this today. You can use this example project as a starting point for your next project. 

Happy coding!

<div class="ad-banner">
  <hr>
  💡 Do you enjoy our content and want to read super-detailed guides about AI Engineering? If so, be sure to check out our premium offer!

  <div style="text-align: center; margin-top: 0.7rem;">
    <a href="https://steady.page/en/tinztwins-hub/about" class="btn btn--primary">Unlock Premium</a>      
  </div>
</div>

<div class="ad-banner">
    <hr class="hr-text" data-content="Our Merch Shop">
    <a href="https://shop.tinztwinshub.com/merch/"><img src="../../assets/img/ads/ai_and_coding_merch.webp" alt="AI and Coding Merch" nopin="nopin"></a>
    <small style="display: block; margin-bottom: 0.5rem; margin-top: 0.5rem;"><strong>✨ Read without banner ads? </strong><a href="https://steady.page/en/tinztwins-hub/about" target="_blank" rel="noopener">Become a member</a> or <a href="https://steady.page/en/log_in?publication=tinztwins-hub" target="_blank" rel="noopener">log in</a></small>
</div>