Pakistan's First Oracle Blog

Subscribe to Pakistan's First Oracle Blog feed
Blog By Fahd Mirza ChughtaiFahd Mirzahttp://www.blogger.com/profile/14722451950835849728noreply@blogger.comBlogger574125
Updated: 6 hours 58 min ago

Create RAG Agents with Crew AI and LlamaIndex

Fri, 2024-06-21 20:54

 This video is a step-by-step tutorial to build RAG-powered AI agents using LlamaIndex, CrewAI and OpenAI on your own data.




Code:



!pip install llama-index-core
!pip install llama-index-readers-file
!pip install llama-index-embeddings-openai
!pip install llama-index-llms-llama-api
!pip install 'crewai[tools]'

import os
from crewai import Agent, Task, Crew, Process
from crewai_tools import LlamaIndexTool
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
from llama_index.llms.openai import OpenAI


reader = SimpleDirectoryReader(input_files=["finance.csv"])
docs = reader.load_data()

docs[1].get_content()

from google.colab import userdata
os.environ['OPENAI_API_KEY']=userdata.get('OPENAI_API_KEY')

llm = OpenAI(model="gpt-4o")
index = VectorStoreIndex.from_documents(docs)
query_engine = index.as_query_engine(similarity_top_k=5, llm=llm)

query_tool = LlamaIndexTool.from_query_engine(
    query_engine,
    name="Finance Query Tool",
    description="Use this tool to lookup the financial data of products and their sales",
)

query_tool.args_schema.schema()



researcher = Agent(
    role="Senior Market Analyst",
    goal="Uncover insights about product sales trends",
    backstory="""You work at a market research firm.
  Your goal is to understand sales patterns across different product categories.""",
    verbose=True,
    allow_delegation=False,
    tools=[query_tool],
)
writer = Agent(
    role="Product Content Specialist",
    goal="Craft compelling content on product trends",
    backstory="""You are a renowned Content Specialist, known for your insightful and engaging articles.
  You transform complex sales data into compelling narratives.""",
    verbose=True,
    allow_delegation=False,
)

# Create tasks for your agents
task1 = Task(
    description="""Analyze the sales data of top 5 products in the last quarter.""",
    expected_output="Detailed sales report with trends and insights",
    agent=researcher,
)

task2 = Task(
    description="""Using the insights provided, develop an engaging blog
  post that highlights the top-selling products and their market trends.
  Your post should be informative yet accessible, catering to a casual audience.
  Make it sound cool, avoid complex words.""",
    expected_output="Full blog post of at least 4 paragraphs",
    agent=writer,
)

# Instantiate your crew with a sequential process
crew = Crew(
    agents=[researcher, writer],
    tasks=[task1, task2],
    verbose=2,  # You can set it to 1 or 2 to different logging levels
)

result = crew.kickoff()

print("######################")
print(result)
Categories: DBA Blogs

Create a Local AI Agent with Ollama and Langchain Easily - Tutorial

Thu, 2024-06-20 21:40

 This video is a step-by-step tutorial to create an agentic application with Langchain and Ollama locally with function calling.



Code:

conda create -n agentic python=3.11 -y && conda activate agentic

pip install langchain-experimental

from langchain_experimental.llms.ollama_functions import OllamaFunctions

model = OllamaFunctions(model="llama3:8b", format="json")

model = model.bind_tools(
    tools=[
        {
            "name": "get_current_weather",
            "description": "Get the current weather in a given location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "The city and state, " "e.g. San Francisco, CA",
                    },
                    "unit": {
                        "type": "string",
                        "enum": ["celsius", "fahrenheit"],
                    },
                },
                "required": ["location"],
            },
        }
    ],
    function_call={"name": "get_current_weather"},
)

from langchain_core.messages import HumanMessage

model.invoke("what is the weather in Boston?")
Categories: DBA Blogs

AI Agents Monitoring and Analytics with AgentOps and AutoGen

Wed, 2024-06-19 02:00

This video is a step-by-step tutorial to install and integrate AgentOps with AutoGen to monitor, test and replay analytics.



Code:

conda create -n agentops python=3.11 -y && conda activate agentops

pip install pyautogen agentops


export AGENTOPS_API_KEY=""

export OPENAI_API_KEY=""


import agentops
import autogen
from autogen import ConversableAgent, UserProxyAgent, config_list_from_json

agentops.init(tags=["fahdmirza"])

import os

llm_config = {
    "config_list": [{"model": "gpt-4", "api_key": os.environ["OPENAI_API_KEY"]}],
}



assistant = autogen.ConversableAgent("agent", llm_config=llm_config)
user_proxy = UserProxyAgent("user", code_execution_config=False)

agentops.end_session("Success")

================================

import agentops
import autogen
from typing import Annotated, Literal
from autogen import ConversableAgent, register_function
import os

agentops.start_session(tags=["agentictools3"])

Operator = Literal["+", "-", "*", "/"]


def calculator(a: int, b: int, operator: Annotated[Operator, "operator"]) -> int:
    if operator == "+":
        return a + b
    elif operator == "-":
        return a - b
    elif operator == "*":
        return a * b
    elif operator == "/":
        return int(a / b)
    else:
        raise ValueError("Invalid operator")

llm_config = {
    "config_list": [{"model": "gpt-4", "api_key": os.environ["OPENAI_API_KEY"]}],
}


# Create the agent that uses the LLM.
assistant = ConversableAgent(
    name="Assistant",
    system_message="You are a helpful AI assistant. "
    "You can help with simple calculations. "
    "Return 'TERMINATE' when the task is done.",
    llm_config=llm_config,
)

# The user proxy agent is used for interacting with the assistant agent
# and executes tool calls.
user_proxy = ConversableAgent(
    name="User",
    llm_config=False,
    is_termination_msg=lambda msg: msg.get("content") is not None and "TERMINATE" in msg["content"],
    human_input_mode="NEVER",
)

assistant.register_for_llm(name="calculator", description="A simple calculator")(calculator)
user_proxy.register_for_execution(name="calculator")(calculator)

# Register the calculator function to the two agents.
register_function(
    calculator,
    caller=assistant,  # The assistant agent can suggest calls to the calculator.
    executor=user_proxy,  # The user proxy agent can execute the calculator calls.
    name="calculator",  # By default, the function name is used as the tool name.
    description="A simple calculator",  # A description of the tool.
)

