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.
Then, the Exa.wrap method takes your existing OpenAI client and wraps it with Exa-powered RAG capabilities.
Python
exa_openai = exa.wrap(openai)
Which can be used just like any native OpenAI client to perform RAG with Exa search results:
Python
completion = openai_exa.chat.completions.create( model="gpt-4o", messages=[{"role": "user", "content": "What is the latest climate tech news?"}] )print(completion.choices[0].message.content)
Stdout
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.
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.
Python
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}")
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.
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 OpenAIopenai = OpenAI(api_key='OPENAI_API_KEY')from exa_py import Exaexa = 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)
Stdout
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.