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.comBlogger556125
Updated: 3 weeks 2 days ago

How to Implement Tool Use with Command R Plus Model Locally

Tue, 2024-04-30 03:24

 This video is a simple hands-on tutorial to show how to implement Tool Use with your own data locally by using Cohere Comand R+ model.





Code:




# pip install cohere
# export COHERE_API_KEY="<>"

import cohere
import os
import json

# Mock database containing daily weather reports
weather_database = {
    'Sydney': {
        '2023-09-28': {
            'weather': 'Sunny',
            'temperature': 22,
        },
        '2023-09-29': {
            'weather': 'Rainy',
            'temperature': 18,
        },
        '2023-09-30': {
            'weather': 'Cloudy',
            'temperature': 20,
        }
    },
    'Tokyo': {
        '2023-09-28': {
            'weather': 'Sunny',
            'temperature': 25,
        },
        '2023-09-29': {
            'weather': 'Cloudy',
            'temperature': 22,
        },
        '2023-09-30': {
            'weather': 'Rainy',
            'temperature': 20,
        }
    }
}

# Mock city catalog
city_catalog = {
    'Sydney': {
        'country': 'Australia',
        'population': 5000000,
    },
    'Tokyo': {
        'country': 'Japan',
        'population': 10000000,
    }
}

def query_daily_weather_report(city: str, date: str) -> dict:
    """
    Function to retrieve the weather report for the given city and date
    """
    report = weather_database.get(city, {}).get(date, {})
    if report:
        return {
            'city': city,
            'date': date,
            'summary': f"Weather: {report['weather']}, Temperature: {report['temperature']}°C"
        }
    else:
        return {
            'city': city,
            'date': date,
            'summary': 'No weather data available for this city and date.'
        }

def query_city_catalog(city: str) -> dict:
    """
    Function to retrieve information about the given city
    """
    info = city_catalog.get(city, {})
    return {
        'city': city,
        'info': info
    }

functions_map = {
    "query_daily_weather_report": query_daily_weather_report,
    "query_city_catalog": query_city_catalog
}

tools = [
    {
        "name": "query_daily_weather_report",
        "description": "Connects to a database to retrieve weather information for a given city and date.",
        "parameter_definitions": {
            "city": {
                "description": "Retrieves weather data for this city.",
                "type": "str",
                "required": True
            },
            "date": {
                "description": "Retrieves weather data for this date, formatted as YYYY-MM-DD.",
                "type": "str",
                "required": True
            }
        }
    },
    {
        "name": "query_city_catalog",
        "description": "Connects to a city catalog with information about cities, countries, and populations.",
        "parameter_definitions": {
            "city": {
                "description": "Retrieves city information data for this city.",
                "type": "str",
                "required": True
            }
        }
    }
]

preamble = """
## Task & Context
You help people answer their questions and other requests interactively. You will be asked a very wide array of requests on all kinds of topics. You will be equipped with a wide range of search engines or similar tools to help you, which you use to research your answer. You should focus on serving the user's needs as best you can, which will be wide-ranging.

## Style Guide
Unless the user asks for a different style of answer, you should answer in full sentences, using proper grammar and spelling.
"""

message = "Can you provide a weather summary for Sydney on 29th September 2023, and also give me some details about the city of Tokyo, such as its country and population?"

api_key = os.getenv('COHERE_API_KEY')
co = cohere.Client(api_key)

response = co.chat(
    message=message,
    tools=tools,
    preamble=preamble,
    model="command-r"
)

print("The model recommends doing the following tool calls:")
print("\n".join(str(tool_call) for tool_call in response.tool_calls))

tool_results = []
# Iterate over the tool calls generated by the model
for tool_call in response.tool_calls:
    # here is where you would call the tool recommended by the model, using the parameters recommended by the model
    print(f"= running tool {tool_call.name}, with parameters: {tool_call.parameters}")
    output = functions_map[tool_call.name](**tool_call.parameters)
    # store the output in a list
    outputs = [output]
    print(f"== tool results: {outputs}")
    # create a new dictionary with the relevant information
    tool_result = {
        "call": {"name": tool_call.name},
        "outputs": outputs
    }
    # add the new dictionary to the list
    tool_results.append(tool_result)

print("Tool results that will be fed back to the model in step 4:")
print(json.dumps(tool_results, indent=4))

response = co.chat(
    message=message,
    tools=tools,
    tool_results=tool_results,
    preamble=preamble,
    model="command-r",
    temperature=0.3
)

print("Final answer:")
print(response.text)
Categories: DBA Blogs

Most Easy Way to Implement RAG with AI Model

Tue, 2024-04-30 02:31

 This video is a simple hands-on tutorial to show how to implement RAG with your own files locally by using Cohere Comand R+ model.




Code:


#pip install cohere

import cohere
import os

api_key = os.getenv('COHERE_API_KEY')
co = cohere.Client(api_key)

def read_file(filename):
    try:
        with open(filename, 'r') as file:
            content = file.read()
        return content
    except FileNotFoundError:
        print(f"{filename} not found in the current directory.")
        return None
   
def generate_rag_response_with_citations(query, documents):
    response = co.chat(
        model="command-r",
        message=query,
        documents=documents
    )  

    # Extracting text and citations from the response
    response_text = response.text
    citations = response.citations

    return response_text, citations

filename1 = 'file1.txt'
file_content1 = read_file(filename1)

filename2 = 'file2.txt'
file_content2 = read_file(filename2)


documents = [
    { "title": "Whois", "text": file_content1},
    { "title": "Location", "text": file_content2}
]

#query = "Where Fahd Mirza lives?"
query = "Who is Fahd Mirza?"

text,citation=generate_rag_response_with_citations(query,documents)
print(text)
print(citation)
Categories: DBA Blogs

Use Hugging Face API Locally for Free Model Access

Fri, 2024-04-26 18:39

 This video is a hands-on step-by-step tutorial with code to show you how to use hugging face inference API locally for free.




Code:


#pip install huggingface_hub

#export HF_TOKEN="<>"


from huggingface_hub import InferenceClient

import json


repo_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"


llm_client = InferenceClient(

    model=repo_id,

    timeout=120,

)


def call_llm(inference_client: InferenceClient, prompt: str):

    response = inference_client.post(

        json={

            "inputs": prompt,

            "parameters": {"max_new_tokens": 200},

            "task": "text-generation",

        },

    )

    return json.loads(response.decode())[0]["generated_text"]



response=call_llm(llm_client, "write me a crazy joke")

print (response)

Categories: DBA Blogs

Instant Voice Cloning with AI - Install OpenVoice Locally

Fri, 2024-04-26 18:37

 This video is a hands-on step-by-step tutorial to install OpenVoice v2 locally to clone voice with AI. 




Code: (OpenVoice/demo_part3.ipynb at main · myshell-ai/OpenVoice (github.com))

conda create -n openvoice python=3.9

conda activate openvoice

git clone git@github.com:myshell-ai/OpenVoice.git

cd OpenVoice

pip install -e .


wget https://myshell-public-repo-hosting.s3.amazonaws.com/openvoice/checkpoints_1226.zip


pip install git+https://github.com/myshell-ai/MeloTTS.git

python -m unidic download


import os

import torch

from openvoice import se_extractor

from openvoice.api import ToneColorConverter


ckpt_converter = 'checkpoints_v2/converter'

