2 posts tagged with "openai"

View All Tags

Run MindsDB with Docker, Connect Databases, Add GPT Agent, and Query with Natural Language

This guide walks you through running MindsDB using Docker, connecting your own database, adding a GPT-based AI agent, and querying it—all in detail.

1. Running MindsDB Using Docker#

Developers running MindsDB using Docker container

follow this step-by-step deployment guide to get started on Nife’s hybrid cloud—no Docker or manual setup needed

Prerequisites:

  • Docker installed on your machine.
  • If you're new to Docker, see Install Docker for platform-specific instructions.

Start MindsDB:

docker run -d \
--name mindsdb \
-p 47334:47334 \
-p 47335:47335 \
mindsdb/mindsdb:latest
  • 47334: HTTP API & Web GUI
  • 47335: MySQL API

Once started, access the MindsDB GUI at: http://localhost:47334 Or use the API at the same port.

if you're curious about how MindsDB works behind the scenes, check out this introduction to MindsDB architecture.

2. Adding a Database Connection#

MindsDB can connect to many databases (MySQL, PostgreSQL, etc.). Here’s how to add a MySQL database:

SQL Command (run in MindsDB SQL editor or via API):

CREATE DATABASE mysql_conn
WITH ENGINE = 'mysql',
PARAMETERS = {
"host": "your-mysql-host",
"port": 3306,
"database": "your_db_name",
"user": "your_db_user",
"password": "your_db_password"
};
  • Replace the values with your actual database details.
  • You can also use a connection URL:
CREATE DATABASE mysql_conn
WITH ENGINE = 'mysql',
PARAMETERS = {
"url": "mysql://user:password@host:3306/db_name"
};

Check the connection:

SHOW DATABASES;

or

SELECT * FROM mysql_conn.your_table LIMIT 5;

If you need a sample MySQL database for testing, you can find open datasets at MySQL Sample Databases.

3. Adding a GPT AI Agent#

To use GPT (like GPT-4o) for natural language Q\&A, you need an OpenAI API key.

Step 1: Create the Agent

CREATE AGENT my_gpt_agent
USING
model = 'gpt-4o',
openai_api_key = 'your_openai_api_key',
include_tables = ['mysql_conn.your_table'],
prompt_template = '
mysql_conn.your_table contains your business data.
Answer questions using this data.
';
  • model: The LLM to use (e.g., 'gpt-4o').
  • openai_api_key: Your OpenAI API key.
  • include_tables: List the tables the agent can access.
  • prompt_template: (Optional) Describe your data to help the agent answer accurately.

Not sure which model to use? Here's a comparison of GPT-4o vs Gemini vs Claude.

Step 2: Verify the Agent

SHOW AGENTS WHERE name = 'my_gpt_agent';

4. Asking Questions to the Agent#

Developer interacting with GPT agent using natural language queries about MindsDB

You can now ask natural language questions to your agent:

SELECT answer
FROM my_gpt_agent
WHERE question = 'How many customers signed up last month?';
  • The agent will use the connected data and GPT model to answer your question in natural language.

If you're new to prompt design, this OpenAI cookbook has great examples for GPT-based workflows.

5. Full Example Workflow#

  1. Start MindsDB with Docker (see above).
  2. Connect your database (e.g., MySQL).
  3. Create a GPT agent linked to your data.
  4. Ask questions using SQL.

6. Tips and Troubleshooting#

Developer troubleshooting MindsDB errors with help from a teammate
  • Multiple Databases: Repeat the CREATE DATABASE step for each data source.
  • Other Models: You can use other LLMs (Gemini, Anthropic, etc.) by changing the model and API key.
  • Data Security: Never expose your API keys in public code or logs.
  • Error Handling: If you get connection errors, double-check your database credentials and network access.

7. Reference Table#

StepCommand/Action
Run MindsDBdocker run -d -p 47334:47334 -p 47335:47335 mindsdb/mindsdb:latest
Add DatabaseCREATE DATABASE mysql_conn WITH ENGINE = 'mysql', PARAMETERS = {...};
Create GPT AgentCREATE AGENT my_gpt_agent USING model = 'gpt-4o', ...;
Ask a QuestionSELECT answer FROM my_gpt_agent WHERE question = '...';

Final Thoughts#

MindsDB bridges the gap between traditional SQL databases and modern AI models, allowing you to ask complex questions over your structured data using natural language.

With Docker for setup, SQL for control, and GPT-4o for intelligence, this workflow empowers developers, analysts, and product teams alike.

Whether you’re building an analytics dashboard, data chatbot, or intelligent reporting tool—you now have a full pipeline from data to insight using MindsDB + GPT.

