Monday, March 11, 2024

Is it possible to cache the results from LLM and use it for fine tuning ?

Yes, caching the results from Large Language Models (LLMs) and using them for fine-tuning is possible and can offer several benefits. Here's a breakdown of the concept:

Caching LLM Outputs:

Caching involves storing the LLM's response to a specific query or prompt along with the query itself. This can be done in a database or a dedicated caching system.

Subsequent requests with the same query can retrieve the cached response instead of requiring the LLM to process it again.

Benefits of Caching for Fine-Tuning:


Reduced Training Costs: Reusing cached outputs for training reduces the number of times you need to call the LLM, which can be expensive in terms of computational resources.

Improved Training Efficiency: By using cached outputs, you can potentially speed up the fine-tuning process, especially for repetitive tasks.

Focused Fine-Tuning: You can selectively cache outputs relevant to your fine-tuning goals, allowing you to focus the training process on specific aspects of the LLM.

Challenges and Considerations:


Cache Invalidation: The cache needs to be invalidated or updated when the underlying data or the LLM itself changes. Otherwise, you might be using outdated responses for training.

Cache Size Management: Caching everything can become impractical. Determining what to cache and for how long requires careful consideration based on factors like query frequency, output size, and update rates.

Staleness vs. Cost: Balancing the benefits of using cached data (reduced cost) with the potential drawbacks of staleness (outdated data) is crucial.

Approaches for Caching LLM Outputs:


Query-Based Caching: Cache outputs based on the exact query or prompt received by the LLM. This is the simplest approach but might not be effective if there are many similar queries with slightly different phrasings.

Contextual Caching: Cache outputs considering not just the query but also the context of the interaction. This can involve additional processing and can be more complex to implement.

Overall, caching LLM outputs can be a valuable strategy for optimizing fine-tuning processes. By carefully considering the trade-offs and implementing appropriate caching strategies, you can achieve significant cost savings and efficiency gains.


references:

Gemini 

Sunday, March 10, 2024

What is Psycopg, sqlalchemy

Psycopg2 is a popular Python library that acts as a PostgreSQL database adapter. It implements the Python Database API Specification v2.0, allowing you to interact with PostgreSQL databases from Python applications using a consistent and standardized interface.

Here's a quick summary of Psycopg2 usage:

1. Installation:

Use pip to install Psycopg2:

Bash

pip install psycopg2

Python

import psycopg2


# Connect to the database

conn = psycopg2.connect(dbname="your_database", user="your_user", password="your_password", host="your_host")


# Create a cursor object

cur = conn.cursor()


# Execute a query

cur.execute("SELECT * FROM your_table")


# Fetch results (can be one row at a time or all at once)

rows = cur.fetchone()  # One row

# or

rows = cur.fetchall()  # All rows


# Print results

for row in rows:

    print(row)


# Close the cursor and connection

cur.close()

conn.close()




SQLAlchemy is a powerful Python library known as an Object Relational Mapper (ORM). It acts as a bridge between Python objects and relational databases, simplifying how you interact with databases in your Python applications. Here's a summary of SQLAlchemy and its usage:


Summary:


ORM Approach: SQLAlchemy uses an Object Relational Mapping approach, allowing you to define Python classes that map to database tables. This makes your code more readable and maintainable as you work with objects instead of raw SQL queries.

Flexibility: It offers flexibility. You can use the ORM for rapid development or switch to writing raw SQL queries when needed for complex operations.

Database Agnostic: SQLAlchemy is database-agnostic. It supports various relational databases (like PostgreSQL, MySQL, SQLite) through dialects, allowing you to use the same core concepts across different database systems.

Basic Usage:


Here's a simplified example of using SQLAlchemy:


Import and Define Engine:

Python

from sqlalchemy import create_engine


# Connect to the database (replace placeholders)

engine = create_engine('postgresql://user:password@host/database')



Python

from sqlalchemy import Column, Integer, String

from sqlalchemy.ext.declarative import declarative_base


Base = declarative_base()  # Base class for models


class User(Base):

    __tablename__ = 'users'  # Table name


    id = Column(Integer, primary_key=True)

    name = Column(String)

    email = Column(String)


PEP 249 – Python Database API Specification v2.0

The Python Database API Specification v2.0, documented in PEP 249, defines a common interface for accessing databases with Python. This standardization allows developers to write Python code that can interact with various database management systems (DBMS) without needing to learn specific APIs for each one.


Here are the key aspects of the Python Database API Specification v2.0:


Provides a Consistent Interface: It defines a set of classes and methods that all compliant database modules (like sqlite3, psycopg2, etc.) must implement. This consistency allows you to use similar code structures to connect, execute queries, retrieve data, and manage transactions regardless of the underlying database.

Improves Code Portability: Code written using the Database API can be easily ported to work with different databases by simply switching the database module used. This saves development time and reduces the need to write database-specific code.

