Nothing Special   »   [go: up one dir, main page]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LFX Workspace: Create a Wasm-based LLM app for financial analysts [Term-3] #3715

Open
3 of 7 tasks
lazyperson1020 opened this issue Aug 31, 2024 · 79 comments
Open
3 of 7 tasks
Assignees
Labels
LFX Mentorship Tasks for LFX Mentorship participants

Comments

@lazyperson1020
Copy link
lazyperson1020 commented Aug 31, 2024

Project Title

Create a Wasm-based LLM app for financial analysts

Motivation

The WasmEdge community has developed a decentralised computing infrastructure named Gaianet where users can create and deploy customized AI agents as nodes. Each gaianet node can be fine-tuned with specific knowledge, allowing for personalized responses rather than generic AI outputs. In this project, we aim to develop a use case for analysis of the SEC (Securities Exchange Council) financial reports (like 10-K,10-Q) by an agent application.

Details

We download the SEC reports and press releases and then store them in a database for data retrieval. An open-source LLM is used to generate summary and question-answer pairs on the documents for analysis and a knowledge base is prepared from it for deployment of public node. Further the agent app utilises the database/web and LLM function call is performed to generate answers for the users.

Milestones

  • M1 [1 Week]

    • Develop a script to download all SEC-published financial reports and related press releases from the web.
  • M2 [2 Weeks]

    • Store the documents in SQL database with the functionality to retrieve data by date ranges and/or stock symbols.
  • M3 [1 Week]

    • Develop summaries and multiple QAs for each file using an open-source LLM on a public Gaia node.
  • M4 [1.5 Weeks]

    • Create a script to generate vector embeddings for each file from the summary / QA text.
  • M5 [1.5 Weeks]

    • Create a public Gaianet node with the knowledge base.
  • M6 [2 Weeks]

    • Create an agent app that can take user query and generates the answer from the LLM.
    • Implementation of LLM function calling to get the external data.
  • M7 [2 Weeks]

    • Bug solving
    • Working on documentation
    • Weeks reserved for unexpected delays

Appendix

The project's milestones will be refined and updated as the work progresses

@lazyperson1020 lazyperson1020 added the LFX Mentorship Tasks for LFX Mentorship participants label Aug 31, 2024
@lazyperson1020
Copy link
Author

Hi @juntao Do we plan to use external libraries to download the fillings like sec-edgar downloader , etc. Also, regarding the database for download of files, are we planning to host it on the cloud or somewhere. It would be of great help if you could possibly share some resources.

@juntao
Copy link
Member
juntao commented Sep 2, 2024

You can use any library (and programming language) to develop this script.

Initially, the document database should run locally -- perhaps a local MySQL or SQLite server. I think we can easily connect to remote database servers later.

@lazyperson1020
Copy link
Author

Dear @juntao , I tried making a script in python which could fetch the sec filings and save in SQL, but I wanted to know one thing. The libraries primarily download the filings as files (maybe text,pdf,html) on the disk first which can then be saved to sql database as binary large object file (BLOB). Do we need it this way or instead we directly save it to the database after parsing them and without saving on disk?
Screenshot 2024-09-04 at 7 33 26 AM

@juntao
Copy link
Member
juntao commented Sep 4, 2024

The PDF can be saved as BLOB for sure. The text version needs to be inserted into a prompt later, so it needs to be clean text -- ideally markdown. I would suggest that you use an LLM to create the markdown file from the original text. Both the orginal text and the markdown text could be stored as BLOB or TEXT.

The database table should have fields for the stock symbol and start / end / publication time for each report so that we can search for them later.

@lazyperson1020
Copy link
Author

Hi @juntao I have written this script to download the SEC filings in the MySQL database and this to parse it using LLAMA-PARSE and save the parsed text (from markdown) in a new column.
Screenshot 2024-09-17 at 7 52 52 AM
Please add necessary database connection values and llama-parse key to try it.

@juntao
Copy link
Member
juntao commented Sep 17, 2024

Cool. Can you provide docs on how to run this? I mean what files I need to edit, what are the meaning of variables I need to change, and how to run it. Thanks.

@lazyperson1020
Copy link
Author

Please use this. I will provide a better version of the code once you give your insights. Thanks.

@lazyperson1020
Copy link
Author
lazyperson1020 commented Sep 19, 2024

Hey @juntao Are you able to run it? Could you please guide on the next steps?

@juntao
Copy link
Member
juntao commented Sep 19, 2024

Never wait for me. According to the plan, the next step is to build the SQL database for the application to search for the latest reports.

