first stab at session-based fetching
This commit is contained in:
parent
f33c388e1f
commit
28e3eb2dc2
2 changed files with 86 additions and 41 deletions
120
__init__.py
120
__init__.py
|
@ -11,10 +11,12 @@
|
|||
# limitations under the License.
|
||||
|
||||
from ovos_workshop.skills.fallback import FallbackSkill
|
||||
import sseclient
|
||||
import requests
|
||||
import json
|
||||
import html
|
||||
import re
|
||||
|
||||
from urllib.parse import quote_plus
|
||||
|
||||
class KagiSkill(FallbackSkill):
|
||||
|
||||
|
@ -22,12 +24,50 @@ class KagiSkill(FallbackSkill):
|
|||
self.register_fallback(self.handle_fallback_kagi, 80)
|
||||
|
||||
def handle_fallback_kagi(self, message):
|
||||
if "kagiApiKey" not in self.settings:
|
||||
if "kagiSession" in self.settings:
|
||||
return self.get_using_session(message)
|
||||
elif "kagiApiKey" in self.settings:
|
||||
return self.get_using_api(message)
|
||||
else:
|
||||
self.log.error(
|
||||
"kagi not configured yet, please set your kagiApiKey in %s",
|
||||
"kagi not configured yet, please set kagiSession or kagiApiKey in %s",
|
||||
self.settings.path,
|
||||
)
|
||||
return False # not configured yet
|
||||
|
||||
def get_using_session(self, message):
|
||||
session_cookie = self.settings.get("kagiSession", "")
|
||||
header_content = {
|
||||
'Accept': 'text/event-stream',
|
||||
'cookie': f'kagi_session={session_cookie}'
|
||||
}
|
||||
read_url = f'https://kagi.com/stream_fastgpt?query={quote_plus(message.data.get('utterance'))}'
|
||||
try:
|
||||
client = sseclient.SSEClient(read_url,
|
||||
headers=header_content,
|
||||
timeout=(3, 10))
|
||||
dirty_response = None
|
||||
for event in client:
|
||||
if event.data == '"[DONE]"':
|
||||
break;
|
||||
dirty_response = event.data
|
||||
if dirty_response is None:
|
||||
self.log.error('bad session response from kagi')
|
||||
return False
|
||||
clean_response = self.clean_session_string(dirty_response)
|
||||
self.speak(clean_response)
|
||||
return True
|
||||
except Exception as e:
|
||||
self.log.error(f'error fetching kagi session response: {str(e)}')
|
||||
return False
|
||||
finally:
|
||||
if client:
|
||||
try:
|
||||
client.close()
|
||||
except:
|
||||
pass
|
||||
|
||||
def get_using_api(self, message):
|
||||
api_key = self.settings.get("kagiApiKey", "")
|
||||
header_content = {
|
||||
'Authorization': f'Bot {api_key}',
|
||||
|
@ -36,48 +76,46 @@ class KagiSkill(FallbackSkill):
|
|||
}
|
||||
js_data = json.dumps({'query': message.data.get('utterance')})
|
||||
read_url = 'https://kagi.com/api/v0/fastgpt'
|
||||
response = requests.post(read_url,
|
||||
data=js_data,
|
||||
headers=header_content,
|
||||
verify=False)
|
||||
json_data = json.loads(response.text)
|
||||
dirty_response = self.get_kagi_response(json_data)
|
||||
if dirty_response is None:
|
||||
self.speak("I wasn't able to retrieve an answer.")
|
||||
else:
|
||||
clean_response = self.clean_string(dirty_response)
|
||||
try:
|
||||
response = requests.post(read_url,
|
||||
data=js_data,
|
||||
headers=header_content,
|
||||
verify=False)
|
||||
json_data = json.loads(response.text)
|
||||
dirty_response = self.get_api_response(json_data)
|
||||
if dirty_response is None:
|
||||
self.log.error('bad api response from kagi')
|
||||
return False
|
||||
clean_response = self.clean_api_string(dirty_response)
|
||||
self.speak(clean_response)
|
||||
return True
|
||||
return True
|
||||
except Exception as e:
|
||||
self.log.error(f'error fetching kagi api response: {str(e)}')
|
||||
return False
|
||||
|
||||
def clean_string(self, text):
|
||||
# Remove asterisks and underscores
|
||||
def clean_session_string(self, html_text):
|
||||
pattern_refs = re.compile(r'<div[^>]*>.*$', re.DOTALL)
|
||||
pattern_sups = re.compile(r'<sup[^>]*>.*?</sup>', re.DOTALL)
|
||||
pattern_tags = re.compile(r'<.*?>')
|
||||
text = re.sub(pattern_refs, '', html_text)
|
||||
text = re.sub(pattern_sups, '', text)
|
||||
text = re.sub(pattern_tags, '', text)
|
||||
return html.unescape(text)
|
||||
|
||||
def clean_api_string(self, text):
|
||||
text = text.replace('*', '').replace('_', '')
|
||||
|
||||
# Remove numbers in the brackets that Kagi uses for refs (e.g., 【1】【12】)
|
||||
text = re.sub(r'\【\d+】', '', text)
|
||||
|
||||
return text
|
||||
|
||||
def get_kagi_response(self, json_data):
|
||||
try:
|
||||
# Check if "data" exists and is a dict
|
||||
if not isinstance(json_data.get("data"), dict):
|
||||
self.log.error("'data' missing from kagi response")
|
||||
return None
|
||||
|
||||
# Check if "output" exists in data
|
||||
if "output" not in json_data["data"]:
|
||||
self.log.error("'output' missing from kagi response")
|
||||
return None
|
||||
|
||||
# Check if the output value is a string
|
||||
output_value = json_data["data"]["output"]
|
||||
if not isinstance(output_value, str):
|
||||
self.log.error(f"'output' was not a string in kagi response (found {type(output_value).__name__})")
|
||||
return None
|
||||
|
||||
return output_value
|
||||
|
||||
except Exception as e:
|
||||
self.log.error(f"error parsing kagi response: {str(e)}")
|
||||
def get_api_response(self, json_data):
|
||||
if not isinstance(json_data.get("data"), dict):
|
||||
self.log.error("'data' missing from kagi api response")
|
||||
return None
|
||||
if "output" not in json_data["data"]:
|
||||
self.log.error("'output' missing from kagi api response")
|
||||
return None
|
||||
output_value = json_data["data"]["output"]
|
||||
if not isinstance(output_value, str):
|
||||
self.log.error(f"'output' was not a string in kagi api response (found {type(output_value).__name__})")
|
||||
return None
|
||||
return output_value
|
||||
|
|
7
requirements.txt
Normal file
7
requirements.txt
Normal file
|
@ -0,0 +1,7 @@
|
|||
certifi==2025.1.31
|
||||
charset-normalizer==3.4.1
|
||||
idna==3.10
|
||||
requests==2.32.3
|
||||
six==1.17.0
|
||||
sseclient==0.0.27
|
||||
urllib3==2.3.0
|
Loading…
Add table
Reference in a new issue