Skip to content

Commit

Permalink
News endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
estheruary committed Nov 7, 2023
1 parent 34fb984 commit 0a75b06
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ for reference in response.references:
print(reference.snippet)
print(reference.url)

response: KaginawaEnrichWebResponse = client.enrich_web(query="Best fermented hot sauce")
response: KaginawaEnrichResponse = client.enrich_web(query="Best fermented hot sauce")
# or
response: KaginawaEnrichResponse = client.enrich_news(query="Is Oliver Tree okay?")

for result in response.results:
print(result.rank)
Expand Down
2 changes: 1 addition & 1 deletion kaginawa/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from pkg_resources import parse_version

__version__ = parse_version("0.0.6")
__version__ = parse_version("0.0.7")
27 changes: 25 additions & 2 deletions kaginawa/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from kaginawa.exceptions import KaginawaError
from kaginawa.models import (
KaginawaEnrichWebResponse,
KaginawaEnrichResponse,
KaginawaFastGPTResponse,
KaginawaSummarizationResponse,
)
Expand Down Expand Up @@ -104,7 +104,30 @@ def enrich_web(self, query: str):
except requests.RequestException as e:
raise KaginawaError("Error calling /v0/enrich/web") from e

return KaginawaEnrichWebResponse.from_raw(raw_response)
return KaginawaEnrichResponse.from_raw(raw_response).results[0]

def enrich_news(self, query: str):
"""Query the Teclis index for relevant web results for a given query.
Parameters:
query (str): The search query.
Returns:
KaginawaEnrichWebResponse: A model representing the response.
"""

try:
res = self.session.get(
f"{self.api_base}/v0/enrich/news",
params={"q": query},
)
res.raise_for_status()

raw_response = res.json()
except requests.RequestException as e:
raise KaginawaError("Error calling /v0/enrich/web") from e

return KaginawaEnrichResponse.from_raw(raw_response).results[0]

def summarize(
self,
Expand Down
2 changes: 1 addition & 1 deletion kaginawa/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def from_raw(cls, raw_result: Dict[str, Any]):


@dataclass
class KaginawaEnrichWebResponse(KaginawaResponse):
class KaginawaEnrichResponse(KaginawaResponse):
results: List[KaginawaSearchResult]

@classmethod
Expand Down
6 changes: 3 additions & 3 deletions test/test_models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime, timedelta

from kaginawa.models import (
KaginawaEnrichWebResponse,
KaginawaEnrichResponse,
KaginawaFastGPTResponse,
KaginawaResponse,
KaginawaSearchResult,
Expand Down Expand Up @@ -105,7 +105,7 @@ def test_kagi_fastgpt_response_no_references(self):
assert res.tokens == tokens
assert len(res.references) == 0

def test_kagi_enrich_web_response(self):
def test_kagi_enrich_response(self):
enrich_web_response = {
"meta": {
"id": (id := "cbad1ea2-ddef-42e7-af75-81bce2c64924"),
Expand All @@ -127,7 +127,7 @@ def test_kagi_enrich_web_response(self):
),
}

res = KaginawaEnrichWebResponse.from_raw(enrich_web_response)
res = KaginawaEnrichResponse.from_raw(enrich_web_response)

assert res.id == id
assert res.node == node
Expand Down

0 comments on commit 0a75b06

Please sign in to comment.