device = "cuda:0" if torch.cuda.is_available() else "cpu"

output_dir = 'outputs_v2'


tone_color_converter = ToneColorConverter(f'{ckpt_converter}/config.json', device=device)

tone_color_converter.load_ckpt(f'{ckpt_converter}/checkpoint.pth')


os.makedirs(output_dir, exist_ok=True)


reference_speaker = 'resources/example_reference.mp3' # This is the voice you want to clone

target_se, audio_name = se_extractor.get_se(reference_speaker, tone_color_converter, vad=False)


from melo.api import TTS


texts = {

    'EN_NEWEST': "Did you ever hear a folk tale about a giant turtle?",  # The newest English base speaker model

    'EN': "Did you ever hear a folk tale about a giant turtle?",

    'ES': "El resplandor del sol acaricia las olas, pintando el cielo con una paleta deslumbrante.",

    'FR': "La lueur dorée du soleil caresse les vagues, peignant le ciel d'une palette éblouissante.",

    'ZH': "在这次vacation中,我们计划去Paris欣赏埃菲尔铁塔和卢浮宫的美景。",

    'JP': "彼は毎朝ジョギングをして体を健康に保っています。",

    'KR': "안녕하세요! 오늘은 날씨가 정말 좋네요.",

}



src_path = f'{output_dir}/tmp.wav'


# Speed is adjustable

speed = 1.0


for language, text in texts.items():

    model = TTS(language=language, device=device)

    speaker_ids = model.hps.data.spk2id

    

    for speaker_key in speaker_ids.keys():

        speaker_id = speaker_ids[speaker_key]

        speaker_key = speaker_key.lower().replace('_', '-')

        

        source_se = torch.load(f'checkpoints_v2/base_speakers/ses/{speaker_key}.pth', map_location=device)

        model.tts_to_file(text, speaker_id, src_path, speed=speed)

        save_path = f'{output_dir}/output_v2_{speaker_key}.wav'


        # Run the tone color converter

        encode_message = "@MyShell"

        tone_color_converter.convert(

            audio_src_path=src_path, 

            src_se=source_se, 

            tgt_se=target_se, 

            output_path=save_path,

            message=encode_message)

Categories: DBA Blogs

Easily Fine-Tune Any Model for Free Locally - No Machine Learning Required

Thu, 2024-04-25 22:58

 This video is a hands-on step-by-step tutorial to install AutoTrain by hugging face on local system and then fine-tune any model on custom dataset.





$ conda create -n autotrain python=3.10

$ conda activate autotrain

$ pip install autotrain-advanced

$ conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia

$ conda install -c "nvidia/label/cuda-12.1.0" cuda-nvcc

$ export HF_TOKEN=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

$ autotrain app --host 0.0.0.0 --port 8000


Dataset Used:


text,label

"This movie was fantastic!",positive

"The food at that restaurant was terrible.",negative

"I love this song!",positive

"The customer service was excellent.",positive

"I couldn't finish reading this book.",negative

Categories: DBA Blogs

How to Quantize a Model with Hugging Face Quanto

Thu, 2024-04-18 03:55

 This video is a hands-on step-by-step primer about how to quantize any model using Hugging Face Quanto which is a versatile pytorch quantization toolkit.



!pip install transformers==4.35.0

!pip install quanto==0.0.11

!pip install torch==2.1.1

!pip install sentencepiece==0.2.0


model_name = "google/flan-t5-small"


import sentencepiece as spm

from transformers import T5Tokenizer, T5ForConditionalGeneration

tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-small")


model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-small")


input_text = "Meaning of happiness is "

input_ids = tokenizer(input_text, return_tensors="pt").input_ids


outputs = model.generate(input_ids)

print(tokenizer.decode(outputs[0]))


from helper import compute_module_sizes

module_sizes = compute_module_sizes(model)

print(f"The model size is {module_sizes[''] * 1e-9} GB")


from quanto import quantize, freeze

import torch


quantize(model, weights=torch.int8, activations=None)

freeze(model)


module_sizes = compute_module_sizes(model)

print(f"The model size is {module_sizes[''] * 1e-9} GB")


input_text = "Meaning of happiness is "

input_ids = tokenizer(input_text, return_tensors="pt").input_ids

outputs = model.generate(input_ids)

print(tokenizer.decode(outputs[0]))

Categories: DBA Blogs

How to do RAG in OpenAI GPT4 Locally with File Search

Thu, 2024-04-18 01:50

 This video is a hands-on step-by-step primer about how to use RAG with Open AI File Search. OpenAI now supports RAG which means that now you can attach your own files and custom data to OpenAI assistant and talk to your documents with GPT4. 

Make sure you have installed latest version of openai on your local system:

pip install openai --upgrade

also make sure to have data.txt in the same folder as your script. 

from openai import OpenAI
client = OpenAI()
 
assistant = client.beta.assistants.create(
  name="Personal Assistant",
  instructions="You are an empathetic. Use you knowledge base to answer questions.",
  model="gpt-4-turbo",
  tools=[{"type": "file_search"}],
)

# Create a vector store caled "Personal Data"
vector_store = client.beta.vector_stores.create(
    name="Personal Data",
    expires_after={
         "anchor": "last_active_at",
         "days": 1
    }
)
 
# Ready the files for upload to OpenAI
file_paths = ["data.txt"]
file_streams = [open(path, "rb") for path in file_paths]
 
# Use the upload and poll SDK helper to upload the files, add them to the vector store,
# and poll the status of the file batch for completion.
file_batch = client.beta.vector_stores.file_batches.upload_and_poll(
  vector_store_id=vector_store.id, files=file_streams
)
 
# You can print the status and the file counts of the batch to see the result of this operation.
print(file_batch.status)
print(file_batch.file_counts)

assistant = client.beta.assistants.update(
  assistant_id=assistant.id,
  tool_resources={"file_search": {"vector_store_ids": [vector_store.id]}},
)

thread = client.beta.threads.create(
  tool_resources={"file_search": {"vector_store_ids": [vector_store.id]}},
  messages=[
    {
      "role": "user",
      "content": "Who is Fahd Mirza?",
    }
  ]
)
 
# The thread now has a vector store with that file in its tool resources.
print(thread.tool_resources.file_search)

run = client.beta.threads.runs.create_and_poll(
    thread_id=thread.id, assistant_id=assistant.id
)

messages = list(client.beta.threads.messages.list(thread_id=thread.id, run_id=run.id))

message_content = messages[0].content[0].text
annotations = message_content.annotations
citations = []
for index, annotation in enumerate(annotations):
    message_content.value = message_content.value.replace(annotation.text, f"[{index}]")
    if file_citation := getattr(annotation, "file_citation", None):
        cited_file = client.files.retrieve(file_citation.file_id)
        citations.append(f"[{index}] {cited_file.filename}")

print(message_content.value)
print("\n".join(citations))
Categories: DBA Blogs

How to Use LLM Function Calling Locally for Free

Wed, 2024-04-17 23:38

 Function calling in AI simply means that you can call external APIs from within your AI-powered application. Whenever you read that a model can do function calling, it means that it can take a natural language query of user and convert it to a function call. Then you can execute that function call to your API endpoint to get the data, and give it to LLM as additional context and get more grounded latest response as per your application requirement.




import openai

