okami.mom/albedo
git clone https://git.nkp.pet/okami.mom/albedo
36a1020
main
1from __future__import annotations 2 3from typing import cast 4 5from albedo_api import Host ,PythonApi ,Record 6import albedo_trace 7 8 9class WebSearch (Record ): 10"""One answered search: `r.answer` and `r["answer"]` both work. It prints 11as the answer followed by its numbered sources.""" 12 13answer :str 14sources :list [Record ] 15 16def __str__ (self )-> str : 17lines = [self .answer ,"" ]if self .answer else [] 18for index ,source in enumerate (self .sources ,1 ): 19dated = f" ( { source . published } )" if source .published else "" 20lines .append (f"[ { index } ] { source . title } { dated } \n { source . url } " ) 21if source .snippet : 22lines .append (f" { source . snippet } " ) 23return "\n" .join (lines ) 24 25__repr__ = __str__ 26 27def markdown (self )-> str : 28"""The answer and a list of its linked sources, for the transcript.""" 29lines = [self .answer ,"" ]if self .answer else [] 30lines += [f"- [ { source . title } ]( { source . url } )" for source in self .sources ] 31return "\n" .join (lines ) 32 33 34host :Host 35 36 37async def web_search (query :str ,limit :int = 8 )-> WebSearch : 38"""Search the web: a written answer and the sources behind it.""" 39found = cast ( 40dict ,await host ("web_search.search" , {"query" :query ,"limit" :limit }) 41 ) 42result = WebSearch ( 43answer = found ["answer" ], 44sources = [Record (source )for source in found ["sources" ]], 45 ) 46albedo_trace .note ("web" ,query ,result .markdown ()) 47return result 48 49 50def setup (api :PythonApi )-> dict [str ,object ]: 51global host 52host = api .host 53return {"web_search" :web_search }