惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - Franky
有赞技术团队
有赞技术团队
博客园 - 司徒正美
量子位
N
News and Events Feed by Topic
T
Threatpost
Last Week in AI
Last Week in AI
D
Darknet – Hacking Tools, Hacker News & Cyber Security
酷 壳 – CoolShell
酷 壳 – CoolShell
C
CERT Recently Published Vulnerability Notes
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
I
Intezer
人人都是产品经理
人人都是产品经理
T
Tenable Blog
IT之家
IT之家
雷峰网
雷峰网
腾讯CDC
博客园 - 聂微东
V
Visual Studio Blog
S
SegmentFault 最新的问题
Scott Helme
Scott Helme
Spread Privacy
Spread Privacy
月光博客
月光博客
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
V2EX
大猫的无限游戏
大猫的无限游戏
Apple Machine Learning Research
Apple Machine Learning Research
爱范儿
爱范儿
T
Tailwind CSS Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
罗磊的独立博客
N
Netflix TechBlog - Medium
J
Java Code Geeks
宝玉的分享
宝玉的分享
F
Full Disclosure
WordPress大学
WordPress大学
A
Arctic Wolf
小众软件
小众软件
AWS News Blog
AWS News Blog
Attack and Defense Labs
Attack and Defense Labs
NISL@THU
NISL@THU
AI
AI
Hugging Face - Blog
Hugging Face - Blog
F
Fortinet All Blogs
云风的 BLOG
云风的 BLOG
N
News | PayPal Newsroom
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
Build a RAG application with Runware and LangChain
Rishi Raj Jain · 2026-06-15 · via DEV Community

Retrieval-augmented generation (RAG) connects LLM answers to your own documents instead of relying on training data. This tutorial builds a complete pipeline with Runware handling generation on purpose-built inference infrastructure, which is faster and cheaper than commodity providers, through an OpenAI-compatible endpoint, and LangChain handling the indexing and retrieval layer.

Without retrieval, assistants either hallucinate details such as inventing API fields or policies that don't exist or go stale the moment your docs change. RAG fixes both by pulling the relevant passages before generation.

The RAG pattern

RAG retrieves relevant document chunks at query time and adds them to the prompt so the model answers using that specific context. Unlike memory, which tracks user info across sessions, RAG surfaces up-to-date docs (like APIs or policies). Mixing user chat logs into RAG often leads to stale or sensitive results, so use each for their purpose.

As you scale, remember these tips:

  • Chunk with purpose. Fixed-size splits can break code and headings. Use RecursiveCharacterTextSplitter by default. Adjust splitting if retrieval feels slightly off.

  • Retrieval quality matters most. A wrong top chunk leads to confident but incorrect answers. Increasing k helps up to a point. For exact matches like taskUUID, try hybrid keyword and vector search.

  • Let the model refuse when unsure. It is better to have a short "I don’t know" than a wrong but convincing answer.

The stack here is local embeddings with FastEmbed, FAISS on disk, LangChain LCEL for the chain, and Runware for the LLM call. You can add reranking without changing how you call Runware.

Runware in the stack

Runware runs generation on its Sonic Inference Engine, custom-built for AI and fully optimized from hardware to software. This means faster, cheaper inference for open source models than general cloud providers, with pay-as-you-go pricing and no minimums.

For example, MiniMax M2.5 costs less on Runware ($0.27/$0.95 per million input/output tokens) than on MiniMax’s own API ($0.30/$1.20). At enterprise scale (think 10M+ tokens), this saves nearly $10K a year in inference for the same workload without any code changes.

This tutorial runs chunk embeddings locally with FastEmbed (no embedding bill) and sends only the generation call to Runware, which keeps costs low even at scale.

Pipeline overview

The end-to-end loop looks like this:

  1. Load source text (snippets, Markdown, PDFs).
  2. Split into chunks and embed them locally.
  3. Store vectors in FAISS on disk.
  4. For each question, retrieve the closest chunks.
  5. Send those chunks plus the question to Runware via LangChain.
  6. Optionally run the same question without retrieval to see the difference.

LangChain covers loaders, splitters, the vector store, the retriever, and the chain. Runware covers the model response. In this setup, swapping a Runware model id does not require re-indexing.

Prerequisites

  • Python 3.10 or later
  • A Runware account
  • Enough disk for a small embedding model and FAISS (CPU is fine for a doc-sized corpus)

Set up the environment

Let's start by creating a new Python project. To keep your Python dependencies organized you should create a virtual environment.

First, create and navigate into a local directory:

# Create and move to the new directory
mkdir runware-rag
cd runware-rag

