Simple Usecases on AI Prototyping and few lessons from Developing a WebApp + API + LLM Combination for NLP Tasks
It’s not easy to document a process in progress, I am always late! Since what I am talking about has already become obsolete.
For this post, you can find all files for each project on my GitHub account. See https://github.com/bflaven/ia_usages/tree/main/ia_llms_usecases
Indeed, even though with the help of ChatGPT, I am in the process of making a prototype for an IA combination: “WebApp + API + LLM” to meet the AI needs that I have identified such as transcription, translation, generation using prompt and a bunch of various NLP tasks: Summary, NER, even Spelling Correction which has nothing to do with IA!
This Prototype’s project I have already mentioned in small touches throughout the last posts on this blog and as evidenced by the GitHub directory: https://github.com/bflaven/ia_usages
In this post, I will present two classic cases: News Topic Classification and Sentiment Analysis. As is often the case when we act, we find ourselves confronted with the reality of what needs to be done and the idea we have of it. What are the takeaways from these two POCs applying IA on Data.
What I learn from this experience?
I had already made such experiments but this time, somehow, I needed to present results and drew some conclusion. So, here they are:
- In a data-centric approach, data is key (dataset). The dataset goes through three stages: Bronze, Silver, Gold.
- 80% of the work focuses on Evaluation Strategy, 20% Model Implementation
- The Data-centric approach can be summarized like so :
AI = Model + Data
where you mostly focus on data. - DATA IS A FUEL, as an input as an output
It is quite peculiar because you can choose a model without breaking a sweat especially for classical issues such as News Topic Classification, Sentiment Analysis but you are going to bust your ass gathering the data and structuring it! In an almost caricatured manner, the workload is often distributed as follows:
- 80% of the work is to consolidate the data and making change management.
- The resting 20% of the work is choosing Model and developing a combination
Webapp + API + LLM
. By the way, development that can be made by ChatGPT, Mistral, Codelama, llama2…. Choose your weapon.
Methodology: Some other concerns and few valuable lessons (MLOps).
There are also two major concerns to deal with: Confidentiality, Free.
In News Topic Classification issue, USECASE_2, the fact of having replaced Mistral with ChatGPT, this was therefore done free of charge and securely.
Finally, we followed what we call an ML-OPS process. Process which is defined as follows:
1. Ideation
2. Framing
3. Data Collection
4. Data Preparation
5. Feature Engineering
6. Modelling
7. Deployment
8. Change
9. Run
Here an illustration of the MLOps.
Source: https://ml-ops.org/content/mlops-principles
MLOps has much in common with Design Thinking Process
Build a release, a must-have.
After, this framing and thinking, let do the acting. The time-to-market require to release a product as quick as possible by the end of few sprints even for a POC. Thanks to the LLM such as Mistral of ChatGTP, writing a rough combination rough for a “WebApp + API + LLM” is becoming more and more a piece of cake.
You can check the directory “fastapi_usecase_1_sentiment_analysis”
https://github.com/bflaven/ia_usages/tree/main/ia_llms_usecases/fastapi_usecase_1_sentiment_analysis
With a series of ten or twenty prompts, you can jot a serious candidate. I beat recently my personal record with 50 prompts for a “WebApp + API + LLM” combination. The only reason why the number prompts inflates is that the number API’s endpoints was higher and that induced an increase complexity for the Streamlit or Vite.js UX Webapp.
Find Dataset
Due to some confidentiality, I have substituted my datasets by some sample grabbed on Kaggle. Anyway, the principles are the same and the code is strictly identical.
I found one by searching “News Topic Classification” and “Sentiment Analysis” on Kaggle
- News Topic Classification: https://www.kaggle.com/datasets/vrindakallu/ag-news-topic-classification
- Sentiment Analysis: https://www.kaggle.com/code/robikscube/sentiment-analysis-python-youtube-tutorial/input
Let’s dig the usecases
The first case is called usecase_1_sentiment_analysis, a very classical one. You got text, most of the time comments and you want to classify if the text is negative or positive.
My only issue was that the text was in French but for educational, i have swap the model and give now an English sample and a model trained on English. The French Model is still in the code, commented, is based on “distilcamembert-base-sentiment” found on https://huggingface.co/cmarkea/distilcamembert-base-sentiment
The English model is https://huggingface.co/lxyuan/distilbert-base-multilingual-cased-sentiments-student
I have also drastically reduced the sample to 1000 rows for the Sentiment Analysis because it works easily on my Mac.
See the sample at https://www.kaggle.com/code/robikscube/sentiment-analysis-python-youtube-tutorial/input
The second case is more sophisticated than the first one: usecase_2_text_classification. It is an issue on text classification e.g. Posts made on Social Networks have to re-categorized. I have made 2 user stories on this issue that I have turned to prompts sent to Mistral.
What is amazing is that with LMM is that as far you have created an API that is able to query with a prompt a LLM such as Mistral then you are good to go to implement whatever user story you want! See below, two prompts slightly different but the mechanic remains the same.
V1 choice in a limited number of predefined categories # first_prompt = ChatPromptTemplate.from_template( # "Act as a highly intelligent news chatbot and classify the given news text into one of the following categories only: France, Europe Africa, America, Asia-Pacific, Middle East, Sports, Economy, Technology, Culture, Environment. Do not code. Return only one word answer with only the category name that the given news text belongs to. In the output, return the result in a field named \"category_predicted:\" and return the comment in the field \"category_decision:\" in a python Dictionary.\t News text: {news}?" # ) # V2 no choice let the LLM defined the category first_prompt = ChatPromptTemplate.from_template( "Act as a highly intelligent news chatbot and classify the given news text into the most adequate single category. Do not code. Return only one word answer with only the category name that the given news text belongs to. In the output, return the result in a field named \"category_predicted:\" and return the comment in the field \"category_decision:\" in a python Dictionary.\t News text: {news}?" )
Illustrations
I was looking for illustrations for “Data Science Process Lifecycle” and also to have a big picture on the connection between machine learning, AI…etc
I found these 2 images that I found quite interesting mostly because it is not ugly and incidentally because it is straightforward.
Data Science Lifecycle: This is pretty much all the stages of transformation that you will go through you and your data before learning anything from it.
A classic but effective image to locate the different areas when we talk about AI.
I grab these pictures from these sources:
- Understanding the Data Science Lifecycle: https://www.sudeep.co/data-science/2018/02/09/Understanding-the-Data-Science-Lifecycle.html
- Machine Learning: A Quick Introduction and Five Core Steps: https://centricconsulting.com/blog/machine-learning-a-quick-introduction-and-five-core-steps/
- Natural Language Processing and Machine Learning: https://www.encora.com/insights/natural-language-processing-and-machine-learning
- The ai spectrum: unveilling layers of intelligent system: https://www.scribbr.com/ai-tools/deep-learning/
Presenting the result
In addition, I have made a jupyter notebook to easily present the ML process especially the data exploitation.
You can check the file usecase_1_sentiment_analysis.ipynb
. Below, you have the command to leverage on the anaconda environment created for the usecase.
The jupyter notebook is available at https://github.com/bflaven/ia_usages/tree/main/ia_llms_usecases/usecase_1_sentiment_analysis
How to launch a jupyter notebook on a specific environment?
# In base launch the specific sentiment_analysis with this command python -m ipykernel install --user --name=sentiment_analysis # Activate the env sentiment_analysis source activate sentiment_analysis # Launch the jupyter notebook in this env sentiment_analysis jupyter notebook # Create the virtual environment conda create -n 'environment_name' # Activate the virtual environment conda activate 'environment_name' # Make sure that ipykernel is installed pip install --user ipykernel # Add the new virtual environment to Jupyter python -m ipykernel install --user --name='environment_name' # To list existing Jupyter virtual environments jupyter kernelspec list # To list existing conda environments conda env list ## To remove conda environment conda env remove -n 'environment_name' ## To remove the environment from Jupyter jupyter kernelspec uninstall 'environment_name'
Create an anaconda environment
[env] # Conda Environment conda create --name sentiment_analysis python=3.9.13 conda info --envs source activate sentiment_analysis conda deactivate # if needed to remove conda env remove -n [NAME_OF_THE_CONDA_ENVIRONMENT] conda env remove -n sentiment_analysis conda env remove -n faststream_kafka # update conda conda update -n base -c defaults conda # to export requirements pip freeze > requirements.txt # to install pip install -r requirements.txt
More infos
- Postman
https://www.postman.com/postman-student-programs/workspace/ollama-api/documentation/21521806-f48dc31a-a9f1-4dad-9082-fd07f5cd2fda - GitHub – ggerganov/llama.cpp: Port of Facebook’s LLaMA model in C/C++
https://github.com/ggerganov/llama.cpp - Easy as Ollama: Running Large Language Models Locally with a Elegant Web UI | by Rob Śliwa | Medium
https://medium.com/@robjsliwa_71070/easy-as-ollama-running-large-language-models-locally-with-a-elegant-web-ui-af3255b18141 - GitHub – microsoft/classy-fire: Classy-fire is multiclass text classification approach leveraging OpenAI LLM model APIs optimally using clever parameter tuning and prompting.
https://github.com/microsoft/classy-fire - Six Ways of Running Large Language Models (LLMs) Locally (January 2024)
https://kleiber.me/blog/2024/01/07/six-ways-running-llm-locally/ - Title Not Found
https://github.com/search?q=ollama+python+language%3APython&type=repositories&l=Python&p=2 - GPT4All
https://gpt4all.io/index.html - Build your own RAG and run it locally: Langchain + Ollama + Streamlit | by Duy Huynh | Medium
https://medium.com/@vndee.huynh/build-your-own-rag-and-run-it-locally-langchain-ollama-streamlit-181d42805895 - Few Shot Prompting with CodeLlama, LangChain, and MySQL | by Praveen Yerneni | Medium
https://medium.com/@yernenip/few-shot-prompting-with-codellama-langchain-and-mysql-94020ee16a08 - Run LLMs locally | 🦜️🔗 Langchain
https://python.langchain.com/docs/guides/local_llms - Ollama – running large language models on your machine | Unmesh Gundecha
https://unmesh.dev/post/ollama/ - Mixtral-46.7B – a Hugging Face Space by ehristoforu
https://huggingface.co/spaces/ehristoforu/mixtral-46.7b-chat - How to Run Mixtral 8x7B Locally – Step by Step Tutorial
https://anakin.ai/blog/how-to-run-mixtral-8x7b-locally/ - Create your first LLM playground | liteLLM
https://docs.litellm.ai/docs/tutorials/first_playground - Mixtral 8x7B en Local : Entraînez votre LLM avec vos Propres Données | Scalastic 👨🏻💻
https://scalastic.io/mixtral-ollama-llamaindex-llm/ - Running Mixtral 8×7 locally with LlamaIndex and Ollama | by LlamaIndex | Dec, 2023 | LlamaIndex Blog
https://blog.llamaindex.ai/running-mixtral-8×7-locally-with-llamaindex-e6cebeabe0ab - Local LLM in the Browser Powered by Ollama | by Andrew Nguonly | Medium
https://medium.com/@andrewnguonly/local-llm-in-the-browser-powered-by-ollama-236817f335da - Ollama: Easily run LLMs locally — Klu
https://klu.ai/glossary/ollama - Ollama: Running GGUF Models from Hugging Face | Mark Needham
https://www.markhneedham.com/blog/2023/10/18/ollama-hugging-face-gguf-models/ - Running a Hugging Face Large Language Model (LLM) locally on my laptop | Mark Needham
https://www.markhneedham.com/blog/2023/06/23/hugging-face-run-llm-model-locally-laptop/ - Integrating Llama 2 with Hugging Face and Langchain🦙
https://blog.futuresmart.ai/integrating-llama-2-with-hugging-face-and-langchain - GitHub – dennisypan/Quick-N-Dirty-Sentiment-Analysis: Suppose you got a CSV file with rows of distinct comments and 5-stars ratings on a business (e.g. a restaurant from Yelp). This tool will process data from this CSV input file to (1) produce an output CSV file giving each row a “Sentiment Label” and “Sentiment Score”, and (2) a statement on Command Prompt (Windows) describing the overall sentiment (Positive, Neutral, Negative) of the business.
https://github.com/dennisypan/Quick-N-Dirty-Sentiment-Analysis - GitHub – raheem-m/NYT-API-Comment-Sentiment-Analysis: This is a python program that hits the NYT article search API as well as the Community API to retrieve comments from a set of articles searched from user input. Sentiment analysis is then applied to the first 25 comments, then output to a csv file.
https://github.com/raheem-m/NYT-API-Comment-Sentiment-Analysis - GitHub – marcellobarile/multilang-sentiment: Multi language AFINN-based sentiment analysis for Node.js.
https://github.com/marcellobarile/multilang-sentiment - Problem-solving with ML: automatic document classification | Google Cloud Blog
https://cloud.google.com/blog/products/ai-machine-learning/problem-solving-with-ml-automatic-document-classification - Text Classification – ⚡LangChain + Mixtral8X7B⚡ | Kaggle
https://www.kaggle.com/code/swathysekar/text-classification-langchain-mixtral8x7b - GitHub – kodiks/turkish-news-classification: Turkish News Category Classification Tutorial
https://github.com/kodiks/turkish-news-classification - HuggingFace Türkçe Veri Kümeleri. “Dataset is power”! | by Yavuz Kömeçoğlu | Kodiks | Medium
https://medium.com/kodiks/huggingface-turkce-verikumeleri-6d213dd63966 - Turkish News Category Classification Tutorial | by Serdar Akyol | Kodiks | Medium
https://medium.com/kodiks/turkish-news-category-classification-tutorial-6b9317b49915 - Title Not Found
https://github.com/susanli2016/Machine-Learning-with-Python/blob/master/Consumer_complaints.ipynb - Amazon Fine Food Reviews | Kaggle
https://www.kaggle.com/datasets/snap/amazon-fine-food-reviews/code?datasetId=18 - Deep Learning For NLP: Zero To Transformers & BERT | Kaggle
https://www.kaggle.com/code/tanulsingh077/deep-learning-for-nlp-zero-to-transformers-bert - Building Powerful NLP Library in Python for 2024 | by Fareed Khan | Dec, 2023 | Level Up Coding
https://levelup.gitconnected.com/how-i-created-the-most-powerful-nlp-library-in-python-c26d08a55809 - Gemini – Google DeepMind
https://deepmind.google/technologies/gemini/#introduction - GitHub – rasbt/LLMs-from-scratch: Implementing a ChatGPT-like LLM from scratch, step by step
https://github.com/rasbt/LLMs-from-scratch - Build Your Own Large Language model (LLM) Model with OpenAI using Microsoft Excel file
https://decube.substack.com/p/build-your-own-large-language-model - The 40-hour LLM application roadmap: Learn to build your own LLM applications from scratch | Data Science Dojo
https://datasciencedojo.com/blog/build-custom-llm-application/ - How to create a private ChatGPT with your own data | by Mick Vleeshouwer | Medium
https://medium.com/@imicknl/how-to-create-a-private-chatgpt-with-your-own-data-15754e6378a1 - Title Not Found
https://github.com/openai/openai-cookbook/blob/main/examples/Question_answering_using_embeddings.ipynb - GitHub – stochasticai/xTuring: Build, customize and control you own LLMs. From data pre-processing to fine-tuning, xTuring provides an easy way to personalize open-source LLMs. Join our discord community: https://discord.gg/TgHXuSJEk6
https://github.com/stochasticai/xturing