Skip to main content
Perform a basic vector search:
const searchResponse = await client.search("What was Uber's profit in 2020?");
response
object
The search results from the R2R system.
{
  results: {
    vector_search_results: [
      {
        id: '7ed3a01c-88dc-5a58-a68b-6e5d9f292df2',
        score: 0.780314067545999,
        metadata: {
          text: 'Content of the chunk...',
          title: 'file1.txt',
          version: 'v0',
          chunk_order: 0,
          document_id: 'c9bdbac7-0ea3-5c9e-b590-018bd09b127b',
          extraction_id: '472d6921-b4cd-5514-bf62-90b05c9102cb',
          // ...
        }
      }
      // ...
    ],
    kg_search_results: null
  }
}
query
string
required
The search query.
vector_search_settings
VectorSearchSettings | Record<string, any>
default:"None"
Optional settings for vector search, either a dictionary, a VectorSearchSettings object, or None may be passed. If a dictionary or None is passed, then R2R will use server-side defaults for non-specified fields.
kg_search_settings
Optional[Union[KGSearchSettings, dict]]
default:"None"
Optional settings for knowledge graph search, either a dictionary, a KGSearchSettings object, or None may be passed. If a dictionary or None is passed, then R2R will use server-side defaults for non-specified fields.

Search custom settings

Search with custom settings, such as bespoke document filters and larger search limits
# returns only chunks from documents with title `document_title`
const filtered_search_response = client.search(
    "What was Uber's profit in 2020?",
    { search_filters: {
        $eq: "uber_2021.pdf"
    },
    search_limit: 100
    },
)
Combine traditional keyword-based search with vector search:
const hybrid_search_response = client.search(
    "What was Uber's profit in 2020?",
    { search_filters: {
        $eq: "uber_2021.pdf"
    },
    search_limit: 100
    },
)
hybrid_search_response = client.search(
    "What was Uber's profit in 2020?",
    { use_hybrid_search: true}
)
Utilize knowledge graph capabilities to enhance search results:
const kg_search_response = client.search(
    "What is a fierce nerd?",
    { use_kg_search: true },
)
response
object
The knowledge graph search results from the R2R system.
{
  'results': {
    'vector_search_results': [],
    'kg_search_results': [
      ('MATCH (p:PERSON)-[:FOUNDED]->(c:COMPANY) WHERE c.name = "Airbnb" RETURN p.name',
       [{'name': 'Brian Chesky'}, {'name': 'Joe Gebbia'}, {'name': 'Nathan Blecharczyk'}])
    ]
  }
}

Retrieval-Augmented Generation (RAG)

Basic RAG

Generate a response using RAG:
const ragResponse = await client.rag("What was Uber's profit in 2020?");
response
dict
The RAG response from the R2R system.
{
  'results': {
    'completion': {
      'id': 'chatcmpl-9ySStnC0oEhnGPPV1k8ZYnxBKOuW8',
      'choices': [{
        'finish_reason': 'stop',
        'index': 0,
        'logprobs': None,
        'message': {
          'content': "Uber's profit in 2020 was a net loss of $6.77 billion."
        },
        ...
      }]
    },
    'search_results': {
      'vector_search_results': [...],
      'kg_search_results': None
    }
  }
}
query
str
required
The query for RAG.
vector_search_settings
Optional[Union[VectorSearchSettings, dict]]
default:"None"
Optional settings for vector search, either a dictionary, a VectorSearchSettings object, or None may be passed. If a dictionary is used, non-specified fields will use the server-side default.
kg_search_settings
Optional[Union[KGSearchSettings, dict]]
default:"None"
Optional settings for knowledge graph search, either a dictionary, a KGSearchSettings object, or None may be passed. If a dictionary or None is passed, then R2R will use server-side defaults for non-specified fields.
rag_generation_config
Optional[Union[GenerationConfig, dict]]
default:"None"
Optional configuration for LLM to use during RAG generation, including model selection and parameters. Will default to values specified in r2r.toml.
task_prompt_override
Optional[str]
default:"None"
Optional custom prompt to override the default task prompt.
include_title_if_available
Optional[bool]
default:"True"
Augment document chunks with their respective document titles?

RAG with custom search settings

Use hybrid search in RAG:
const hybridRagResponse = await client.rag({
  query: "Who is Jon Snow?",
  use_hybrid_search: true
});

RAG with custom completion LLM

Use a different LLM model for RAG:
const customLLMRagResponse = await client.rag({
  query: "What is R2R?",
  rag_generation_config: {
    model: "anthropic/claude-3-opus-20240229"
  }
});

