{"slug": "build-a-rag-system-with-claude-chatgpt-apis", "title": "Build a RAG System with Claude & ChatGPT APIs", "summary": "A developer at Gate of AI built a retrieval-augmented generation (RAG) system using Claude and ChatGPT APIs. The system retrieves relevant documents from a JSON repository and generates contextually rich responses by combining both language models. The tutorial covers environment setup, API integration, and query handling for applications like customer support and research assistants.", "body_md": "[> ](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F1lk2k5abtzwax6vsghw1.jpeg)**🚀 Technical Briefing:** This tutorial is part of our deep-dive series on Agentic Workflows at [Gate of AI](https://gateofai.com). For the full technical breakdown, interactive code sandbox, and the native Arabic translation, visit the [original article here](https://gateofai.com/tutorial/build-rag-system-claude-chatgpt-apis/).\n\n```\n<span>Tutorial</span>\n<span>Intermediate</span>\n<span>⏱ 45 min read</span>\n<span>© Gate of AI 2026-06-24</span>\n```\n\nLearn to build a smart, retrieval-augmented generation system using Claude and ChatGPT APIs to leverage the best of both models for enhanced AI interactions.\n\nIn this tutorial, we will build a Retrieval-Augmented Generation (RAG) system that combines the capabilities of Claude and ChatGPT APIs. This system will efficiently fetch relevant data from a document repository and generate contextually rich responses using advanced language models.\n\nThe final application will allow users to input queries, retrieve pertinent information from a pre-indexed document set, and use AI models to generate comprehensive answers. This integrated approach offers robust performance in applications requiring high accuracy and relevance, such as customer support systems and research assistants.\n\nWe will begin by setting up our development environment and installing necessary libraries. This involves setting up Node.js and installing the required SDKs for accessing the APIs.\n\n```\nnpm install openai anthropic dotenv\n```\n\nNext, we'll configure our environment variables to securely store our API keys. Create a `.env`\n\nfile in your project root and add the following variables:\n\n```\nOPENAI_API_KEY=your_openai_api_key\nANTHROPIC_API_KEY=your_anthropic_api_key\n```\n\nFirst, we need to establish a document repository that our RAG system can query. We'll use a simple JSON file to simulate this repository. Ensure the data is structured for quick access and relevance scoring.\n\n``` js\nconst fs = require('fs');\nconst documents = JSON.parse(fs.readFileSync('documents.json', 'utf8'));\n\nfunction getRelevantDocuments(query) {\n  // Simple keyword matching for relevance\n  return documents.filter(doc => doc.text.includes(query));\n}\n\nmodule.exports = { getRelevantDocuments };\n```\n\nThis code reads a JSON file containing our documents and filters them based on the query. The `getRelevantDocuments`\n\nfunction will be used later to fetch relevant documents for any given query.\n\nNext, we'll set up the integration with Claude and ChatGPT APIs to process and generate responses. This involves configuring both APIs and establishing a connection to send and receive data.\n\n``` js\nrequire('dotenv').config();\nconst { OpenAI } = require('openai');\nconst { Anthropic } = require('anthropic');\n\nconst openAIClient = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });\nconst anthropicClient = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });\n\nasync function generateResponseWithClaude(prompt) {\n  const response = await anthropicClient.chat.completions.create({\n    model: \"claude-3-5-sonnet-20241022\",\n    messages: [{ role: \"user\", content: prompt }]\n  });\n  return response.data.choices[0].message.content;\n}\n\nasync function generateResponseWithChatGPT(prompt) {\n  const response = await openAIClient.chat.completions.create({\n    model: \"gpt-4o\",\n    messages: [{ role: \"user\", content: prompt }]\n  });\n  return response.data.choices[0].message.content;\n}\n\nmodule.exports = { generateResponseWithClaude, generateResponseWithChatGPT };\n```\n\nThis code establishes connections to both APIs using the modern SDKs. It defines functions to send a prompt to each service and receive a generated response, which will be used to produce the final answer.\n\nWe'll now build the core logic to handle user queries. This involves retrieving relevant documents and using our integrated AI functions to generate a cohesive response.\n\n``` js\nconst { getRelevantDocuments } = require('./documentRepository');\nconst { generateResponseWithClaude, generateResponseWithChatGPT } = require('./aiIntegrations');\n\nasync function handleUserQuery(query) {\n  const relevantDocs = getRelevantDocuments(query);\n  const combinedContext = relevantDocs.map(doc => doc.text).join('\\n');\n  const prompt = Based on these documents:\\n${combinedContext}\\nAnswer the following question: ${query};\n\nconst claudeResponse = await generateResponseWithClaude(prompt);\n  const chatGPTResponse = await generateResponseWithChatGPT(prompt);\n\nreturn {\n    claude: claudeResponse,\n    chatGPT: chatGPTResponse\n  };\n}\n\nmodule.exports = { handleUserQuery };\n```\n\nThis function combines document retrieval and AI processing to form a complete query handling mechanism. It gathers context from relevant documents and sends it to both Claude and ChatGPT for response generation, allowing you to compare or combine their outputs as needed.\n\n**⚠️ Common Mistake:** Ensure your environment variables are correctly set up and accessible. Misconfigured keys will lead to authentication errors with the APIs.\n\nOnce the setup is complete, it's crucial to test your application to ensure everything is functioning as expected. You can create a simple script to simulate user queries and verify the responses.\n\n``` js\nconst { handleUserQuery } = require('./queryHandler');\n\n(async () => {\n  const query = \"How does the RAG system work?\";\n  const responses = await handleUserQuery(query);\n\nconsole.log(\"Claude's response:\", responses.claude);\n  console.log(\"ChatGPT's response:\", responses.chatGPT);\n})();\n```\n\nRunning this script should output responses from both Claude and ChatGPT, allowing you to assess their quality and relevance based on the provided document context.", "url": "https://wpnews.pro/news/build-a-rag-system-with-claude-chatgpt-apis", "canonical_source": "https://dev.to/gateofai/build-a-rag-system-with-claude-chatgpt-apis-nao", "published_at": "2026-06-25 20:33:31+00:00", "updated_at": "2026-06-25 21:13:06.304899+00:00", "lang": "en", "topics": ["large-language-models", "artificial-intelligence", "natural-language-processing", "ai-tools", "developer-tools"], "entities": ["Gate of AI", "Claude", "ChatGPT", "OpenAI", "Anthropic", "Node.js"], "alternates": {"html": "https://wpnews.pro/news/build-a-rag-system-with-claude-chatgpt-apis", "markdown": "https://wpnews.pro/news/build-a-rag-system-with-claude-chatgpt-apis.md", "text": "https://wpnews.pro/news/build-a-rag-system-with-claude-chatgpt-apis.txt", "jsonld": "https://wpnews.pro/news/build-a-rag-system-with-claude-chatgpt-apis.jsonld"}}