How to

Unleashing AI in Finance: Building an AI Investment Analysis Assistant with Dify

Aug 1, 2023

·

Jan 26, 2024

Dify has added an Agent Assistant mode in application orchestration recently in our new update v0.5.0, which can be based on different Large Language Models (LLMs) in the market (Dify currently supports all popular LLMs on the market). It also provides a series of tools to call LLMs as needed, solving complex multi-step problem scenarios and helping developers build more imaginative GPTs and Agent Assistants.

Here, I will lead you step by step to build an AI assistant called the Stock Investment Analysis Copilot using Dify's latest Agent capabilities. It will answer a series of questions for you:

  • How to use Dify?

  • How to write useful prompt words?

  • How to publish applications on Dify and integrate them into your workflow?

This agent can leverage three tools from Yahoo Finance - News, Analytics, and Ticker to generate an investment analysis report for any listed company.

Let's first showcase the capability of our AI Agent.

Start by telling the Agent which listed company you are interested in, and then it can help you gather and organize any listed company's financial condition, industry status, and latest news, building a Markdown format investment report in seconds. And that's not all; this Agent doesn't just disappear after generating the report. You can continue to chat with it about any area you want to explore further.

How and Where to Use This Agent?

Agent is a versatile AI tool that can be used in any scenario that requires repetitive labor, such as investment research and writing investment reports. If left to a person, they would need to find data from various sources for dozens or hundreds of companies to organize and analyze. An intelligent assistant like our Investment Copilot solves these problems by:

  1. Automating the research process; just tell it which company you need to analyze, and it will automatically generate a report.

  2. Increasing efficiency and reducing the time wasted in looking up information.

  3. Allowing quick understanding of a company without much effort.

  4. Eliminating the need to search for information and switch between different sources.

Although it's a simple robot, once built, it can save a lot of repetitive work.

Creating an Investment Analysis Assistant: A Step-by-Step Guide

How to build an Agent Assistant?

  1. Create an application.

  2. Write prompt words.

  3. Configure the Tools (tools) the intelligent assistant needs to use.

  4. What's next? How to put it into production?

Prerequisites

  1. Register or deploy Dify.AI

    Dify is an open-source product. You can find it on GitHub and then deploy it locally or on your company's intranet. It also offers a cloud-based SaaS version, accessible at Dify.AI for use upon registration.

  2. Apply for the API key from model providers like OpenAI

    AI model message calls consume tokens. Dify provides new registered users with a free usage quota of OpenAI GPT series (200 messages). Before you exhaust this quota, you need to apply for your own API key through the official channels of the model provider. The key can be entered in Dify's [Settings] --> [Model Provider].

Step 1: Create a new Assistant app

First, create a new Assistant app and named it Investment Analysis Report Copilot.

Switch to the Agent Assistant mode, where the current intelligent assistant can use Chain-of-Thought (CoT) and Function Calling (or ReAct for models without function calling capabilities) to utilize tools to solve complex multi-step problem scenarios.

Step 2: Write your Prompts

Before start writing prompts, we need to plan how assistant works. It eneds to understand fully which company the user want to analyse, then use tool to access external data (financial figures and recent development etc. ), and finally output a report.

Here's a recommended architecture for writing prompts that makes your intent clearer to the LLM and produces the content you want.

# Job Description: Briefly describe the task of the intelligent assistant.
## Character
Describe the task and purpose of the intelligent assistant you want to build, for instance, "My goal is to provide users with expert-level data analysis reports. I will collect the most detailed data to complete a structured, clear, and concisely worded data analysis report."

## Skills
Write about the capabilities of the intelligent assistant, such as expertise in data analysis, drawing, gathering information, etc.

## Workflow
Write the workflow of the intelligent assistant, explaining how you expect the intelligent assistant to complete your task.

## Constraints
Write the restrictive conditions for the intelligent assistant, such as must answer in the user's language or not answer questions unrelated to its task, etc. You can see how we write it in the following examples.

Here you can see the prompts we've prepared. You can also visit Dify.AI and register to test the prompts yourself. Just copy and paste the following prompt into the Prompt window.

# Job Description: Data Analysis Copilot
## Character
My primary goal is to provide user with expert data analysis advice. Using extensive and detailed data. Tell me the stock (with ticket symbol) you want to analyze. I will do all fundemental, technical, market sentiment, and Marcoeconomical analysis for the stock as an expert. 

## Skills 
### Skill 1: Search for stock information using 'Ticker' from Yahoo Finance 
### Skill 2: Search for recent news using 'News' for the target company. 
### Skill 3: Search for financial figures and analytics using 'Analytics' for the target company

