Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ Before running the agent, you need to configure the following API keys either in
3. Run the agent:

```bash
hatch run python src/airline_agent/agent.py --kb-path data/kb.json --vector-db-path data/vector-db --validation-mode cleanlab
hatch run python src/airline_agent/agent.py --kb-path data/kb.json --vector-db-path data/vector-db --validation-mode agent
```

**Note:** Use `--validation-mode` to control validation: `none` (default, no validation), `cleanlab` (standard validation), or `cleanlab_log_tools` (validation with post-chat-turn tool logging)
**Note:** Use `--validation-mode` to control validation: `none` (default, no validation), `cleanlab` (standard validation), `cleanlab_log_tools` (validation with post-chat-turn tool logging), `agent` (same as cleanlab log tools but using an Agent wrapper)

## Example queries

Expand Down
29 changes: 22 additions & 7 deletions src/airline_agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from dotenv import load_dotenv
from pydantic_ai import Agent

from airline_agent.cleanlab_utils.cleanlab_agent import CleanlabAgent
from airline_agent.cleanlab_utils.validate_utils import (
get_tools_in_openai_format,
run_cleanlab_validation,
Expand Down Expand Up @@ -38,19 +39,31 @@ def get_cleanlab_project() -> Project:
def run_agent(agent: Agent, *, validation_mode: str) -> None:
message_history: list[ModelMessage] = []
project = None
if validation_mode != "none":
project = get_cleanlab_project()
thread_id = str(uuid.uuid4())
openai_tools = get_tools_in_openai_format(agent)

if validation_mode != "none":
project = get_cleanlab_project()
if validation_mode == "agent":
agent = cast(
Agent,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In mypy, cast is used to assume a typing relationship. It's useful for working around limitations of the type checker. It is up to the programmer to ensure this is correct (e.g., you can cast(str, 3) and this is ok according to mypy).

In this case, I believe this is an incorrect cast: CleanlabAgent is not an Agent. I think we should be using the AbstractAgent type in most places (and CleanlabAgent is indeed an AbstractAgent, by the class hierarchy CleanlabAgent -> Wrapper -> AbstractAgent).

CleanlabAgent(
wrapped=agent,
cleanlab_project=cast(
Project, project
), # project cannot be None since get_cleanlab_project raises if not found
context_retrieval_tools=["search", "get_article"],
thread_id=thread_id,
),
)

while True:
user_input = input("\033[96mYou:\033[0m ").strip()
if not user_input:
continue

result = agent.run_sync(user_input, message_history=message_history)

if validation_mode == "cleanlab":
result = agent.run_sync(user_input, message_history=message_history)
message_history, final_response = run_cleanlab_validation(
project=cast(Project, project), # project cannot be None since get_cleanlab_project raises if not found
query=user_input,
Expand All @@ -60,6 +73,7 @@ def run_agent(agent: Agent, *, validation_mode: str) -> None:
thread_id=thread_id,
)
elif validation_mode == "cleanlab_log_tools":
result = agent.run_sync(user_input, message_history=message_history)
message_history, final_response = run_cleanlab_validation_logging_tools(
project=cast(Project, project), # project cannot be None since get_cleanlab_project raises if not found
query=user_input,
Expand All @@ -68,7 +82,8 @@ def run_agent(agent: Agent, *, validation_mode: str) -> None:
tools=openai_tools,
thread_id=thread_id,
)
else: # validation_mode == "none"
else: # validation_mode == "none" or "agent" where agent is wrapped with CleanlabAgent above
result = agent.run_sync(user_input, message_history=message_history)
message_history.extend(result.new_messages())
final_response = result.output

Expand All @@ -90,9 +105,9 @@ def main() -> None:
parser.add_argument("--vector-db-path", type=str, required=True, help="Path to the vector database directory.")
parser.add_argument(
"--validation-mode",
choices=["none", "cleanlab", "cleanlab_log_tools"],
choices=["none", "cleanlab", "cleanlab_log_tools", "agent"],
default="none",
help="Validation mode: 'none' (no validation), 'cleanlab' (run_cleanlab_validation), 'cleanlab_log_tools' (run_cleanlab_validation_logging_tools)",
help="Validation mode: 'none' (no validation), 'cleanlab' (run_cleanlab_validation), 'cleanlab_log_tools' (run_cleanlab_validation_logging_tools), 'agent' (use CleanlabAgent wrapper)",
)

args = parser.parse_args()
Expand Down
Loading
Loading