MCPcopy Create free account
hub / github.com/InternLM/InternBootcamp / BaseInteraction

Class BaseInteraction

internbootcamp/src/base_interaction.py:20–94  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

18import json
19
20class BaseInteraction:
21 def __init__(self, config: dict[str, Any], *args, **kwargs):
22 self.config = config
23 self.name: str = config.get("name", "interaction_agent") # More general agent default role name
24 self._instance_dict: dict[str, Any] = {}
25
26 async def start_interaction(self, instance_id: Optional[str] = None, identity: dict[str, Any] = None, **kwargs) -> str:
27 """Create a tool instance.
28
29 Args:
30 instance_id: The instance id of the tool.
31 identity: The identity of the interaction of a specific task.
32
33 Returns:
34 The instance id of the tool.
35 """
36 # identity类型限制
37 if not isinstance(identity, dict):
38 try:
39 identity = json.loads(identity)
40 except:
41 print(f"[DEBUG BaseInteraction] Error in start_interaction: identity is not a dict")
42 return None
43 if instance_id is None:
44 instance_id = str(uuid4())
45 self._instance_dict[instance_id] = {
46 "identity": identity,
47 }
48 return instance_id
49 else:
50 if instance_id not in self._instance_dict:
51 self._instance_dict[instance_id] = {
52 "identity": identity,
53 }
54 return instance_id
55
56 async def generate_response(
57 self, instance_id: str, messages: list[dict[str, Any]], **kwargs
58 ) -> tuple[bool, str, float, dict[str, Any]]: # More clear response generation method
59 """
60 Generates a response for the current turn of interaction.
61 Returns a tuple containing:
62 - should_terminate_sequence (bool): True if the interaction sequence should end.
63 - response_content (str): The textual content of the response.
64 - current_turn_score (float): The score for this specific turn/response.
65 - additional_data (dict): Any extra information or metadata.
66 """
67 should_terminate_sequence: bool = False # if True, end rollout
68 response_content: str = "Your current result seems acceptable."
69 current_turn_score: float = 0.8
70 additional_data: dict[str, Any] = {}
71 return should_terminate_sequence, response_content, current_turn_score, additional_data
72
73 async def calculate_score(self, instance_id: str, **kwargs) -> float: # More clear score calculation method
74 """
75 Calculates a score for the interaction,
76 potentially considering aspects like partial exposure & in-context task switching.
77 should be invoke at turn-level

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected