-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: enabled saving and evaluation for moderator (#271) #272
Merged
Merged
Changes from 24 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
693f792
feat: FastAPI Implementation of Sotopia Part Two (w websocket) (#252)
XuhuiZhou 5a9f4b7
Add customizable evaluation dimensions (#256)
bugsz dea25d3
Feat/addtional fast apis for non-streaming simulation and managing re…
XuhuiZhou cadf06d
fix ci error
XuhuiZhou 187a21b
solving pytests
XuhuiZhou ec5c394
improve the tests
XuhuiZhou 1a1244e
add custom eval fast api (#268)
XuhuiZhou ae4014e
fix mypy error
XuhuiZhou ab6903a
aact moderator (#257)
XuhuiZhou 1f4fb0a
Deploy the api to modal (#267)
bugsz cb6b2d1
Feature/sotopia demo UI (#261)
XuhuiZhou 70293aa
remove dev tag
XuhuiZhou 2526be1
add custom eval
XuhuiZhou b0b53d8
base dimension
XuhuiZhou 22a1ecf
fix ui mypy
XuhuiZhou 302835a
fix mypy
XuhuiZhou 0e44603
add delete dimension
XuhuiZhou 520a1dd
update streamlit ui
XuhuiZhou 5ffdee3
ignores the ui directory
XuhuiZhou f9e2ea3
Committing changes before push
openhands-agent a45e440
pytest for eval dimension
XuhuiZhou 24ca6a3
fix mypy
XuhuiZhou 6b2db2a
clean up comments
bugsz 66da649
feat: enabled saving and evaluation for moderator (#271)
JXZhou0224 bbf6061
back compatible with evaluators[draft]
XuhuiZhou 7558927
add evaluation node
XuhuiZhou 8fc24b5
implemented the evaluator for multiagent
a67166e
Merge branch 'main' into feature/multiparty
XuhuiZhou 02ea368
fix mypy errors
cb441c0
fix mypy errors in sotopia/database
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,116 @@ | ||
## Overview | ||
|
||
Evaluation dimensions are used to evaluate the quality of social interactions. | ||
In original Sotopia paper, there are 7 dimensions to evaluate the quality of social interactions, where we named them as `sotopia` evaluation dimensions: | ||
- believability | ||
- relationship | ||
- knowledge | ||
- secret | ||
- social rules | ||
- financial and material benefits | ||
- goal | ||
|
||
The `SotopiaDimensions` can be used directly without initializing the database. It provides a set of predefined evaluation dimensions that are ready to use for evaluating social interactions. For example, | ||
|
||
```python | ||
from sotopia.envs.parallel import ParallelSotopiaEnv | ||
from sotopia.envs.evaluators import EvaluationForTwoAgents, ReachGoalLLMEvaluator, RuleBasedTerminatedEvaluator, SotopiaDimensions | ||
|
||
env = ParallelSotopiaEnv( | ||
env_profile=env_profile, | ||
model_name=model_names["env"], | ||
action_order="round-robin", | ||
evaluators=[ | ||
RuleBasedTerminatedEvaluator(max_turn_number=20, max_stale_turn=2), | ||
], | ||
terminal_evaluators=[ | ||
ReachGoalLLMEvaluator( | ||
model_names["env"], | ||
EvaluationForTwoAgents[SotopiaDimensions], # type: ignore | ||
# TODO check how to do type annotation | ||
), | ||
], | ||
) | ||
``` | ||
|
||
|
||
However we observe under many use cases people may want to evaluate with customized evaluation metrics, so we provide a way to build custom evaluation dimensions. | ||
For a quick reference, you can directly check out the `examples/use_custom_dimensions.py`. | ||
|
||
### CustomEvaluationDimension | ||
The [`CustomEvaluationDimension`](/python_API/database/evaluation_dimensions) is a class that can be used to create a custom evaluation dimension. | ||
There are four parameters: | ||
- name: the name of the dimension | ||
- description: the description of the dimension | ||
- range_low: the minimum score of the dimension (should be an integer) | ||
- range_high: the maximum score of the dimension (should be an integer) | ||
|
||
### CustomEvaluationDimensionList | ||
The [`CustomEvaluationDimensionList`](/python_API/database/evaluation_dimensions) is a class that can be used to create a custom evaluation dimension list based on the existing dimensions. It helps one to group multiple dimensions together for a specific use case. | ||
There are two parameters: | ||
- name: the name of the dimension list | ||
- dimension_pks: the primary keys of the dimensions in the dimension list | ||
|
||
### EvaluationDimensionBuilder | ||
The [`EvaluationDimensionBuilder`](/python_API/database/evaluation_dimensions) is a class that can be used to generate a custom evaluation dimension model based on the existing dimensions. | ||
|
||
|
||
## Usage | ||
### Initialize the database | ||
The default evaluation metric is still `SotopiaDimensions` in `sotopia.env.evaluators`.There is no `CustomEvaluationDimension` in the database by default. To initialize the database, please refer to `examples/use_custom_dimensions.py`. | ||
|
||
|
||
### Use the custom evaluation dimensions | ||
After you initialize your customized evaluation dimensions, you can choose to use any one of these methods provided below: | ||
|
||
#### Method 1: Choose dimensions by names | ||
```python | ||
evaluation_dimensions = ( | ||
EvaluationDimensionBuilder.select_existing_dimension_model_by_name( | ||
["transactivity", "verbal_equity"] | ||
) | ||
) | ||
``` | ||
|
||
#### Method 2: Directly choose the grouped evaluation dimension list | ||
```python | ||
evaluation_dimensions = ( | ||
EvaluationDimensionBuilder.select_existing_dimension_model_by_list_name( | ||
"sotopia" | ||
) | ||
) | ||
``` | ||
|
||
#### Method 3: Build a custom evaluation dimension model temporarily | ||
We provide multiple ways to build a custom evaluation dimension model with `EvaluationDimensionBuilder`, specifically: | ||
- `generate_dimension_model`: build an evaluation dimension from existing dimension primary keys. | ||
- `generate_dimension_model_from_dict`: build an evaluation dimension from a dictionary that specifies the parameters of the `CustomEvaluationDimension`. For example | ||
```json | ||
[ | ||
{ | ||
"name": "believability", | ||
"description": "The believability of the interaction", | ||
"range_low": 0, | ||
"range_high": 10 | ||
}, | ||
... | ||
] | ||
``` | ||
- `select_existing_dimension_model_by_name`: build an evaluation dimension from existing dimension names. For example `['believability', 'goal']` | ||
- `select_existing_dimension_model_by_list_name`: build an evaluation dimension from existing `CustomEvaluationDimensionList` list names. For example, directly use `sotopia`. | ||
|
||
|
||
After you get the evaluation dimension model, you can pass it as a parameter for the `Evaluator`, for example, | ||
```python | ||
evaluation_dimensions = ( | ||
EvaluationDimensionBuilder.select_existing_dimension_model_by_list_name( | ||
"sotopia" | ||
) | ||
) | ||
terminal_evaluators=[ | ||
ReachGoalLLMEvaluator( | ||
model_names["env"], | ||
EvaluationForTwoAgents[evaluation_dimensions], # type: ignore | ||
), | ||
], | ||
``` |
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,6 @@ | ||
# Deploy Sotopia Python API to Modal | ||
We offer a script to deploy Sotopia Python API to [Modal](https://modal.com/). | ||
To do so, simply go to the `sotopia/sotopia/ui` directory and run the following command: | ||
```bash | ||
modal deploy sotopia/ui/modal_api_server.py | ||
``` |
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,54 @@ | ||
# `evaluation_dimensions.py` | ||
|
||
This module provides classes and utilities for defining and managing custom evaluation dimensions within the Sotopia environment. It includes classes for individual dimensions, lists of dimensions, and a builder for creating dimension models. | ||
|
||
## Classes | ||
|
||
### `CustomEvaluationDimension` | ||
|
||
Represents a custom evaluation dimension with specific attributes such as name, description, and score range. | ||
|
||
#### Attributes | ||
- `name`: `str`. The name of the dimension. | ||
- `description`: `str`. A brief description of the dimension. | ||
- `range_low`: `int`. The minimum score for the dimension. | ||
- `range_high`: `int`. The maximum score for the dimension. | ||
|
||
### `CustomEvaluationDimensionList` | ||
|
||
Groups multiple custom evaluation dimensions together. | ||
|
||
#### Attributes | ||
- `name`: `str`. The name of the dimension list. | ||
- `dimension_pks`: `list[str]`. A list of primary keys for the dimensions included in the list. | ||
|
||
### `EvaluationDimensionBuilder` | ||
|
||
Provides utility methods to create and manage evaluation dimension models. | ||
|
||
#### Methods | ||
- `create_range_validator(low: int, high: int)`: Creates a validator for score ranges. | ||
|
||
**Arguments:** | ||
- `low`: `int`. The minimum score allowed. | ||
- `high`: `int`. The maximum score allowed. | ||
|
||
- `build_dimension_model(dimension_ids: list[str])`: Builds a dimension model from primary keys. | ||
|
||
**Arguments:** | ||
- `dimension_ids`: `list[str]`. A list of dimension primary keys. | ||
|
||
- `build_dimension_model_from_dict(dimensions: list[dict[str, Union[str, int]]])`: Builds a dimension model from a dictionary. | ||
|
||
**Arguments:** | ||
- `dimensions`: `list[dict[str, Union[str, int]]]`. A list of dictionaries specifying dimension attributes. | ||
|
||
- `select_existing_dimension_model_by_name(dimension_names: list[str])`: Selects a dimension model by dimension names. | ||
|
||
**Arguments:** | ||
- `dimension_names`: `list[str]`. A list of dimension names. | ||
|
||
- `select_existing_dimension_model_by_list_name(list_name: str)`: Selects a dimension model by list name. | ||
|
||
**Arguments:** | ||
- `list_name`: `str`. The name of the dimension list. |
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
142 changes: 142 additions & 0 deletions
142
examples/experimental/sotopia_original_replica/llm_agent_sotopia.py
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,142 @@ | ||
import logging | ||
import sys | ||
import json | ||
from rich.logging import RichHandler | ||
|
||
from aact import NodeFactory | ||
|
||
from sotopia.experimental.agents.base_agent import BaseAgent | ||
from sotopia.experimental.agents.datamodels import Observation, AgentAction | ||
from sotopia.database.persistent_profile import AgentProfile | ||
|
||
from sotopia.generation_utils import agenerate | ||
from sotopia.generation_utils.generate import StrOutputParser | ||
|
||
# Check Python version | ||
if sys.version_info >= (3, 11): | ||
pass | ||
else: | ||
pass | ||
|
||
# Configure logging | ||
FORMAT = "%(asctime)s - %(levelname)s - %(name)s - %(message)s" | ||
logging.basicConfig( | ||
level=logging.WARNING, | ||
format=FORMAT, | ||
datefmt="[%X]", | ||
handlers=[RichHandler()], | ||
) | ||
|
||
|
||
@NodeFactory.register("llm_agent") | ||
class LLMAgent(BaseAgent[Observation, AgentAction]): | ||
def __init__( | ||
self, | ||
input_channels: list[str], | ||
output_channel: str, | ||
query_interval: int, | ||
node_name: str, | ||
model_name: str, | ||
goal: str, | ||
agent_name: str | None = None, | ||
background: dict | None = None, | ||
agent_pk: str | None = None, | ||
redis_url: str = "redis://localhost:6379/0", | ||
): | ||
super().__init__( | ||
[(input_channel, Observation) for input_channel in input_channels], | ||
[(output_channel, AgentAction)], | ||
redis_url, | ||
node_name, | ||
) | ||
self.output_channel = output_channel | ||
self.query_interval = query_interval | ||
self.count_ticks: int = 0 | ||
self.message_history: list[Observation] = [] | ||
self.goal: str = goal | ||
self.model_name: str = model_name | ||
self.agent_profile_pk: str = agent_pk | ||
self.name: str = agent_name | ||
self.background: dict = background | ||
|
||
def set_profile(self, use_pk_value: bool): | ||
profile: AgentProfile = None | ||
if not use_pk_value: | ||
if " " in self.name: | ||
first_name, last_name = self.name.split(" ", 1) | ||
else: | ||
first_name = self.name | ||
last_name = "" | ||
profile = AgentProfile( | ||
first_name=first_name, last_name=last_name, **self.background | ||
) | ||
profile.save() | ||
else: | ||
profile = AgentProfile.get(pk=self.agent_profile_pk) | ||
|
||
self.agent_profile_pk = profile.pk | ||
self.name = " ".join([profile.first_name, profile.last_name]).strip() | ||
self.background = profile.model_dump() | ||
|
||
def _format_message_history(self, message_history: list[Observation]) -> str: | ||
## TODO: akhatua Fix the mapping of action to be gramatically correct | ||
return "\n".join(message.to_natural_language() for message in message_history) | ||
|
||
async def aact(self, obs: Observation) -> AgentAction: | ||
if obs.turn_number == -1: | ||
args = json.loads(obs.last_turn) | ||
self.set_profile(args["use_pk_value"]) | ||
return AgentAction( | ||
agent_name=self.name, | ||
output_channel=self.output_channel, | ||
action_type="none", | ||
argument=json.dumps( | ||
{"pk": self.agent_profile_pk, "model_name": self.model_name} | ||
), | ||
) | ||
|
||
self.message_history.append(obs) | ||
|
||
if len(obs.available_actions) == 1 and "none" in obs.available_actions: | ||
return AgentAction( | ||
agent_name=self.name, | ||
output_channel=self.output_channel, | ||
action_type="none", | ||
argument="", | ||
) | ||
elif len(obs.available_actions) == 1 and "leave" in obs.available_actions: | ||
self.shutdown_event.set() | ||
return AgentAction( | ||
agent_name=self.name, | ||
output_channel=self.output_channel, | ||
action_type="leave", | ||
argument="", | ||
) | ||
else: | ||
history = self._format_message_history(self.message_history) | ||
action: str = await agenerate( | ||
model_name=self.model_name, | ||
template="Imagine that you are a friend of the other persons. Here is the " | ||
"conversation between you and them.\n" | ||
"You are {agent_name} in the conversation.\n" | ||
"{message_history}\n" | ||
"and you plan to {goal}.\n" | ||
"You can choose to interrupt the other person " | ||
"by saying something or not to interrupt by outputting notiong. What would you say? " | ||
"Please only output a sentence or not outputting anything." | ||
"{format_instructions}", | ||
input_values={ | ||
"message_history": history, | ||
"goal": self.goal, | ||
"agent_name": self.name, | ||
}, | ||
temperature=0.7, | ||
output_parser=StrOutputParser(), | ||
) | ||
|
||
return AgentAction( | ||
agent_name=self.name, | ||
output_channel=self.output_channel, | ||
action_type="speak", | ||
argument=action, | ||
) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note
self.background
is not used anywhere when generating the agent actions