Skip to content

Commit

Permalink
cleaning up
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Dunne committed Oct 22, 2024
1 parent c0c1faf commit a3cf27b
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 13 deletions.
5 changes: 3 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
"pythonTerminalEnvVarActivation"
],
"python.testing.pytestArgs": [
"redbox-core"
"redbox-core/tests"
],
"python.pythonPath": "redbox-core/.venv/bin/python",
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
"python.testing.pytestEnabled": true,
}
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ services:
condition: service_healthy
elasticsearch:
condition: service_healthy

networks:
- redbox-app-network
env_file:
Expand Down
6 changes: 3 additions & 3 deletions redbox-core/redbox/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
get_metadata_retriever,
get_parameterised_retriever,
)
from redbox.graph.nodes.tools import build_gov_uk_search_tool, build_search_documents_tool
from redbox.graph.nodes.tools import build_govuk_search_tool, build_search_documents_tool
from redbox.graph.root import get_root_graph
from redbox.models.chain import RedboxState
from redbox.models.chat import ChatRoute
Expand Down Expand Up @@ -57,11 +57,11 @@ def __init__(
chunk_resolution=ChunkResolution.normal,
)

search_gov_uk = build_gov_uk_search_tool()
search_govuk = build_govuk_search_tool()

tools: dict[str, StructuredTool] = {
"_search_documents": search_documents,
"_search_gov_uk": search_gov_uk,
"_search_govuk": search_govuk,
}

self.graph = get_root_graph(
Expand Down
6 changes: 3 additions & 3 deletions redbox-core/redbox/graph/nodes/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ def _search_documents(query: str, state: Annotated[dict, InjectedState]) -> dict
return _search_documents


def build_gov_uk_search_tool(num_results: int = 10) -> Tool:
def build_govuk_search_tool(num_results: int = 10) -> Tool:
"""Constructs a tool that searches gov.uk and sets state["documents"]."""

tokeniser = tiktoken.encoding_for_model("gpt-4o")

@tool
def _search_gov_uk(query: str, state: Annotated[dict, InjectedState]) -> dict[str, Any]:
def _search_govuk(query: str, state: Annotated[dict, InjectedState]) -> dict[str, Any]:
"""
Search for documents on gov.uk based on a query string.
This endpoint is used to search for documents on gov.uk. There are many types of documents on gov.uk.
Expand Down Expand Up @@ -183,4 +183,4 @@ def _search_gov_uk(query: str, state: Annotated[dict, InjectedState]) -> dict[st

return {"documents": structure_documents_by_group_and_indices(mapped_documents)}

return _search_gov_uk
return _search_govuk
2 changes: 1 addition & 1 deletion redbox-core/redbox/graph/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def get_agentic_search_graph(tools: dict[str, StructuredTool], debug: bool = Fal
builder = StateGraph(RedboxState)

# Tools
agent_tool_names = ["_search_documents", "_search_gov_uk"]
agent_tool_names = ["_search_documents", "_search_govuk"]
agent_tools: list[StructuredTool] = [tools.get(tool_name) for tool_name in agent_tool_names]

# Processes
Expand Down
43 changes: 43 additions & 0 deletions redbox-core/tests/graph/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,43 @@ def assert_number_of_events(num_of_events: int):
],
test_id="No Such Keyword with docs",
),
generate_test_cases(
query=RedboxQuery(
question="@gadget Tell me about travel advice to cuba",
s3_keys=[],
user_uuid=uuid4(),
chat_history=[],
permitted_s3_keys=["s3_key"],
),
test_data=[
RedboxTestData(
number_of_docs=1,
tokens_in_all_docs=10000,
expected_llm_response=[
AIMessage(
content="",
additional_kwargs={
"tool_calls": [
{
"id": "call_e4003b",
"function": {
"arguments": '{\n "query": "travel advice to cuba"\n}',
"name": "_search_govuk",
},
"type": "function",
}
]
},
),
"answer",
"AI is a lie",
],
expected_route=ChatRoute.gadget,
s3_keys=["s3_key"],
),
],
test_id="Agentic govuk search",
),
]
for test_case in generated_cases
]
Expand All @@ -434,7 +471,13 @@ def _search_documents(query: str) -> dict[str, Any]:
"""Tool to search documents."""
return {"documents": structure_documents_by_group_and_indices(test_case.docs)}

@tool
def _search_govuk(query: str) -> dict[str, Any]:
"""Tool to search gov.uk for travel advice and other government information."""
return {"documents": structure_documents_by_group_and_indices(test_case.docs)}

mocker.patch("redbox.app.build_search_documents_tool", return_value=_search_documents)
mocker.patch("redbox.app.build_govuk_search_tool", return_value=_search_govuk)
mocker.patch("redbox.graph.nodes.processes.get_chat_llm", return_value=llm)

# Instantiate app
Expand Down
6 changes: 3 additions & 3 deletions redbox-core/tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from langgraph.prebuilt import InjectedState

from redbox.graph.nodes.tools import (
build_gov_uk_search_tool,
build_govuk_search_tool,
build_search_documents_tool,
has_injected_state,
is_valid_tool,
Expand Down Expand Up @@ -152,8 +152,8 @@ def test_search_documents_tool(
assert group_docs[doc.metadata["uuid"]] == doc


def test_gov_uk_search_tool():
tool = build_gov_uk_search_tool()
def test_govuk_search_tool():
tool = build_govuk_search_tool()

state_update = tool.invoke(
{
Expand Down

0 comments on commit a3cf27b

Please sign in to comment.