def Get_Beach_Patrol_Info(name, location):
    if 'Bondi' in name:
        print(f"The beach {name} at {location} is patrolled.")
    elif 'Marley' in name:
        print(f"The beach {name} of {location } is unpatrolled.")
    else:
        print(f"Beach not found.")
       


def get_gorilla_response(prompt="", model="gorilla-openfunctions-v2", functions=[]):
    openai.api_key = "EMPTY"  # Hosted for free with from UC Berkeley
    openai.api_base = "http://luigi.millennium.berkeley.edu:8000/v1"
    try:
        completion = openai.ChatCompletion.create(
            model="gorilla-openfunctions-v2",
            temperature=0.0,
            messages=[{"role": "user", "content": prompt}],
            functions=functions,
        )
        return completion.choices[0]
    except:
        print("error occurred.")

beach_custom_functions = [
    {
        'name': 'Get_Beach_Patrol_Info',
        'description': 'Get name and location of beach from the body of the input text',
        'parameters': {
            'type': 'object',
            'properties': {
                'name': {
                    'type': 'string',
                    'description': 'Name of the beach'
                },
                'location': {
                    'type': 'string',
                    'description': 'Location where beach is located.'
                }
            }
        }
    }
]

beach_1_description="""Bondi Beach is an iconic destination located in Sydney, Australia,
                         renowned for its golden sands and lively atmosphere. It attracts surfers,
                         sunbathers, and tourists alike, offering stunning ocean views and a
                         vibrant coastal culture."""

beach_2_description="""Marley Beach (also known as Big Marley Beach) is a stunning beach in the
                         upper Royal National Park, on the outskirts of southern Sydney, Australia.
                         The beach is one of eleven beaches located within the territory of the
                         Royal National Park."""

beach_description=[beach_1_description,beach_2_description]

for i in beach_description:
    response=get_gorilla_response(prompt=i, functions=beach_custom_functions)
    func=response.message.content  
    print(func)
    eval(func)
   
Categories: DBA Blogs

How to Fix 401 Authorization Required in Oracle Database for Wallet

Wed, 2024-04-17 18:51

 I frequently use Oracle wallet for my databases to store certificates. For one site, I started receiving '401 authorization required' error and it seemed that cert was expired. If you are after TLDR; then following shows how we fixed it.


SQL> select utl_http.request('https://dummysite.com',null,'file:/scripts/src/oracle/wallets','XXXXXXXXXX') from dual;

 

