|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import Any, Dict, List, Literal, Optional, Union |
| 4 | + |
| 5 | +from pydantic import BaseModel, ConfigDict, Discriminator, Field, Tag |
| 6 | +from typing_extensions import Annotated, TypeAlias |
| 7 | + |
| 8 | +from asknews_sdk.dto.base import BaseSchema |
| 9 | +from asknews_sdk.dto.chat import WebSearchResult |
| 10 | +from asknews_sdk.dto.news import SearchResponseDictItem |
| 11 | + |
| 12 | + |
| 13 | +def object_discriminator(v: Any) -> str: |
| 14 | + if isinstance(v, dict): |
| 15 | + return v.get("object", "") |
| 16 | + return getattr(v, "object", "") |
| 17 | + |
| 18 | + |
| 19 | +def kind_discriminator(v: Any) -> str: |
| 20 | + if isinstance(v, dict): |
| 21 | + return v.get("kind", "") |
| 22 | + return getattr(v, "kind", "") |
| 23 | + |
| 24 | + |
| 25 | +class DeepNewsSources(BaseModel): |
| 26 | + news: Annotated[List[SearchResponseDictItem], Field(title="News")] |
| 27 | + web: Annotated[List[WebSearchResult], Field(title="Web")] |
| 28 | + |
| 29 | + |
| 30 | +class CreateDeepNewsRequestMessage(BaseModel): |
| 31 | + role: Annotated[str, Field(title="Role")] |
| 32 | + content: Annotated[str, Field(title="Content")] |
| 33 | + name: Annotated[Optional[str], Field(None, title="Name")] |
| 34 | + function_call: Annotated[Optional[Dict[str, Any]], Field(None, title="Function Call")] |
| 35 | + |
| 36 | + |
| 37 | +class CreateDeepNewsResponseChoice(BaseModel): |
| 38 | + index: Annotated[int, Field(title="Index")] |
| 39 | + message: CreateDeepNewsRequestMessage |
| 40 | + finish_reason: Annotated[Optional[str], Field(None, title="Finish Reason")] |
| 41 | + |
| 42 | + |
| 43 | +class CreateDeepNewsResponseStreamChoice(BaseModel): |
| 44 | + index: Annotated[int, Field(title="Index")] |
| 45 | + delta: CreateDeepNewsRequestMessage |
| 46 | + finish_reason: Annotated[Optional[str], Field(None, title="Finish Reason")] |
| 47 | + |
| 48 | + |
| 49 | +class CreateDeepNewsResponseUsage(BaseModel): |
| 50 | + prompt_tokens: Annotated[int, Field(title="Prompt Tokens")] |
| 51 | + completion_tokens: Annotated[int, Field(title="Completion Tokens")] |
| 52 | + total_tokens: Annotated[int, Field(title="Total Tokens")] |
| 53 | + |
| 54 | + |
| 55 | +class CreateDeepNewsRequest(BaseSchema): |
| 56 | + model_config = ConfigDict( |
| 57 | + extra="allow", |
| 58 | + ) |
| 59 | + model: Annotated[Optional[str], Field(title="Model")] = "deepseek" |
| 60 | + messages: Annotated[List[CreateDeepNewsRequestMessage], Field(title="Messages")] |
| 61 | + temperature: Annotated[Optional[float], Field(title="Temperature")] = 0.9 |
| 62 | + top_p: Annotated[Optional[float], Field(title="Top P")] = 1.0 |
| 63 | + n: Annotated[Optional[int], Field(title="N")] = 1 |
| 64 | + stream: Annotated[Optional[bool], Field(title="Stream")] = False |
| 65 | + stop: Annotated[Optional[Union[str, List[str]]], Field(title="Stop")] = None |
| 66 | + max_tokens: Annotated[Optional[int], Field(title="Max Tokens")] = 9999 |
| 67 | + presence_penalty: Annotated[Optional[int], Field(title="Presence Penalty")] = 0 |
| 68 | + frequency_penalty: Annotated[Optional[int], Field(title="Frequency Penalty")] = 0 |
| 69 | + user: Annotated[Optional[str], Field(title="User")] = None |
| 70 | + inline_citations: Annotated[ |
| 71 | + Optional[Literal["markdown_link", "numbered", "none"]], |
| 72 | + Field(title="Type of inline citation formatting."), |
| 73 | + ] = "markdown_link" |
| 74 | + journalist_mode: Annotated[ |
| 75 | + Optional[bool], |
| 76 | + Field( |
| 77 | + title=( |
| 78 | + "Activate journalist mode, with improved alignment for making claims" |
| 79 | + "with supporting evidence. Improved journalistic style." |
| 80 | + ), |
| 81 | + ), |
| 82 | + ] = True |
| 83 | + asknews_watermark: Annotated[ |
| 84 | + Optional[bool], Field(title='Append "Generated by AskNews AI" watermark') |
| 85 | + ] = True |
| 86 | + append_references: Annotated[Optional[bool], Field(title="Append References or not")] = True |
| 87 | + conversational_awareness: Annotated[ |
| 88 | + Optional[bool], Field(title="Conversational Awareness") |
| 89 | + ] = False |
| 90 | + filter_params: Annotated[ |
| 91 | + Optional[Dict[str, Any]], |
| 92 | + Field(title="Any filter param available on the /news endpoint."), |
| 93 | + ] = None |
| 94 | + sources: Annotated[ |
| 95 | + Optional[ |
| 96 | + Union[ |
| 97 | + Literal["asknews", "google"], |
| 98 | + List[Literal["asknews", "google"]], |
| 99 | + ] |
| 100 | + ], |
| 101 | + Field(title="Sources"), |
| 102 | + ] = "asknews" |
| 103 | + search_depth: Annotated[Optional[int], Field(title="Search Depth")] = 2 |
| 104 | + max_depth: Annotated[Optional[int], Field(title="Max Depth")] = 4 |
| 105 | + return_sources: Annotated[ |
| 106 | + Optional[bool], |
| 107 | + Field( |
| 108 | + title="Return all collected sources as objects as the last token of the stream." |
| 109 | + ), |
| 110 | + ] = True |
| 111 | + |
| 112 | + |
| 113 | +class CreateDeepNewsResponse(BaseSchema): |
| 114 | + id: Annotated[str, Field(title="Id")] |
| 115 | + created: Annotated[int, Field(title="Created")] |
| 116 | + object: Annotated[Optional[str], Field("chat.completion", title="Object")] |
| 117 | + model: Annotated[Optional[str], Field("deepseek", title="Model")] |
| 118 | + usage: CreateDeepNewsResponseUsage |
| 119 | + choices: Annotated[List[CreateDeepNewsResponseChoice], Field(title="Choices")] |
| 120 | + sources: Annotated[DeepNewsSources, Field(title="Sources")] |
| 121 | + |
| 122 | + |
| 123 | +class CreateDeepNewsResponseStreamChunk(BaseSchema): |
| 124 | + __content_type__ = "text/event-stream" |
| 125 | + |
| 126 | + id: Annotated[str, Field(title="Id")] |
| 127 | + created: Annotated[int, Field(title="Created")] |
| 128 | + object: Annotated[Optional[str], Field("chat.completion.chunk", title="Object")] |
| 129 | + model: Annotated[Optional[str], Field("deepseek", title="Model")] |
| 130 | + usage: CreateDeepNewsResponseUsage |
| 131 | + choices: Annotated[List[CreateDeepNewsResponseStreamChoice], Field(title="Choices")] |
| 132 | + |
| 133 | + |
| 134 | +class CreateDeepNewsResponseStreamSourcesNewsSource(BaseModel): |
| 135 | + kind: Literal["news"] = "news" |
| 136 | + data: SearchResponseDictItem |
| 137 | + |
| 138 | + |
| 139 | +class CreateDeepNewsResponseStreamSourcesWebSource(BaseModel): |
| 140 | + kind: Literal["web"] = "web" |
| 141 | + data: WebSearchResult |
| 142 | + |
| 143 | + |
| 144 | +class CreateDeepNewsResponseStreamSource(BaseSchema): |
| 145 | + __content_type__ = "text/event-stream" |
| 146 | + |
| 147 | + id: Annotated[str, Field(title="Id")] |
| 148 | + created: Annotated[int, Field(title="Created")] |
| 149 | + object: Annotated[Optional[str], Field(title="Object")] = "chat.completion.sources" |
| 150 | + source: Annotated[ |
| 151 | + Union[ |
| 152 | + Annotated[CreateDeepNewsResponseStreamSourcesNewsSource, Tag("news")], |
| 153 | + Annotated[CreateDeepNewsResponseStreamSourcesWebSource, Tag("web")], |
| 154 | + ], |
| 155 | + Field(title="Source"), |
| 156 | + Discriminator(kind_discriminator), |
| 157 | + ] |
| 158 | + |
| 159 | + |
| 160 | +CreateDeepNewsResponseStream: TypeAlias = Annotated[ |
| 161 | + Union[ |
| 162 | + Annotated[CreateDeepNewsResponseStreamChunk, Tag("chat.completion.chunk")], |
| 163 | + Annotated[CreateDeepNewsResponseStreamSource, Tag("chat.completion.source")], |
| 164 | + ], |
| 165 | + Discriminator(object_discriminator), |
| 166 | +] |
0 commit comments