# Let the assistant start the conversation.  It will end when the user types exit.
user_proxy.initiate_chat(assistant, message="What is (1423 - 123) / 3 + (32 + 23) * 5?")

agentops.end_session("Success")

Categories: DBA Blogs

Convert Text to SQL with Ollama and Vanna with Any Database - Local and Free

Mon, 2024-06-17 17:29

 

This video is a step-by-step tutorial to install Vanna.ai locally with Ollama and with your own custom database to chat with database and convert text to SQL using AI.




Code:

import sqlite3
import pandas as pd

df=pd.read_csv('/home/Ubuntu/finance.csv')

conn=sqlite3.connect('financedb.db')

df = pd.read_sql_query("SELECT * FROM finance", conn)

# Print the DataFrame
print(df)

# Close the connection
conn.close()

%pip install 'vanna[chromadb,ollama]'
from vanna.ollama import Ollama
from vanna.chromadb import ChromaDB_VectorStore
class MyVanna(ChromaDB_VectorStore, Ollama):
    def __init__(self, config=None):
        ChromaDB_VectorStore.__init__(self, config=config)
        Ollama.__init__(self, config=config)

vn = MyVanna(config={'model': 'mistral'})

vn.connect_to_sqlite('financedb.db')

df_ddl = vn.run_sql("SELECT type, sql FROM sqlite_master WHERE sql is not null")

for ddl in df_ddl['sql'].to_list():
  vn.train(ddl=ddl)
 
# The following are methods for adding training data. Make sure you modify the examples to match your database.

# DDL statements are powerful because they specify table names, colume names, types, and potentially relationships
vn.train(ddl="""
    CREATE TABLE IF NOT EXISTS myproducts (
        id INT PRIMARY KEY,
        productname VARCHAR(100),
        Segment INT
    )
""")

# Sometimes you may want to add documentation about your business terminology or definitions.
vn.train(documentation="Our business defines deals with various products in various countries related to finance.")

# You can also add SQL queries to your training data. This is useful if you have some queries already laying around. You can just copy and paste those from your editor to begin generating new SQL.
vn.train(sql="SELECT * FROM finance WHERE Product='Montana'")  

# At any time you can inspect what training data the package is able to reference
training_data = vn.get_training_data()
training_data

