Skip to content

Commit 93cf721

Browse files
committed
fix: add forecast endpoint, bump httpx
1 parent 96644c9 commit 93cf721

File tree

6 files changed

+402
-334
lines changed

6 files changed

+402
-334
lines changed

asknews_sdk/api/chat.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
CreateChatCompletionRequest,
66
CreateChatCompletionResponse,
77
CreateChatCompletionResponseStream,
8+
ForecastResponse,
89
HeadlineQuestionsResponse,
910
ListModelResponse,
1011
)
@@ -145,6 +146,36 @@ def get_headline_questions(
145146
)
146147
return HeadlineQuestionsResponse.model_validate(response.content)
147148

149+
def get_forecast(
150+
self,
151+
query: str,
152+
lookback: int = 7,
153+
articles_per_day: int = 1,
154+
method: Literal["nl", "kw", "both"] = "kw",
155+
model: Literal["gpt-4o"] = "gpt-4o",
156+
*,
157+
http_headers: Optional[Dict] = None,
158+
) -> ForecastResponse:
159+
"""
160+
Get an expert forecast, complete with full sources
161+
and reasoning.
162+
163+
https://docs.asknews.app/en/reference#get-/v1/chat/forecast
164+
"""
165+
response = self.client.request(
166+
method="GET",
167+
endpoint="/v1/chat/forecast",
168+
headers=http_headers,
169+
query={
170+
"query": query,
171+
"lookback": lookback,
172+
"articles_per_day": articles_per_day,
173+
"method": method,
174+
"model": model,
175+
},
176+
)
177+
return ForecastResponse.model_validate(response.content)
178+
148179

149180
class AsyncChatAPI(BaseAPI):
150181
"""
@@ -280,3 +311,33 @@ async def get_headline_questions(
280311
query={"queries": queries},
281312
)
282313
return HeadlineQuestionsResponse.model_validate(response.content)
314+
315+
async def get_forecast(
316+
self,
317+
query: str,
318+
lookback: int = 7,
319+
articles_per_day: int = 1,
320+
method: Literal["nl", "kw", "both"] = "kw",
321+
model: Literal["gpt-4o"] = "gpt-4o",
322+
*,
323+
http_headers: Optional[Dict] = None,
324+
) -> ForecastResponse:
325+
"""
326+
Get an expert forecast, complete with full sources
327+
and reasoning.
328+
329+
https://docs.asknews.app/en/reference#get-/v1/chat/forecast
330+
"""
331+
response = await self.client.request(
332+
method="GET",
333+
endpoint="/v1/chat/forecast",
334+
headers=http_headers,
335+
query={
336+
"query": query,
337+
"lookback": lookback,
338+
"articles_per_day": articles_per_day,
339+
"method": method,
340+
"model": model,
341+
},
342+
)
343+
return ForecastResponse.model_validate(response.content)

asknews_sdk/api/stories.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ def search_stories(
6262
obj_type: Optional[List[Literal["story", "story_update"]]] = None,
6363
provocative: Literal["unknown", "low", "medium", "high", "all"] = "all",
6464
citation_method: Literal["brackets", "urls", "none"] = "brackets",
65+
strategy: Literal[
66+
"default", "topstories", "topstories_continents", "topstories_categories"
67+
] = "default",
6568
*,
6669
http_headers: Optional[Dict] = None,
6770
) -> StoriesResponse:
@@ -129,6 +132,7 @@ def search_stories(
129132
"uuids": uuids,
130133
"provocative": provocative,
131134
"citation_method": citation_method,
135+
"strategy": strategy,
132136
},
133137
headers=http_headers,
134138
accept=[(StoriesResponse.__content_type__, 1.0)],
@@ -247,6 +251,9 @@ async def search_stories(
247251
obj_type: Optional[List[Literal["story", "story_update"]]] = None,
248252
provocative: Literal["unknown", "low", "medium", "high", "all"] = "all",
249253
citation_method: Literal["brackets", "urls", "none"] = "brackets",
254+
strategy: Literal[
255+
"default", "topstories", "topstories_continents", "topstories_categories"
256+
] = "default",
250257
*,
251258
http_headers: Optional[Dict] = None,
252259
) -> StoriesResponse:
@@ -314,6 +321,7 @@ async def search_stories(
314321
"uuids": uuids,
315322
"provocative": provocative,
316323
"citation_method": citation_method,
324+
"strategy": strategy,
317325
},
318326
headers=http_headers,
319327
accept=[(StoriesResponse.__content_type__, 1.0)],

asknews_sdk/dto/chat.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from typing_extensions import Annotated
88

99
from asknews_sdk.dto.base import BaseSchema
10+
from asknews_sdk.dto.news import SearchResponseDictItem
1011

1112

1213
class CreateChatCompletionRequestMessage(BaseModel):
@@ -109,5 +110,14 @@ class ListModelResponse(BaseSchema):
109110
data: Annotated[List[ModelItem], Field(title="Data")]
110111

111112

112-
class HeadlineQuestionsResponse(BaseSchema, RootModel[Dict[str, List[str]]]):
113-
...
113+
class HeadlineQuestionsResponse(BaseSchema, RootModel[Dict[str, List[str]]]): ...
114+
115+
116+
class ForecastResponse(BaseModel):
117+
forecast: str
118+
resolution_criteria: str
119+
date: datetime
120+
reasoning: str
121+
sources: list[SearchResponseDictItem]
122+
timeline: list[str]
123+
opposite_request: str

asknews_sdk/dto/stories.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111

1212
class RedditPerspective(BaseModel):
13-
sentiment: Annotated[int, Field(title="Sentiment")]
13+
sentiment: Annotated[Union[int, float], Field(title="Sentiment")]
1414
relevant: Annotated[bool, Field(title="Relevant")]
1515
summary: Annotated[str, Field(title="Summary")]
1616

@@ -28,7 +28,7 @@ class RedditThread(BaseModel):
2828
id: Annotated[UUID, Field(title="Id")]
2929
key_takeaways: Annotated[List[str], Field(title="Key Takeaways")]
3030
keywords: Annotated[List[str], Field(title="Keywords")]
31-
sentiment: Annotated[int, Field(title="Sentiment")]
31+
sentiment: Annotated[Union[int, float], Field(title="Sentiment")]
3232
subreddit_name: Annotated[str, Field(title="Subreddit Name")]
3333
subreddit_url: Annotated[str, Field(title="Subreddit Url")]
3434
summary: Annotated[str, Field(title="Summary")]
@@ -141,4 +141,4 @@ class StoryResponse(BaseSchema):
141141

142142
class StoriesResponse(BaseSchema):
143143
stories: Annotated[List[StoryResponse], Field(title="Stories")]
144-
offset: Annotated[Union[int, str], Field(title="Offset")]
144+
offset: Annotated[Optional[Union[int, str]], Field(title="Offset")]

0 commit comments

Comments
 (0)