Skip to content

Commit 56e13be

Browse files
authored
fix: Format resource list to show relevant resource fields clearly (#25)
1 parent 829c840 commit 56e13be

3 files changed

Lines changed: 44 additions & 41 deletions

File tree

app/src/app_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def db_session(self) -> db.Session:
3131
PROMPT_VERSIONS: dict = {
3232
"sample_rag": "UHJvbXB0VmVyc2lvbjox",
3333
"extract_supports": "UHJvbXB0VmVyc2lvbjoz",
34-
"generate_referrals": "UHJvbXB0VmVyc2lvbjo0",
34+
"generate_referrals": "UHJvbXB0VmVyc2lvbjo2",
3535
}
3636

3737

app/src/pipelines/generate_referrals/pipeline_wrapper.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import logging
33
from pprint import pformat
4+
from uuid import UUID
45

56
import hayhooks
67
from hayhooks import BasePipelineWrapper
@@ -9,7 +10,6 @@
910
from haystack.dataclasses.chat_message import ChatMessage
1011
from haystack_integrations.components.generators.amazon_bedrock import AmazonBedrockChatGenerator
1112
from pydantic import BaseModel
12-
from sqlalchemy.inspection import inspect
1313

1414
from src.app_config import config
1515
from src.common import haystack_utils
@@ -19,9 +19,11 @@
1919

2020

2121
class Resource(BaseModel):
22-
resource_name: str
23-
resource_addresses: list[str]
24-
resource_phones: list[str]
22+
name: str
23+
addresses: list[str]
24+
phones: list[str]
25+
emails: list[str]
26+
website: str
2527
description: str
2628
justification: str
2729

@@ -50,12 +52,12 @@ def setup(self) -> None:
5052

5153
# Called for the `generate-referrals/run` endpoint
5254
def run_api(self, query: str) -> dict:
53-
supports_from_db = retrieve_supports_from_db()
55+
supports_from_db = format_support_strings()
5456
response = self.pipeline.run(
5557
{
5658
"prompt_builder": {
5759
"query": query,
58-
"supports": supports_from_db,
60+
"supports": supports_from_db.values(),
5961
"resource_json": resource_as_json,
6062
},
6163
}
@@ -80,15 +82,16 @@ def run_chat_completion(self, model: str, messages: list, body: dict) -> None:
8082
)
8183

8284

83-
def retrieve_supports_from_db() -> list[str]:
84-
all_supports: list[str] = []
85+
def format_support_strings() -> dict[UUID, str]:
8586
with config.db_session() as db_session, db_session.begin():
86-
all_db_supports = db_session.query(Support).all()
87-
88-
for support in all_db_supports:
89-
support_dict = {
90-
c.key: getattr(support, c.key) for c in inspect(Support).mapper.column_attrs
91-
}
92-
support_as_str = json.dumps(support_dict, default=str)
93-
all_supports.append(support_as_str)
94-
return all_supports
87+
return {
88+
support.id: (
89+
f"Name: {support.name}\n"
90+
f"- Description: {support.description}\n"
91+
f"- Addresses: {', '.join(support.addresses)}\n"
92+
f"- Phones: {', '.join(support.phone_numbers)}\n"
93+
f"- Website: {support.website}\n"
94+
f"- Email Addresses: {', '.join(support.email_addresses)}\n"
95+
)
96+
for support in db_session.query(Support).all()
97+
}
Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
import json
2-
31
import pytest
4-
from sqlalchemy.inspection import inspect
52

63
from src.adapters import db
74
from src.db.models.support_listing import Support
8-
from src.pipelines.generate_referrals.pipeline_wrapper import retrieve_supports_from_db
5+
from src.pipelines.generate_referrals.pipeline_wrapper import format_support_strings
96
from tests.src.db.models.factories import SupportFactory, SupportListingFactory
107

118

129
@pytest.fixture
13-
def seed_supports(db_session: db.Session):
10+
def seed_supports(enable_factory_create, db_session: db.Session):
1411
# remove all pre-existing Support records
1512
db_session.query(Support).delete()
1613

1714
support_listing = SupportListingFactory.create()
18-
supports = []
19-
for i in range(0, 3):
20-
support = SupportFactory.create(support_listing=support_listing, name=f"support{i}")
21-
support_as_json_str = json.dumps(
22-
{c.key: getattr(support, c.key) for c in inspect(Support).mapper.column_attrs},
23-
default=str,
24-
)
25-
supports.append(support_as_json_str)
26-
return supports
27-
28-
29-
def test_retrieve_supports_from_db(enable_factory_create, seed_supports, db_session: db.Session):
30-
supports_from_db = retrieve_supports_from_db()
31-
32-
assert len(supports_from_db) == 3
33-
assert seed_supports[0] == supports_from_db[0]
34-
assert seed_supports[1] == supports_from_db[1]
35-
assert seed_supports[2] == supports_from_db[2]
15+
return [SupportFactory.create(support_listing=support_listing) for _ in range(3)]
16+
17+
18+
def test_format_support_strings(seed_supports):
19+
support_strings = format_support_strings()
20+
21+
assert len(support_strings) == len(seed_supports)
22+
23+
db_supports = {support.id: support for support in seed_supports}
24+
for k, v in support_strings.items():
25+
support = db_supports[k]
26+
# Verify that support data is somewhere in the resulting strings
27+
assert support.name in v
28+
assert support.description in v
29+
for address in support.addresses:
30+
assert address in v
31+
for phone_number in support.phone_numbers:
32+
assert phone_number in v
33+
assert support.website in v
34+
for email_address in support.email_addresses:
35+
assert email_address in v

0 commit comments

Comments
 (0)