exa-py 1.9.1__tar.gz → 1.10.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of exa-py might be problematic. Click here for more details.

@@ -1,21 +1,24 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: exa-py
3
- Version: 1.9.1
3
+ Version: 1.10.0
4
4
  Summary: Python SDK for Exa API.
5
+ License: MIT
5
6
  Author: Exa AI
6
7
  Author-email: hello@exa.ai
7
- Requires-Python: >=3.9,<4.0
8
+ Requires-Python: >=3.9
9
+ Classifier: License :: OSI Approved :: MIT License
8
10
  Classifier: Programming Language :: Python :: 3
9
11
  Classifier: Programming Language :: Python :: 3.9
10
12
  Classifier: Programming Language :: Python :: 3.10
11
13
  Classifier: Programming Language :: Python :: 3.11
12
14
  Classifier: Programming Language :: Python :: 3.12
13
15
  Classifier: Programming Language :: Python :: 3.13
14
- Requires-Dist: openai (>=1.48,<2.0)
15
- Requires-Dist: pydantic (>=2.10.6,<3.0.0)
16
- Requires-Dist: pytest-mock (>=3.14.0,<4.0.0)
17
- Requires-Dist: requests (>=2.32.3,<3.0.0)
18
- Requires-Dist: typing-extensions (>=4.12.2,<5.0.0)
16
+ Requires-Dist: httpx (>=0.28.1)
17
+ Requires-Dist: openai (>=1.48)
18
+ Requires-Dist: pydantic (>=2.10.6)
19
+ Requires-Dist: pytest-mock (>=3.14.0)
20
+ Requires-Dist: requests (>=2.32.3)
21
+ Requires-Dist: typing-extensions (>=4.12.2)
19
22
  Description-Content-Type: text/markdown
20
23
 
21
24
  # Exa
@@ -0,0 +1,2 @@
1
+ from .api import Exa as Exa
2
+ from .api import AsyncExa as AsyncExa
@@ -22,6 +22,7 @@ from typing import (
22
22
  overload,
23
23
  )
24
24
 
25
+ import httpx
25
26
  import requests
26
27
  from openai import OpenAI
27
28
  from openai.types.chat.chat_completion_message_param import ChatCompletionMessageParam
@@ -730,6 +731,56 @@ class StreamAnswerResponse:
730
731
  self._raw_response.close()
731
732
 
732
733
 
734
+ class AsyncStreamAnswerResponse:
735
+ """A class representing a streaming answer response."""
736
+
737
+ def __init__(self, raw_response: httpx.Response):
738
+ self._raw_response = raw_response
739
+ self._ensure_ok_status()
740
+
741
+ def _ensure_ok_status(self):
742
+ if self._raw_response.status_code != 200:
743
+ raise ValueError(
744
+ f"Request failed with status code {self._raw_response.status_code}: {self._raw_response.text}"
745
+ )
746
+
747
+ def __aiter__(self):
748
+ async def generator():
749
+ async for line in self._raw_response.aiter_lines():
750
+ if not line:
751
+ continue
752
+ decoded_line = line.removeprefix("data: ")
753
+ try:
754
+ chunk = json.loads(decoded_line)
755
+ except json.JSONDecodeError:
756
+ continue
757
+
758
+ content = None
759
+ citations = None
760
+
761
+ if "choices" in chunk and chunk["choices"]:
762
+ if "delta" in chunk["choices"][0]:
763
+ content = chunk["choices"][0]["delta"].get("content")
764
+
765
+ if (
766
+ "citations" in chunk
767
+ and chunk["citations"]
768
+ and chunk["citations"] != "null"
769
+ ):
770
+ citations = [
771
+ AnswerResult(**to_snake_case(s)) for s in chunk["citations"]
772
+ ]
773
+
774
+ stream_chunk = StreamChunk(content=content, citations=citations)
775
+ if stream_chunk.has_data():
776
+ yield stream_chunk
777
+ return generator()
778
+
779
+ def close(self) -> None:
780
+ """Close the underlying raw response to release the network socket."""
781
+ self._raw_response.close()
782
+
783
+
733
784
  T = TypeVar("T")
734
785
 
735
786
 
