forked from langroid/langroid
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
task orchestration edge cases (langroid#324)
* task orchestration edge cases * Task: add `allow_null_result` arg * meilisearch version * update table_chat_agent sys msg; add extract example * example fix
- Loading branch information
1 parent
8fe3ea8
commit 0397b37
Showing
12 changed files
with
159 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
""" | ||
Extract structured information from a passage using a tool/function. | ||
python3 examples/extract/capitals.py | ||
""" | ||
|
||
from rich import print | ||
from pydantic import BaseModel | ||
from typing import List | ||
import langroid as lr | ||
|
||
|
||
class City(BaseModel): | ||
name: str | ||
country: str | ||
population: int | ||
|
||
|
||
class CitiesData(BaseModel): | ||
cities: List[City] | ||
|
||
|
||
PASSAGE = """ | ||
Berlin is the capital of Germany. It has a population of 3,850,809. | ||
Paris, France's capital, has 2.161 million residents. | ||
Lisbon is the capital and the largest city of Portugal with the population of 504,718. | ||
""" | ||
|
||
|
||
class CitiesMessage(lr.agent.ToolMessage): | ||
"""Tool/function to use to extract/present structured capitals info""" | ||
|
||
request: str = "capital_info" | ||
purpose: str = "Collect information about city <capitals> from a passage" | ||
capitals: List[CitiesData] | ||
|
||
def handle(self) -> str: | ||
"""Tool handler: Print the info about the capitals. | ||
Any format errors are intercepted by Langroid and passed to the LLM to fix.""" | ||
print(f"Correctly extracted Capitals Info: {self.capitals}") | ||
return "DONE" # terminates task | ||
|
||
|
||
agent = lr.ChatAgent( | ||
lr.ChatAgentConfig( | ||
name="CitiesExtractor", | ||
use_functions_api=True, | ||
use_tools=False, | ||
system_message=f""" | ||
From the passage below, extract info about city capitals, and present it | ||
using the `capital_info` tool/function. | ||
PASSAGE: {PASSAGE} | ||
""", | ||
) | ||
) | ||
# connect the Tool to the Agent, so it can use it to present extracted info | ||
agent.enable_message(CitiesMessage) | ||
|
||
# wrap the agent in a task and run it | ||
task = lr.Task( | ||
agent, | ||
interactive=False, | ||
llm_delegate=True, | ||
single_round=False, | ||
) | ||
|
||
task.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
""" | ||
Other tests for Task are in test_chat_agent.py | ||
""" | ||
import pytest | ||
|
||
from langroid.agent.chat_agent import ChatAgent, ChatAgentConfig | ||
from langroid.agent.task import Task | ||
from langroid.utils.configuration import Settings, set_global | ||
from langroid.utils.constants import NO_ANSWER | ||
|
||
|
||
@pytest.mark.parametrize("allow_null_result", [True, False]) | ||
def test_task_empty_response(test_settings: Settings, allow_null_result: bool): | ||
set_global(test_settings) | ||
agent = ChatAgent(ChatAgentConfig(name="Test")) | ||
task = Task( | ||
agent, | ||
interactive=False, | ||
single_round=True, | ||
allow_null_result=allow_null_result, | ||
system_message=""" | ||
User will send you a number. | ||
If it is EVEN, repeat the number, else return empty string. | ||
ONLY return these responses, say NOTHING ELSE | ||
""", | ||
) | ||
|
||
response = task.run("4") | ||
assert response.content == "4" | ||
response = task.run("3") | ||
assert response.content == NO_ANSWER |