Simplifies Learning Curve: By learning a single API, developers can interact with various databases more easily. This reduces the learning curve associated with using different database technologies in Python applications.

The Database API v2.0 specification includes definitions for:


Connection Objects: Represent a connection to a database server.

Cursor Objects: Used to execute SQL statements and retrieve data from the database.

Type Objects and Constructors: Specify the data types used in database tables and queries.

Optional Extensions: Additional functionalities like two-phase commit transactions and error handling extensions can be implemented by database modules.

Benefits of Using the Database API:


Increased Code Maintainability: Code becomes more maintainable and reusable as it's not tied to a specific database system.

Improved Developer Productivity: Developers can focus on application logic rather than learning different database APIs.

Simplified Database Switching: Switching databases becomes easier, requiring only a change in the database module used.

In summary, the Python Database API Specification v2.0 offers a standardized way to interact with databases from Python, promoting code portability, developer efficiency, and application maintainability.


References:

Gemini


Thursday, March 7, 2024

Displaying sidebar and container and graphs with Streamlit

The hierarchy and arrangement of pages on your app can have a large impact on your user experience. 

Passing an element to st.sidebar() will make this element pinned to the left, allowing users to focus on the content in your app.

But st.spinner() and st.echo() are not supported with st.sidebar.

st.container() is used to create an invisible container where you can put elements in order to create a useful arrangement and hierarchy.

st.pyplot(): This function is used to display a matplotlib.pyplot figure.

import streamlit as stimport matplotlib.pyplot as pltimport numpy as nprand=np.random.normal(1, 2, size=20)fig, ax = plt.subplots()ax.hist(rand, bins=15)st.pyplot(fig)

st.line_chart(): This function is used to display a line chart.

import streamlit as stimport pandas as pdimport numpy as npdf= pd.DataFrame(    np.random.randn(10, 2),    columns=['x', 'y'])st.line_chart(df)

st.bar_chart(): This function is used to display a bar chart.

import streamlit as stimport pandas as pdimport numpy as npdf= pd.DataFrame(    np.random.randn(10, 2),    columns=['x', 'y'])st.bar_chart(df)

st.area_chart(): This function is used to display an area chart.

import streamlit as stimport pandas as pdimport numpy as npdf= pd.DataFrame(    np.random.randn(10, 2),    columns=['x', 'y'])st.area_chart(df)

st.altair_chart(): This function is used to display an altair chart.

import streamlit as stimport numpy as npimport pandas as pdimport altair as alt​df = pd.DataFrame(   np.random.randn(500, 3),   columns=['x','y','z'])​c = alt.Chart(df).mark_circle().encode(   x='x' , 'y'=y , size='z', color='z', tooltip=['x', 'y', 'z'])st.altair_chart(c, use_container_width=True)

st.graphviz_chart(): This function is used to display graph objects, which can be completed using different nodes and edges.

import streamlit as stimport graphviz as graphvizst.graphviz_chart('''    digraph {        Big_shark -> Tuna        Tuna -> Mackerel        Mackerel -> Small_fishes        Small_fishes -> Shrimp    }''')

refenrences:

https://www.datacamp.com/tutorial/streamlit

Displaying Input widgets,Progress and Status with Streamlit

st.checkbox(): This function returns a Boolean value. When the box is checked, it returns a True value, otherwise a False value. st.button(): This function is used to display a button widget. st.radio(): This function is used to display a radio button widget. st.selectbox(): This function is used to display a select widget. st.multiselect(): This function is used to display a multiselect widget. st.select_slider(): This function is used to display a select slider widget. st.slider(): This function is used to display a slider widget.

st.checkbox('yes')

st.button('Click')

st.radio('Pick your gender',['Male','Female'])

st.selectbox('Pick your gender',['Male','Female'])

st.multiselect('choose a planet',['Jupiter', 'Mars', 'neptune'])

st.select_slider('Pick a mark', ['Bad', 'Good', 'Excellent'])

st.slider('Pick a number', 0,50)


st.number_input(): This function is used to display a numeric input widget. st.text_input(): This function is used to display a text input widget. st.date_input(): This function is used to display a date input widget to choose a date. st.time_input(): This function is used to display a time input widget to choose a time. st.text_area(): This function is used to display a text input widget with more than a line text. st.file_uploader(): This function is used to display a file uploader widget. st.color_picker(): This function is used to display color picker widget to choose a color.


st.number_input('Pick a number', 0,10)

st.text_input('Email address')

st.date_input('Travelling date')

st.time_input('School time')

st.text_area('Description')

st.file_uploader('Upload a photo')

st.color_picker('Choose your favorite color')


st.balloons(): This function is used to display balloons for celebration. st.progress(): This function is used to display a progress bar. st.spinner(): This function is used to display a temporary waiting message during execution.


st.balloons()

st.progress(10)