Streaming RAG

Stream RAG responses for real-time applications:
const streamResponse = await client.rag({
  query: "Who was Aristotle?",
  rag_generation_config: { stream: true }
});

if (streamResponse instanceof ReadableStream) {
  const reader = streamResponse.getReader();
  while (true) {
    const { done, value } = await reader.read();
    if (done) break;
    console.log(new TextDecoder().decode(value));
  }
}

Advanced RAG Techniques

R2R supports advanced Retrieval-Augmented Generation (RAG) techniques that can be easily configured at runtime. These techniques include Hypothetical Document Embeddings (HyDE) and RAG-Fusion, which can significantly enhance the quality and relevance of retrieved information. To use an advanced RAG technique, you can specify the search_strategy parameter in your vector search settings:
const client = new R2RClient();

// Using HyDE
const hydeResponse = await client.rag(
    "What are the main themes in Shakespeare's plays?",
    {
        vector_search_settings: {
            search_strategy: "hyde",
            search_limit: 10
        }
    }
);

// Using RAG-Fusion
const ragFusionResponse = await client.rag(
    "Explain the theory of relativity",
    {
        vector_search_settings: {
            search_strategy: "rag_fusion",
            search_limit: 20
        }
    }
);
For a comprehensive guide on implementing and optimizing advanced RAG techniques in R2R, including HyDE and RAG-Fusion, please refer to our Advanced RAG Cookbook.

Customizing RAG

Putting everything together for highly custom RAG functionality:
const customRagResponse = await client.rag({
  query: "Who was Aristotle?",
  use_vector_search: true,
  use_hybrid_search: true,
  use_kg_search: true,
  kg_generation_config: {},
  rag_generation_config: {
    model: "anthropic/claude-3-haiku-20240307",
    temperature: 0.7,
    stream: true
  }
});

Agents

Multi-turn agentic RAG

The R2R application includes agents which come equipped with a search tool, enabling them to perform RAG. Using the R2R Agent for multi-turn conversations:
const messages = [
  { role: "user", content: "What was Aristotle's main contribution to philosophy?" },
  { role: "assistant", content: "Aristotle made numerous significant contributions to philosophy, but one of his main contributions was in the field of logic and reasoning. He developed a system of formal logic, which is considered the first comprehensive system of its kind in Western philosophy. This system, often referred to as Aristotelian logic or term logic, provided a framework for deductive reasoning and laid the groundwork for scientific thinking." },
  { role: "user", content: "Can you elaborate on how this influenced later thinkers?" }
];