You will need to develop and test a special prompt that asks an LLM to extract all stock symbols from a paragraph of user question or comments. For example, if the user mentions Nvidia and Apple, it should respond with NVDA and AAPL. You can use a standard llama 3.1 model or a llama-3-groq tool call model for this.

https://docs.gaianet.ai/user-guide/nodes

@lazyperson1020
Copy link
Author
lazyperson1020 commented Sep 23, 2024

I was testing some prompts in this Colab. I tried for some of the LLMs on GAIA and most of them gave long answers. I think using multiple LLMs / asking multiple times can help us extract the exact stock symbols.

Also I have implemented tool calling feature for adding SEC filings in SQL in this script.

For the question-answer pairs and summary, do we add them in the database itself or keep them separately for just the knowledge base?

@juntao
Copy link
Member
juntao commented Sep 23, 2024

Hmm, if the long answer is a problem, you could ask the model to return JSON format as in too call? This way, your client application can extract the JSON from the response. If no valid JSON is in the response, you could simply ask the LLM to re-generate.

@lazyperson1020
Copy link
Author
lazyperson1020 commented Sep 29, 2024
  1. I am getting time-out error while I was trying to create summaries from the markdown from the gaianet llama 3 public node. Is there any solution for this?
Screenshot 2024-09-29 at 5 27 50 AM
  1. The approach I am using for generating embeddings from summary and ques-ans pairs is using CSV embed. It is fine or do I use some other way for better results?

@juntao
Copy link
Member
juntao commented Sep 29, 2024

I think you need to write the client side project so that it blocks until one request finishes before starting the next request.

You cannot run multiple requests in parallel (eg async api calls) since the server only has one GPU.

@lazyperson1020
Copy link
Author
  1. Sir I got the error 500 Internal Server Error: Failed to get chat completions. Reason: The number of prompt tokens is greater than the context size: 232682 > 81920 while I was trying to generate summaries. Even though the prompt tokens for llama is 8000 it is showing 81920 due to some inconsistencies. I have rectified it by making chunks after token counting (to lie within 8000) and then generate summary one by one

  2. After parsing the html, there are some areas in markdown where we have gibberish text. How do I clean this before summarisation?

Screenshot 2024-10-01 at 7 43 36 AM

@juntao
Copy link
Member
juntao commented Oct 1, 2024

The llama node you are using have 80k context length. So, it is correct to say 81920. You are using it to generate QA for your chunk? That means the chunk is too big. You will need to sub-divide it.

@juntao
Copy link
Member
juntao commented Oct 1, 2024

Most RAG systems require chunks no greater than 300 tokens. We relaxed it to 81920 tokens -- that is 300x increase. But you are sending text much longer than that.

@lazyperson1020
Copy link
Author

But if I send greater than 8000 then I get time-out error again and again. How will I get the appropriate chunk size to not get time-out error while even being within the bounds of 80k.

@juntao
Copy link
Member
juntao commented Oct 1, 2024

I think you need to

1 use small chunks

2 Slow down the requests -- make each request blocking and wait for 5 seconds after each request before sending next one.

It could take 10+ hours to run through a large document.

@juntao
Copy link
Member
juntao commented Oct 1, 2024

If the time-out problem persists, we could start a new api server just for this. But before we do that, I think we need to try simple things.

You could also pause your program for 5 minutes when you see a time-out and then retry.

@lazyperson1020
Copy link
Author

Okay sir I was thinking of that but thought that longer times would be inconvenient . What about the gibberish text part?

@juntao
Copy link
Member
juntao commented Oct 1, 2024

Shorter input would not produce gibberish. If your input is close to its context length, it tends to degrade.

@lazyperson1020
Copy link
Author

Sir I was getting gibberish text in some parts of markdown which was parsed via LlamaParse.

@juntao
Copy link
Member
juntao commented Oct 1, 2024

