GenAI For Tester
  • Introduction
  • Session 1: Introduction to Generative AI in Testing
    • 1. Overview of Generative AI
    • 2. Popular AI Models and Their Usage
    • 3. Setting Up AI Tools for Testing
    • 4. Prompt Engineering for Software Testing
      • Prompt Managerment
  • Session 2: AI-Assisted Test Case Generation
    • Exam #1: eCommerce Domain - Checkout Flow
    • Exam #2: Mobile App - User Login and Authentication
    • Exam #3: API Testing - User Registration Endpoint
  • Session 3: Advanced AI in Test Automation
    • 🐍 Python 3.12 Setup Guide
    • Chrome AI Asistant
    • Setup Github Copilot in VS Code
    • Playwright MCP Server
    • SQLite MCP to interact with your DB
    • Browser-use Browser AI Agent
    • Postman PostBot AI for API Testing
    • Self Healing Elements with AgentQL and Playwright
  • n8n flexible AI workflow automation for technical teams
    • Setup n8n with docker
  • Build small thing with LLM
    • Create chatbot with gemini model
    • Create R.A.G with germini
    • Create AI Agent tool for query DB
  • Get selenium locator with llm and java
  • Group 1
    • Setup Local DB
Powered by GitBook
On this page
  1. Build small thing with LLM

Create AI Agent tool for query DB

Step 1: create ducks.db file

Step 2: Seeding data

import duckdb

# Connect to or create a DuckDB database file
con = duckdb.connect("ducks.duckdb")

# Create the ducks table
con.execute("""
CREATE TABLE ducks (
    id VARCHAR,
    color VARCHAR,
    firstName VARCHAR,
    lastName VARCHAR,
    gender VARCHAR
);
""")

# Insert data
con.execute("""
INSERT INTO ducks VALUES
('kA0KgL', 'red', 'Marty', 'McFly', 'male'),
('dx3ngL', 'teal', 'Duckota', 'Fanning', 'female'),
('FQ4dU1', 'yellow', 'Duck', 'Norris', 'male'),
('JqS7ZZ', 'red', 'James', 'Pond', 'male'),
('ZM5uJL', 'black', 'Darth', 'Wader', 'male'),
('05FuKa', 'yellow', 'Clint', 'Beakwood', 'male'),
('wKq9zD', 'yellow', 'Mary', 'Quackens', 'female'),
('QCab5l', 'orange', 'Ducky', 'Balboa', 'male'),
('eKiyA5', 'orange', 'Captain', 'Quack', 'male'),
('YiSGQl', 'teal', 'Wonder', 'Duck', 'female');
""")

# Confirm the data
result = con.execute("SELECT * FROM ducks LIMIT 10").fetchall()
for row in result:
    print(row)

# Close connection
con.close()

Step 3: Build AI Agent

import duckdb
from langchain_core.tools import tool
from langchain_google_genai import ChatGoogleGenerativeAI
con = duckdb.connect("ducks.duckdb")
schema = con.execute(f"DESCRIBE ducks").fetchdf()
schema_str = schema.to_string(index=False)

@tool
def query(query:str):
    """
    queries the database for information and returns the results
    
    Args:
        query: str: the query to be executed
    """
    return str(con.execute(query).fetchone()[0])

google_llm = ChatGoogleGenerativeAI(
    model="gemini-2.0-flash",
    temperature=0,
    max_tokens=None,
    timeout=None,
    max_retries=2,
    api_key="xxx",
).bind_tools([query])


    
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
def query_ai_agent(input):
    
    # Initialize the prompt template
    prompt = ChatPromptTemplate.from_messages([
        ("human", f"You are provied You are given a DuckDB schema for table 'ducks'\n\n: {schema_str}\n\n.\n\n Answer the user query: {input} in the single sentence"),
        MessagesPlaceholder(variable_name="agent_scratchpad"),
    ])

    # Create the agent and executor with out llm, tools and prompt
    agent = create_tool_calling_agent(google_llm, [query],prompt)
    agent_executor = AgentExecutor(agent=agent, tools=[query], verbose=True)

    # Run our query 
    res = agent_executor.invoke({"input": input})
    print(res["output"])    
    
query_ai_agent("What are the color of duck with first name 'Marty'?")    

Here is output:


> Entering new AgentExecutor chain...

Invoking: `query` with `{'query': "SELECT color FROM ducks WHERE firstName = 'Marty'"}`


redThe color of the duck with the first name 'Marty' is red.

PreviousCreate R.A.G with germiniNextGet selenium locator with llm and java

Last updated 1 month ago