ryanbalch commited on
Commit
faa9fb4
Β·
1 Parent(s): 70aa23a

fixing freeplay logging

Browse files
api/scripts/freeplay_playground.py CHANGED
@@ -36,9 +36,7 @@ variables = {
36
  # ]
37
  history = [
38
  {'role': 'user', 'content': 'tell me about some players in everglade fc'},
39
- # {'role': 'assistant', 'content': ''},
40
- {'role': 'tool_call', 'content': '{"number": 23, "name": "Brian Davis", "age": 27, "nationality": "USA", "shirt_number"...c image full of energy and anticipation."}'},
41
- # {'role': 'tool', 'content': '{"number": 10, "name": "Matthew Martin", "age": 24, "nationality": "USA", "shirt_numb...he dramatic flair he brings to the game."}'},
42
  {'role': 'assistant', 'content': 'Everglade FC is bursting with talent! Here are some standout players to watch:\n\n1. **...rglade FC brings to the field! Go, team! 🌟'},
43
  ]
44
 
 
36
  # ]
37
  history = [
38
  {'role': 'user', 'content': 'tell me about some players in everglade fc'},
39
+ {'role': 'tool', 'content': '{"number": 23, "name": "Brian Davis", "age": 27, "nationality": "USA", "shirt_number"...c image full of energy and anticipation."}', 'tool_call_id': 'abc123'},
 
 
40
  {'role': 'assistant', 'content': 'Everglade FC is bursting with talent! Here are some standout players to watch:\n\n1. **...rglade FC brings to the field! Go, team! 🌟'},
41
  ]
42
 
api/utils/freeplay_helpers.py CHANGED
@@ -5,10 +5,14 @@ from functools import lru_cache
5
  from typing import Union, Optional, List
6
  from freeplay import Freeplay, RecordPayload, ResponseInfo, CallInfo
7
  from freeplay.resources.prompts import FormattedPrompt
8
- from langchain_core.messages import BaseMessage
9
 
10
  FREEPLAY_PROJECT_ID = os.getenv("FREEPLAY_PROJECT_ID")
11
-
 
 
 
 
12
 
13
  # @lru_cache(maxsize=1)
14
  def _get_fp_client():
@@ -102,9 +106,19 @@ class FreeplayClient:
102
 
103
  # convert messages to Freeplay format
104
  if state['messages'] and isinstance(state['messages'][0], dict):
 
105
  all_messages = state['messages']
106
  else:
107
- all_messages = [{'role': m.type, 'content': m.content} for m in state['messages'] if m.content]
 
 
 
 
 
 
 
 
 
108
 
109
  # fix session if we landed here and it's missing
110
  if not self.session:
 
5
  from typing import Union, Optional, List
6
  from freeplay import Freeplay, RecordPayload, ResponseInfo, CallInfo
7
  from freeplay.resources.prompts import FormattedPrompt
8
+ from langchain_core.messages import BaseMessage, ToolMessage
9
 
10
  FREEPLAY_PROJECT_ID = os.getenv("FREEPLAY_PROJECT_ID")
11
+ _role_map = {
12
+ 'human': 'user',
13
+ 'ai': 'assistant',
14
+ 'tool': 'tool',
15
+ }
16
 
17
  # @lru_cache(maxsize=1)
18
  def _get_fp_client():
 
106
 
107
  # convert messages to Freeplay format
108
  if state['messages'] and isinstance(state['messages'][0], dict):
109
+ # if it's a dict leave it alone and just send it on
110
  all_messages = state['messages']
111
  else:
112
+ # otherwise assume it's langchain messages and we need to parse them for freeplay
113
+ # all_messages = [{'role': _role_map[m.type], 'content': m.content} for m in state['messages'] if m.content]
114
+ all_messages = [
115
+ {
116
+ 'role': _role_map[m.type],
117
+ 'content': m.content,
118
+ **({'tool_call_id': m.tool_call_id} if isinstance(m, ToolMessage) else {})
119
+ }
120
+ for m in state['messages'] if m.content
121
+ ]
122
 
123
  # fix session if we landed here and it's missing
124
  if not self.session:
api/workflows/base.py CHANGED
@@ -124,8 +124,7 @@ async def should_continue(state: AgentState,
124
  messages=messages,
125
  )
126
  # inform freeplay of final response
127
- # TODO: wait for feedback from freeplay about tool messages
128
- # freeplay_client.record_session(state)
129
  # trigger on_workflow_end callback
130
  if hasattr(handler, 'on_workflow_end'):
131
  await handler.on_workflow_end(state)
 
124
  messages=messages,
125
  )
126
  # inform freeplay of final response
127
+ freeplay_client.record_session(state)
 
128
  # trigger on_workflow_end callback
129
  if hasattr(handler, 'on_workflow_end'):
130
  await handler.on_workflow_end(state)