with st.spinner('Wait for it...'):    time.sleep(10)


st.success(): This function is used to display a success message. st.error(): This function is used to display an error message. st.warnig(): This function is used to display a warning message. st.info(): This function is used to display an informational message. st.exception(): This function is used to display an exception message.


st.success("You did it !")

st.error("Error")

st.warnig("Warning")

st.info("It's easy to build a streamlit app")

st.exception(RuntimeError("RuntimeError exception"))


refenrences:

https://www.datacamp.com/tutorial/streamlit


Displaying Text and images, video, audio with Streamlit

st.title(): This function allows you to add the title of the app. st.header(): This function is used to set header of a section. st.markdown(): This function is used to set a markdown of a section. st.subheader(): This function is used to set sub-header of a section. st.caption(): This function is used to write caption. st.code(): This function is used to set a code. st.latex(): This function is used to display mathematical expressions formatted as LaTeX.

st.title ("this is the app title")st.header("this is the markdown")st.markdown("this is the header")st.subheader("this is the subheader")st.caption("this is the caption")st.code("x=2021")st.latex(r''' a+a r^1+a r^2+a r^3 ''')


st.image(): This function is used to display an image. st.audio(): This function is used to display an audio. st.video(): This function is used to display a video.

st.image("kid.jpg")st.audio("Audio.mp3")st.video("video.mp4")

refenrences:

https://www.datacamp.com/tutorial/streamlit


Wednesday, March 6, 2024

Streamlit - Deploying AI app onto stremlit

Streamlit is a Python library that is open-source, providing a seamless way to develop and distribute interactive web applications and data visualizations. With Streamlit, you can effortlessly create web apps using Python code, enhanced by its robust additional features. The library comes equipped with integrated support for various data visualization libraries such as matplotlib, pandas, and plotly, simplifying the process of generating interactive charts and graphs that dynamically update based on user input. It is a popular tool among data scientists, machine learning (ML) engineers and developers looking to share interactive web apps with their audience.

The process is pretty easy. Below are few steps 

mkdir streamlit-app

cd streamlit-app

touch streamlit_app.py

touch requirements.txt


Requirements txt file contents are 


streamlit==1.22.0

langchain==0.0.176

openai==0.27.7

tiktoken==0.4.0

unstructured==0.6.8

tabulate==0.9.0

pdf2image==1.16.3

pytesseract==0.3.10


To test locally, below can be done 


pip install -r requirements.txt


Create a python file like this below say streamlit_app.py


import validators, streamlit as st

from langchain.chat_models import ChatOpenAI

from langchain.document_loaders import UnstructuredURLLoader

from langchain.chains.summarize import load_summarize_chain

from langchain.prompts import PromptTemplate



# Streamlit app

st.subheader('Summarize URL')

# Get OpenAI API key and URL to be summarized

with st.sidebar:

    openai_api_key = st.text_input("OpenAI API key", value="", type="password")

    st.caption("*If you don't have an OpenAI API key, get it [here](https://platform.openai.com/account/api-keys).*")

    model = st.selectbox("OpenAI chat model", ("gpt-3.5-turbo", "gpt-3.5-turbo-16k"))

    st.caption("*If the article is long, choose gpt-3.5-turbo-16k.*")

url = st.text_input("URL", label_visibility="collapsed")

# If 'Summarize' button is clicked

if st.button("Summarize"):

    # Validate inputs

    if not openai_api_key.strip() or not url.strip():

        st.error("Please provide the missing fields.")

    elif not validators.url(url):

        st.error("Please enter a valid URL.")

    else:

        try:

            with st.spinner("Please wait..."):

                # Load URL data

                loader = UnstructuredURLLoader(urls=[url])

                data = loader.load()

                

                # Initialize the ChatOpenAI module, load and run the summarize chain

                llm = ChatOpenAI(temperature=0, model=model, openai_api_key=openai_api_key)

                prompt_template = """Write a summary of the following in 250-300 words:

                    

                    {text}

                """

                prompt = PromptTemplate(template=prompt_template, input_variables=["text"])

                chain = load_summarize_chain(llm, chain_type="stuff", prompt=prompt)

                summary = chain.run(data)

                st.success(summary)

        except Exception as e:

            st.exception(f"Exception: {e}")



python streamlit_app.py # or python3 streamlit_app.py



To deploy, need to create a git repository and push the files above to it


git init # Initialize a git repository

git add . # Add files to your new commit

git commit -m "first commit" # Make the commit

git remote add origin <YOUR_REPOSITORY_URL> # Connects your local git repository to your remote Github one

git push origin main # Pushes your code to your remote repo


Now create Streamlit account and authorise the streamlit to read the GitHub repo. 


Now there will be option to deploy. Once it is done, the app will be available at the given location! 






References

https://medium.com/@alfredolhuissier/streamlit-how-to-deploy-your-ai-app-7a516548eb90