Skip to content

Commit

Permalink
added observation from using tools, added customAgentExecutor class w…
Browse files Browse the repository at this point in the history
…ith extended logging capabilities
  • Loading branch information
iljamak committed Mar 1, 2024
1 parent 0b4b7e0 commit ed67969
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 162 deletions.
49 changes: 7 additions & 42 deletions my-app/src/components/chat-interface.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Message = {
useTools: boolean,
tool?: string,
toolInput?: string,
action?: string,
actionOutput?: string,
actionInput?: string
};
};
Expand All @@ -33,7 +33,7 @@ export function ChatInterface() {
conversationalStage: string,
tool?: string,
toolInput?: string,
action?: string,
actionOutput?: string,
actionInput?: string
}[]>([]);
const [maxHeight, setMaxHeight] = useState('80vh'); // Default to 100% of the viewport height
Expand Down Expand Up @@ -110,42 +110,7 @@ export function ChatInterface() {
}

if (stream) {
const reader = response.body?.getReader();
const decoder = new TextDecoder();

const botMessage: Message = { id: session_id, text: '', sender: 'bot' };
setMessages((prevMessages) => [...prevMessages, botMessage]);

reader.read().then(function processText({ done, value }) {
if (done) {
console.log("Stream complete");
return;
}
const str = decoder.decode(value, { stream: true });

str.split('\n').forEach((line) => {
if (!line.trim()) return;
try {
const data = JSON.parse(line);
setMessages((prevMessages) => {
const newMessages = [...prevMessages];
const lastMessageIndex = newMessages.length - 1;
const lastMessage = newMessages[lastMessageIndex];
if (lastMessage.sender === 'bot') {
if (!lastMessage.text.endsWith(data.token)) {
lastMessage.text += data.token;
}
newMessages[lastMessageIndex] = lastMessage;
}
return newMessages;
});
} catch (error) {
console.error('Error parsing JSON:', error);
}
});

reader.read().then(processText);
});
{/*Not implemented*/}
} else {
if (!stream) {
const data = await response.json();
Expand All @@ -157,7 +122,7 @@ export function ChatInterface() {
conversationalStage: data.conversational_stage,
tool: data.tool,
toolInput: data.tool_input,
action: data.action,
actionOutput: data.action_output,
actionInput: data.action_input
}]);
const botMessageText = `${data.response}`;
Expand Down Expand Up @@ -248,12 +213,12 @@ export function ChatInterface() {
{process.toolInput && (
<div><strong>Tool Input:</strong> {process.toolInput}</div>
)}
{process.action && (
<div><strong>Action:</strong> {process.action}</div>
)}
{process.actionInput && (
<div><strong>Action Input:</strong> {process.actionInput}</div>
)}
{process.actionOutput && (
<div><strong>Action Output:</strong> {process.actionOutput}</div>
)}
</div>
))}
</div>
Expand Down
6 changes: 3 additions & 3 deletions salesgpt/agents.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from salesgpt.templates import CustomPromptTemplateForTools
from salesgpt.tools import get_tools, setup_knowledge_base


from salesgpt.custom_invoke import CustomAgentExecutor
def _create_retry_decorator(llm: Any) -> Callable[[Any], Any]:
import openai

Expand All @@ -40,7 +40,7 @@ class SalesGPT(Chain):
conversation_stage_id: str = "1"
current_conversation_stage: str = CONVERSATION_STAGES.get("1")
stage_analyzer_chain: StageAnalyzerChain = Field(...)
sales_agent_executor: Union[AgentExecutor, None] = Field(...)
sales_agent_executor: Union[CustomAgentExecutor, None] = Field(...)
knowledge_base: Union[RetrievalQA, None] = Field(...)
sales_conversation_utterance_chain: SalesConversationChain = Field(...)
conversation_stage_dict: Dict = CONVERSATION_STAGES
Expand Down Expand Up @@ -336,7 +336,7 @@ def from_llm(cls, llm: ChatLiteLLM, verbose: bool = False, **kwargs) -> "SalesGP
allowed_tools=tool_names,
)

sales_agent_executor = AgentExecutor.from_agent_and_tools(
sales_agent_executor = CustomAgentExecutor.from_agent_and_tools(
agent=sales_agent_with_tools, tools=tools, verbose=verbose,return_intermediate_steps=True
)
else:
Expand Down
9 changes: 7 additions & 2 deletions salesgpt/custom_invoke.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from langchain.chains.base import Chain
from typing import Dict, Any, Optional
# Corrected import path for RunnableConfig
from langchain.agents import AgentExecutor
from langchain_core.runnables import RunnableConfig, ensure_config
from langchain_core.outputs import RunInfo
from langchain.callbacks.manager import (
Expand All @@ -10,7 +11,7 @@
from langchain_core.load.dump import dumpd
import inspect

class CustomChain(Chain):
class CustomAgentExecutor(AgentExecutor):
def invoke(
self,
input: Dict[str, Any],
Expand Down Expand Up @@ -79,4 +80,8 @@ def invoke(
# Include intermediate steps in the final outputs
final_outputs["intermediate_steps"] = intermediate_steps

return final_outputs
return final_outputs


if __name__=="__main__":
agent = CustomAgentExecutor()
110 changes: 0 additions & 110 deletions salesgpt/invoke_custom.py

This file was deleted.

10 changes: 5 additions & 5 deletions salesgpt/salesgptapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,19 @@ def do(self, human_input=None):
agent_action = res_str[0]
tool,tool_input,log = agent_action.tool, agent_action.tool_input, agent_action.log
actions = re.search(r"Action: (.*?)[\n]*Action Input: (.*)",log)
action = actions.group(1)
action_input = actions.group(2)
action_input= actions.group(2)
action_output = ai_log['intermediate_steps'][1]['outputs']['intermediate_steps'][0][1]
except:
tool,tool_input,action,action_input = "","","",""
tool,tool_input,action,action_input,action_output = "","","","",""
else:
tool,tool_input,action,action_input = "","","",""
tool,tool_input,action,action_input,action_output = "","","","",""
return {
"bot_name": reply.split(": ")[0],
"response": reply.split(": ")[1].rstrip('<END_OF_TURN>'),
"conversational_stage": self.sales_agent.current_conversation_stage,
"tool": tool,
"tool_input": tool_input,
"action": action,
"action_output": action_output,
"action_input": action_input
}

Expand Down

0 comments on commit ed67969

Please sign in to comment.