UTL_HTTP.REQUEST('https://dummysite.com',NULL,

--------------------------------------------------------------------------------

401 Authorization Required

 

SQL> select utl_http.request('https://dummysite.com',null,'file:/d01/oracle/prod2db/10.2.0/appsutil/wallet', null) from dual;

 

UTL_HTTP.REQUEST('https://dummysite.com',NULL,

--------------------------------------------------------------------------------

401 Authorization Required


All I did was I added the root certificates in the wallet located at /scripts/src/oracle/wallets for fixing this issue.


For details:


First identify which http call from within the application or database was failing:


SQL> select distinct sql_text,sql_id from v$sql where upper(sql_text) like  '%UTL%HTTP%';


SQL_TEXT

--------------------------------------------------------------------------------

SQL_ID

-------------

select distinct sql_text,sql_id from v$sql where upper(sql_text) like  '%UTL%HTT

P%'

gcgfyfty86c84



SQL> select distinct sql_text,sql_id from v$sqlarea where upper(sql_text) like  '%UTL%HTTP%';


SQL_TEXT

--------------------------------------------------------------------------------

SQL_ID

-------------

select distinct sql_text,sql_id from v$sql where upper(sql_text) like  '%UTL%HTT

P%'

gcgfyfty86c84


select distinct sql_text,sql_id from v$sqlarea where upper(sql_text) like  '%UTL

%HTTP%'

34x064xsfa0dy


and then locate the cert destination and put root certificate from there from your CA.



[oracle@oraapps3 ~]$ cd /scripts/src/oracle/wallets

[oracle@oraapps3 wallets]$ ls -lrt

total 96

-rw-r--r-- 1 oracle oinstall  1115 Aug 22  2023 test2.TESTint.net.cert

-rw-r--r-- 1 oracle oinstall   837 Aug 22  2023 test2

-rw------- 1 oracle oinstall 14765 Jan 25 13:47 ewallet.p12

[oracle@oraapps3 wallets]$ orapki wallet display -wallet .

Enter wallet password:      

   

Requested Certificates: 

User Certificates:

Trusted Certificates: 

Subject:        OU=Class 3 Public Primary Certification Authority,O=VeriSign\, Inc.,C=US

Subject:        CN=Entrust.net Certification Authority (2048),OU=(c) 1999 Entrust.net Limited,OU=www.entrust.net/CPS_2048 incorp. by ref. (limits liab.),O=Entrust.net

Subject:        CN=Entrust.net Secure Server Certification Authority,OU=(c) 2000 Entrust.net Limited,OU=www.entrust.net/SSL_CPS incorp. by ref. (limits liab.),O=Entrust.net

Categories: DBA Blogs

AnythingLLM - Any LLM, Any Document, Full Control, Full Privacy, Local, Secure

Sun, 2024-04-07 18:14

This video shows how to locally install AnythingLLM to privately and securly and remotely run any LLM with any RAG document. It all runs locally with zero required internet connectivity.



Categories: DBA Blogs

A Way to Identify gc block lost wait event' Wait event in Oracle database

Wed, 2024-04-03 17:35

 One of the most annoying Oracle wait event is 'gc block lost wait event'. In this post, I am sharing my approach to resolve this issue in an production environment.

First use the netstat command to get the hang of the system:

% netstat --tcp --numeric  

Active Internet connections (w/o servers)  

Proto Recv-Q Send-Q Local Address           Foreign Address         State       

tcp        0      0 192.168.128.152:993     192.168.128.120:3853   ESTABLISHED

tcp        0      0 192.168.128.152:143     192.168.128.194:3076   ESTABLISHED

tcp        0      0 192.168.128.152:45771   192.168.128.34:389      TIME_WAIT

tcp        0      0 192.168.128.152:110     192.168.33.123:3521     TIME_WAIT

tcp        0      0 192.168.128.152:25      192.168.231.27:44221    TIME_WAIT

tcp        0    256 192.168.128.152:22      192.168.128.78:47258   ESTABLISHED

If you want to see what (TCP) ports your machine is listening on, use netstat --tcp --listening.

Another useful flag to add to this is --programs which indicates which process is listening on the specified port.

The following example shows a machine listening on ports 80 (www), 443 (https), 22 (ssh), and 25 (smtp);


Code Listing 2: netstat --tcp --listening --programs


# sudo netstat --tcp --listening --programs

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address   Foreign Address   State     PID/Program name

tcp        0      0 *:www           *:*               LISTEN    28826/apache2

tcp        0      0 *:ssh           *:*               LISTEN    26604/sshd

tcp        0      0 *:smtp          *:*               LISTEN    6836/

tcp        0      0 *:https         *:*               LISTEN    28826/apache2

Note: Using --all displays both connections and listening ports.


The next example uses netstat --route to display the routing table. For most people, this will show one IP and and the gateway address but if you have more than one interface or have multiple IPs assigned to an interface, this command can help troubleshoot network routing problems.


Code Listing 3: netstat --route


% netstat --route

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0

0.0.0.0         192.168.1.1     0.0.0.0         UG    1      0        0 eth0

The last example of netstat uses the --statistics flag to display networking statistics. Using this flag by itself displays all IP, TCP, UDP, and ICMP connection statistics.

To just show some basic information. For example purposes, only the output from --raw is displayed here.

Combined with the uptime command, this can be used to get an overview of how much traffic your machine is handling on a daily basis.


Code Listing 4: netstat --statistics --route


% netstat --statistics --raw

Ip:

    620516640 total packets received

    0 forwarded

    0 incoming packets discarded

    615716262 incoming packets delivered

    699594782 requests sent out

    5 fragments dropped after timeout

    3463529 reassemblies required

    636730 packets reassembled ok

    5 packet reassembles failed

    310797 fragments created

// ICMP statistics truncated

Note: For verbosity, the long names for the various flags were given. Most can be abbreviated to avoid excessive typing (e.g. netstat -tn, netstat -tlp, netstat -r, and netstat -sw).


and now check the AWR report:


1) 


Top 10 Foreground Events by Total Wait Time

Top 10 Foreground Events by Total Wait Time

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

                                           Total Wait       Wait   % DB Wait

Event                                Waits Time (sec)    Avg(ms)   time Class

------------------------------ ----------- ---------- ---------- ------ --------

DB CPU                                         3691.6              70.3

gc cr block lost                       947      511.7     540.31    9.7 Cluster

library cache lock                  30,871      422.1      13.67    8.0 Concurre

db file sequential read            252,506      189.6       0.75    3.6 User I/O

gc buffer busy acquire               7,745      183.7      23.72    3.5 Cluster

gc cr block busy                    90,856      141.4       1.56    2.7 Cluster

gc cr multi block request            1,768       71.4      40.40    1.4 Cluster

name-service call wait                 398       31.7      79.68     .6 Other

gc cr block 2-way                  165,758       30.5       0.18     .6 Cluster

log file sync                       25,866       20.5       0.79     .4 Commit


Both OS level and AWR level info should tell you if this event is the issue or not and then you can simply look at the sessions contributing to it.

Categories: DBA Blogs

OCI Generative AI Agents

Tue, 2024-04-02 02:53

 Artificial intelligence (AI) is revolutionizing our interactions with the world, presenting both opportunities and complexities for organizations embracing new AI technologies. Nowhere is this more evident than with generative AI, which integrates knowledge assimilation from various sources to automate tasks and boost human creativity and productivity. This transformative technology empowers organizations to summarize documents, create tables, generate meaningful text, develop code, and synthesize innovative ideas.


Oracle's strategic approach revolves around three AI modalities: infrastructure, models and services, and application integration. Oracle has embedded AI across its cloud applications like CRM, ERP, HCM, CX, and EMR to enhance their functionalities. Similarly, Oracle is introducing generative AI capabilities into its database portfolio, akin to its integration of machine learning (ML) features into Oracle Database service and MySQL HeatWave.


Oracle Cloud Infrastructure (OCI) Generative AI Agents harnesses the potential of large language models (LLMs) and retrieval-augmented generation (RAG), enabling users to query extensive enterprise knowledge bases. Soon, users will access real-time information via natural language interfaces for direct action.


OCI Generative AI Agents RAG service empowers employees to glean insights and answers from disparate sources within the company through natural conversations. The service matches semantic similarities between user queries and documents in a centralized embeddings vector store, consolidating data from diverse corporate systems like ERP, HCM, CX, and data lakes. Users receive results using the most current information, even from dynamic data stores, rather than static reports or outdated indexes.


At the heart of this solution is a large language models or LLMs in short, facilitating natural, conversational interactions with users. You can either use Meta's Llama model or a model from Cohere and that list is going to grow. Think of it as a knowledgeable colleague you can ask business questions in everyday language. This is complemented by an embeddings model that generates embeddings vectors for the corporate document corpus stored in the embeddings vector database. 


The retrieval-augmented generation (RAG) system, comprising the vector database, embeddings model, and text generation model, synthesizes responses grounded in retrieved documents. Instead of presenting a list of documents, the AI service delivers natural language responses, supported with references and links to original sources. In the future, users will seamlessly transition from information retrieval to immediate actions like sending emails, contacting customers, or managing orders within the same interface.

Categories: DBA Blogs

Select AI Intro in Oracle Database

Sun, 2024-03-31 02:39

Select AI is probably one of the most magical AI feature in Oracle Autonomous database. I have been using it for some time now and it's feels simply like magic.

Select AI represents a cutting-edge feature within the Oracle Autonomous Database, introduced last year, that empowers users to interact with their data using natural language queries. This innovative capability enables individuals to pose questions and receive insights without needing to comprehend the underlying data storage or SQL formulation. By simply directing queries in languages like English, Spanish, French, Portuguese, and more, users can leverage the full potential of their database.


The underlying technology behind Select AI leverages advanced generative AI models, such as those developed by OpenAI or Cohere, which excel at understanding intent and generating SQL code based on natural language prompts. This approach allows developers to extend and build new natural language applications seamlessly within the SQL framework. Select AI streamlines this process by integrating with Oracle SQL, utilizing a standard SELECT statement followed by AI and the natural language query.


One of the remarkable features of Select AI is its conversational capabilities, enhanced with recent updates. Users can engage in a dialogue with the database, posing follow-up questions that build upon previous interactions. This chat history is leveraged by the AI models to provide context-aware responses, resembling a real-life conversation that facilitates deeper exploration of data insights.


Moreover, Select AI is designed to be flexible and adaptable to various business needs. It supports multiple Large Language Models (LLMs) including Cohere and OpenAI, with potential for expansion to include domain-specific models in the future. This pluggable design allows organizations to select the most suitable LLM for their specific use cases, ensuring optimal performance and alignment with business objectives.


With Autonomous Database Select AI, developers can create AI-enabled applications that comprehend users' natural language queries within a conversational context, enabling secure and efficient access to valuable insights from private data sources. This groundbreaking capability marks a significant leap forward in data analytics, harnessing the power of generative AI and natural language processing to democratize access to database insights.

Categories: DBA Blogs

How to Install OpenDevin Locally

Sat, 2024-03-30 20:00

This is a step by step easy tutorial to locally install OpenDevin which is an open-source project aiming to replicate Devin, an autonomous AI software engineer who is capable of executing complex engineering tasks and collaborating actively with users on software development projects.





Commands Used:


Pre-requisites:


Update OS:

sudo apt-get update


Install git:

sudo apt-get install git


Install Nodejs:

sudo apt-get install nodejs


Install npm:

sudo apt install npm


Install Rust:

curl --proto '=https' --tlsv1.3 https://sh.rustup.rs -sSf | sh


Install docker:

sudo apt-get update

sudo apt-get install ca-certificates curl

sudo install -m 0755 -d /etc/apt/keyrings

sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc

sudo chmod a+r /etc/apt/keyrings/docker.asc


# Add the repository to Apt sources:

echo \

  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \

  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \

  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

sudo apt-get update

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin


Install conda:

wget https://repo.anaconda.com/archive/Anaconda3-2022.05-Linux-x86_64.sh

bash Anaconda3-2022.05-Linux-x86_64.sh


Install uvicorn:

sudo apt install uvicorn


Backend Commands:

git clone https://github.com/OpenDevin/OpenDevin.git

cd OpenDevin

conda create -n opendevin python=3.11

conda activate opendevin 

docker ps

docker pull ghcr.io/opendevin/sandbox

which python

python --version

/home/ubuntu/anaconda3/envs/opendevin/bin/python -m pip install pipenv

/home/ubuntu/anaconda3/envs/opendevin/bin/python -m pipenv install -v

/home/ubuntu/anaconda3/envs/opendevin/bin/python -m pipenv shell

uvicorn opendevin.server.listen:app --port 3000


Frontend Commands:

python -m pipenv requirements > requirements.txt && python -m pip install -r requirements.txt

PYTHONPATH=`pwd` /home/ubuntu/anaconda3/envs/opendevin/bin/python opendevin/main.py -d ./workspace/ -i 100 -t "Write a bash script that prints 'hello world'"

Categories: DBA Blogs

Oracle Database Comprehensive Resource Management Strategy

Thu, 2024-03-21 21:20

 The Oracle Resource Manager is a powerful tool for administrators to maintain system stability, optimize performance, and manage workload prioritization within Oracle Database environments. It provides granular control over resource allocation and ensures that database resources are utilized efficiently to meet business requirements and service level objectives.


Resource Manager is a feature that enables administrators to manage and prioritize system resources such as CPU and memory among different users, applications, sessions, and consumer groups within the database. The Resource Manager allows for the control of resource allocation based on specified policies, ensuring that critical workloads receive adequate resources while preventing resource contention and maintaining overall system performance.


This Oracle script is setting up a comprehensive resource management strategy using Oracle Resource Manager. It defines consumer groups to classify different types of workloads and establishes resource plans with directives to allocate resources and prioritize workload processing based on specified criteria like workload type, time of day, and user groups. This provided Oracle PL/SQL script demonstrates the configuration and utilization of Oracle Resource Manager to manage database resource allocation based on workload characteristics. 


PROCEDURE create_consumer_group(

  consumer_group  IN  VARCHAR2,

  comment         IN  VARCHAR2,

  cpu_mth         IN  VARCHAR2 DEFAULT 'ROUND-ROBIN')


The create_consumer_groups.sql script uses this procedure to create the OLTP and batch consumer groups.


CONN sys AS SYSDBA

BEGIN

  DBMS_RESOURCE_MANAGER.clear_pending_area;

  DBMS_RESOURCE_MANAGER.create_pending_area;


  -- Create the consumer groups

  DBMS_RESOURCE_MANAGER.create_consumer_group(

    consumer_group => 'oltp_consumer_group',

    comment        => 'OLTP process consumer group.');


  DBMS_RESOURCE_MANAGER.create_consumer_group(

    consumer_group => 'batch_consumer_group',

    comment        => 'Batch process consumer group.');


  DBMS_RESOURCE_MANAGER.validate_pending_area;

  DBMS_RESOURCE_MANAGER.submit_pending_area;

END;

/


BEGIN

  DBMS_RESOURCE_MANAGER.clear_pending_area();

  DBMS_RESOURCE_MANAGER.create_pending_area();


  -- Delete consumer groups.

  DBMS_RESOURCE_MANAGER.delete_consumer_group (

    consumer_group => 'oltp_consumer_group'); 


  DBMS_RESOURCE_MANAGER.delete_consumer_group (

    consumer_group => 'batch_consumer_group'); 


  DBMS_RESOURCE_MANAGER.validate_pending_area;

  DBMS_RESOURCE_MANAGER.submit_pending_area();

END;

/


PROCEDURE create_plan (

  plan                      IN  VARCHAR2,

  comment                   IN  VARCHAR2,

  cpu_mth                   IN  VARCHAR2 DEFAULT 'EMPHASIS',

  active_sess_pool_mth      IN  VARCHAR2 DEFAULT 'ACTIVE_SESS_POOL_ABSOLUTE',

  parallel_degree_limit_mth IN  VARCHAR2 DEFAULT 'PARALLEL_DEGREE_LIMIT_ABSOLUTE',

  queueing_mth              IN  VARCHAR2 DEFAULT 'FIFO_TIMEOUT')                       


PROCEDURE create_plan_directive (

  plan                      IN  VARCHAR2,

   group_or_subplan          IN  VARCHAR2,

  comment                   IN  VARCHAR2,

  cpu_p1                    IN  NUMBER DEFAULT NULL,

  cpu_p2                    IN  NUMBER DEFAULT NULL,

  cpu_p3                    IN  NUMBER DEFAULT NULL,

  cpu_p4                    IN  NUMBER DEFAULT NULL,

  cpu_p5                    IN  NUMBER DEFAULT NULL,

  cpu_p6                    IN  NUMBER DEFAULT NULL,

  cpu_p7                    IN  NUMBER DEFAULT NULL,

  cpu_p8                    IN  NUMBER DEFAULT NULL,

  active_sess_pool_p1       IN  NUMBER DEFAULT NULL,

  queueing_p1               IN  NUMBER DEFAULT NULL,

  parallel_degree_limit_p1  IN  NUMBER DEFAULT NULL,

   switch_group              IN  VARCHAR2 DEFAULT NULL,

  switch_time               IN  NUMBER DEFAULT NULL,

  switch_estimate           IN  BOOLEAN DEFAULT FALSE,

  max_est_exec_time         IN  NUMBER DEFAULT NULL,

  undo_pool                 IN  NUMBER DEFAULT NULL,

  max_idle_time             IN  NUMBER DEFAULT NULL,

  max_idle_blocker_time     IN  NUMBER DEFAULT NULL,

  switch_time_in_call       IN  NUMBER DEFAULT NULL)


  BEGIN

  DBMS_RESOURCE_MANAGER.clear_pending_area;

  DBMS_RESOURCE_MANAGER.create_pending_area;


  -- Create a new plan

  DBMS_RESOURCE_MANAGER.create_plan(

    plan    => 'day_plan',

    comment => 'Plan suitable for daytime processing.');


  -- Assign consumer groups to plan and define priorities

  DBMS_RESOURCE_MANAGER.create_plan_directive (

    plan             => 'day_plan',

    group_or_subplan => 'oltp_consumer_group',

     comment          => 'Give OLTP processes higher priority - level 1',

    cpu_p1           => 80,

    switch_group     => 'batch_consumer_group',

    switch_time      => 60);


  DBMS_RESOURCE_MANAGER.create_plan_directive (

    plan             => 'day_plan',

    group_or_subplan => 'batch_consumer_group',

    comment          => 'Give batch processes lower priority - level 2',

    cpu_p2           => 100);


  DBMS_RESOURCE_MANAGER.create_plan_directive(

    plan             => 'day_plan',

    group_or_subplan => 'OTHER_GROUPS',

    comment          => 'all other users - level 3',

    cpu_p3           => 100);


  DBMS_RESOURCE_MANAGER.validate_pending_area;

  DBMS_RESOURCE_MANAGER.submit_pending_area;

END;

/


BEGIN

  DBMS_RESOURCE_MANAGER.clear_pending_area;

  DBMS_RESOURCE_MANAGER.create_pending_area;


  -- Create a new plan

  DBMS_RESOURCE_MANAGER.create_plan(

    plan    => 'night_plan',

    comment => 'Plan suitable for daytime processing.');


  -- Assign consumer groups to plan and define priorities

  DBMS_RESOURCE_MANAGER.create_plan_directive (

    plan             => 'night_plan',

    group_or_subplan => 'batch_consumer_group',

    comment          => 'Give batch processes lower priority - level 2',

    cpu_p1           => 80);


  DBMS_RESOURCE_MANAGER.create_plan_directive (

    plan             => 'night_plan',

    group_or_subplan => 'oltp_consumer_group',

    comment          => 'Give OLTP processes higher priority - level 1',

    cpu_p2           => 100);


  DBMS_RESOURCE_MANAGER.create_plan_directive(

    plan             => 'night_plan',

    group_or_subplan => 'OTHER_GROUPS',

    comment          => 'all other users - level 3',

    cpu_p3           => 100);


  DBMS_RESOURCE_MANAGER.validate_pending_area;

  DBMS_RESOURCE_MANAGER.submit_pending_area;

END;

/


BEGIN

DBMS_RESOURCE_MANAGER.CREATE_PENDING_AREA();

DBMS_RESOURCE_MANAGER.CREATE_PLAN(PLAN => 'bugdb_plan', 

   COMMENT => 'Resource plan/method for bug users sessions');

DBMS_RESOURCE_MANAGER.CREATE_PLAN(PLAN => 'maildb_plan', 

   COMMENT => 'Resource plan/method for mail users sessions');

DBMS_RESOURCE_MANAGER.CREATE_PLAN(PLAN => 'mydb_plan', 

   COMMENT => 'Resource plan/method for bug and mail users sessions');

DBMS_RESOURCE_MANAGER.CREATE_CONSUMER_GROUP(CONSUMER_GROUP => 'Online_group', 

   COMMENT => 'Resource consumer group/method for online bug users sessions');

DBMS_RESOURCE_MANAGER.CREATE_CONSUMER_GROUP(CONSUMER_GROUP => 'Batch_group', 

   COMMENT => 'Resource consumer group/method for batch job bug users sessions');

DBMS_RESOURCE_MANAGER.CREATE_CONSUMER_GROUP(CONSUMER_GROUP => 'Bug_Maint_group',

   COMMENT => 'Resource consumer group/method for users sessions for bug db maint');

DBMS_RESOURCE_MANAGER.CREATE_CONSUMER_GROUP(CONSUMER_GROUP => 'Users_group', 

   COMMENT => 'Resource consumer group/method for mail users sessions');

DBMS_RESOURCE_MANAGER.CREATE_CONSUMER_GROUP(CONSUMER_GROUP => 'Postman_group',

   COMMENT => 'Resource consumer group/method for mail postman');

DBMS_RESOURCE_MANAGER.CREATE_CONSUMER_GROUP(CONSUMER_GROUP => 'Mail_Maint_group', 

   COMMENT => 'Resource consumer group/method for users sessions for mail db maint');

DBMS_RESOURCE_MANAGER.CREATE_PLAN_DIRECTIVE(PLAN => 'bugdb_plan',

   GROUP_OR_SUBPLAN => 'Online_group',

   COMMENT => 'online bug users sessions at level 1', MGMT_P1 => 80, MGMT_P2=> 0);

DBMS_RESOURCE_MANAGER.CREATE_PLAN_DIRECTIVE(PLAN => 'bugdb_plan', 

   GROUP_OR_SUBPLAN => 'Batch_group', 

   COMMENT => 'batch bug users sessions at level 1', MGMT_P1 => 20, MGMT_P2 => 0,

   PARALLEL_DEGREE_LIMIT_P1 => 8);

DBMS_RESOURCE_MANAGER.CREATE_PLAN_DIRECTIVE(PLAN => 'bugdb_plan', 

   GROUP_OR_SUBPLAN => 'Bug_Maint_group',

   COMMENT => 'bug maintenance users sessions at level 2', MGMT_P1 => 0, MGMT_P2 => 100);

DBMS_RESOURCE_MANAGER.CREATE_PLAN_DIRECTIVE(PLAN => 'bugdb_plan', 

   GROUP_OR_SUBPLAN => 'OTHER_GROUPS', 

   COMMENT => 'all other users sessions at level 3', MGMT_P1 => 0, MGMT_P2 => 0,

   MGMT_P3 => 100);

DBMS_RESOURCE_MANAGER.CREATE_PLAN_DIRECTIVE(PLAN => 'maildb_plan', 

   GROUP_OR_SUBPLAN => 'Postman_group',

   COMMENT => 'mail postman at level 1', MGMT_P1 => 40, MGMT_P2 => 0);

DBMS_RESOURCE_MANAGER.CREATE_PLAN_DIRECTIVE(PLAN => 'maildb_plan',

   GROUP_OR_SUBPLAN => 'Users_group',

   COMMENT => 'mail users sessions at level 2', MGMT_P1 => 0, MGMT_P2 => 80);

DBMS_RESOURCE_MANAGER.CREATE_PLAN_DIRECTIVE(PLAN => 'maildb_plan',

   GROUP_OR_SUBPLAN => 'Mail_Maint_group',

   COMMENT => 'mail maintenance users sessions at level 2', MGMT_P1 => 0, MGMT_P2 => 20);

DBMS_RESOURCE_MANAGER.CREATE_PLAN_DIRECTIVE(PLAN => 'maildb_plan',

   GROUP_OR_SUBPLAN => 'OTHER_GROUPS', 

   COMMENT => 'all other users sessions at level 3', MGMT_P1 => 0, MGMT_P2 => 0,

   MGMT_P3 => 100);

DBMS_RESOURCE_MANAGER.CREATE_PLAN_DIRECTIVE(PLAN => 'mydb_plan', 

   GROUP_OR_SUBPLAN => 'maildb_plan', 

   COMMENT=> 'all mail users sessions at level 1', MGMT_P1 => 30);

DBMS_RESOURCE_MANAGER.CREATE_PLAN_DIRECTIVE(PLAN => 'mydb_plan', 

   GROUP_OR_SUBPLAN => 'bugdb_plan', 

   COMMENT => 'all bug users sessions at level 1', MGMT_P1 => 70);

DBMS_RESOURCE_MANAGER.VALIDATE_PENDING_AREA();

DBMS_RESOURCE_MANAGER.SUBMIT_PENDING_AREA();

END;

/


Categories: DBA Blogs

Temp Tablespace Matters in Oracle

Tue, 2024-03-19 02:28

 When managing Oracle temporary tablespaces, it's important to prioritize timely data commitment or rollback to release temporary space efficiently. 

Additionally, keep an eye out for sessions that remain active without progressing for an extended period; terminating these sessions may be necessary to optimize resource usage. Another crucial consideration is the allocation of small sport segments at the end of a temporary file, which can hinder successful file shrinking. To gain insights into this issue, check the High Water Mark (HWM) of the file to understand its allocation status and potential space usage implications. These practices help maintain the performance and manageability of Oracle temporary tablespaces effectively.


Following are some queries to manage these Temp tablespaces efficiently:


Use this query to get Temp tablespace usage:


SQL> select FILE#, STATUS,  Bytes/1024/1024 Size, tf.name Filename, ts.name TS_NAME  

    from v$tempfile tf , v$tablespace ts where tf.ts# = ts.ts#;


Use this query to get allocated temp space:


SQL> SELECT a.username, a.sid, a.serial#, a.osuser, b.tablespace, b.blocks, c.sql_text

      FROM v$session a, v$tempseg_usage b, v$sqlarea

  WHERE a.saddr = b.session_addr AND c.address= a.sql_address

      AND c.hash_value = a.sql_hash_value ORDER BY b.tablespace, b.blocks;

  

Use this query to get high water mark of Temp Tablespaces:


SQL>select sum( u.blocks * blk.block_size)/1024/1024 "MB. in sort segments", (hwm.max * blk.block_size)/1024/1024 "HWM"

    from v$sort_usage u, (select block_size from dba_tablespaces

     where contents = 'TEMPORARY') blk, 

(select segblk#+blocks max from

     v$sort_usage where segblk# = (select max(segblk#)

from v$sort_usage) ) hwm 

