.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
ollama.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Ollama model search engine for searxng"""
3
4from urllib.parse import urlencode
5from datetime import datetime
6from lxml import html
7
8from searx.utils import eval_xpath_list, eval_xpath_getindex, eval_xpath, extract_text
9from searx.result_types import EngineResults
10
11about = {
12 "website": "https://ollama.com",
13 "wikidata_id": "Q124636097",
14 "use_official_api": False,
15 "require_api_key": False,
16 "results": "HTML",
17}
18
19categories = ["it", "repos"]
20
21base_url = "https://ollama.com"
22
23results_xpath = '//li[@x-test-model]'
24title_xpath = './/span[@x-test-search-response-title]/text()'
25content_xpath = './/p[@class="max-w-lg break-words text-neutral-800 text-md"]/text()'
26url_xpath = './a/@href'
27publish_date_xpath = './/span[contains(@class, "flex items-center")]/@title'
28
29
30def request(query, params):
31 query_params = {"q": query}
32
33 params['url'] = f"{base_url}/search?{urlencode(query_params)}"
34 return params
35
36
37def response(resp) -> EngineResults:
38 res = EngineResults()
39
40 dom = html.fromstring(resp.text)
41
42 for item in eval_xpath_list(dom, results_xpath):
43 published_date = None
44 try:
45 published_date = datetime.strptime(
46 extract_text(eval_xpath(item, publish_date_xpath)), "%b %d, %Y %I:%M %p %Z"
47 )
48 except ValueError:
49 pass
50
51 res.add(
52 res.types.MainResult(
53 title=extract_text(eval_xpath(item, title_xpath)),
54 content=extract_text(eval_xpath(item, content_xpath)),
55 url=f"{base_url}{eval_xpath_getindex(item, url_xpath, 0)}",
56 publishedDate=published_date,
57 )
58 )
59
60 return res
request(query, params)
Definition ollama.py:30
EngineResults response(resp)
Definition ollama.py:37