Afterwards, create and activate a new virtual environment:

# Create a virtual environment
python -m venv venv

# Active the virtual environment (Windows)
.\venv\Scripts\activate.bat

# Active the virtual environment (Linux)
source ./venv/bin/activate

Now, create a requirements.txt file with the following dependencies:

langchain
langchain-openai
langchain-community
langchain-text-splitters
fastembed
faiss-cpu
python-dotenv

Then, you can install the dependencies with the following command:

pip install -r requirements.txt

Finally, obtain the Runware API Key and create an .env file with the copied value:

RUNWARE_API_KEY="your_runware_api_key_here"

Step 1. Prepare your corpus

To fill your search index, you'll need some source text. For this tutorial, we'll use a set of brief snippets from the Runware documentation to make it easy to check your results. If you’re working with your own materials, you can use LangChain's various document loaders to ingest content such as Markdown files, PDFs, web pages, or even entire sitemaps.

Create corpus.py with those snippets:

RUNWARE_DOC_SNIPPETS = [
    "Runware exposes an OpenAI-compatible chat completions endpoint at [https://api.runware.ai/v1/chat/completions](https://api.runware.ai/v1/chat/completions?utm_source=devto&utm_medium=community-site&utm_campaign=2026-06-rag-application-blog&utm_content=2026-06-11_devto-sponsored-article). Point existing OpenAI SDKs at that base URL with your Runware API key.",
    "The Runware API accepts an array of tasks per POST. Each task includes taskType, taskUUID, and modality-specific fields. You can batch image, video, audio, and text in one call.",
    "textInference is the task type for LLM chat on the native API. OpenAI-compatible clients use chat completions instead.",
    "Runware supports deliveryMethod sync, stream, and async. Async tasks can include webhookURL for completion callbacks.",
    "includeCost on a native task request returns the USD cost for that task when set to true.",
    "DeepSeek-V4-Flash on Runware supports thinkingLevel off, high, and max, jsonSchema for structured output, and tool definitions on textInference.",
    "Generated output URLs are retained for a limited time by default. Use ttl where supported to control retention.",
]

If a question has no match in these snippets, the assistant should say it does not know.

Step 2. Chunk and index

Before retrieval, split the docs into chunks and turn each chunk into a vector with FastEmbed. It's an ONNX embedding model that runs fine on CPU and downloads itself the first time you call it.

Define the embedding model once in embeddings.py so indexing and retrieval always use the same one. A mismatch here quietly wrecks your results.

from langchain_community.embeddings import FastEmbedEmbeddings

EMBEDDING_MODEL = "BAAI/bge-small-en-v1.5"

def get_embeddings():
    return FastEmbedEmbeddings(model_name=EMBEDDING_MODEL)

Create index.py to build and save the FAISS index:

from langchain_community.vectorstores import FAISS
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_core.documents import Document

from corpus import RUNWARE_DOC_SNIPPETS
from embeddings import get_embeddings

def build_vectorstore():
    docs = [
        Document(page_content=s, metadata={"source": f"snippet-{i}"})
        for i, s in enumerate(RUNWARE_DOC_SNIPPETS)
    ]

    splitter = RecursiveCharacterTextSplitter(chunk_size=400, chunk_overlap=80)
    chunks = splitter.split_documents(docs)

    return FAISS.from_documents(chunks, get_embeddings())

if __name__ == "__main__":
    store = build_vectorstore()
    store.save_local("faiss_runware_docs")
    print("Saved faiss_runware_docs")

python index.py

Re-run this whenever your docs change. Use the same embedding model for indexing and querying. Treat faiss_runware_docs as a build artifact and version it like the rest of your code.

Step 3. Connect the LLM to Runware