const agentResponse = await client.agent({
  messages,
  use_vector_search: true,
  use_hybrid_search: true
});
Note that any of the customization seen in AI powered search and RAG documentation above can be applied here.
response
object
The agent endpoint will return the entire conversation as a response, including internal tool calls.
{
  results: [
    {
      role: 'system',
      content: '## You are a helpful agent that can search for information.\n\nWhen asked a question, perform a search to find relevant information and provide a response.\n\nThe response should contain line-item attributions to relevent search results, and be as informative if possible.\nIf no relevant results are found, then state that no results were found.\nIf no obvious question is present, then do not carry out a search, and instead ask for clarification.\n',
      name: null,
      function_call: null,
      tool_calls: null
    },
    {
      role: 'user',
      content: "What was Aristotle's main contribution to philosophy?",
      name: null,
      function_call: null,
      tool_calls: null
    },
    {
      role: 'assistant',
      content: "Aristotle made numerous significant contributions to philosophy, but one of his main contributions was in the field of logic and reasoning. He developed a system of formal logic, which is considered the first comprehensive system of its kind in Western philosophy. This system, often referred to as Aristotelian logic or term logic, provided a framework for deductive reasoning and laid the groundwork for scientific thinking.",
      name: null,
      function_call: null,
      tool_calls: null
    },
    {
      role: 'user',
      content: 'Can you elaborate on how this influenced later thinkers?',
      name: null,
      function_call: null,
      tool_calls: null
    },
    {
      role: 'assistant',
      content: null,
      name: null,
      function_call: {
        name: 'search',
        arguments: '{"query":"Aristotle\'s influence on later thinkers in philosophy"}'
      },
      tool_calls: null
    },
    {
      role: 'function',
      content: '1. ormation: List of writers influenced by Aristotle More than 2300 years after his death, Aristotle remains one of the most influential people who ever lived.[142][143][144] He contributed to almost every field of human knowledge then in existence, and he was the founder of many new fields. According to the philosopher Bryan Magee, "it is doubtful whether any human being has ever known as much as he did".[145]\n2. subject of contemporary philosophical discussion. Aristotle\'s views profoundly shaped medieval scholarship. The influence of his physical science extended from late antiquity and the Early Middle Ages into the Renaissance, and was not replaced systematically until the Enlightenment and theories such as classical mechanics were developed. He influenced Judeo-Islamic philosophies during the Middle Ages, as well as Christian theology, especially the Neoplatonism of the Early Church and the scholastic tradition\n3. the scholastic tradition of the Catholic Church. Aristotle was revered among medieval Muslim scholars as "The First Teacher", and among medieval Christians like Thomas Aquinas as simply "The Philosopher", while the poet Dante called him "the master of those who know". His works contain the earliest known formal study of logic, and were studied by medieval scholars such as Peter Abelard and Jean Buridan. Aristotle\'s influence on logic continued well into the 19th century. In addition, his ethics, although\n4. hilosophy\nFurther information: Peripatetic school The immediate influence of Aristotle\'s work was felt as the Lyceum grew into the Peripatetic school. Aristotle\'s students included Aristoxenus, Dicaearchus, Demetrius of Phalerum, Eudemos of Rhodes, Harpalus, Hephaestion, Mnason of Phocis, Nicomachus, and Theophrastus. Aristotle\'s influence over Alexander the Great is seen in the latter\'s bringing with him on his expedition a host of zoologists, botanists, and researchers. He had also learned a great deal\n5. scholastic philosophers. Alkindus greatly admired Aristotle\'s philosophy,[168] and Averroes spoke of Aristotle as the "exemplar" for all future philosophers.[169] Medieval Muslim scholars regularly described Aristotle as the "First Teacher".[167] The title was later used by Western philosophers (as in the famous poem of Dante) who were influenced by the tradition of Islamic philosophy.[170]\n\nMedieval Europe\nFurther information: Aristotelianism and Syllogism § Medieval\n6. those by James of Venice and William of Moerbeke. After the Scholastic Thomas Aquinas wrote his Summa Theologica, working from Moerbeke\'s translations and calling Aristotle "The Philosopher",[172] the demand for Aristotle\'s writings grew, and the Greek manuscripts returned to the West, stimulating a revival of Aristotelianism in Europe that continued into the Renaissance.[173] These thinkers blended Aristotelian philosophy with Christianity, bringing the thought of Ancient Greece into the Middle Ages.',
      name: 'search',
      function_call: null,
      tool_calls: null
    },
    {
      role: 'assistant',
      content: "Aristotle's contributions to philosophy, particularly his development of formal logic, had a profound influence on later thinkers across various cultures and eras. Here are some key ways in which his work influenced subsequent intellectual traditions:\n\n1. Medieval Islamic Philosophy: Aristotle was highly revered among medieval Muslim scholars, who referred to him as \"The First Teacher.\" His works were extensively studied and commented upon by philosophers such as Averroes and Avicenna. These scholars played a crucial role in preserving and expanding upon Aristotle's ideas, which later influenced Western thought [5].\n\n2. Christian Scholasticism: In medieval Europe, Aristotle's works were integrated into Christian theology, particularly through the efforts of Thomas Aquinas, who referred to Aristotle as \"The Philosopher.\" Aquinas' synthesis of Aristotelian philosophy with Christian doctrine became a cornerstone of Scholasticism, a dominant intellectual tradition in medieval Europe [3][6].\n\n3. Renaissance and Enlightenment: Aristotle's influence persisted into the Renaissance, where his works were revived and studied extensively. This period saw a renewed interest in classical texts, and Aristotle's ideas continued to shape scientific and philosophical inquiry until the Enlightenment, when new scientific paradigms began to emerge [2][6].\n\n4. Development of Logic: Aristotle's system of formal logic remained the standard for centuries and was studied by medieval scholars such as Peter Abelard and Jean Buridan. His influence on logic extended well into the 19th century, shaping the development of this field [3].\n\n5. Peripatetic School: Aristotle's immediate influence was also felt through the Peripatetic school, which he founded. His students, including Theophrastus, carried on his work and further developed his ideas, ensuring that his intellectual legacy continued [4].\n\nOverall, Aristotle's contributions laid the groundwork for many fields of study and influenced a wide range of thinkers, making him one of the most significant figures in the history of Western philosophy and beyond.",
      name: null,
      function_call: null,
      tool_calls: null
    }
  ]
}
messages
Array<Message>
required
The array of messages to pass to the RAG agent.
use_vector_search
boolean
default:true
Whether to use vector search.
search_filters
object
Optional filters for the search.
search_limit
number
default:10
The maximum number of search results to return.
use_hybrid_search
boolean
default:false
Whether to perform a hybrid search (combining vector and keyword search).
use_kg_search
boolean
default:false
Whether to use knowledge graph search.
generation_config
object
Optional configuration for knowledge graph search generation.
rag_generation_config
GenerationConfig
Optional configuration for RAG generation, including model selection and parameters.
task_prompt_override
string
Optional custom prompt to override the default task prompt.
include_title_if_available
boolean
default:true
Whether to include document titles in the context if available.

