Skip to content

Instantly share code, notes, and snippets.

@iamdejan
Created June 28, 2026 15:47
Show Gist options
  • Select an option

  • Save iamdejan/254f279f4908570b4fd827047a3deb3d to your computer and use it in GitHub Desktop.

Select an option

Save iamdejan/254f279f4908570b4fd827047a3deb3d to your computer and use it in GitHub Desktop.
Example of RAG using OpenRouter models.
# an example of RAG using OpenRouter
import numpy as np
from openai import OpenAI
# Initialize the OpenRouter client
client = OpenAI(
base_url="https://openrouter.ai",
api_key="your_openrouter_api_key_here",
)
# 1. Choose your models from OpenRouter
EMBEDDING_MODEL = "openai/text-embedding-3-large" # Embedding model
LLM_MODEL = "google/gemini-2.5-flash" # Inference LLM, can switch to LiquidAI/LFM2.5-1.2B-Instruct or Qwen/Qwen3.5-0.8B
# Mock data simulating your vector database
documents = [
"OpenRouter supports text completions, embeddings, and reranking APIs.",
"Python is an interpreted, high-level, general-purpose programming language.",
"To build a RAG application, you need an embedding model and an LLM.",
]
# Function to generate embeddings using OpenRouter
def get_embedding(text, model=EMBEDDING_MODEL):
response = client.embeddings.create(input=[text], model=model)
return response.data[0].embedding
# 2. Store: Generate embeddings for documents (to save in your vector DB)
print("Generating document embeddings...")
doc_embeddings = [get_embedding(doc) for doc in documents]
# 3. Retrieve: Embed user query and find the closest document
query = "How do I make a RAG app on OpenRouter?"
print(f"\nUser Query: {query}")
query_embedding = get_embedding(query)
# Simple cosine similarity function (simulating vector database search)
def cosine_similarity(a, b):
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
# Find the most relevant document
similarities = [
cosine_similarity(query_embedding, doc_emb) for doc_emb in doc_embeddings
]
best_match_idx = np.argmax(similarities)
retrieved_context = documents[best_match_idx]
print(f"Retrieved Context: {retrieved_context}")
# 4. Inference: Pass context and query to the LLM
print(f"\nSending to LLM ({LLM_MODEL})...")
chat_completion = client.chat.completions.create(
model=LLM_MODEL,
messages=[
{
"role": "system",
"content": "Answer the query using only the provided context.",
},
{
"role": "user",
"content": f"Context: {retrieved_context}\n\nQuery: {query}",
},
],
)
print(f"\nLLM Response:\n{chat_completion.choices[0].message.content}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment