Skip to content

Commit

Permalink
Add agent step callback type (run-llama#7652)
Browse files Browse the repository at this point in the history
  • Loading branch information
logan-markewich authored Sep 12, 2023
1 parent fd5c65f commit 9ad04f2
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
52 changes: 36 additions & 16 deletions llama_index/agent/openai_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,15 @@ def chat(
chat_history: Optional[List[ChatMessage]] = None,
function_call: Union[str, dict] = "auto",
) -> AgentChatResponse:
chat_response = self._chat(
message, chat_history, function_call, mode=ChatResponseMode.WAIT
)
assert isinstance(chat_response, AgentChatResponse)
with self.callback_manager.event(
CBEventType.AGENT_STEP,
payload={EventPayload.MESSAGES: [message]},
) as e:
chat_response = self._chat(
message, chat_history, function_call, mode=ChatResponseMode.WAIT
)
assert isinstance(chat_response, AgentChatResponse)
e.on_end(payload={EventPayload.RESPONSE: chat_response})
return chat_response

@trace_method("chat")
Expand All @@ -350,10 +355,15 @@ async def achat(
chat_history: Optional[List[ChatMessage]] = None,
function_call: Union[str, dict] = "auto",
) -> AgentChatResponse:
chat_response = await self._achat(
message, chat_history, function_call, mode=ChatResponseMode.WAIT
)
assert isinstance(chat_response, AgentChatResponse)
with self.callback_manager.event(
CBEventType.AGENT_STEP,
payload={EventPayload.MESSAGES: [message]},
) as e:
chat_response = await self._achat(
message, chat_history, function_call, mode=ChatResponseMode.WAIT
)
assert isinstance(chat_response, AgentChatResponse)
e.on_end(payload={EventPayload.RESPONSE: chat_response})
return chat_response

@trace_method("chat")
Expand All @@ -363,10 +373,15 @@ def stream_chat(
chat_history: Optional[List[ChatMessage]] = None,
function_call: Union[str, dict] = "auto",
) -> StreamingAgentChatResponse:
chat_response = self._chat(
message, chat_history, function_call, mode=ChatResponseMode.STREAM
)
assert isinstance(chat_response, StreamingAgentChatResponse)
with self.callback_manager.event(
CBEventType.AGENT_STEP,
payload={EventPayload.MESSAGES: [message]},
) as e:
chat_response = self._chat(
message, chat_history, function_call, mode=ChatResponseMode.STREAM
)
assert isinstance(chat_response, StreamingAgentChatResponse)
e.on_end(payload={EventPayload.RESPONSE: chat_response})
return chat_response

@trace_method("chat")
Expand All @@ -376,10 +391,15 @@ async def astream_chat(
chat_history: Optional[List[ChatMessage]] = None,
function_call: Union[str, dict] = "auto",
) -> StreamingAgentChatResponse:
chat_response = await self._achat(
message, chat_history, function_call, mode=ChatResponseMode.STREAM
)
assert isinstance(chat_response, StreamingAgentChatResponse)
with self.callback_manager.event(
CBEventType.AGENT_STEP,
payload={EventPayload.MESSAGES: [message]},
) as e:
chat_response = await self._achat(
message, chat_history, function_call, mode=ChatResponseMode.STREAM
)
assert isinstance(chat_response, StreamingAgentChatResponse)
e.on_end(payload={EventPayload.RESPONSE: chat_response})
return chat_response


Expand Down
1 change: 1 addition & 0 deletions llama_index/callbacks/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class CBEventType(str, Enum):
FUNCTION_CALL = "function_call"
RERANKING = "reranking"
EXCEPTION = "exception"
AGENT_STEP = "agent_step"


class EventPayload(str, Enum):
Expand Down
2 changes: 2 additions & 0 deletions llama_index/callbacks/wandb_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,8 @@ def _map_event_type_to_span_kind(
span_kind = self._trace_tree.SpanKind.LLM
elif event_type == CBEventType.QUERY:
span_kind = self._trace_tree.SpanKind.AGENT
elif event_type == CBEventType.AGENT_STEP:
span_kind = self._trace_tree.SpanKind.AGENT
elif event_type == CBEventType.RETRIEVE:
span_kind = self._trace_tree.SpanKind.TOOL
elif event_type == CBEventType.SYNTHESIZE:
Expand Down

0 comments on commit 9ad04f2

Please sign in to comment.