Skip to content

Commit

Permalink
truncate when search for followups
Browse files Browse the repository at this point in the history
  • Loading branch information
mruwnik committed May 22, 2024
1 parent 6258bb1 commit fbad8ae
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
3 changes: 2 additions & 1 deletion api/src/stampy_chat/callbacks.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions api/src/stampy_chat/followups.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions api/tests/stampy_chat/test_followups.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import pytest
from unittest.mock import patch, Mock


from stampy_chat.followups import Followup, search_authored, multisearch_authored


@pytest.mark.parametrize("query, expected_result", [
("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
])
Expand All @@ -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),
Expand Down Expand Up @@ -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"]) == [
Expand Down

0 comments on commit fbad8ae

Please sign in to comment.