diff --git a/api/src/stampy_chat/callbacks.py b/api/src/stampy_chat/callbacks.py index c2951ca..e64d993 100644 --- a/api/src/stampy_chat/callbacks.py +++ b/api/src/stampy_chat/callbacks.py @@ -1,7 +1,7 @@ import threading +import traceback from queue import Queue from typing import Any, Dict, List, Callable, Iterator -from flask import stream_with_context from langchain.callbacks.base import BaseCallbackHandler from langchain.schema import BaseMessage @@ -151,6 +151,7 @@ def error_handler(function, callback): try: function(callback) except Exception as e: + logger.error(traceback.format_exc()) callback(e) callback(None) diff --git a/api/src/stampy_chat/followups.py b/api/src/stampy_chat/followups.py index 3dbe5d1..931a2e3 100644 --- a/api/src/stampy_chat/followups.py +++ b/api/src/stampy_chat/followups.py @@ -31,9 +31,11 @@ def get_followups(query): if not query.strip(): return [] - url = 'https://nlp.stampy.ai/api/search?query=' + quote(query) - response = requests.get(url).json() - return [Followup(entry['title'], entry['pageid'], entry['score']) for entry in response] + url = 'https://nlp.stampy.ai/api/search?query=' + quote(query[:4093]) # make sure there aren't too many characters + response = requests.get(url) + if response.status_code == 200: + return [Followup(entry['title'], entry['pageid'], entry['score']) for entry in response.json()] + return [] # search with multiple queries, combine results diff --git a/api/tests/stampy_chat/test_followups.py b/api/tests/stampy_chat/test_followups.py index 2f7c54b..af6e60d 100644 --- a/api/tests/stampy_chat/test_followups.py +++ b/api/tests/stampy_chat/test_followups.py @@ -1,6 +1,7 @@ import pytest from unittest.mock import patch, Mock + from stampy_chat.followups import Followup, search_authored, multisearch_authored @@ -8,7 +9,7 @@ ("what is agi", [Followup("agi title", "agi", 0.5)],), ("what is ai", [Followup("ai title", "ai", 0.5)],)]) def test_search_authored(query, expected_result): - response = Mock(json=lambda: [ + response = Mock(status_code=200, json=lambda: [ {'title': r.text, 'pageid': r.pageid, 'score': r.score} for r in expected_result ]) @@ -27,7 +28,7 @@ def test_multisearch_authored(_logger): {'pageid': '5', 'title': f'result 5', 'score': 0.543}, ] - response = Mock(json=lambda: results) + response = Mock(json=lambda: results, status_code=200) with patch('requests.get', return_value=response): assert multisearch_authored(["what is this?", "how about this?"]) == [ Followup('result 2', '2', 0.623), @@ -57,7 +58,7 @@ def test_multisearch_authored_duplicates(_logger): } def getter(url): query = url.split('query=')[-1] - return Mock(json=lambda: results[query]) + return Mock(json=lambda: results[query], status_code=200) with patch('requests.get', getter): assert multisearch_authored(["query1", "query2", "query3"]) == [