Repository: meta-llama/llama-stack
File: docs/docs/getting_started/demo_script.py (Python)

Code before:
# This source code is licensed under the terms described in the LICENSE file in
# the root directory of this source tree.

"""
Demo script showing RAG with both Responses API and Chat Completions API.

This example demonstrates two approaches to RAG with Llama Stack:
1. Responses API - Automatic agentic tool calling with file search
2. Chat Completions API - Manual retrieval with explicit control

Run this script after starting a Llama Stack server:
    llama stack run starter
"""

import io
import os

from openai import OpenAI

# Initialize OpenAI client pointing to Llama Stack server
client = OpenAI(base_url="http://localhost:8321/v1/", api_key="none")

print("RAG demonstration\n")

data="abc is green"
print(f"Data: {data}\n")

buf = io.BytesIO(str(data).encode('utf-8'))
f = client.files.create(file=("abc.txt", buf), purpose="assistants")

vs = client.vector_stores.create()
client.vector_stores.files.create(vs.id, file_id=f.id)

query = "What is the color of abc?"
print(f"Query: {query}\n")

print(
    """
RAG using Responses API:
   - Automatic tool calling (model decides when to search)
   - Simpler code, less control
   - Best for: Conversational agents, automatic workflows

"""
)

print("Reply via Responses API:")
resp = client.responses.create(
    model=os.getenv("INFERENCE_MODEL", "ollama/llama3.2:3b"),
    input=query,
    tools=[{"type": "file_search", "vector_store_ids": [vs.id]}],

Review comment (suggestion):
Thanks, @makelinux for this PR! I was using Paul Graham's example because its the common example used across introductory RAG tutorials as shown in getting started examples like the one from Mistral AI here:
https://docs.mistral.ai/capabilities/embeddings/rag_quickstart

Code after:
# This source code is licensed under the terms described in the LICENSE file in
# the root directory of this source tree.

"""
Demo script showing RAG with both Responses API and Chat Completions API.

This example demonstrates two approaches to RAG with Llama Stack:
1. Responses API - Automatic agentic tool calling with file search
2. Chat Completions API - Manual retrieval with explicit control

Run this script after starting a Llama Stack server:
    llama stack run starter
"""

import io
import os

import requests
from openai import OpenAI

# Initialize OpenAI client pointing to Llama Stack server
client = OpenAI(base_url="http://localhost:8321/v1/", api_key="none")

print("RAG demonstration\n")

url = "https://www.paulgraham.com/greatwork.html"
print(f"Fetching document from: {url}")

vs = client.vector_stores.create()

response = requests.get(url)
pseudo_file = io.BytesIO(str(response.content).encode("utf-8"))
uploaded_file = client.files.create(
    file=(url, pseudo_file, "text/html"), purpose="assistants"
)
client.vector_stores.files.create(vector_store_id=vs.id, file_id=uploaded_file.id)
print(f"File uploaded and added to vector store: {uploaded_file.id}")

query = "How do you do great work?"
print(f"Query: {query}\n")

print(
    """
RAG using Responses API:
   - Automatic tool calling (model decides when to search)
   - Simpler code, less control
   - Best for: Conversational agents, automatic workflows

"""
)