Multi-turn agentic RAG with streaming

The response from the RAG agent may be streamed directly back:
const messages = [
  { role: "user", content: "What was Aristotle's main contribution to philosophy?" },
  { role: "assistant", content: "Aristotle made numerous significant contributions to philosophy, but one of his main contributions was in the field of logic and reasoning. He developed a system of formal logic, which is considered the first comprehensive system of its kind in Western philosophy. This system, often referred to as Aristotelian logic or term logic, provided a framework for deductive reasoning and laid the groundwork for scientific thinking." },
  { role: "user", content: "Can you elaborate on how this influenced later thinkers?" }
];

const agentResponse = await client.agent({
  messages,
  use_vector_search: true,
  use_hybrid_search: true,
  rag_generation_config: { stream: true }
});

if (agentResponse instanceof ReadableStream) {
  const reader = agentResponse.getReader();
  while (true) {
    const { done, value } = await reader.read();
    if (done) break;
    console.log(new TextDecoder().decode(value));
  }
}
response
ReadableStream
The agent endpoint will stream back its response, including internal tool calls.
<function_call><name>search</name><arguments>{"query":"Aristotle's influence on later thinkers in philosophy"}</arguments><results>"{"id":"b234931e-0cfb-5644-8f23-560a3097f5fe","score":1.0,"metadata":{"text":"ormation: List of writers influenced by Aristotle More than 2300 years after his death, Aristotle remains one of the most influential people who ever lived.[142][143][144] He contributed to almost every field of human knowledge then in existence, and he was the founder of many new fields. According to the philosopher Bryan Magee, \"it is doubtful whether any human being has ever known as much as he did\".[145]","title":"aristotle.txt","user_id":"2acb499e-8428-543b-bd85-0d9098718220","document_id":"9fbe403b-c11c-5aae-8ade-ef22980c3ad1","extraction_id":"69431c4a-30cf-504f-8fab-7dcfc7580c63","associatedQuery":"Aristotle's influence on later thinkers in philosophy"}}","{"id":"1827ac2c-2a06-5bc2-ad29-aa14b4d99540","score":1.0,"metadata":{"text":"subject of contemporary philosophical discussion. Aristotle's views profoundly shaped medieval scholarship. The influence of his physical science extended from late antiquity and the Early Middle Ages into the Renaissance, and was not replaced systematically until the Enlightenment and theories such as classical mechanics were developed. He influenced Judeo-Islamic philosophies during the Middle Ages, as well as Christian theology, especially the Neoplatonism of the Early Church and the scholastic tradition","title":"aristotle.txt","user_id":"2acb499e-8428-543b-bd85-0d9098718220","document_id":"9fbe403b-c11c-5aae-8ade-ef22980c3ad1","extraction_id":"69431c4a-30cf-504f-8fab-7dcfc7580c63","associatedQuery":"Aristotle's influence on later thinkers in philosophy"}}","{"id":"94718936-ea92-5e29-a5ee-d4a6bc037384","score":1.0,"metadata":{"text":"the scholastic tradition of the Catholic Church. Aristotle was revered among medieval Muslim scholars as \"The First Teacher\", and among medieval Christians like Thomas Aquinas as simply \"The Philosopher\", while the poet Dante called him \"the master of those who know\". His works contain the earliest known formal study of logic, and were studied by medieval scholars such as Peter Abelard and Jean Buridan. Aristotle's influence on logic continued well into the 19th century. In addition, his ethics, although","title":"aristotle.txt","user_id":"2acb499e-8428-543b-bd85-0d9098718220","document_id":"9fbe403b-c11c-5aae-8ade-ef22980c3ad1","extraction_id":"69431c4a-30cf-504f-8fab-7dcfc7580c63","associatedQuery":"Aristotle's influence on later thinkers in philosophy"}}","{"id":"16483f14-f8a2-5c5c-8fcd-1bcbbd6603e4","score":1.0,"metadata":{"text":"hilosophy\nFurther information: Peripatetic school The immediate influence of Aristotle's work was felt as the Lyceum grew into the Peripatetic school. Aristotle's students included Aristoxenus, Dicaearchus, Demetrius of Phalerum, Eudemos of Rhodes, Harpalus, Hephaestion, Mnason of Phocis, Nicomachus, and Theophrastus. Aristotle's influence over Alexander the Great is seen in the latter's bringing with him on his expedition a host of zoologists, botanists, and researchers. He had also learned a great deal","title":"aristotle.txt","user_id":"2acb499e-8428-543b-bd85-0d9098718220","document_id":"9fbe403b-c11c-5aae-8ade-ef22980c3ad1","extraction_id":"69431c4a-30cf-504f-8fab-7dcfc7580c63","associatedQuery":"Aristotle's influence on later thinkers in philosophy"}}","{"id":"26eb20ee-a203-5ad5-beaa-511cc526aa6e","score":1.0,"metadata":{"text":"scholastic philosophers. Alkindus greatly admired Aristotle's philosophy,[168] and Averroes spoke of Aristotle as the \"exemplar\" for all future philosophers.[169] Medieval Muslim scholars regularly described Aristotle as the \"First Teacher\".[167] The title was later used by Western philosophers (as in the famous poem of Dante) who were influenced by the tradition of Islamic philosophy.[170]\n\nMedieval Europe\nFurther information: Aristotelianism and Syllogism § Medieval","title":"aristotle.txt","user_id":"2acb499e-8428-543b-bd85-0d9098718220","document_id":"9fbe403b-c11c-5aae-8ade-ef22980c3ad1","extraction_id":"69431c4a-30cf-504f-8fab-7dcfc7580c63","associatedQuery":"Aristotle's influence on later thinkers in philosophy"}}","{"id":"a08fd1b4-4e6f-5487-9af6-df2f6cfe1048","score":1.0,"metadata":{"text":"those by James of Venice and William of Moerbeke. After the Scholastic Thomas Aquinas wrote his Summa Theologica, working from Moerbeke's translations and calling Aristotle \"The Philosopher\",[172] the demand for Aristotle's writings grew, and the Greek manuscripts returned to the West, stimulating a revival of Aristotelianism in Europe that continued into the Renaissance.[173] These thinkers blended Aristotelian philosophy with Christianity, bringing the thought of Ancient Greece into the Middle Ages.","title":"aristotle.txt","user_id":"2acb499e-8428-543b-bd85-0d9098718220","document_id":"9fbe403b-c11c-5aae-8ade-ef22980c3ad1","extraction_id":"69431c4a-30cf-504f-8fab-7dcfc7580c63","associatedQuery":"Aristotle's influence on later thinkers in philosophy"}}"</results></function_call><completion>Aristotle's contributions to philosophy, particularly his development of formal logic, had a profound influence on later thinkers across various cultures and eras. Here are some key ways in which his work influenced subsequent intellectual traditions:

