RAG with Exa results for your OpenAI chat completion in just one line of code

What this doc covers

  • Introduce the OpenAI Exa Wrapper
  • Show how to use it to perform RAG in one line of code

Get Started

First install both the Exa and OpenAI libraries

pip install openai exa_py

Then, import and instantiate both the Exa and OpenAI clients. If you don’t have an OpenAI key, you’ll have to obtain one from the OpenAI dashboard. The same goes for Exa.

Get Exa API Key

from openai import OpenAI
openai = OpenAI(api_key='OPENAI_API_KEY')

from exa_py import Exa
exa = Exa('EXA_API_KEY')

Then, the Exa.wrap method takes your existing OpenAI client and wraps it with Exa-powered RAG capabilities.

exa_openai = exa.wrap(openai)

Which can be used just like any native OpenAI client to perform RAG with Exa search results:

completion = exa_openai.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": "What is the latest climate tech news?"}]
    )

print(completion.choices[0].message.content)
Here are some of the latest climate tech news articles:

1. **Title:** [The world’s on the verge of a carbon storage boom](https://www.technologyreview.com/2024/06/12/1093477/the-worlds-on-the-verge-of-a-carbon-storage-boom/)
    - **Summary:** Companies are planning to drill boreholes to inject carbon dioxide deep underground for storage, marking a significant trend in carbon capture projects driven by subsidies and climate targets.

2. **Title:** [Ground Floor Green: Early Stage Climate VC](https://techcrunch.com/video/ground-floor-green-early-stage-climate-vc/)
    - **Summary:** Climate tech investment is on the rise, with a focus on smarter investments in early-stage companies. The challenge lies in balancing hope and hype in selecting winners.

3. **Title:** [Climate tech startups raised nearly $40 billion in funding last year. Check out 5 of the best pitch decks that caught the eyes of investors.](https://www.businessinsider.com/5-climate-tech-pitch-decks-investors-2022-6)
    - **Summary:** Climate tech startups raised nearly $40 billion in funding in 2021, with a focus on areas like carbon accounting and market plays. The top areas for emissions reduction received only a fraction of overall investment, indicating untapped potential.

Motivation

Exa is designed from the ground up to enable seamless, accurate, and performant RAG (Retrieval-Augmented Generation). Exa provides factual, up to date information needed to ground LLM generations.

But good RAG requires more than just great search. The client needs to decide when to use RAG, with what queries. They need to handle chunking, prompting, and chaining LLM calls. We provide the Exa OpenAI wrapper that, with one line of code, does all that and turns any OpenAI chat completion into an Exa powered RAG.

exa_openai = exa.wrap(openai)

questions = [
    "How did bats evolve their wings?",
    "How did Rome defend Italy from Hannibal?",
]

for question in questions:
    completion = exa_openai.chat.completions.create( # calling the wrapper
        model="gpt-4o",
        messages=[{"role": "user", "content": question}]
    )
    
    print(f"Question: {question}\nAnswer: {completion.choices[0].message.content}")

Advanced Usage

The Exa OpenAI wrapper will intelligently handle calling Exa and providing the relevant web data to the LLM, with good defaults. However, if you need more control, the Exa OpenAI wrapper's chat.completions.create() takes a few extra parameters.

Supporting models

Exa OpenAI wrapper supports any model that supports function calling (https://platform.openai.com/docs/guides/function-calling)

exa_openai.chat.completions.create(
    model="gpt-4o",
    messages=messages
)

Supporting method

Exa OpenAI wrapper only supports chat.completion.create()currently.

Options to include Exa results

You can specify use_exa to determine whether to include Exa results for a given request:

  • auto Exa will intelligently determine whether to include results
  • required Exa results will always be included
  • none Exa results will never be included
completion = exa_openai.chat.completions.create(
    model="gpt-4o",
    messages=messages,
    use_exa="required"
)

Number of Exa results to fetch

You can set the number of results Exa will fetch (default 3)

exa_openai.chat.completions.create(
    model="gpt-4o",
    messages=messages,
    num_results=1
)

Maximum character fetched per Exa result

You can set the maximum length of each result Exa returns (default 2048 characters)

NOTE: this is measured in characters, not tokens

exa_openai.chat.completions.create(
    model="gpt-4o",
    messages=messages,
    result_max_len=1024
)

Support for Exa search parameters

Moreover, the Exa OpenAI wrapper supports any parameters that the exa.search() function accepts:

exa_openai.chat.completions.create(
    model="gpt-4o",
    messages=messages,
    include_domains=["arxiv.org"],
    category="research paper",
    start_published_date="2019-01-01"
)

Wrap up and end-to-end RAG code example

Below is a code block you can copy into any Python script or Jupyter notebook to test out a complete RAG example

from openai import OpenAI
openai = OpenAI(api_key='OPENAI_API_KEY')

from exa_py import Exa
exa = Exa('EXA_API_KEY')

exa_openai = exa.wrap(openai)

messages = [{"role": "user", "content": "How can I optimally integrate rag into my app"}]

# exa_openai.chat.completions.create("gpt-4-turbo", messages) 
completion = exa_openai.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": "What is the latest climate tech news?"}]
    )

print(completion.choices[0].message.content)
Here are some of the latest climate tech news articles:

1. Title: [The world’s on the verge of a carbon storage boom](https://www.technologyreview.com/2024/06/12/1093477/the-worlds-on-the-verge-of-a-carbon-storage-boom/)
   Description: Companies are gearing up for carbon storage projects driven by growing government subsidies, national climate targets, and declining revenue in traditional oil and gas activities.

2. Title: [Climate tech startups raised nearly $40 billion in funding last year. Check out 5 of the best pitch decks that caught the eyes of investors.](https://www.businessinsider.com/5-climate-tech-pitch-decks-investors-2022-6)
   Description: Climate tech startups raised nearly $40 billion in funding last year, with mobility and energy startups receiving significant investments. The top five technologies with the potential to reduce emissions received a smaller share of the investment dollars.

3. Title: [As the planet warms, climate tech is getting scorching hot](https://techcrunch.com/2023/06/29/as-the-planet-warms-climate-tech-is-getting-scorching-hot/)
   Description: Venture capital is pouring into climate tech startups as the urgency to combat climate change grows. Key areas of focus include electric vehicles, batteries, clean power generation, and fusion startups.

These articles highlight the trends, developments, and investments in the climate tech sector.