group by hwm.max * blk.block_size/1024/1024;   

 

Use this query to shrink the Temp Tablespaces:


SQL> alter tablespace temp shrink space;  

Categories: DBA Blogs

How to Create Urdu Hindi AI Model and Dataset from New Dataset

Fri, 2024-03-15 21:54

 This video is hands on step-by-step tutorial to create a new dataset, an AI model, fine-tune the model on dataset and then push it to hugging face.




Code:

%%capture

import torch

major_version, minor_version = torch.cuda.get_device_capability()

# Must install separately since Colab has torch 2.2.1, which breaks packages

!pip install "unsloth[colab-new] @ git+https://github.com/unslothai/unsloth.git"

if major_version >= 8:

    # Use this for new GPUs like Ampere, Hopper GPUs (RTX 30xx, RTX 40xx, A100, H100, L40)

    !pip install --no-deps packaging ninja flash-attn xformers trl peft accelerate bitsandbytes

else:

    # Use this for older GPUs (V100, Tesla T4, RTX 20xx)

    !pip install --no-deps xformers trl peft accelerate bitsandbytes

pass


!pip install einops


from unsloth import FastLanguageModel

import torch

max_seq_length = 2048 # Choose any! We auto support RoPE Scaling internally!

dtype = None # None for auto detection. Float16 for Tesla T4, V100, Bfloat16 for Ampere+