Oh. Then it is a LlamaParse problem. :( Which backend LLM are you using with LlamaParse?

@lazyperson1020
Copy link
Author

I dont know the backend since I am just using the free API. Might be llama I guess. You can view the parsed markdown for Apple 10k.

https://docs.cloud.llamaindex.ai/llamaparse/getting_started

@juntao
Copy link
Member
juntao commented Oct 1, 2024

@juntao
Copy link
Member
juntao commented Oct 3, 2024

Hi @lazyperson1020

I have been thinking about this project. Many people told me that when they search for financial information:

1 They are only concerned about now. Historical data is interesting but not useful.

2 They know exactly which company / ticker symbol that they'd like to learn more.

So, semantic search on past financial reports is probably not going to be useful. Given that, I'd like to make some changes to our approach.

1 We should focus on a SQL database that stores the latest financial statements. It is indexed by the stock symbol. We will use our tool to collect the financial reports, turn them into text, and save them in the database.

2 Let's build a web UI that allows the user to enter a stock symbol. It will create a page that has:

  • A real time price chart
  • Several news items about the stock
  • A summary / interpretation of the news using the latest financial statement as the context

3 If the user enters a question about the stock symbol, it will use news + latest financial statement as the context to answer it.

What do you think? Here is an example we could learn from: https://github.com/bklieger-groq/stockbot-on-groq

@juntao
Copy link
Member
juntao commented Oct 15, 2024

Next week is great. Thank you!

@juntao
Copy link
Member
juntao commented Oct 16, 2024

@lazyperson1020
Copy link
Author

Dear Sir,
I would like you to check this out. Using mullti- ai agents we can create a RAG app comparable to the perplexity example. Otherwise we could you this - https://cookbook.openai.com/examples/structured_outputs_multi_agent . Our current approach which is devoid of this won't stand out.

@juntao
Copy link
Member
juntao commented Oct 24, 2024

My apologies for the delay. Yeah, the CrewAI demo is cool. Can you replicate their work but switch the backend from OpenAI to a Gaia node?

If that is successful, we need to add a web UI to it.

I saw that they have RAG search in the workflow. We should try to improve that. I do not like the idea of breaking up financial documents into small pieces. So, perhaps we should just do a SQL search on the latest reports and put the comprehensive summary (from another LLM) into the agent context.

@lazyperson1020
Copy link
Author
Screen.Recording.2024-10-26.at.1.13.45.PM.mov

How about this?
I will fix some bugs and upload the code.

@juntao
Copy link
Member
juntao commented Oct 27, 2024

This looks great. Thank you.

One thing I would like to do is to start with a standard HTML form input. We should just have a "search box" where the user must enter a stock symbol or company name. It will respond with the price chart + report summary + news. After that, the user can ask follow up questions in the chat ui.

For the initial search box, I think we should allow for any natural description of the company he wants to search. For example, maybe "Elon Musk's car company" will map to the TSLA stock symbol. I am thinking that maybe perform a search and let the LLM figure out the stock symbol based on the search results.

https://www.google.com/search?q=%22elon+musk%27s+car+company%22+stock+ticker+symbol

Then, in the follow up conversation, we should have report summary + latest stock price + news in the "system prompt".

Bonus feature: We could setup a Gaia node with "knowledge" on financial terms (e.g., what is a P/E ratio?). This knowledge could be a public text book or QA set, and we can create a vector DB from it for the node.

@lazyperson1020
Copy link
Author

For the initial search box, I think we should allow for any natural description of the company he wants to search. For example, maybe "Elon Musk's car company" will map to the TSLA stock symbol. I am thinking that maybe perform a search and let the LLM figure out the stock symbol based on the search results.

For this initial search , do we perform a google search? If we want to inject latest data which is beyond the knowledge of the llm then this would help us find the stock symbol even for some new company.

@lazyperson1020
Copy link
Author

My current approach uses a tool call to fetch the ticker according to query and then it proceeds with transferring the (news+report summary+price chart) in the prompt and then answering questions until the I fetch a different ticker in the query. So if I want to put search operation for some natural description, then maybe we could use one more api call to first analyse the query and then predict some ticker. If the tool call is successful for that, then we can proceed via the above strategy.

@juntao
Copy link
Member
juntao commented Oct 29, 2024

Which model did you use to generate the tool call? My experience with tool call is that they are not very reliable.

I think asking the user to start by entering a stock symbol would be a faster and better experience.

@lazyperson1020
Copy link
Author

I think asking the user to start by entering a stock symbol would be a faster and better experience.

If we directly start by entering a symbol then do we really need a natural description? I used llamatool gaia node for tool calling and it was working fine mostly.

@juntao
Copy link
Member
juntao commented Oct 30, 2024

Perhaps we can simply provide a list of popular symbols on the UI for the users to click on and select.

This is the first time the user interact with our app, and we want it to be very reliable.

@lazyperson1020
Copy link
Author

Then sir what about the natural description of the company? I mean if the user provides the symbol then we dont need to map to the symbol right

@juntao
Copy link
Member
juntao commented Oct 30, 2024

Yeah. I think for the initial MVP, we can ask for the symbol and focus on providing good information and engagement after we have the symbol.

We can improve symbol lookup using NL later.

@lazyperson1020
Copy link
Author

https://github.com/lazyperson1020/LFX-Updates
Hey sir, please check this out and let me know the suggestions if any. Also tell me if you face any problem while using it.

@juntao
Copy link
Member
juntao commented Nov 3, 2024

Can you make a single command to start both the database manager and chatbot? For example, the chatbot could be started on port 8080 and the database manager could be started on port 8081. Also, please read the parameters (e.g., database connection, LLM API endpoint, llamaparse API key etc.) from env vars or from the command line args. This way, I can run the entire app inside a Docker container.

Also, I thought the app need to search and summarize latest news. You do not need an API key for a search service?

@lazyperson1020
Copy link
Author
lazyperson1020 commented Nov 3, 2024

I have used yfinance for searching the latest news. The problem is that they are protected content and sometimes cant be scraped. Which service do you want me to use instead?

@juntao
Copy link
Member
juntao commented Nov 3, 2024

Hmm, I thought we are using yfinance for charts and data etc. And we can use a real search engine API, such as Tavily, Duckduckgo and Bing, to collect the news.

But, lets try your current version first. Let me know when you have a server version that I can run in Docker. Thanks.

@lazyperson1020
Copy link
Author
lazyperson1020 commented Nov 5, 2024

Sir please check if it is fine now.
Also please provide with the next steps

@lazyperson1020
Copy link
Author

Any updates sir??

@juntao
Copy link
Member
juntao commented Nov 9, 2024

Sorry for the delay. Can you provide a Dockerfile or a Docker image that I can simply run with a

docker run

command? You can map the necessary ports to the host and pass arguments (eg api keys) on the docker command line.

Example: https://github.com/GaiaNet-AI/gaianet-node/tree/main/docker

Thanks!

@lazyperson1020
Copy link
Author

Sir check now & tell if you face any problems

@juntao
Copy link
Member
juntao commented Nov 10, 2024

Cool! It works.

Screenshot 2024-11-09 at 11 59 09 PM

However, it fails to fetch the latest SEC report ...

Screenshot 2024-11-10 at 12 17 16 AM

and it fails to fetch some news content. Are those news from Y! Finance?

Screenshot 2024-11-10 at 12 17 50 AM

Also, the 8502 server is not running.

Screenshot 2024-11-09 at 11 58 26 PM

About the UI:

PC Desktop: Can we make the search panel as the main panel (2/3 of the screen width)? The chat panel could be 1/3 of the screen.

Mobile: It now defaults to the chat panel. I think it should default to the search panel.

Also, can we remove the "deploy" button at the top right? Thanks.

@juntao
Copy link
Member
juntao commented Nov 10, 2024

Also, I would like the user to enter anything in the search box, and it would suggest symbols in a dropdown menu for the user to select. For example, the user could enter Tesla automobile and the dropdown would be TSLA.

Finally, can we support crypto symbols such as

  • BTC
  • ETH
  • SOL
  • BNB
  • DOGE
  • XRP
  • ADA
  • TRX
  • BCH
  • DOT
  • POL
  • LTC
  • NEAR
  • UNI

They would not have SEC reports. Just latest charts and news. But you cannot find them on Yahoo Finance. You could try the following

@lazyperson1020
Copy link
Author

Also, the 8502 server is not running.

This may happen due to wrong database connection parameters

@juntao
Copy link
Member
juntao commented Nov 10, 2024

Oh, right. Do you have a SQL script that I need to run to initialize the database? (i.e., create tables etc)

@lazyperson1020
Copy link
Author
lazyperson1020 commented Nov 12, 2024

Sir you just need to set the connection values ( try it on local sql server ) and just put the name of your choice for DB_Name variable.

Also how do I map the natural description to stock ?

@juntao
Copy link
Member
juntao commented Nov 13, 2024

I think the best way to map natural language to stock symbol is to use an LLM. The system prompt should contain a list of stock symbols and its associated description. Then, we ask the LLM to map the user input to a most like stock symbol on that list.

@lazyperson1020
Copy link
Author

Dear Sir, please excuse me for a slight delay. I will update in some time.

@lazyperson1020
Copy link
Author

Sir do I need to provide documentation/demonstration also?

@juntao
Copy link
Member
juntao commented Nov 21, 2024

I think your repo should suffice. Maybe change it to a different name -- e.g., financial-research-agent and then transfer the repo to the LlamaEdge or WasmEdge repo.

But, I would like to see two features we described:

1 Suggest stock symbols (maybe in a drop-down menu) based on natural language description.

2 Support crypto symbols (BTC, ETH, SOL, etc)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LFX Mentorship Tasks for LFX Mentorship participants
Projects
None yet
Development

No branches or pull requests

4 participants
@juntao @hydai @lazyperson1020 and others