-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai.py
More file actions
36 lines (30 loc) · 1.22 KB
/
ai.py
File metadata and controls
36 lines (30 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import re
from config import OPENAI
_LINK_RE = re.compile(r'https?://\S+')
_SUFFIX_RE = re.compile(r'(?:\[\d+]|[,.)])+$')
async def find_link(query: str) -> str | None:
completion = await OPENAI.chat.completions.create(
model='sonar-reasoning',
temperature=0.1,
messages=[
{
'role': 'system',
'content': (
'You try to find homepage websites for requested POIs. '
'Those links will be added to the POIs on OpenStreetMap. '
'Avoid non-exact and false positive matches! '
"If there isn't a high-quality match, say nothing. "
'If there is: say just the single best-matching fully-qualified URL in plain-text.'
),
},
{
'role': 'user',
'content': f'Can you search for a homepage website for this POI? {query}',
},
],
)
response = completion.choices[0].message.content.rsplit('</think>', 1)[-1].strip() # pyright: ignore [reportOptionalMemberAccess]
match = _LINK_RE.search(response)
if match is None:
return None
return _SUFFIX_RE.sub('', match.group())