load_in_4bit = True # Use 4bit quantization to reduce memory usage. Can be False.


model, tokenizer = FastLanguageModel.from_pretrained(

    model_name = "unsloth/gemma-7b-bnb-4bit", # Choose ANY! eg teknium/OpenHermes-2.5-Mistral-7B

    max_seq_length = max_seq_length,

    dtype = dtype,

    load_in_4bit = load_in_4bit,

    token = " ", # use one if using gated models like meta-llama/Llama-2-7b-hf

)


model = FastLanguageModel.get_peft_model(

    model,

    r = 16, # Choose any number > 0 ! Suggested 8, 16, 32, 64, 128

    target_modules = ["q_proj", "k_proj", "v_proj", "o_proj",

                      "gate_proj", "up_proj", "down_proj",],

    lora_alpha = 16,

    lora_dropout = 0, # Supports any, but = 0 is optimized

    bias = "none",    # Supports any, but = "none" is optimized

    use_gradient_checkpointing = True,

    random_state = 3407,

    use_rslora = False,  # We support rank stabilized LoRA

    loftq_config = None, # And LoftQ

)


alpaca_prompt = """ذیل میں ایک ہدایت ہے جو فلم کے نام کی وضاحت کرتی ہے، اس کے ساتھ ایک ان پٹ بھی ہے جو مزید دستاویزات فراہم کرتا ہے۔ گانے کے بول لکھنے کے لیے ایک لمحہ نکالیں جو فلم کے نام کے معنی سے میل کھاتا ہے۔


### Instruction:

{}


### Input:

{}


### Response:

{}"""


