Skip to content

Commit

Permalink
working async
Browse files Browse the repository at this point in the history
  • Loading branch information
Filip Michalsky committed Mar 26, 2024
1 parent ae5dd2c commit 82ed5a1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 23 deletions.
66 changes: 54 additions & 12 deletions salesgpt/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,28 +270,70 @@ async def astep(self, stream: bool = False):
Generator: A streaming generator object if stream is set to True. Otherwise, it returns None.
"""
if not stream:
self._acall(inputs={})
return await self.acall(inputs={})
else:
return await self._astreaming_generator()

@time_logger
def acall(self, *args, **kwargs):
async def acall(self, inputs: Dict[str, Any]) -> Dict[str, Any]:

"""
This method is currently not implemented.
Executes one step of the sales agent.
This function overrides the input temporarily with the current state of the conversation,
generates the agent's utterance using either the sales agent executor or the sales conversation utterance chain,
adds the agent's response to the conversation history, and returns the AI message.
Parameters
----------
\*args : tuple
Variable length argument list.
\*\*kwargs : dict
Arbitrary keyword arguments.
inputs : Dict[str, Any]
The initial inputs for the sales agent.
Returns
-------
Dict[str, Any]
The AI message generated by the sales agent.
Raises
------
NotImplementedError
Indicates that this method has not been implemented yet.
"""
raise NotImplementedError("This method has not been implemented yet.")
# override inputs temporarily
inputs = {
"input": "",
"conversation_stage": self.current_conversation_stage,
"conversation_history": "\n".join(self.conversation_history),
"salesperson_name": self.salesperson_name,
"salesperson_role": self.salesperson_role,
"company_name": self.company_name,
"company_business": self.company_business,
"company_values": self.company_values,
"conversation_purpose": self.conversation_purpose,
"conversation_type": self.conversation_type,
}

# Generate agent's utterance
if self.use_tools:
ai_message = await self.sales_agent_executor.ainvoke(inputs)
output = ai_message["output"]
else:
ai_message = await self.sales_conversation_utterance_chain.ainvoke(
inputs, return_intermediate_steps=True
)
output = ai_message["text"]

# Add agent's response to conversation history
agent_name = self.salesperson_name
output = agent_name + ": " + output
if "<END_OF_TURN>" not in output:
output += " <END_OF_TURN>"
self.conversation_history.append(output)

if self.verbose:
tool_status = "USE TOOLS INVOKE:" if self.use_tools else "WITHOUT TOOLS:"
print(f"{tool_status}\n#\n#\n#\n#\n------------------")
print(f"AI Message: {ai_message}")
print()
print(f"Output: {output.replace('<END_OF_TURN>', '')}")

return ai_message

@time_logger
def _prep_messages(self):
Expand Down
21 changes: 10 additions & 11 deletions salesgpt/salesgptapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,17 @@ async def do(self, human_input=None):
if self.sales_agent.conversation_history
else ""
)
print("AI LOG INTERMEDIATE STEPS: ", ai_log["intermediate_steps"])

if (
self.use_tools
and ai_log["intermediate_steps"][1]["outputs"]["intermediate_steps"]
is not []
self.use_tools and
"intermediate_steps" in ai_log and
len(ai_log["intermediate_steps"]) > 0
):

try:
res_str = ai_log["intermediate_steps"][1]["outputs"][
"intermediate_steps"
][0]
tool_search_result = res_str[0]
res_str = ai_log["intermediate_steps"][0]
print("RES STR: ", res_str)
agent_action = res_str[0]
tool, tool_input, log = (
agent_action.tool,
Expand All @@ -119,10 +119,9 @@ async def do(self, human_input=None):
)
actions = re.search(r"Action: (.*?)[\n]*Action Input: (.*)", log)
action_input = actions.group(2)
action_output = ai_log["intermediate_steps"][1]["outputs"][
"intermediate_steps"
][0][1]
except:
action_output = res_str[1]
except Exception as e:
print("ERROR: ", e)
tool, tool_input, action, action_input, action_output = (
"",
"",
Expand Down

0 comments on commit 82ed5a1

Please sign in to comment.