You can deploy MindsDB in just a few minutes using Nife.io, a unified platform for AI applications, cloud deployment, and DevOps automation.

Explore and launch MindsDB instantly from the OpenHub App Marketplace .

Enhancing LLMs with Retrieval-Augmented Generation (RAG): A Technical Deep Dive

Large Language Models (LLMs) have transformed natural language processing, enabling impressive feats like summarization, translation, and conversational agents. However, they’re not without limitations. One major drawback is their static nature—LLMs can't access knowledge beyond their training data, which makes handling niche or rapidly evolving topics a challenge.

This is where Retrieval-Augmented Generation (RAG) comes in. RAG is a powerful architecture that enhances LLMs by retrieving relevant, real-time information and combining it with generative capabilities. In this guide, we’ll explore how RAG works, walk through implementation steps, and share code snippets to help you build a RAG-enabled system.

What Is Retrieval-Augmented Generation (RAG)?#

Illustration showing team discussing Retrieval-Augmented Generation (RAG)

RAG integrates two main components:

  1. Retriever: Fetches relevant context from a knowledge base based on the user's query.
  2. Generator (LLM): Uses the retrieved context along with the query to generate accurate, grounded responses.

Instead of relying solely on what the model "knows," RAG allows it to augment answers with external knowledge.

Learn more from the original RAG paper by Facebook AI.

Why Use Retrieval-Augmented Generation for LLMs?#

Here are some compelling reasons to adopt RAG:

  • Real-time Knowledge: Update the knowledge base anytime without retraining the model.
  • Improved Accuracy: Reduces hallucinations by anchoring responses in factual data.
  • Cost Efficiency: Avoids the need for expensive fine-tuning on domain-specific data.

Core Components of a Retrieval-Augmented Generation System#

Diagram of core components in a Retrieval-Augmented Generation system

1. Retriever#

The retriever uses text embeddings to match user queries with relevant documents.

Example with LlamaIndex:#

from llama_index import SimpleRetriever, EmbeddingRetriever
retriever = EmbeddingRetriever(index_path="./vector_index")
query = "What is RAG in AI?"
retrieved_docs = retriever.retrieve(query, top_k=3)

2. Building Your Knowledge Base with Vector Embeddings#

Your retriever needs a knowledge base with embedded documents.

Key Steps:#

  • Document Loading: Ingest your data.
  • Chunking: Break text into meaningful chunks.
  • Embedding: Generate vector representations.
  • Indexing: Store them in a vector database like FAISS or Pinecone.

Example with OpenAI Embeddings:#

from openai.embeddings_utils import get_embedding
import faiss
documents = ["Doc 1 text", "Doc 2 text"]
embeddings = [get_embedding(doc) for doc in documents]
index = faiss.IndexFlatL2(len(embeddings[0]))
index.add(embeddings)

3. Integrating the LLM for Contextual Answer Generation#

After retrieval, the documents are passed to the LLM along with the query.

Example:#

from transformers import pipeline
generator = pipeline("text-generation", model="gpt-3.5-turbo")
context = "\n".join([doc.text for doc in retrieved_docs])
augmented_query = f"{context}\nQuery: {query}"
response = generator(augmented_query, max_length=200)
print(response[0]['generated_text'])

You can experiment with Hugging Face’s Transformers library for more customization.

Best Practices for Building Effective RAG Systems#

 Visual highlighting best practices for building efficient RAG workflows
  • Chunk Size: Balance between too granular (noisy) and too broad (irrelevant).

  • Retrieval Enhancements:

    • Combine embeddings with keyword search.
    • Add metadata filters (e.g., date, topic).
    • Use rerankers to boost relevance.
    • Use rerankers like Cohere Rerank or OpenAI’s function calling to boost relevance.

RAG vs. Fine-Tuning#

| Feature | RAG | Fine-Tuning | | -- | | -- | | Flexibility | ✅ High | ❌ Low | | Real-Time Updates | ✅ Yes | ❌ No | | Cost | ✅ Lower | ❌ Higher | | Task Adaptation | ✅ Dynamic | ✅ Specific |

RAG is ideal when you need accurate, timely responses without the burden of retraining.

Final Thoughts#

RAG brings the best of both worlds: LLM fluency and factual accuracy from external data. Whether you're building a smart chatbot, document assistant, or search engine, RAG provides the scaffolding for powerful, informed AI systems.

Start experimenting with RAG and give your LLMs a real-world upgrade!

Discover Seamless Deployment with Oikos on Nife.io

Looking for a streamlined, hassle-free deployment solution? Check out Oikos on Nife.io to explore how it simplifies application deployment with high efficiency and scalability. Whether you're managing microservices, APIs, or full-stack applications, Oikos provides a robust platform to deploy with ease.