Exa will parse through your messages and send only the last message to /answer or /research.

Answer

To use Exa’s /answer endpoint via the chat completions interface:
  1. Replace base URL with https://api.exa.ai
  2. Replace API key with your Exa API key
  3. Replace model name with exa.
See the full /answer endpoint reference here.
from openai import OpenAI

client = OpenAI(
  base_url="https://api.exa.ai", # use exa as the base url
  api_key="YOUR_EXA_API_KEY", # update your api key
)

completion = client.chat.completions.create(
  model="exa",
  messages = [
  {"role": "system", "content": "You are a helpful assistant."},
  {"role": "user", "content": "What are the latest developments in quantum computing?"}
],

# use extra_body to pass extra parameters to the /answer endpoint
  extra_body={
    "text": True # include full text from sources
  }
)

print(completion.choices[0].message.content)  # print the response content
print(completion.choices[0].message.citations)  # print the citations

Research

To use Exa’s research models via the chat completions interface:
  1. Replace base URL with https://api.exa.ai
  2. Replace API key with your Exa API key
  3. Replace model name with exa-research or exa-research-pro
See the full /research endpoint reference here.
import os
from openai import OpenAI

client = OpenAI(
    base_url="https://api.exa.ai",
    api_key=os.environ["EXA_API_KEY"],
)

completion = client.chat.completions.create(
    model="exa-research", # or exa-research-pro
    messages=[
        {"role": "user", "content": "What makes some LLMs so much better than others?"}
    ],
    stream=True,
)

for chunk in completion:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

Chat Wrapper

Exa provides a Python wrapper that automatically enhances any OpenAI chat completion with RAG capabilities. With one line of code, you can turn any OpenAI chat completion into an Exa-powered RAG system that handles search, chunking, and prompting automatically.
from openai import OpenAI
from exa_py import Exa

# Initialize clients
openai = OpenAI(api_key='OPENAI_API_KEY')
exa = Exa('EXA_API_KEY')

# Wrap the OpenAI client
exa_openai = exa.wrap(openai)

# Use exactly like the normal OpenAI client
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)
The wrapped client works exactly like the native OpenAI client, except it automatically improves your completions with relevant search results when needed. The wrapper supports any parameters from the exa.search() function.
completion = exa_openai.chat.completions.create(
    model="gpt-4o",
    messages=messages,
    use_exa="auto",              # "auto", "required", or "none"
    num_results=5,               # defaults to 3
    result_max_len=1024,         # defaults to 2048 characters
    include_domains=["arxiv.org"],
    category="research paper",
    start_published_date="2019-01-01"
)