Connecting to Runware looks identical to connecting to OpenAI (that's the point!). The OpenAI-compatible endpoint means you can drop Runware into an existing LangChain app by changing two lines. What you get in return is Runware's inference infrastructure with lower cost per token, faster response times, and access to hundreds of open source models under the same schema (all on pay-as-you-go billing).

Create llm.py:

import os
from langchain_openai import ChatOpenAI

def runware_chat():
    return ChatOpenAI(
        model="deepseek-v4-flash",
        openai_api_key=os.environ["RUNWARE_API_KEY"],
        openai_api_base="https://api.runware.ai/v1",
        temperature=0.2,
        max_tokens=512,
    )

That model field is the only thing you change to switch models. Runware hosts 400K+ model variants under one consistent endpoint and schema, so when you swap deepseek-v4-flash for any other supported model, you just update that string and nothing else. You do not need to migrate or re-index. The same API key and base URL also work for image, video, audio, and text tasks, so this RAG pattern extends easily to multimodal pipelines without adding another vendor.

For taskUUID, webhooks, includeCost, or mixed-modality batches, see the native API docs. This tutorial uses the OpenAI-compatible endpoint because it drops into LangChain without extra code. The includeCost flag is covered in Production notes.

Step 4. Build the chain

Wire the retriever, prompt, and LLM together with LangChain Expression Language (LCEL).

Create chain.py:

from operator import itemgetter

from langchain_community.vectorstores import FAISS
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough

from embeddings import get_embeddings
from llm import runware_chat

def load_retriever():
    store = FAISS.load_local(
        "faiss_runware_docs",
        get_embeddings(),
        allow_dangerous_deserialization=True,
    )
    return store.as_retriever(search_kwargs={"k": 4})

def format_docs(docs):
    return "\n\n".join(
        f"[{d.metadata.get('source', 'doc')}] {d.page_content}" for d in docs
    )

prompt = ChatPromptTemplate.from_messages([
    (
        "system",
        "You answer questions about Runware using only the context below. "
        "If the context is insufficient, say you do not know. "
        "Do not invent API fields, prices, or behavior.",
    ),
    ("human", "Context:\n{context}\n\nQuestion: {question}"),
])

def build_rag_chain():
    retriever = load_retriever()
    llm = runware_chat()

    return (
        {
            "context": itemgetter("question") | retriever | format_docs,
            "question": itemgetter("question"),
        }
        | prompt
        | llm
        | StrOutputParser()
    )

Keep the system prompt strict. If the retrieved context is empty or off-topic, the model should say so instead of guessing.

Step 5. Test with and without retrieval

In your project root, create a file named main.py.

from chain import build_rag_chain
from llm import runware_chat
from dotenv import load_dotenv

load_dotenv()

QUESTION = "How does Runware handle async text results and cost visibility?"

def main():
    rag = build_rag_chain()
    answer = rag.invoke({"question": QUESTION})
    print("=== With retrieval ===")
    print(answer)
    print()

    bare = runware_chat()
    no_context = bare.invoke(
        "Answer in one short paragraph: How does Runware handle async text results and cost visibility?"
    )
    print("=== Without retrieval ===")
    print(no_context.content)

if __name__ == "__main__":
    main()

python main.py

A good run surfaces terms from your corpus like webhookURL, includeCost, and async delivery. Ask something outside corpus.py and the assistant should refuse instead of making something up.

=== With retrieval ===
Based on the context provided, Runware supports async tasks with a `webhookURL` for completion callbacks. For cost visibility, setting `includeCost` to `true` on a native task request returns the USD cost for that task. However, the context does not specify how async text results are delivered beyond the webhook callback, nor does it detail cost visibility specifically for async text tasks.

=== Without retrieval ===
Runware handles async text results by returning a unique `taskId` immediately upon submission, allowing users to poll or receive webhook callbacks for the final output once processing completes. For cost visibility, Runware provides real-time pricing estimates before task execution and itemized cost breakdowns in the response, showing the exact credits or fees consumed per request.

Step 6. Add streaming (optional)

Runware supports "stream": true on the compatible endpoint. Stream the chain when a human reads the answer live.

for chunk in rag.stream({"question": QUESTION}):
    print(chunk, end="", flush=True)

For batch work use the Runware native API directly as it supports webhooks and per-task cost breakdowns. See Production notes for cost attribution guidance.

Production notes

A few things to address before production:

  • Treat the vector store as a versioned build artifact, and log which embedding model built each version. Chasing down a model mismatch after the fact is painful.

  • Redact secrets before indexing. Chunking is local, but retrieved text goes to Runware’s API.

  • Tune k against real user questions. If you get the right topic but the wrong paragraph, reach for reranking or hybrid search before you jump to a bigger model.

  • Keep a set of golden questions with expected source snippets and run them in CI. This catches wrong citations and missing abstentions early.

  • On Runware's native API, includeCost helps attribute spend during pilots.

Wrap-up

The key thing to take away is the separation of concerns within RAG system. LangChain owns everything up to the retrieval step, and Runware handles generation through a standard chat completions interface. That boundary matters because it means you can tune retrieval without touching the LLM, and swap models without rebuilding the index.

Most quality problems in RAG come from the retrieval side, not the model. If answers are off, check what chunks actually got retrieved before assuming the model needs to be bigger. From here, the practical next steps are tighter chunking, a small set of real user questions to eval against, and clear rules for when the assistant should refuse to answer.