1. Medieval Islamic Philosophy: Aristotle was highly revered among medieval Muslim scholars, who referred to him as "The First Teacher." His works were extensively translated into Arabic and studied by philosophers such as Averroes and Avicenna. These scholars not only preserved Aristotle's works but also expanded upon them, influencing both Islamic and Western thought.

2. Christian Scholasticism: In medieval Europe, Aristotle's works were integrated into Christian theology, particularly through the efforts of Thomas Aquinas, who referred to Aristotle as "The Philosopher." Aquinas's synthesis of Aristotelian philosophy with Christian doctrine became a cornerstone of Scholasticism, a dominant intellectual tradition in medieval Europe.

3. Renaissance and Enlightenment: Aristotle's influence persisted into the Renaissance and Enlightenment periods. His works on logic, ethics, and natural sciences were foundational texts for scholars during these eras. The revival of Aristotelianism during the Renaissance helped bridge the gap between ancient Greek philosophy and modern scientific thought.

4. Development of Logic: Aristotle's system of formal logic remained the standard for centuries and was studied by medieval scholars such as Peter Abelard and Jean Buridan. His influence on logic extended well into the 19th century, shaping the development of this field.

5. Peripatetic School: Aristotle's immediate influence was felt through the Peripatetic school, which he founded. His students, including Theophrastus, continued to develop and disseminate his ideas, ensuring that his philosophical legacy endured.

Overall, Aristotle's contributions to logic, ethics, natural sciences, and metaphysics created a foundation upon which much of Western intellectual tradition was built. His work influenced a wide range of fields and thinkers, making him one of the most pivotal figures in the history of philosophy.</completion>
I