EOS_TOKEN = tokenizer.eos_token # Must add EOS_TOKEN

def formatting_prompts_func(examples):

    instructions = examples["urdu_instruction"]

    inputs       = examples["urdu_input"]

    outputs      = examples["urdu_output"]

    texts = []

    for instruction, input, output in zip(instructions, inputs, outputs):

        # Must add EOS_TOKEN, otherwise your generation will go on forever!

        text = alpaca_prompt.format(instruction, input, output) + EOS_TOKEN

        texts.append(text)

    return { "text" : texts, }

pass


from datasets import load_dataset

dataset = load_dataset("fahdmirzac/urdu_bollywood_songs_dataset", split = "train")

dataset = dataset.map(formatting_prompts_func, batched = True,)


from huggingface_hub import login

access_token = "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"

login(token=access_token)


from trl import SFTTrainer

from transformers import TrainingArguments


trainer = SFTTrainer(

    model = model,

    tokenizer = tokenizer,

    train_dataset = dataset,

    dataset_text_field = "text",

    max_seq_length = max_seq_length,

    dataset_num_proc = 2,

    packing = False, # Can make training 5x faster for short sequences.

    args = TrainingArguments(

        per_device_train_batch_size = 2,

        gradient_accumulation_steps = 4,

        warmup_steps = 5,

        max_steps = 100,

        learning_rate = 2e-4,

        fp16 = not torch.cuda.is_bf16_supported(),

        bf16 = torch.cuda.is_bf16_supported(),

        logging_steps = 1,

        optim = "adamw_8bit",

        weight_decay = 0.01,

        lr_scheduler_type = "linear",

        seed = 3407,

        output_dir = "outputs",

    ),

)


trainer_stats = trainer.train()


FastLanguageModel.for_inference(model) # Enable native 2x faster inference

inputs = tokenizer(

[

    alpaca_prompt.format(

        "دیے گئے فلم کے نام کے بارے میں ایک مختصر گیت کے بول لکھیں۔", # instruction

        "کیوں پیار ہو گیا", # input

        "", # output - leave this blank for generation!

    )

], return_tensors = "pt").to("cuda")


outputs = model.generate(**inputs, max_new_tokens = 200, use_cache = True)

tokenizer.batch_decode(outputs)


FastLanguageModel.for_inference(model) # Enable native 2x faster inference

inputs = tokenizer(

[

    alpaca_prompt.format(

        "دیے گئے فلم کے نام کے بارے میں ایک مختصر گیت کے بول لکھیں۔", # instruction

        "رنگ", # input

        "", # output - leave this blank for generation!

    )

], return_tensors = "pt").to("cuda")


outputs = model.generate(**inputs, max_new_tokens = 200, use_cache = True)

tokenizer.batch_decode(outputs)


model.push_to_hub("fahdmirzac/Gemma_Urdu_Hindi_Bollywood_Songs", token = "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")

Categories: DBA Blogs

Using Claude 3 Haiku Vision with Amazon Bedrock Locally

Fri, 2024-03-15 02:58

 This video is a hands-on guide as how to use vision features of Anthropic's Claude 3 Haiku AI model with Amazon Bedrock.



Code Used:

import boto3
import json
import base64
from botocore.exceptions import ClientError

bedrock = boto3.client(service_name="bedrock-runtime",region_name='us-east-1')

modelId = "anthropic.claude-3-haiku-20240307-v1:0"

accept = "application/json"
contentType = "application/json"


# prompt = "What is written in this image?"
# image_path = "./images/ab55.png"

# prompt = "How many faces are there in this image and what are the expressions of those faces?"
# image_path = "./images/expression.png"

# prompt = "Tell me a short story about this image."
# image_path = "./images/hiking.png"

prompt = "What's the location in this image?"
image_path = "./images/whereisthis.png"


