页面简介
Use Exa's endpoints as a drop-in replacement for OpenAI - supporting both chat completions and responses APIs.
正文摘录
Coding Agent QuickstartNew to Exa? Get started in under a minute. Overview Exa provides OpenAI-compatible endpoints that work with the OpenAI SDK: EndpointOpenAI InterfaceModels AvailableUse Case/chat/completionsChat Completions APIexaTraditional chat interface/responsesResponses APIexa-agentAgent API (async research, enrichment, list-building) /chat/completions routes to /answer, and /responses routes to the Agent API. See Agent via Responses API below. Answer To use Exa’s /answer endpoint via the chat completions interface: Replace base URL with https://api.exa.ai Replace API key with your Exa API key Replace model name with exa. See the full /answer endpoint reference. For custom routing behavior, contact support@exa.ai. PythonJavaScriptcURLimport os from openai import OpenAI client = OpenAI( base_url="https://api.exa.ai", # use exa as the base url api_key=os.environ["EXA_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 Agent via Responses API Exa’s /responses endpoint exposes the Agent API through the OpenAI Responses interface, so the OpenAI SDKs work against it unchanged. Set model: "exa-agent" and choose an execution mode: ModeRequestBehaviorSynchronousdefault (no stream/background)The request blocks and returns the completed response object.Streamingstream: trueThe request streams OpenAI Responses events (SSE) as the run progresses, ending with response.completed.Backgroundbackground: trueThe request returns immediately with an in_progress response; poll GET /responses/{id} for the result. Set reasoning.effort (minimal, low, medium, high, xhigh, auto, ultra) to trade cost against depth, and cancel a run with POST /responses/{id}/cancel. The Agent guide covers the run model, output shape, and effort pricing that back this surface. high, xhigh, and ultra reasoning.effort runs too long for a synchronous request and return 400. Use stream: true or background: true for those runs. /responses has no budget field; ultra uses its default per-run cap. Use previous_response_id to continue a completed Responses run. Synchronous The request blocks until the run completes and returns the terminal response object. PythonJavaScriptcURLimport os from openai import OpenAI client = OpenAI( base_url="https://api.exa.ai", api_key=os.environ["EXA_API_KEY"], ) response = client.responses.create( model="exa-agent", input="Find the top 5 AI startups founded in 2025 with their funding amounts", reasoning={"effort": "medium"}, ) print(response.output_text) Streaming Set stream: true to receive Responses stream events over SSE. Events carry a monotonic sequence_number and finish with response.completed; there is no [DONE] sentinel. The stream may include : keep-alive comment lines, which SSE clients ignore. PythonJavaScriptcURLimport os from openai import OpenAI client = OpenAI( base_url="https://api.exa.ai", api_key=os.environ["EXA_API_KEY"], ) with client.responses.stream( model="exa-agent", input="Find the top 5 AI startups founded in 2025 with their funding amounts", ) as stream: for event in stream: if event.type == "response.output_text.delta": print(event.delta, end="", flush=True) final = stream.get_final_response() print("\n\n", final.output_text) Background Set background: true to start a run without holding the connection open, then poll GET /responses/{id} until it reaches a terminal status. To stream instead of polling, use Streaming. PythonJavaScriptcURLimport os import time from openai import OpenAI client = OpenAI( base_url="https://api.exa.ai", api_key=os.environ["EXA_API_KEY"], ) response = client.responses.create( model="exa-agent", input="Find the top 5 AI startups founded in 2025 with their funding amounts", background=True, ) # Poll until complete while response.status in ("queued", "in_progress"): time.sleep(5) response = client.responses.retrieve(response.id) print(response.output_text) 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. Pythonimport os from openai import OpenAI from exa_py import Exa # Initialize clients openai = OpenAI(api_key=os.environ["OPENAI_API_KEY"]) exa = Exa(api_key=os.environ["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-5.6-sol", 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-5.6-sol", 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="publication", start_published_date="2019-01-01" )