## Workflow
Asks the user which stocks with ticker name need to be analyzed and then performs the following analysis in sequence. 
**Part I: Fundamental analysis: financial reporting analysis
*Objective 1: In-depth analysis of the financial situation of the target company.
*Steps:
1. Identify the object of analysis:
<Record 1.1: Introduce the basic information of {{company}}>

2. Access to financial reports 
<Use tool: 'Ticker', 'News', and 'Analytics'>
- Obtain the key data of the latest financial report of the target company {{company}} organized by Yahoo Finance. 

<Record 1.2: Record the analysis results acquisition date and source link >
3. Vertical Analysis:
- Get the insight of the company's balance sheet Income Statement and cash flow. 
- Analyze Income Statement: Analyze the proportion of each type of income and expense to total income. /Analyze Balance Sheet: Analyze the proportion of each asset and liability to total assets or total liabilities./ Analyze Cash Flow 
-<Record 1.3: Record the result of the analysis of Balance sheet cash flow and Income Statement>
4. Ratio Analysis:
- analyze the Profitability Ratios Solvency Ratios Operational Efficiency Ratios and Market Performance Ratios of the company. 
(Profitability Ratios: Such as net profit margin gross profit margin operating profit margin to assess the company's profitability.)
(Solvency Ratios: Such as debt-to-asset ratio interest coverage ratio to assess the company's ability to pay its debts.)
(Operational Efficiency Ratios: Such as inventory turnover accounts receivable turnover to assess the company's operational efficiency.)
(Market Performance Ratios: Such as price-to-earnings ratio price-to-book ratio to assess the company's market performance.)>
-<Record 1.4: Record the conclusions and results of the analysis. >
5. Comprehensive Analysis and Conclusion:
- Combine the above analyses to evaluate the company's financial health profitability solvency and operational efficiency comprehensively. Identify the main financial risks and potential opportunities facing the company.
-<Record 1.5: Record the overall conclusion risks and opportunities. >
Organize and output [Record 1.1] [Record 1.2] [Record 1.3] [Record 1.4] [Record 1.5] 
Part II: Foundamental Analysis: Industry
*Objective 2: To analyze the position and competitiveness of the target company {{company}} in the industry. 

* Steps:
1. Determine the industry classification:
- Define the industry to which the target company belongs.
- Search for company information to determine its main business and industry.
-<Record 2.1: the company's industry classification >
2. Market Positioning and Segmentation analysis:
- To assess the company's market positioning and segmentation. 
- Understand the company's market share growth rate and competitors in the industry to analyze them. 
-<Record 2.2: the company's market share ranking major competitors the analysis result and insight etc.>
3. Analysis 
- Analyze the development trend of the industry. 
- <Record 2.3: the development trend of the industry. > 
4. Competitors
- Analyze the competition around the target company 
- <Record

Step 3: Configure the Tools that the agent needs to use

It's very simple. There are some built-in tools in Dify, you just need to select these tools and add them to the application. Here, we need to select the three tools under Yahoo Finance from the menu in Tools --> Add. Yahoo Finance is a service that can obtain real-time news, stocks, and other financial information and data, allowing the intelligent assistant to access external information.

If you don't see the option for tools, please make sure the mode has been switched to Agent Assistant mode.

Step 4: What's next? How to put it into production?

Now, an intelligent Company Analysis Report Copilot capable of Q&A is ready. So how do you integrate it into your existing workflow?

Select Overview from the left sidebar. We offer two methods:

  1. An out-of-the-box AI WebApp, which you can try by copying the link into a browser.

  2. Or a Backend-as-a-Service API, which can be integrated into the backend of your application.

A Showcase of our Copilot!

If you feel that such an agent does not meet your needs, you can try modifying the prompts. Start with our template and modify it according to your style, such as changing the output text style, modifying the generated text template, or customizing new Tools to introduce new data sources.Such assistants are not only for generating text. They can also be used to monitor news, such as providing timely feedback if there is any negative news or new developments regarding the company you are researching.

Start Building AI Assistants Now

Ever thought about creating your own AI Assistant? With Dify, you can start from build a new Assistant, opting for the Agent Assistant mode. Or, take an existing Chat App and switch it to Agent Assistant type right from its prompt setup page. Our docs are a great starting point for your adventure.

We're all for you exploring Dify to turn your one-of-a-kind ideas into reality. Whether you're building your own Agent Assistant or contributing new tools to us, we can't wait to see your creations.

Don’t forget to join our Discord community to share your thoughts and questions. We’re all ears!