Whenever you ask a new question, it will find the 10 most relevant pieces of training data and use it as part of the LLM prompt to generate the SQL.
```python
vn.ask(question="How many products are sold in Canada?")

from vanna.flask import VannaFlaskApp
app = VannaFlaskApp(vn)
app.run()




Categories: DBA Blogs

How-To Fine-Tune Stable Diffusion Model Using SimpleTuner on My Own Images Locally

Sat, 2024-06-15 21:50

This video is a step-by-step tutorial to install SimpleTuner Locally and fine-tune stable diffusion 3 medium locally on your own images. 




Code:

conda create -n simpletuner python=3.11 -y

https://github.com/bghira/SimpleTuner.git

cd SimpleTuner

pip install https://github.com/turboderp/exllamav2/releases/download/v0.1.5/exllamav2-0.1.5+cu118.torch2.3.1-cp311-cp311-linux_x86_64.whl

pip install transformers
pip install -U huggingface-hub
pip install xformers torchvision torchaudio

curl -sSL https://install.python-poetry.org | python3 -
pip install -U poetry pip
poetry install --no-root


apt -y install git-lfs
mkdir -p datasets
pushd datasets
    git clone https://huggingface.co/datasets/ptx0/pseudo-camera-10k
popd

-- Get config files from this link : https://github.com/bghira/SimpleTuner/blob/main/documentation/QUICKSTART.md

bash train_sdxl.sh
Categories: DBA Blogs

Easiest Tutorial to Create Agentic Database Application with Your Own Databases

Sat, 2024-06-15 00:03

 This video is a step-by-step tutorial to build from scratch AI-powered database agents to use with your own data.


Code:

!pip install pyodbc==5.1.0
!pip install tabulate==0.9.0
!pip install openai==1.12.0
!pip install langchain==0.1.6
!pip install langchain-community==0.0.20
!pip install langchain-core==0.1.23
!pip install langchain-experimental==0.0.49
!pip install langchain-openai==0.0.5
!pip install pandas==2.2.2

import os
from IPython.display import Markdown, HTML, display
from langchain_openai import OpenAI
from langchain.agents import create_sql_agent
from langchain.agents.agent_toolkits import SQLDatabaseToolkit
from langchain.sql_database import SQLDatabase
from langchain_openai import AzureChatOpenAI
from sqlalchemy import create_engine
import pandas as pd

file_url = "finance.csv"
df = pd.read_csv(file_url).fillna(value = 0)

database_file_path = "test.db"
engine = create_engine(f'sqlite:///{database_file_path}')

df.to_sql(
    'prodinfo',
    con=engine,
    if_exists='replace',
    index=False
)

MSSQL_AGENT_PREFIX = """

You are an agent designed to interact with a SQL database.
## Instructions:
- Given an input question, create a syntactically correct {dialect} query
to run, then look at the results of the query and return the answer.
- Unless the user specifies a specific number of examples they wish to
obtain, **ALWAYS** limit your query to at most {top_k} results.
- You can order the results by a relevant column to return the most
interesting examples in the database.
- Never query for all the columns from a specific table, only ask for
the relevant columns given the question.
- You have access to tools for interacting with the database.
- You MUST double check your query before executing it.If you get an error
while executing a query,rewrite the query and try again.
- DO NOT make any DML statements (INSERT, UPDATE, DELETE, DROP etc.)
to the database.
- DO NOT MAKE UP AN ANSWER OR USE PRIOR KNOWLEDGE, ONLY USE THE RESULTS
OF THE CALCULATIONS YOU HAVE DONE.
- Your response should be in Markdown. However, **when running  a SQL Query
in "Action Input", do not include the markdown backticks**.
Those are only for formatting the response, not for executing the command.
- ALWAYS, as part of your final answer, explain how you got to the answer
on a section that starts with: "Explanation:". Include the SQL query as
part of the explanation section.
- If the question does not seem related to the database, just return
"I don\'t know" as the answer.
- Only use the below tools. Only use the information returned by the
below tools to construct your query and final answer.
- Do not make up table names, only use the tables returned by any of the
tools below.

## Tools:

"""

MSSQL_AGENT_FORMAT_INSTRUCTIONS = """

## Use the following format:

Question: the input question you must answer.
Thought: you should always think about what to do.
Action: the action to take, should be one of [{tool_names}].
Action Input: the input to the action.
Observation: the result of the action.
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer.
Final Answer: the final answer to the original input question.

"""

llm = OpenAI(openai_api_key="")

db = SQLDatabase.from_uri(f'sqlite:///{database_file_path}')
toolkit = SQLDatabaseToolkit(db=db, llm=llm)

QUESTION = """Which was the most popular product in Canada in 2014 in terms of Units Sold?"""

agent_executor_SQL = create_sql_agent(
    prefix=MSSQL_AGENT_PREFIX,
    format_instructions = MSSQL_AGENT_FORMAT_INSTRUCTIONS,
    llm=llm,
    toolkit=toolkit,
    top_k=30,
    verbose=True
)

agent_executor_SQL.invoke(QUESTION)
Categories: DBA Blogs

How-To Train Stable Diffusion 3 Medium On Your Image Dataset Locally

Thu, 2024-06-13 02:25

This video is a step-by-step tutorial to fine-tune Stable Diffusion 3 Medium locally on your own custom image dataset. 

 



Code:

conda create -n sdft python=3.11 -y

pip install peft
pip install datasets
pip install huggingface_hub
pip install wandb
pip install bitsandbytes
pip install pillow
pip install git+https://github.com/huggingface/transformers
pip install accelerate
pip install sentencepiece

git clone https://github.com/huggingface/diffusers
cd diffusers
pip install -e .

cd examples/dreambooth

pip install -r requirements_sd3.txt

huggingface-cli login            

accelerate config default



from huggingface_hub import snapshot_download

mkdir /home/Ubuntu/dog
local_dir = "/home/Ubuntu/dog"
snapshot_download(
    "diffusers/dog-example",
    local_dir=local_dir, repo_type="dataset",
    ignore_patterns=".gitattributes",
)

============

export MODEL_NAME="stabilityai/stable-diffusion-3-medium-diffusers"
export INSTANCE_DIR="/home/Ubuntu/dog"
export OUTPUT_DIR="trained-sd3-lora"

accelerate launch train_dreambooth_lora_sd3.py \
  --pretrained_model_name_or_path=$MODEL_NAME  \
  --instance_data_dir=$INSTANCE_DIR \
  --output_dir=$OUTPUT_DIR \
  --mixed_precision="fp16" \
  --instance_prompt="a photo of sks dog" \
  --resolution=512 \
  --train_batch_size=1 \
  --gradient_accumulation_steps=4 \
  --learning_rate=1e-5 \
  --report_to="wandb" \
  --lr_scheduler="constant" \
  --lr_warmup_steps=0 \
  --max_train_steps=500 \
  --validation_prompt="A photo of sks dog in a bucket" \
  --validation_epochs=25 \
  --seed="0" \
  --push_to_hub


Categories: DBA Blogs

Qwen-Agents to Build AI Agents with Qwen Models Locally - Step by Step Tutorial

Tue, 2024-06-11 16:16

 This video is a quick easy tutorial to install Qwen-Agent locally and create agentic applications with Qwen2 and Ollama for function calling and RAG.


Code:


conda create -n qwenagent python=3.11 -y

pip install -U qwen-agent

import json
import os

from qwen_agent.llm import get_chat_model


# Example dummy function hard coded to return the same weather
# In production, this could be your backend API or an external API
def get_current_weather(location, unit='fahrenheit'):
    """Get the current weather in a given location"""
    if 'tokyo' in location.lower():
        return json.dumps({'location': 'Tokyo', 'temperature': '10', 'unit': 'celsius'})
    elif 'san francisco' in location.lower():
        return json.dumps({'location': 'San Francisco', 'temperature': '72', 'unit': 'fahrenheit'})
    elif 'paris' in location.lower():
        return json.dumps({'location': 'Paris', 'temperature': '22', 'unit': 'celsius'})
    else:
        return json.dumps({'location': location, 'temperature': 'unknown'})


def test():
    llm = get_chat_model({
        'model': 'qwen2:7b',
        'model_server': 'http://localhost:11434/v1',  # api_base
        'api_key': 'EMPTY',
    })

    # Step 1: send the conversation and available functions to the model
    messages = [{'role': 'user', 'content': "What's the weather like in San Francisco?"}]
    functions = [{
        'name': 'get_current_weather',
        'description': 'Get the current weather in a given location',
        'parameters': {
            'type': 'object',
            'properties': {
                'location': {
                    'type': 'string',
                    'description': 'The city and state, e.g. San Francisco, CA',
                },
                'unit': {
                    'type': 'string',
                    'enum': ['celsius', 'fahrenheit']
                },
            },
            'required': ['location'],
        },
    }]

    print('# Assistant Response 1:')
    responses = []
    for responses in llm.chat(messages=messages, functions=functions, stream=True):
        print(responses)

    messages.extend(responses)  # extend conversation with assistant's reply

    # Step 2: check if the model wanted to call a function
    last_response = messages[-1]
    if last_response.get('function_call', None):

        # Step 3: call the function
        # Note: the JSON response may not always be valid; be sure to handle errors
        available_functions = {
            'get_current_weather': get_current_weather,
        }  # only one function in this example, but you can have multiple
        function_name = last_response['function_call']['name']
        function_to_call = available_functions[function_name]
        function_args = json.loads(last_response['function_call']['arguments'])
        function_response = function_to_call(
            location=function_args.get('location'),
            unit=function_args.get('unit'),
        )
        print('# Function Response:')
        print(function_response)

        # Step 4: send the info for each function call and function response to the model
        messages.append({
            'role': 'function',
            'name': function_name,
            'content': function_response,
        })  # extend conversation with function response

        print('# Assistant Response 2:')
        for responses in llm.chat(
                messages=messages,
                functions=functions,
                stream=True,
        ):  # get a new response from the model where it can see the function response
            print(responses)


if __name__ == '__main__':
    test()
Categories: DBA Blogs

Fine-Tune an Audio Model in Any Language on Your Own Audio Files

Tue, 2024-06-11 02:49

 This video is a step-by-step tutorial to fine-tune or train your own AI model in any language on any custom dataset locally easily. This example fine-tunes a model in Urdu audio files locally.





Code:



conda create -n audio python=3.11 -y
conda activate audio
pip install torch
pip install transformers
pip install datasets
pip install huggingface_hub
pip install soundfile
pip install librosa
pip install evaluate jiwer
pip install accelerate -U
pip install tensorboardX

git clone https://github.com/libsndfile/libsndfile.git
cd libsndfile/
sudo apt install autoconf autogen automake build-essential libasound2-dev   libflac-dev libogg-dev libtool libvorbis-dev libopus-dev libmp3lame-dev   libmpg123-dev pkg-config python3
autoreconf -vif
./configure --enable-werror
sudo make
sudo make check
sudo mkdir /usr/local/lib/python3.8/dist-packages/_soundfile_data/

from huggingface_hub.hf_api import HfFolder
HfFolder.save_token('<>')

from datasets import load_dataset, DatasetDict

common_voice = DatasetDict()

common_voice["train"] = load_dataset(
    "mozilla-foundation/common_voice_13_0", "ur", split="train+validation"
)
common_voice["test"] = load_dataset(
    "mozilla-foundation/common_voice_13_0", "ur", split="test"
)

print(common_voice)

common_voice = common_voice.select_columns(["audio", "sentence"])

from transformers import WhisperProcessor

processor = WhisperProcessor.from_pretrained(
    "openai/whisper-small", language="urdu", task="transcribe"
)

common_voice["train"].features

from datasets import Audio

sampling_rate = processor.feature_extractor.sampling_rate
common_voice = common_voice.cast_column("audio", Audio(sampling_rate=sampling_rate))

def prepare_dataset(example):
    audio = example["audio"]
    example = processor(audio=audio["array"],sampling_rate=audio["sampling_rate"],text=example["sentence"],)
    example["input_length"] = len(audio["array"]) / audio["sampling_rate"]
    return example

common_voice = common_voice.map(
    prepare_dataset, remove_columns=common_voice.column_names["train"], num_proc=1
)  

max_input_length = 30.0

def is_audio_in_length_range(length):
    return length < max_input_length

common_voice["train"] = common_voice["train"].filter(
    is_audio_in_length_range,
    input_columns=["input_length"],
)
   
import torch

from dataclasses import dataclass
from typing import Any, Dict, List, Union

@dataclass
class DataCollatorSpeechSeq2SeqWithPadding:
    processor: Any
    def __call__(
        self, features: List[Dict[str, Union[List[int], torch.Tensor]]]
    ) -> Dict[str, torch.Tensor]:
        input_features = [{"input_features": feature["input_features"][0]} for feature in features]
        batch = self.processor.feature_extractor.pad(input_features, return_tensors="pt")
        label_features = [{"input_ids": feature["labels"]} for feature in features]
        labels_batch = self.processor.tokenizer.pad(label_features, return_tensors="pt")
        labels = labels_batch["input_ids"].masked_fill(labels_batch.attention_mask.ne(1), -100)
        if (labels[:, 0] == self.processor.tokenizer.bos_token_id).all().cpu().item():
            labels = labels[:, 1:]
        batch["labels"] = labels
        return batch    

data_collator = DataCollatorSpeechSeq2SeqWithPadding(processor=processor)

import evaluate

metric = evaluate.load("wer")

from transformers.models.whisper.english_normalizer import BasicTextNormalizer

normalizer = BasicTextNormalizer()


def compute_metrics(pred):
    pred_ids = pred.predictions
    label_ids = pred.label_ids
    label_ids[label_ids == -100] = processor.tokenizer.pad_token_id
    pred_str = processor.batch_decode(pred_ids, skip_special_tokens=True)
    label_str = processor.batch_decode(label_ids, skip_special_tokens=True)
    wer_ortho = 100 * metric.compute(predictions=pred_str, references=label_str)
    pred_str_norm = [normalizer(pred) for pred in pred_str]
    label_str_norm = [normalizer(label) for label in label_str]
    pred_str_norm = [pred_str_norm[i] for i in range(len(pred_str_norm)) if len(label_str_norm[i]) > 0]
    label_str_norm = [label_str_norm[i] for i in range(len(label_str_norm)) if len(label_str_norm[i]) > 0 ]
    wer = 100 * metric.compute(predictions=pred_str_norm, references=label_str_norm)
    return {"wer_ortho": wer_ortho, "wer": wer}
   
from transformers import WhisperForConditionalGeneration

model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-small")

from functools import partial
model.config.use_cache = False
model.generate = partial(model.generate, language="urdu", task="transcribe", use_cache=True)


from transformers import Seq2SeqTrainingArguments

training_args = Seq2SeqTrainingArguments(
    output_dir="./whisper-small-ur",
    per_device_train_batch_size=16,
    gradient_accumulation_steps=1,
    learning_rate=1e-5,
    lr_scheduler_type="constant_with_warmup",
    warmup_steps=50,
    max_steps=500,
    gradient_checkpointing=True,
    fp16=True,
    fp16_full_eval=True,
    evaluation_strategy="steps",
    per_device_eval_batch_size=16,
    predict_with_generate=True,
    generation_max_length=225,
    save_steps=500,
    eval_steps=500,
    logging_steps=25,
    report_to=["tensorboard"],
    load_best_model_at_end=True,
    metric_for_best_model="wer",
    greater_is_better=False,
    push_to_hub=True,
)

from transformers import Seq2SeqTrainer

trainer = Seq2SeqTrainer(
    args=training_args,
    model=model,
    train_dataset=common_voice["train"],
    eval_dataset=common_voice["test"],
    data_collator=data_collator,
    compute_metrics=compute_metrics,
    tokenizer=processor,
)

trainer.train()

from datasets import load_dataset
from datasets import Audio

minds = load_dataset("mozilla-foundation/common_voice_13_0", name="ur", split="train")
minds = minds.cast_column("audio", Audio(sampling_rate=16_000))

example = minds[0]
print(example["sentence"])

from transformers import pipeline
pipe = pipeline(task="automatic-speech-recognition", model="./whisper-small-ur")
print(pipe(example["audio"]["array"]))
Categories: DBA Blogs

How-To Fine-Tune Qwen2 Model On My Own Data for Free

Fri, 2024-06-07 04:10

 This video shows a step-by-step process to fine-tune Qwen2 LLM locally on custom dataset for free using UnslothUnsloth easily on Google Colab.


Code:

%%capture
# Installs Unsloth, Xformers (Flash Attention) and all other packages!
!pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"
!pip install --no-deps xformers "trl<0.9.0" peft accelerate bitsandbytes

from unsloth import FastLanguageModel
import torch
max_seq_length = 2048
dtype = None
load_in_4bit = True

fourbit_models = [
    "unsloth/Qwen2-0.5b-bnb-4bit",
]

model, tokenizer = FastLanguageModel.from_pretrained(
    model_name = "unsloth/Qwen2-0.5B",
    max_seq_length = max_seq_length,
    dtype = dtype,
    load_in_4bit = load_in_4bit,
)


model = FastLanguageModel.get_peft_model(
    model,
    r = 16,
    target_modules = ["q_proj", "k_proj", "v_proj", "o_proj",
                      "gate_proj", "up_proj", "down_proj",],
    lora_alpha = 16,
    lora_dropout = 0,
    bias = "none",
    use_gradient_checkpointing = "unsloth",
    random_state = 3407,
    use_rslora = False,
    loftq_config = None,
)


alpaca_prompt = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.

### Instruction:
{}

### Input:
{}

### Response:
{}"""

EOS_TOKEN = tokenizer.eos_token # Must add EOS_TOKEN
def formatting_prompts_func(examples):
    instructions = examples["instruction"]
    inputs       = examples["input"]
    outputs      = examples["output"]
    texts = []
    for instruction, input, output in zip(instructions, inputs, outputs):
        text = alpaca_prompt.format(instruction, input, output) + EOS_TOKEN
        texts.append(text)
    return { "text" : texts, }
pass

from datasets import load_dataset
dataset = load_dataset("yahma/alpaca-cleaned", split = "train")
dataset = dataset.map(formatting_prompts_func, batched = True,)

from trl import SFTTrainer
from transformers import TrainingArguments
from unsloth import is_bfloat16_supported

trainer = SFTTrainer(
    model = model,
    tokenizer = tokenizer,
    train_dataset = dataset,
    dataset_text_field = "text",
    max_seq_length = max_seq_length,
    dataset_num_proc = 2,
    args = TrainingArguments(
        per_device_train_batch_size = 2,
        gradient_accumulation_steps = 8,

        # Use num_train_epochs = 1, warmup_ratio for full training runs!
        warmup_steps = 20,
        max_steps = 120,

        learning_rate = 5e-5,
        fp16 = not is_bfloat16_supported(),
        bf16 = is_bfloat16_supported(),
        logging_steps = 1,
        optim = "adamw_8bit",
        weight_decay = 0.01,
        lr_scheduler_type = "linear",
        seed = 3407,
        output_dir = "outputs",
    ),
)

trainer_stats = trainer.train()
Categories: DBA Blogs

ChatTTS Text to Speech Model Installation Locally

Wed, 2024-06-05 21:44

 This video shows a step-by-step process to locally install ChatTTS which is a text-to-speech model designed specifically for dialogue scenario such as LLM assistant. It supports both English and Chinese languages.



Code:

conda create -n chattts python=3.11 -y

git clone https://github.com/2noise/ChatTTS.git

cd ChatTTS

pip install -r requirements.txt
pip install gradio
pip install pynini
pip install nemo_text_processing
pip install WeTextProcessing
pip install torchaudio


import torch
torch._dynamo.config.cache_size_limit = 64
torch._dynamo.config.suppress_errors = True
torch.set_float32_matmul_precision('high')
import torchaudio
import ChatTTS
from IPython.display import Audio
import warnings
warnings.filterwarnings('ignore')

texts = ["hello, this is Fahd , and AI Youtuber. hahahaha. Subscribe to the channel please and also share it among your network",]

chat = ChatTTS.Chat()
chat.load_models()
       
wavs = chat.infer(texts)

torchaudio.save("/home/Ubuntu/audiooutput/myaudio1.wav", torch.from_numpy(wavs[0]), 24000)
Categories: DBA Blogs

Integrate Local LLMs with Mobile and Web Apps

Tue, 2024-06-04 02:55

 This video installs Firebase Genkit locally which is a framework with powerful tooling to help app developers build, test, deploy, and monitor AI-powered features.


Code:

conda create -n genkit python=3.11

node -v
sudo npm cache clean -f
sudo npm install -g n
sudo n stable
sudo n latest
restart the shell
node -v

ollama pull mistral

mkdir genkitmistral
cd genkitmistral

sudo npm i -g genkit
npm init -y
genkit init

then go to src/src and change model to mistral

genkit start
Categories: DBA Blogs

How to Run Llama3 70B on a Single 4GB GPU Locally

Sun, 2024-06-02 23:28

 This video shows how to locally run full Llama3 70B model on 4GB GPU VRAM.




from airllm import AutoModel
MAX_LENGTH = 128
model = AutoModel.from_pretrained("v2ray/Llama-3-70B")
input_text = [        
  'What is the capital of Australia?'    
]
input_tokens = model.tokenizer(input_text,    
  return_tensors="pt",    
  return_attention_mask=False,    
  truncation=True,    
  max_length=MAX_LENGTH,    
  padding=False)

generation_output = model.generate(    
  input_tokens['input_ids'].cuda(),    
  max_new_tokens=20,    
  use_cache=True,    
  return_dict_in_generate=True)

output = model.tokenizer.decode(generation_output.sequences[0])
print(output)
Categories: DBA Blogs

How to Create RAG Pipeline Locally with Free Models on Your Own Data

Sun, 2024-06-02 21:14

This video is a step-by-step tutorial to locally install FastRAG with Haystack to build a local RAG pipeline on your own custom data.




Code:

conda create -n fastrag python=3.11

pip install torch
pip install transformers
pip install haystack-ai
pip install accelerate
pip install llama-cpp-haystack

from haystack import Document, Pipeline
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack.components.retrievers.in_memory import InMemoryBM25Retriever
from haystack.components.rankers import TransformersSimilarityRanker
from haystack.components.builders.prompt_builder import PromptBuilder

document_collection = [{'id': '11457596',
  'text': 'Quest", the "Ultima" series, "EverQuest", the "Warcraft" series, and the "Elder Scrolls" series of games as well as video games set in Middle-earth itself. Research also suggests that some consumers of fantasy games derive their motivation from trying to create an epic fantasy narrative which is influenced by "The Lord of the Rings". In 1965, songwriter Donald Swann, who was best known for his collaboration with Michael Flanders as Flanders & Swann, set six poems from "The Lord of the Rings" and one from "The Adventures of Tom Bombadil" ("Errantry") to music. When Swann met with Tolkien to play the',
  'title': 'The Lord of the Rings'},
 {'id': '11457582',
  'text': 'helped "The Lord of the Rings" become immensely popular in the United States in the 1960s. The book has remained so ever since, ranking as one of the most popular works of fiction of the twentieth century, judged by both sales and reader surveys. In the 2003 "Big Read" survey conducted in Britain by the BBC, "The Lord of the Rings" was found to be the "Nation\'s best-loved book". In similar 2004 polls both Germany and Australia also found "The Lord of the Rings" to be their favourite book. In a 1999 poll of Amazon.com customers, "The Lord of the',
  'title': 'The Lord of the Rings'},
 {'id': '11457540',
  'text': 'of Tolkien\'s works is such that the use of the words "Tolkienian" and "Tolkienesque" has been recorded in the "Oxford English Dictionary". The enduring popularity of "The Lord of the Rings" has led to numerous references in popular culture, the founding of many societies by fans of Tolkien\'s works, and the publication of many books about Tolkien and his works. "The Lord of the Rings" has inspired, and continues to inspire, artwork, music, films and television, video games, board games, and subsequent literature. Award-winning adaptations of "The Lord of the Rings" have been made for radio, theatre, and film. In',
  'title': 'The Lord of the Rings'},
 {'id': '11457587',
  'text': 'has been read as fitting the model of Joseph Campbell\'s "monomyth". "The Lord of the Rings" has been adapted for film, radio and stage. The book has been adapted for radio four times. In 1955 and 1956, the BBC broadcast "The Lord of the Rings", a 13-part radio adaptation of the story. In the 1960s radio station WBAI produced a short radio adaptation. A 1979 dramatization of "The Lord of the Rings" was broadcast in the United States and subsequently issued on tape and CD. In 1981, the BBC broadcast "The Lord of the Rings", a new dramatization in 26',
  'title': 'The Lord of the Rings'},
 {'id': '11457592',
  'text': '"The Lord of the Rings", was released on the internet in May 2009 and has been covered in major media. "Born of Hope", written by Paula DiSante, directed by Kate Madison, and released in December 2009, is a fan film based upon the appendices of "The Lord of the Rings". In November 2017, Amazon acquired the global television rights to "The Lord of the Rings", committing to a multi-season television series. The series will not be a direct adaptation of the books, but will instead introduce new stories that are set before "The Fellowship of the Ring". Amazon said the',
  'title': 'The Lord of the Rings'},
 {'id': '7733817',
  'text': 'The Lord of the Rings Online The Lord of the Rings Online: Shadows of Angmar is a massive multiplayer online role-playing game (MMORPG) for Microsoft Windows and OS X set in a fantasy universe based upon J. R. R. Tolkien\'s Middle-earth writings, taking place during the time period of "The Lord of the Rings". It launched in North America, Australia, Japan, and Europe in 2007. Originally subscription-based, it is free-to-play, with a paid VIP subscription available that provides players various perks.  The game\'s environment is based on "The Lord of the Rings" and "The Hobbit". However, Turbine does not',
  'title': 'The Lord of the Rings Online'},
 {'id': '22198847',
  'text': 'of "The Lord of the Rings", including Ian McKellen, Andy Serkis, Hugo Weaving, Elijah Wood, Ian Holm, Christopher Lee, Cate Blanchett and Orlando Bloom who reprised their roles. Although the "Hobbit" films were even more commercially successful than "The Lord of the Rings", they received mixed reviews from critics. Numerous video games were released to supplement the film series. They include: "," Pinball, "", "", , "", "", "", "", "The Lord of the Rings Online", "", "", "", "Lego The Lord of the Rings", "Guardians of Middle-earth", "", and "".',
  'title': 'The Lord of the Rings (film series)'},
 {'id': '24071573',
  'text': 'Lord of the Rings (musical) The Lord of the Rings is the most prominent of several theatre adaptations of J. R. R. Tolkien\'s epic high fantasy novel of the same name, with music by A. R. Rahman, Christopher Nightingale and the band Värttinä, and book and lyrics by Matthew Warchus and Shaun McKenna. Set in the world of Middle-earth, "The Lord of the Rings" tells the tale of a humble hobbit who is asked to play the hero and undertake a treacherous mission to destroy an evil, magic ring without being seduced by its power. The show was first performed',
  'title': 'Lord of the Rings (musical)'},
 {'id': '11457536',
  'text': 'The Lord of the Rings The Lord of the Rings is an epic high fantasy novel written by English author and scholar J. R. R. Tolkien. The story began as a sequel to Tolkien\'s 1937 fantasy novel "The Hobbit", but eventually developed into a much larger work. Written in stages between 1937 and 1949, "The Lord of the Rings" is one of the best-selling novels ever written, with over 150 million copies sold. The title of the novel refers to the story\'s main antagonist, the Dark Lord Sauron, who had in an earlier age created the One Ring to rule',
  'title': 'The Lord of the Rings'},
 {'id': '13304003',
  'text': "The Lord of the Rings (disambiguation) The Lord of the Rings is a fantasy novel by J. R. R. Tolkien. The title refers to Sauron, the story's main antagonist. The Lord of the Rings may also refer to:",
  'title': 'The Lord of the Rings (disambiguation)'}]

store = InMemoryDocumentStore()

documents = [Document(id=item["id"], content=item["text"], meta={"title": item["title"]}) for item in document_collection]

store.write_documents(documents)

retriever = InMemoryBM25Retriever(
    document_store= store,
    top_k= 10
)    
reranker = TransformersSimilarityRanker(
    model="cross-encoder/ms-marco-MiniLM-L-6-v2",
    top_k=3,
)

from haystack_integrations.components.generators.llama_cpp import LlamaCppGenerator


prompt = """\
Context:
{% for doc in documents %}
    {{ doc.content }}
{% endfor %}
Question: {{query}}
Answer: """

generator = LlamaCppGenerator(
    model="models/marcoroni-7b-v3.Q4_K_M.gguf",
)

generator.warm_up()

pipe = Pipeline()

pipe.add_component("retriever", retriever)
pipe.add_component("reranker", reranker)
pipe.add_component("prompt_builder", PromptBuilder(template=prompt))
pipe.add_component("llamacpp", generator)

pipe.connect("retriever", "reranker.documents")
pipe.connect("reranker", "prompt_builder.documents")
pipe.connect("prompt_builder", "llamacpp")

question = "Who is the main villan in Lord of the Rings?"

result = pipe.run(
    {
        "retriever": {"query": question},
        "reranker": {"query": question},
        "prompt_builder": {"query": question},
        "llamacpp": {"generation_kwargs": {
            "temperature": 0,
            "max_tokens": 20}},
    }
)


print(result["llamacpp"]["replies"][0])




Categories: DBA Blogs

Mobile AI Development: How to Run AI Models on Devices

Sat, 2024-06-01 22:34

 This video shows how you can install and run LLMs on smart devices like mobile phone, tablets, IOT devices, drones, cameras, smart watches etc by using Qualcomm AI Hub.




Code:

pip install torch
pip install transformers
pip install ipywidgets
pip install psutil
pip install huggingface_hub
pip install pillow
pip install qai-hub
pip install qai_hub_models
pip install "qai_hub_models[yolov7]"

huggingface-cli login

qai-hub configure --api_token <token>

qai-hub list-devices

from PIL import Image


from typing import Tuple
import torch
import qai_hub as hub
from qai_hub_models.models.yolov7 import Model as YOLOv7Model

# Load YOLOv7 in PyTorch
torch_model = YOLOv7Model.from_pretrained()
torch_model.eval()

# Trace the PyTorch model using one data point of provided sample inputs to
# torch tensor to trace the model.
example_input = [torch.tensor(data[0]) for name, data in torch_model.sample_inputs().items()]
pt_model = torch.jit.trace(torch_model, example_input)

# Select a device
device = hub.Device("Samsung Galaxy S23")

# Compile model for a specific device
compile_job = hub.submit_compile_job(
    model=pt_model,
    device=device,
    input_specs=torch_model.get_input_spec(),
)

# Get target model to run on a cloud hosted device
target_model = compile_job.get_target_model()

# Profile the previously compiled model on a cloud hosted device
profile_job = hub.submit_profile_job(
    model=target_model,
    device=device,
)

# Perform on-device inference on a cloud hosted device
input_data = torch_model.sample_inputs()
inference_job = hub.submit_inference_job(
    model=target_model,
    device=device,
    inputs=input_data,
)

# Returns the output as dict{name: numpy}
on_device_output = inference_job.download_output_data()

print(on_device_output)

Categories: DBA Blogs

Free Local RAG Pipeline - Step-by-Step Tutorial

Fri, 2024-05-31 23:19

 This video is a step-by-step tutorial to locally install BeyondLLM with Ollama. Beyond LLM offers an all-in-one toolkit for experimentation, evaluation, and deployment of Retrieval-Augmented Generation (RAG) systems.


Code:


conda create -n beyondllm python=3.11

pip install beyondllm
pip install llama-index-embeddings-huggingface
pip install ollama

from beyondllm.source import fit
data = fit(path="mypdf.pdf", dtype="pdf")

from beyondllm.embeddings import HuggingFaceEmbeddings
embed_model = HuggingFaceEmbeddings(model_name="BAAI/bge-small-en-v1.5")

from beyondllm.retrieve import auto_retriever

retriever = auto_retriever(                            
    data=data,
    embed_model=embed_model,
    type="normal",
    top_k=5
 )
 
from beyondllm.llms import OllamaModel
llm = OllamaModel(model="mistral")

user_prompt = "who is fahd mirza?"
system_prompt = "You are an AI assistant...."

from beyondllm import generator
pipeline = generator.Generate(question=user_prompt, system_prompt = system_prompt, llm = llm, retriever=retriever)

print(pipeline.call())


print(pipeline.get_context_relevancy())
print(pipeline.get_answer_relevancy())
print(pipeline.get_groundedness())

print(pipeline.get_rag_triad_evals())
Categories: DBA Blogs

How to do RAG with Local Free Models Using RAPTOR with Own Files

Fri, 2024-05-31 19:58

This video is a step-by-step tutorial to locally install RAPTOR and use it with local free models. Raptor introduces a novel approach to retrieval-augmented language models by constructing a recursive tree structure from documents.



Code:



# conda create -n raptor python=3.11
# conda activate raptor
# git clone https://github.com/parthsarthi03/raptor.git
# cd raptor
# pip install -r requirements.txt
# pip install sentence-transformer

import os
os.environ["OPENAI_API_KEY"] = "NotApplicable"

import torch
from raptor import BaseSummarizationModel, BaseQAModel, BaseEmbeddingModel, RetrievalAugmentationConfig
from transformers import AutoTokenizer, pipeline
from raptor import RetrievalAugmentation


# You can define your own Summarization model by extending the base Summarization Class.
class GEMMASummarizationModel(BaseSummarizationModel):
    def __init__(self, model_name="google/gemma-2b-it"):
        # Initialize the tokenizer and the pipeline for the GEMMA model
        self.tokenizer = AutoTokenizer.from_pretrained(model_name)
        self.summarization_pipeline = pipeline(
            "text-generation",
            model=model_name,
            model_kwargs={"torch_dtype": torch.bfloat16},
            device=torch.device('cuda' if torch.cuda.is_available() else 'cpu'),  # Use "cpu" if CUDA is not available
        )

    def summarize(self, context, max_tokens=150):
        # Format the prompt for summarization
        messages=[
            {"role": "user", "content": f"Write a summary of the following, including as many key details as possible: {context}:"}
        ]
       
        prompt = self.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
       
        # Generate the summary using the pipeline
        outputs = self.summarization_pipeline(
            prompt,
            max_new_tokens=max_tokens,
            do_sample=True,
            temperature=0.7,
            top_k=50,
            top_p=0.95
        )
       
        # Extracting and returning the generated summary
        summary = outputs[0]["generated_text"].strip()
        return summary
       

class GEMMAQAModel(BaseQAModel):
    def __init__(self, model_name= "google/gemma-2b-it"):
        # Initialize the tokenizer and the pipeline for the model
        self.tokenizer = AutoTokenizer.from_pretrained(model_name)
        self.qa_pipeline = pipeline(
            "text-generation",
            model=model_name,
            model_kwargs={"torch_dtype": torch.bfloat16},
            device=torch.device('cuda' if torch.cuda.is_available() else 'cpu'),
        )

    def answer_question(self, context, question):
        # Apply the chat template for the context and question
        messages=[
              {"role": "user", "content": f"Given Context: {context} Give the best full answer amongst the option to question {question}"}
        ]
        prompt = self.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
       
        # Generate the answer using the pipeline
        outputs = self.qa_pipeline(
            prompt,
            max_new_tokens=256,
            do_sample=True,
            temperature=0.7,
            top_k=50,
            top_p=0.95
        )
       
        # Extracting and returning the generated answer
        answer = outputs[0]["generated_text"][len(prompt):]
        return answer

from sentence_transformers import SentenceTransformer
class SBertEmbeddingModel(BaseEmbeddingModel):
    def __init__(self, model_name="sentence-transformers/multi-qa-mpnet-base-cos-v1"):
        self.model = SentenceTransformer(model_name)

    def create_embedding(self, text):
        return self.model.encode(text)

RAC = RetrievalAugmentationConfig(summarization_model=GEMMASummarizationModel(), qa_model=GEMMAQAModel(), embedding_model=SBertEmbeddingModel())

RA = RetrievalAugmentation(config=RAC)

with open('demo/sample.txt', 'r') as file:
    text = file.read()
   
RA.add_documents(text)

question = "How did Cinderella reach her happy ending?"

answer = RA.answer_question(question=question)

print("Answer: ", answer)

 



Categories: DBA Blogs

How to Install AutoCoder on Google Colab in GGUF Format

Tue, 2024-05-28 00:09

 This video shows a step-by-step process to install AutoCoder in GGUF format on Google Colab for free.




Code:


!wget https://huggingface.co/ukung/AutoCoder_S_6.7B-GGUF/resolve/main/AutoCoder_S_6.7B-q5_k_m.gguf?download=true

import torch

!pip install llama-cpp-python

from llama_cpp import Llama
llm = Llama(
      model_path="/content/AutoCoder_S_6.7B-q5_k_m.gguf",
      n_gpu_layers=32,
      seed=1337,
      n_ctx=2048,
)

output = llm(
      "Q: write a script to say hello? A: ",
      max_tokens=1500,
      stop=["Q:", "\n"],
      echo=True
)
print(output['choices'][0])
Categories: DBA Blogs

Create Local AI Agents with LangGraph Easily with Ollama

Sun, 2024-05-26 00:23

 This video is a step-by-step tutorial to locally create AI agents with Langgraph and Ollama.



Code:


conda create -n langgraph python=3.11

export OPENAI_API_KEY=""
export TAVILY_API_KEY=""

pip install -U langchain-nomic langchain_community tiktoken langchainhub chromadb langchain langgraph tavily-python
pip install langchain-openai

ollama pull mistral

from langchain.prompts import PromptTemplate
from langchain_community.chat_models import ChatOllama
from langchain_core.output_parsers import JsonOutputParser
from langchain_community.tools.tavily_search import TavilySearchResults
from langchain import hub
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent

llm = ChatOpenAI(api_key="ollama",model="mistral",base_url="http://localhost:11434/v1",)

tools = [TavilySearchResults(max_results=1)]

llm_with_tools = llm.bind_tools(tools)

prompt = hub.pull("wfh/react-agent-executor")
prompt.pretty_print()

agent_executor = create_react_agent(llm_with_tools, tools, messages_modifier=prompt)

response=agent_executor.invoke({"messages": [("user", "What is Oracle database")]})

for message in response['messages']:
    print(message.content)

Categories: DBA Blogs

How to Create AI Agents Locally with Custom Data

Sat, 2024-05-25 19:23

 This video is a step-by-step tutorial to locally create AI agents with your own data with LlamaIndex and Ollama.




import json
from llama_index.core.tools import QueryEngineTool, ToolMetadata
from llama_index.core.agent import ReActAgent
from llama_index.llms.ollama import Ollama
from llama_index.core import Settings

from llama_index.core import (
    SimpleDirectoryReader,
    VectorStoreIndex,
    StorageContext,
    load_index_from_storage,
)


llm = Ollama(model="llama3:latest", request_timeout=120.0)
Settings.llm = llm


try:
    storage_context = StorageContext.from_defaults(persist_dir="./storage/oracle")
    dba_index = load_index_from_storage(storage_context)
    index_loaded = True
except:
    index_loaded = False

print (index_loaded)    

oracle_docs = SimpleDirectoryReader(input_files=["oracledocs.pdf"]).load_data()

oracle_index = VectorStoreIndex.from_documents(oracle_docs)

# persist index
oracle_index.storage_context.persist(persist_dir="./storage/oracle")

oracle_engine = oracle_index.as_query_engine(similarity_top_k=3)

query_engine_tools = [
    QueryEngineTool(
        query_engine=oracle_engine,
        metadata=ToolMetadata(
            name="oracledocs",
            description=(
                "Provides information about Oracle. "
                "Use a detailed plain text question as input to the tool."
            ),
        ),
    ),
]


agent = ReActAgent.from_tools(query_engine_tools,llm=llm,verbose=True)

response = agent.chat("What is Oracle?")
print(str(response))

response = agent.chat(
    "What backup options are available in Oracle database?"
)
print(str(response))
Categories: DBA Blogs

Pages