@@ -789,7 +840,7 @@ class Exa:
789
840
  self,
790
841
  api_key: Optional[str],
791
842
  base_url: str = "https://api.exa.ai",
792
- user_agent: str = "exa-py 1.9.1",
843
+ user_agent: str = "exa-py 1.10.0",
793
844
  ):
794
845
  """Initialize the Exa client with the provided API key and optional base URL and user agent.
795
846
 
@@ -1837,3 +1888,333 @@ class Exa:
1837
1888
  options["stream"] = True
1838
1889
  raw_response = self.request("/answer", options)
1839
1890
  return StreamAnswerResponse(raw_response)
1891
+
1892
+ class AsyncExa(Exa):
1893
+ def __init__(self, api_key: str, api_base: str = "https://api.exa.ai"):
1894
+ super().__init__(api_key, api_base)
1895
+ self._client = None
1896
+
1897
+ @property
1898
+ def client(self) -> httpx.AsyncClient:
1899
+ # this may only be a
1900
+ if self._client is None:
1901
+ self._client = httpx.AsyncClient(
1902
+ base_url=self.base_url,
1903
+ headers=self.headers,
1904
+ timeout=60
1905
+ )
1906
+ return self._client
1907
+
1908
+ async def async_request(self, endpoint: str, data):
1909
+ """Send a POST request to the Exa API, optionally streaming if data['stream'] is True.
1910
+
1911
+ Args:
1912
+ endpoint (str): The API endpoint (path).
1913
+ data (dict): The JSON payload to send.
1914
+
1915
+ Returns:
1916
+ Union[dict, requests.Response]: If streaming, returns the Response object.
1917
+ Otherwise, returns the JSON-decoded response as a dict.
1918
+
1919
+ Raises:
1920
+ ValueError: If the request fails (non-200 status code).
1921
+ """
1922
+ if data.get("stream"):
1923
+ request = httpx.Request(
1924
+ 'POST',
1925
+ self.base_url + endpoint,
1926
+ json=data,
1927
+ headers=self.headers
1928
+ )
1929
+ res = await self.client.send(request, stream=True)
1930
+ return res
1931
+
1932
+ res = await self.client.post(self.base_url + endpoint, json=data, headers=self.headers)
1933
+ if res.status_code != 200:
1934
+ raise ValueError(
1935
+ f"Request failed with status code {res.status_code}: {res.text}"
1936
+ )
1937
+ return res.json()
1938
+
1939
+ async def search(
1940
+ self,
1941
+ query: str,
1942
+ *,
1943
+ num_results: Optional[int] = None,
1944
+ include_domains: Optional[List[str]] = None,
1945
+ exclude_domains: Optional[List[str]] = None,
1946
+ start_crawl_date: Optional[str] = None,
1947
+ end_crawl_date: Optional[str] = None,
1948
+ start_published_date: Optional[str] = None,
1949
+ end_published_date: Optional[str] = None,
1950
+ include_text: Optional[List[str]] = None,
1951
+ exclude_text: Optional[List[str]] = None,
1952
+ use_autoprompt: Optional[bool] = None,
1953
+ type: Optional[str] = None,
1954
+ category: Optional[str] = None,
1955
+ flags: Optional[List[str]] = None,
1956
+ moderation: Optional[bool] = None,
1957
+ ) -> SearchResponse[_Result]:
1958
+ """Perform a search with a prompt-engineered query to retrieve relevant results.
1959
+
1960
+ Args:
1961
+ query (str): The query string.
1962
+ num_results (int, optional): Number of search results to return (default 10).
1963
+ include_domains (List[str], optional): Domains to include in the search.
1964
+ exclude_domains (List[str], optional): Domains to exclude from the search.
1965
+ start_crawl_date (str, optional): Only links crawled after this date.
1966
+ end_crawl_date (str, optional): Only links crawled before this date.
1967
+ start_published_date (str, optional): Only links published after this date.
1968
+ end_published_date (str, optional): Only links published before this date.
1969
+ include_text (List[str], optional): Strings that must appear in the page text.
1970
+ exclude_text (List[str], optional): Strings that must not appear in the page text.
1971
+ use_autoprompt (bool, optional): Convert query to Exa (default False).
1972
+ type (str, optional): 'keyword' or 'neural' (default 'neural').
1973
+ category (str, optional): e.g. 'company'
1974
+ flags (List[str], optional): Experimental flags for Exa usage.
1975
+ moderation (bool, optional): If True, the search results will be moderated for safety.
1976
+
1977
+ Returns:
1978
+ SearchResponse: The response containing search results, etc.
1979
+ """
1980
+ options = {k: v for k, v in locals().items() if k != "self" and v is not None}
1981
+ validate_search_options(options, SEARCH_OPTIONS_TYPES)
1982
+ options = to_camel_case(options)
1983
+ data = await self.async_request("/search", options)
1984
+ cost_dollars = parse_cost_dollars(data.get("costDollars"))
1985
+ return SearchResponse(
1986
+ [Result(**to_snake_case(result)) for result in data["results"]],
1987
+ data["autopromptString"] if "autopromptString" in data else None,
1988
+ data["resolvedSearchType"] if "resolvedSearchType" in data else None,
1989
+ data["autoDate"] if "autoDate" in data else None,
1990
+ cost_dollars=cost_dollars,
1991
+ )
1992
+
1993
+ async def search_and_contents(self, query: str, **kwargs):
1994
+ options = {k: v for k, v in {"query": query, **kwargs}.items() if v is not None}
1995
+ # If user didn't ask for any particular content, default to text
1996
+ if (
1997
+ "text" not in options
1998
+ and "highlights" not in options
1999
+ and "summary" not in options
2000
+ and "extras" not in options
2001
+ ):
2002
+ options["text"] = True
2003
+
2004
+ validate_search_options(
2005
+ options,
2006
+ {
2007
+ **SEARCH_OPTIONS_TYPES,
2008
+ **CONTENTS_OPTIONS_TYPES,
2009
+ **CONTENTS_ENDPOINT_OPTIONS_TYPES,
2010
+ },
2011
+ )
2012
+
2013
+ # Nest the appropriate fields under "contents"
2014
+ options = nest_fields(
2015
+ options,
2016
+ [
2017
+ "text",
2018
+ "highlights",
2019
+ "summary",
2020
+ "subpages",
2021
+ "subpage_target",
2022
+ "livecrawl",
2023
+ "livecrawl_timeout",
2024
+ "extras",
2025
+ ],
2026
+ "contents",
2027
+ )
2028
+ options = to_camel_case(options)
2029
+ data = await self.async_request("/search", options)
2030
+ cost_dollars = parse_cost_dollars(data.get("costDollars"))
2031
+ return SearchResponse(
2032
+ [Result(**to_snake_case(result)) for result in data["results"]],
2033
+ data["autopromptString"] if "autopromptString" in data else None,
2034
+ data["resolvedSearchType"] if "resolvedSearchType" in data else None,
2035
+ data["autoDate"] if "autoDate" in data else None,
2036
+ cost_dollars=cost_dollars,
2037
+ )
2038
+
2039
+ async def get_contents(self, urls: Union[str, List[str], List[_Result]], **kwargs):
2040
+ options = {
2041
+ k: v
2042
+ for k, v in {"urls": urls, **kwargs}.items()
2043
+ if k != "self" and v is not None
2044
+ }
2045
+ if (
2046
+ "text" not in options
2047
+ and "highlights" not in options
2048
+ and "summary" not in options
2049
+ and "extras" not in options
2050
+ ):
2051
+ options["text"] = True
2052
+
2053
+ validate_search_options(
2054
+ options,
2055
+ {**CONTENTS_OPTIONS_TYPES, **CONTENTS_ENDPOINT_OPTIONS_TYPES},
2056
+ )
2057
+ options = to_camel_case(options)
2058
+ data = await self.async_request("/contents", options)
2059
+ cost_dollars = parse_cost_dollars(data.get("costDollars"))
2060
+ return SearchResponse(
2061
+ [Result(**to_snake_case(result)) for result in data["results"]],
2062
+ data.get("autopromptString"),
2063
+ data.get("resolvedSearchType"),
2064
+ data.get("autoDate"),
2065
+ cost_dollars=cost_dollars,
2066
+ )
2067
+
2068
+ async def find_similar(
2069
+ self,
2070
+ url: str,
2071
+ *,
2072
+ num_results: Optional[int] = None,
2073
+ include_domains: Optional[List[str]] = None,
2074
+ exclude_domains: Optional[List[str]] = None,
2075
+ start_crawl_date: Optional[str] = None,
2076
+ end_crawl_date: Optional[str] = None,
2077
+ start_published_date: Optional[str] = None,
2078
+ end_published_date: Optional[str] = None,
2079
+ include_text: Optional[List[str]] = None,
2080
+ exclude_text: Optional[List[str]] = None,
2081
+ exclude_source_domain: Optional[bool] = None,
2082
+ category: Optional[str] = None,
2083
+ flags: Optional[List[str]] = None,
2084
+ ) -> SearchResponse[_Result]:
2085
+ """Finds similar pages to a given URL, potentially with domain filters and date filters.
2086
+
2087
+ Args:
2088
+ url (str): The URL to find similar pages for.
2089
+ num_results (int, optional): Number of results to return. Default is None (server default).
2090
+ include_domains (List[str], optional): Domains to include in the search.
2091
+ exclude_domains (List[str], optional): Domains to exclude from the search.
2092
+ start_crawl_date (str, optional): Only links crawled after this date.
2093
+ end_crawl_date (str, optional): Only links crawled before this date.
2094
+ start_published_date (str, optional): Only links published after this date.
2095
+ end_published_date (str, optional): Only links published before this date.
2096
+ include_text (List[str], optional): Strings that must appear in the page text.
2097
+ exclude_text (List[str], optional): Strings that must not appear in the page text.
2098
+ exclude_source_domain (bool, optional): Whether to exclude the source domain.
2099
+ category (str, optional): A data category to focus on.
2100
+ flags (List[str], optional): Experimental flags.
2101
+
2102
+ Returns:
2103
+ SearchResponse[_Result]
2104
+ """
2105
+ options = {k: v for k, v in locals().items() if k != "self" and v is not None}
2106
+ validate_search_options(options, FIND_SIMILAR_OPTIONS_TYPES)
2107
+ options = to_camel_case(options)
2108
+ data = await self.async_request("/findSimilar", options)
2109
+ cost_dollars = parse_cost_dollars(data.get("costDollars"))
2110
+ return SearchResponse(
2111
+ [Result(**to_snake_case(result)) for result in data["results"]],
2112
+ data.get("autopromptString"),
2113
+ data.get("resolvedSearchType"),
2114
+ data.get("autoDate"),
2115
+ cost_dollars=cost_dollars,
2116
+ )
2117
+
2118
+ async def find_similar_and_contents(self, url: str, **kwargs):
2119
+ options = {k: v for k, v in {"url": url, **kwargs}.items() if v is not None}
2120
+ # Default to text if none specified
2121
+ if (
2122
+ "text" not in options
2123
+ and "highlights" not in options
2124
+ and "summary" not in options
2125
+ ):
2126
+ options["text"] = True
2127
+
2128
+ validate_search_options(
2129
+ options,
2130
+ {
2131
+ **FIND_SIMILAR_OPTIONS_TYPES,
2132
+ **CONTENTS_OPTIONS_TYPES,
2133
+ **CONTENTS_ENDPOINT_OPTIONS_TYPES,
2134
+ },
2135
+ )
2136
+ # We nest the content fields
2137
+ options = nest_fields(
2138
+ options,
2139
+ [
2140
+ "text",
2141
+ "highlights",
2142
+ "summary",
2143
+ "subpages",
2144
+ "subpage_target",
2145
+ "livecrawl",
2146
+ "livecrawl_timeout",
2147
+ "extras",
2148
+ ],
2149
+ "contents",
2150
+ )
2151
+ options = to_camel_case(options)
2152
+ data = await self.async_request("/findSimilar", options)
2153
+ cost_dollars = parse_cost_dollars(data.get("costDollars"))
2154
+ return SearchResponse(
2155
+ [Result(**to_snake_case(result)) for result in data["results"]],
2156
+ data.get("autopromptString"),
2157
+ data.get("resolvedSearchType"),
2158
+ data.get("autoDate"),
2159
+ cost_dollars=cost_dollars,
2160
+ )
2161
+
2162
+ async def answer(
2163
+ self,
2164
+ query: str,
2165
+ *,
2166
+ stream: Optional[bool] = False,
2167
+ text: Optional[bool] = False,
2168
+ model: Optional[Literal["exa", "exa-pro"]] = None,
2169
+ ) -> Union[AnswerResponse, StreamAnswerResponse]:
2170
+ """Generate an answer to a query using Exa's search and LLM capabilities.
2171
+
2172
+ Args:
2173
+ query (str): The query to answer.
2174
+ text (bool, optional): Whether to include full text in the results. Defaults to False.
2175
+ model (str, optional): The model to use for answering. Either "exa" or "exa-pro". Defaults to None.
2176
+
2177
+ Returns:
2178
+ AnswerResponse: An object containing the answer and citations.
2179
+
2180
+ Raises:
2181
+ ValueError: If stream=True is provided. Use stream_answer() instead for streaming responses.
2182
+ """
2183
+ if stream:
2184
+ raise ValueError(
2185
+ "stream=True is not supported in `answer()`. "
2186
+ "Please use `stream_answer(...)` for streaming."
2187
+ )
2188
+
2189
+ options = {k: v for k, v in locals().items() if k != "self" and v is not None}
2190
+ options = to_camel_case(options)
2191
+ response = await self.async_request("/answer", options)
2192
+
2193
+ return AnswerResponse(
2194
+ response["answer"],
2195
+ [AnswerResult(**to_snake_case(result)) for result in response["citations"]],
2196
+ )
2197
+
2198
+ async def stream_answer(
2199
+ self,
2200
+ query: str,
2201
+ *,
2202
+ text: bool = False,
2203
+ model: Optional[Literal["exa", "exa-pro"]] = None,
2204
+ ) -> AsyncStreamAnswerResponse:
2205
+ """Generate a streaming answer response.
2206
+
2207
+ Args:
2208
+ query (str): The query to answer.
2209
+ text (bool): Whether to include full text in the results. Defaults to False.
2210
+ model (str, optional): The model to use for answering. Either "exa" or "exa-pro". Defaults to None.
2211
+
2212
+ Returns:
2213
+ AsyncStreamAnswerResponse: An object that can be iterated over to retrieve (partial text, partial citations).
2214
+ Each iteration yields a tuple of (Optional[str], Optional[List[AnswerResult]]).
2215
+ """
2216
+ options = {k: v for k, v in locals().items() if k != "self" and v is not None}
2217
+ options = to_camel_case(options)
2218
+ options["stream"] = True
2219
+ raw_response = await self.async_request("/answer", options)
2220
+ return AsyncStreamAnswerResponse(raw_response)
@@ -12,6 +12,7 @@ typing-extensions = "^4.12.2"
12
12
  openai = "^1.48"
13
13
  pydantic = "^2.10.6"
14
14
  pytest-mock = "^3.14.0"
15
+ httpx = "^0.28.1"
15
16
 
16
17
  [tool.poetry.group.dev.dependencies]
17
18
  python-dotenv = "^1.0.1"
@@ -28,3 +29,22 @@ build-backend = "poetry.core.masonry.api"
28
29
 
29
30
  [virtualenvs]
30
31
  in-project = true
32
+
33
+ [project]
34
+ name = "exa-py"
35
+ version = "1.10.0"
36
+ description = "Python SDK for Exa API."
37
+ readme = "README.md"
38
+ requires-python = ">=3.9"
39
+ license = {text = "MIT"}
40
+ authors = [
41
+ {name = "Exa AI", email = "hello@exa.ai"}
42
+ ]
43
+ dependencies = [
44
+ "requests>=2.32.3",
45
+ "typing-extensions>=4.12.2",
46
+ "openai>=1.48",
47
+ "pydantic>=2.10.6",
48
+ "pytest-mock>=3.14.0",
49
+ "httpx>=0.28.1",
50
+ ]
@@ -1 +0,0 @@
1
- from .api import Exa as Exa
File without changes
File without changes
File without changes
File without changes
File without changes