with open(image_path, "rb") as image_file:
    image = base64.b64encode(image_file.read()).decode("utf8")

request_body = {
    "anthropic_version": "bedrock-2023-05-31",
    "max_tokens": 2048,
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": prompt,
                },
                {
                    "type": "image",
                    "source": {
                        "type": "base64",
                        "media_type": "image/png",
                        "data": image,
                    },
                },
            ],
        }
    ],
}

try:
    response = bedrock.invoke_model(
        modelId=modelId,
        body=json.dumps(request_body),
    )

    # Process and print the response
    result = json.loads(response.get("body").read())
    input_tokens = result["usage"]["input_tokens"]
    output_tokens = result["usage"]["output_tokens"]
    output_list = result.get("content", [])

    # print("Invocation details:")
    # print(f"- The input length is {input_tokens} tokens.")
    # print(f"- The output length is {output_tokens} tokens.")

    # print(f"- The model returned {len(output_list)} response(s):")
    for output in output_list:
        print(output["text"])

except ClientError as err:
    print(
        "Couldn't invoke Claude 3 Haiku Vision. Here's why: %s: %s",
        err.response["Error"]["Code"],
        err.response["Error"]["Message"],
    )
    raise
Categories: DBA Blogs

Create AI Agent in AWS with Boto3 Code

Thu, 2024-03-14 22:03

 This video is a step-by-step tutorial with code as how to create Amazon Bedrock AI agents with boto3 in Python to integrate with Lambda.



Code used: Just use any lambda with it of your choice.


import logging
import boto3
import time
import yaml
import json
import io
from botocore.exceptions import ClientError

def create_agent(bedrock, agent_name, foundation_model, role_arn, instruction):
    try:
        # Create a low-level client with the service name
        response = bedrock.create_agent(
            agentName=agent_name,
            foundationModel=foundation_model,
            agentResourceRoleArn=role_arn,
            instruction=instruction,
        )
    except ClientError as e:
        logging.error(f"Couldn't create agent due to: {e}")
        raise
    else:
        return response["agent"]

def create_agent_action_group(bedrock, name, description, agent_id, agent_version, function_arn, api_schema):
    try:
        response = bedrock.create_agent_action_group(
            actionGroupName=name,
            description=description,
            agentId=agent_id,
            agentVersion=agent_version,
            actionGroupExecutor={"lambda": function_arn},
            apiSchema={"payload": api_schema},
        )
        agent_action_group = response["agentActionGroup"]
    except ClientError as e:
        print(f"Error: Couldn't create agent action group. Here's why: {e}")
        raise
    else:
        return agent_action_group

def prepare_agent(bedrock, agent_id):
    try:
        prepared_agent_details = bedrock.prepare_agent(agentId=agent_id)
    except ClientError as e:
        print(f"Couldn't prepare agent. {e}")
        raise
    else:
        return prepared_agent_details

def create_agent_alias(bedrock, name, agent_id):
    try:
        response = bedrock.create_agent_alias(
            agentAliasName=name, agentId=agent_id
        )
        agent_alias = response["agentAlias"]
    except ClientError as e:
        print(f"Couldn't create agent alias. {e}")
        raise
    else:
        return agent_alias



def main():
    # Define your parameters
    bedrock = boto3.client(service_name='bedrock-agent',region_name='us-east-1')
    agent_name = 'AstroAI'
    foundation_model = 'anthropic.claude-v2'
    role_arn = 'bedrock role arn'
    instruction = 'Your task is to generate unique and insightful daily horoscopes for individuals \
                   based on their zodiac sign. Start by analyzing the general characteristics and common \
                   themes associated with each zodiac sign. Consider traits, challenges, opportunities, \
                   and the emotional and physical wellbeing of individuals under each sign. Use this \
                   understanding to create personalized, relevant, and engaging horoscopes that offer \
                   guidance, reflection, and encouragement for the day ahead. Ensure the horoscopes \
                   are varied and resonate with the unique qualities of each sign, contributing \
                   positively to the users day.'

    # Call the create_agent function
    try:
        agent = create_agent(bedrock, agent_name, foundation_model, role_arn, instruction)
        agent_id = agent['agentId']
        print(f"Agent created successfully: {agent_id}")
    except ClientError:
        print("Failed to create the agent.")

    time.sleep(10)

    try:
        with open("api_schema.yaml") as file:
            api_schema=json.dumps(yaml.safe_load(file))
            name="AstroGroup"
            description="AI Astrologer"
            agent_version="DRAFT"
            function_arn="arn:aws:lambda:us-east-1::function:horoscope"
            agentgroup = create_agent_action_group(bedrock, name, description, agent_id, agent_version, function_arn, api_schema)                
            print(agentgroup['actionGroupId'])
    except ClientError as e:
        print(f"Couldn't create agent action group. Here's why: {e}")
        raise        

    time.sleep(5)

    agentprepared = prepare_agent(bedrock, agent_id)                
    print(agentprepared)

    time.sleep(20)

    agentalias = create_agent_alias(bedrock, name, agent_id)
    print(agentalias['agentAliasId'])

if __name__ == "__main__":
    main()

Categories: DBA Blogs

Immutable Data Integrity with Oracle's Blockchain Tables

Sat, 2024-03-02 16:24

 Blockchain tables in Oracle Database implement a unique data organization model, forming chains of insert-only rows. Each row, excluding the first, links back to the previous row using cryptographic hashing. This hashing method leverages row data and the hash of the preceding row to identify any unauthorized modifications through specialized verification procedures. The added capability to store user signatures significantly enhances security against tampering and unauthorized alterations.

Beyond thwarting unauthorized changes initiated by rogue or compromised insiders, blockchain tables boast powerful functionality:


  • Identification of illicit modifications made by circumventing Oracle Database protocols.
  • Detection of user impersonation and unauthorized data insertions on behalf of another user.
  • Safeguarding against data tampering, certifying that all data inserted into the table was done legitimately.
  • The chaining mechanism ensures that each row remains linked to its predecessor through cryptographic hashing. Altering any row disrupts this chain, immediately signaling tampering. However, this additional layer of security necessitates increased processing time during commit operations to chain rows effectively.


Let's explore how to create and interact with a blockchain table in Oracle Database through the following example:


-- Creating a blockchain table

SQL> CREATE BLOCKCHAIN TABLE secure_data_table (data_value VARCHAR2(128), sequence_number NUMBER)

     NO DROP UNTIL 1 DAYS IDLE

     NO DELETE UNTIL 16 DAYS AFTER INSERT

     HASHING USING "SHA2_512" VERSION "v1";    

Table created.


-- Inserting data into the blockchain table

SQL> INSERT INTO secure_data_table VALUES ('Value1', 1);

1 row created.


SQL> COMMIT;

Commit complete.


-- Querying metadata of the blockchain table

SQL> SELECT column_id, column_name, nullable, hidden_column

     FROM all_tab_cols

     WHERE table_name LIKE 'SECURE_DATA_%'

     ORDER BY column_id;


-- Viewing blockchain table details

SQL> SELECT * FROM dba_blockchain_tables;

In this example, secure_data_table is our blockchain table, ensuring immutable data integrity and providing robust security against unauthorized changes. The SHA2_512 hashing algorithm is employed to maintain the integrity of each chained row, guaranteeing data authenticity and traceability within Oracle Database's secure environment.

Categories: DBA Blogs

Pages