Skip to content

Commit c26633c

Browse files
Remove intermediate message, return function call result as well
1 parent ac4a428 commit c26633c

File tree

4 files changed

+42
-47
lines changed

4 files changed

+42
-47
lines changed

examples/game/chat_agent.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,16 @@ def check_crypto_price(currency: str):
6868

6969
user_message = input("Enter a message: ")
7070

71-
response, function_report_response = chat.next(user_message)
71+
response = chat.next(user_message)
72+
73+
if response.function_call:
74+
print(f"Function call: {response.function_call.fn_name}")
7275

7376
if response.message:
7477
print(f"Response: {response.message}")
7578

76-
if function_report_response:
77-
print(f"Response: {function_report_response}")
78-
7979
if response.is_finished:
8080
chat_continue = False
8181
break
8282

83-
84-
8583
print("Chat ended")

src/game_sdk/game/README.md

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -141,41 +141,22 @@ chat = chat_agent.create_chat(
141141
partner_id="user123",
142142
partner_name="User Name",
143143
action_space=[list_of_functions], # Optional
144-
message_handler=lambda message: print(f"Agent: {message.message}") # Required callback for handling agent messages
145144
get_state_fn=lambda: {...} # Optional, allows to push state of the environment to the agent
146145
)
147146

148147
# Run conversation
149-
while chat.next("User message"):
150-
# Continue conversation
151-
pass
148+
chat_continue = True
149+
while chat_continue:
150+
user_message = input("Enter a message: ")
151+
response = chat.next(user_message)
152+
...
153+
if response.is_finished:
154+
chat_continue = False
152155

153156
# End chat
154157
chat.end("Optional ending message")
155158
```
156159

157-
### Message Handler
158-
159-
The `message_handler` is a required callback function that processes messages from the agent. It receives an `AgentMessage` object containing:
160-
- `message`: The text response from the agent
161-
- `chat_id`: The ID of the current chat session
162-
163-
Example message handlers:
164-
165-
```python
166-
# Simple print handler
167-
def print_handler(message):
168-
print(f"Agent: {message.message}")
169-
170-
# Custom logging handler
171-
def log_handler(message):
172-
logger.info(f"Chat {message.chat_id}: {message.message}")
173-
174-
# UI update handler
175-
def ui_handler(message):
176-
update_chat_window(message.message)
177-
```
178-
179160
### Chat Termination
180161

181162
The chat can be terminated in two ways:

src/game_sdk/game/chat_agent.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from typing import Any, Callable, Dict, List, Optional, Tuple
22
from game_sdk.game.custom_types import (
3+
ChatResponse,
4+
FunctionCallResponse,
35
FunctionResult,
46
GameChatResponse,
57
Function,
@@ -23,7 +25,7 @@ def __init__(
2325
)
2426
self.get_state_fn = get_state_fn
2527

26-
def next(self, message: str) -> Tuple[GameChatResponse, Optional[str]]:
28+
def next(self, message: str) -> ChatResponse:
2729

2830
convo_response = self._update_conversation(message)
2931

@@ -46,12 +48,21 @@ def next(self, message: str) -> Tuple[GameChatResponse, Optional[str]]:
4648
"args": convo_response.function_call.args,
4749
}
4850
)
49-
function_report_response = self._report_function_result(result)
50-
51+
response_message = self._report_function_result(result)
52+
function_call_response = FunctionCallResponse(
53+
fn_name=fn_name,
54+
fn_args=convo_response.function_call.args,
55+
result=result,
56+
)
5157
else:
52-
function_report_response = None
53-
54-
return convo_response, function_report_response
58+
response_message = convo_response.message or ""
59+
function_call_response = None
60+
61+
return ChatResponse(
62+
message=response_message,
63+
is_finished=convo_response.is_finished,
64+
function_call=function_call_response,
65+
)
5566

5667
def end(self, message: Optional[str] = None):
5768
self.client.end_chat(
@@ -96,10 +107,10 @@ def __init__(
96107
self,
97108
api_key: str,
98109
prompt: str,
99-
):
110+
):
100111
self._api_key = api_key
101112
self.prompt = prompt
102-
113+
103114
if api_key.startswith("apt-"):
104115
self.client = GAMEClientV2(api_key)
105116
else:
@@ -112,7 +123,7 @@ def create_chat(
112123
action_space: Optional[List[Function]] = None,
113124
get_state_fn: Optional[Callable[[], Dict[str, Any]]] = None,
114125
) -> Chat:
115-
126+
116127
chat_id = self.client.create_chat(
117128
{
118129
"prompt": self.prompt,
@@ -121,9 +132,4 @@ def create_chat(
121132
},
122133
)
123134

124-
return Chat(
125-
chat_id,
126-
self.client,
127-
action_space,
128-
get_state_fn
129-
)
135+
return Chat(chat_id, self.client, action_space, get_state_fn)

src/game_sdk/game/custom_types.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,13 @@ class GameChatResponse(BaseModel):
253253
class AgentMessage(BaseModel):
254254
message: str
255255
chat_id: str
256+
257+
class FunctionCallResponse(BaseModel):
258+
fn_name: str
259+
fn_args: Dict[str, Any]
260+
result: FunctionResult
261+
262+
class ChatResponse(BaseModel):
263+
message: str
264+
is_finished: bool
265+
function_call: Optional[FunctionCallResponse] = None

0 commit comments

Comments
 (0)