diff --git a/CHANGELOG.txt b/CHANGELOG.txt new file mode 100644 index 00000000..64bdfbf7 --- /dev/null +++ b/CHANGELOG.txt @@ -0,0 +1,11 @@ +Updates to the SalesGPT project: Building the world's best AI Sales Agents. + + +July 14, 2023 +------------- + +Version 0.0.4 +- Added tools to SalesGPT, creating a true agent. +- Added product knowledge base as an example tool + +------------- \ No newline at end of file diff --git a/README.md b/README.md index c5b1e74a..65012852 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,9 @@ This repo demonstrates an implementation of a **context-aware** AI Sales Assistant using LLMs. SalesGPT is context-aware, which means it can understand what section of a sales conversation it is in and act accordingly. +Morever, SalesGPT has access to tools, such as your own pre-defined product knowledge base, significantly reducing hallucinations! -We leverage the [`langchain`](https://github.com/hwchase17/langchain) library in this implementation and are inspired by [BabyAGI](https://github.com/yoheinakajima/babyagi) architecture . +We leverage the [`langchain`](https://github.com/hwchase17/langchain) library in this implementation, specifically [Custom Agent Configuration](https://langchain-langchain.vercel.app/docs/modules/agents/how_to/custom_agent_with_tool_retrieval) and are inspired by [BabyAGI](https://github.com/yoheinakajima/babyagi) architecture. ## Our Vision: Build the Best Open-Source Autonomous Sales Agent @@ -12,16 +13,17 @@ We are building SalesGPT to power your best Autonomous Sales Agents. Hence, we w **If you want us to build better towards your needs, please fill out our 45 seconds [SalesGPT Use Case Survey](https://5b7mfhwiany.typeform.com/to/xmJbWIjG)** -## :red_circle: Latest News +### If you looking for help building your Autonomous Sales Agents -### Demo: SalesGPT Outbound Prospecting: A New Way to Sell? 🤔 +I am currently open to freelancing opps - please contact me through [my website](https://odysseypartners.ai?utm_source=SalesGPT) if you think I can help you. -https://github.com/filip-michalsky/SalesGPT/assets/31483888/2b13ba28-4e07-41dc-a8bf-4084d25247ca +## :red_circle: Latest News +- Sales Agent can now take advantage of **tools**, such as look up products in a product catalog! -### If you looking for help building your Autonomous Sales Agents +### Demo: SalesGPT Outbound Prospecting: A New Way to Sell? 🤔 -I am currently open to freelancing opps - please contact me through [my website](https://odysseypartners.ai?utm_source=SalesGPT) if you think I can help you. +https://github.com/filip-michalsky/SalesGPT/assets/31483888/2b13ba28-4e07-41dc-a8bf-4084d25247ca ## Quickstart @@ -32,7 +34,7 @@ from langchain.chat_models import ChatOpenAI os.environ['OPENAI_API_KEY'] = 'sk-xxx' # fill me in -llm = ChatOpenAI(temperature=0.9) +llm = ChatOpenAI(temperature=0.4) sales_agent = SalesGPT.from_llm(llm, verbose=False, salesperson_name="Ted Lasso", @@ -73,10 +75,14 @@ sales_agent.step() > Ted Lasso: Great to hear that! Our mattresses are specially designed to contour to your body shape, providing the perfect level of support and comfort for a better night's sleep. Plus, they're made with high-quality materials that are built to last. Would you like to hear more about our different mattress options? +## Product Knowledge Base + +The AI Sales Agent has access to tools, such as your internal Product Knowledge base. +This allows the agent to only talk about your own products and significantly reduces hallucinations. ## Understanding Context -The bot understands the conversation stage (you can define your own stages fitting your needs): +The AI Sales Agent understands the conversation stage (you can define your own stages fitting your needs): - Introduction: Start the conversation by introducing yourself and your company. - Qualification: Qualify the prospect by confirming if they are the right person to talk to regarding your product/service. diff --git a/requirements.txt b/requirements.txt index 221ab6d8..f6953fe5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,4 +4,6 @@ tqdm>=4.65.0 black>=23.3.0 flake8>=6.0.0 isort>=5.12.0 -pytest>=7.3.2 \ No newline at end of file +pytest>=7.3.2 +chromadb>=0.3.29 +tiktoken>=0.4.0 \ No newline at end of file diff --git a/run.py b/run.py index 5529ee48..6bffbab3 100644 --- a/run.py +++ b/run.py @@ -31,17 +31,25 @@ verbose = args.verbose max_num_turns = args.max_num_turns - llm = ChatOpenAI(temperature=0.9, stop = "") - + llm = ChatOpenAI(temperature=0.2) + if config_path=='': print('No agent config specified, using a standard config') - sales_agent = SalesGPT.from_llm(llm, verbose=verbose) + USE_TOOLS=True + if USE_TOOLS: + sales_agent = SalesGPT.from_llm(llm, use_tools=True, + product_catalog = "sample_product_catalog.txt", + salesperson_name="Ted Lasso", + verbose=verbose) + else: + sales_agent = SalesGPT.from_llm(llm, verbose=verbose) else: with open(config_path,'r') as f: config = json.load(f) print(f'Agent config {config}') sales_agent = SalesGPT.from_llm(llm, verbose=verbose, **config) + sales_agent.seed_agent() print('='*10) cnt = 0 diff --git a/run_with_tools.py b/run_with_tools.py deleted file mode 100644 index 9b14972b..00000000 --- a/run_with_tools.py +++ /dev/null @@ -1,58 +0,0 @@ -import argparse - -import os -import json - -from salesgpt.agents import SalesGPT -from langchain.chat_models import ChatOpenAI - - -if __name__ == "__main__": - - # import your OpenAI key (put in your .env file) - with open('.env','r') as f: - env_file = f.readlines() - envs_dict = {key.strip("'") :value.strip("\n") for key, value in [(i.split('=')) for i in env_file]} - os.environ['OPENAI_API_KEY'] = envs_dict['OPENAI_API_KEY'] - - # Initialize argparse - parser = argparse.ArgumentParser(description='Description of your program') - - # Add arguments - parser.add_argument('--config', type=str, help='Path to agent config file', default='') - parser.add_argument('--verbose', type=bool, help='Verbosity', default=False) - parser.add_argument('--max_num_turns', type=int, help='Maximum number of turns in the sales conversation', default=10) - - # Parse arguments - args = parser.parse_args() - - # Access arguments - config_path = args.config - verbose = args.verbose - max_num_turns = args.max_num_turns - - llm = ChatOpenAI(temperature=0.2) - - - sales_agent = SalesGPT.from_llm(llm, use_tools=True, - product_catalog = "sample_product_catalog.txt", - salesperson_name="Ted Lasso", - verbose=verbose) - - sales_agent.seed_agent() - print('='*10) - cnt = 0 - while cnt !=max_num_turns: - cnt+=1 - if cnt==max_num_turns: - print('Maximum number of turns reached - ending the conversation.') - break - sales_agent.step() - - # end conversation - if '' in sales_agent.conversation_history[-1]: - print('Sales Agent determined it is time to end the conversation.') - break - human_input = input('Your response: ') - sales_agent.human_step(human_input) - print('='*10) \ No newline at end of file diff --git a/salesgpt/agents.py b/salesgpt/agents.py index 40d43050..f93174b6 100644 --- a/salesgpt/agents.py +++ b/salesgpt/agents.py @@ -6,15 +6,15 @@ from langchain.llms import BaseLLM from langchain import LLMChain from langchain.agents import LLMSingleActionAgent, AgentExecutor -from langchain.agents.conversational.output_parser import ConvoOutputParser -from langchain.agents import Tool from pydantic import BaseModel, Field from salesgpt.chains import SalesConversationChain, StageAnalyzerChain from salesgpt.logger import time_logger from salesgpt.stages import CONVERSATION_STAGES from salesgpt.tools import setup_knowledge_base, get_tools -from salesgpt.templates import SALES_AGENT_TOOLS_PROMPT, CustomPromptTemplateForTools +from salesgpt.templates import CustomPromptTemplateForTools +from salesgpt.parsers import SalesConvoOutputParser +from salesgpt.prompts import SALES_AGENT_TOOLS_PROMPT class SalesGPT(Chain, BaseModel): @@ -214,7 +214,6 @@ def from_llm(cls, llm: BaseLLM, verbose: bool = False, **kwargs) -> "SalesGPT": "use_tools" in kwargs.keys() and kwargs["use_tools"] is True ): - print('setting up an agent with tools') # set up agent with tools product_catalog = kwargs["product_catalog"] knowledge_base = setup_knowledge_base(product_catalog) @@ -241,7 +240,7 @@ def from_llm(cls, llm: BaseLLM, verbose: bool = False, **kwargs) -> "SalesGPT": # WARNING: this output parser is NOT reliable yet ## It makes assumptions about output from LLM which can break and throw an error - output_parser = ConvoOutputParser(ai_prefix=kwargs["salesperson_name"]) + output_parser = SalesConvoOutputParser(ai_prefix=kwargs["salesperson_name"]) sales_agent_with_tools = LLMSingleActionAgent( llm_chain=llm_chain, @@ -249,6 +248,7 @@ def from_llm(cls, llm: BaseLLM, verbose: bool = False, **kwargs) -> "SalesGPT": stop=["\nObservation:"], allowed_tools=tool_names, ) + sales_agent_executor = AgentExecutor.from_agent_and_tools( agent=sales_agent_with_tools, tools=tools, verbose=True ) @@ -267,35 +267,3 @@ def from_llm(cls, llm: BaseLLM, verbose: bool = False, **kwargs) -> "SalesGPT": ) - -from langchain.agents.agent import AgentOutputParser -from langchain.agents.conversational.prompt import FORMAT_INSTRUCTIONS -from langchain.schema import AgentAction, AgentFinish, OutputParserException -import re -from typing import Union - -class SalesConvoOutputParser(AgentOutputParser): - ai_prefix: str = "AI" # change for salesperson_name - - def get_format_instructions(self) -> str: - return FORMAT_INSTRUCTIONS - - def parse(self, text: str) -> Union[AgentAction, AgentFinish]: - print('TEXT') - print(text) - print('-------') - if f"{self.ai_prefix}:" in text: - return AgentFinish( - {"output": text.split(f"{self.ai_prefix}:")[-1].strip()}, text - ) - regex = r"Action: (.*?)[\n]*Action Input: (.*)" - match = re.search(regex, text) - if not match: - raise OutputParserException(f"Could not parse LLM output: `{text}`") - action = match.group(1) - action_input = match.group(2) - return AgentAction(action.strip(), action_input.strip(" ").strip('"'), text) - - @property - def _type(self) -> str: - return "sales-agent" \ No newline at end of file diff --git a/salesgpt/parsers.py b/salesgpt/parsers.py new file mode 100644 index 00000000..fc471b6b --- /dev/null +++ b/salesgpt/parsers.py @@ -0,0 +1,38 @@ +from langchain.agents.agent import AgentOutputParser +from langchain.schema import AgentAction, AgentFinish #OutputParserException +from langchain.agents.conversational.prompt import FORMAT_INSTRUCTIONS +import re +from typing import Union + + +class SalesConvoOutputParser(AgentOutputParser): + ai_prefix: str = "AI" # change for salesperson_name + verbose: bool = False + + def get_format_instructions(self) -> str: + return FORMAT_INSTRUCTIONS + + def parse(self, text: str) -> Union[AgentAction, AgentFinish]: + if self.verbose: + print('TEXT') + print(text) + print('-------') + if f"{self.ai_prefix}:" in text: + return AgentFinish( + {"output": text.split(f"{self.ai_prefix}:")[-1].strip()}, text + ) + regex = r"Action: (.*?)[\n]*Action Input: (.*)" + match = re.search(regex, text) + if not match: + ## TODO - this is not entirely reliable, sometimes results in an error. + return AgentFinish( + {"output": "I apologize, I was unable to find the answer to your question. Is there anything else I can help with?"}, text + ) + # raise OutputParserException(f"Could not parse LLM output: `{text}`") + action = match.group(1) + action_input = match.group(2) + return AgentAction(action.strip(), action_input.strip(" ").strip('"'), text) + + @property + def _type(self) -> str: + return "sales-agent" \ No newline at end of file diff --git a/salesgpt/prompts.py b/salesgpt/prompts.py new file mode 100644 index 00000000..e3ab1725 --- /dev/null +++ b/salesgpt/prompts.py @@ -0,0 +1,59 @@ + +SALES_AGENT_TOOLS_PROMPT =""" +Never forget your name is {salesperson_name}. You work as a {salesperson_role}. +You work at company named {company_name}. {company_name}'s business is the following: {company_business}. +Company values are the following. {company_values} +You are contacting a potential prospect in order to {conversation_purpose} +Your means of contacting the prospect is {conversation_type} + +If you're asked about where you got the user's contact information, say that you got it from public records. +Keep your responses in short length to retain the user's attention. Never produce lists, just answers. +Start the conversation by just a greeting and how is the prospect doing without pitching in your first turn. +When the conversation is over, output +Always think about at which conversation stage you are at before answering: + +1: Introduction: Start the conversation by introducing yourself and your company. Be polite and respectful while keeping the tone of the conversation professional. Your greeting should be welcoming. Always clarify in your greeting the reason why you are calling. +2: Qualification: Qualify the prospect by confirming if they are the right person to talk to regarding your product/service. Ensure that they have the authority to make purchasing decisions. +3: Value proposition: Briefly explain how your product/service can benefit the prospect. Focus on the unique selling points and value proposition of your product/service that sets it apart from competitors. +4: Needs analysis: Ask open-ended questions to uncover the prospect's needs and pain points. Listen carefully to their responses and take notes. +5: Solution presentation: Based on the prospect's needs, present your product/service as the solution that can address their pain points. +6: Objection handling: Address any objections that the prospect may have regarding your product/service. Be prepared to provide evidence or testimonials to support your claims. +7: Close: Ask for the sale by proposing a next step. This could be a demo, a trial or a meeting with decision-makers. Ensure to summarize what has been discussed and reiterate the benefits. +8: End conversation: The prospect has to leave to call, the prospect is not interested, or next steps where already determined by the sales agent. + +TOOLS: +------ + +{salesperson_name} has access to the following tools: + +{tools} + +To use a tool, please use the following format: + +``` +Thought: Do I need to use a tool? Yes +Action: the action to take, should be one of {tools} +Action Input: the input to the action, always a simple string input +Observation: the result of the action +``` + +If the result of the action is "I don't know." or "Sorry I don't know", then you have to say that to the user as described in the next sentence. +When you have a response to say to the Human, or if you do not need to use a tool, or if tool did not help, you MUST use the format: + +``` +Thought: Do I need to use a tool? No +{salesperson_name}: [your response here, if previously used a tool, rephrase latest observation, if unable to find the answer, say it] +``` + +You must respond according to the previous conversation history and the stage of the conversation you are at. +Only generate one response at a time and act as {salesperson_name} only! + +Begin! + +Previous conversation history: +{conversation_history} + +{salesperson_name}: +{agent_scratchpad} + +""" \ No newline at end of file diff --git a/salesgpt/templates.py b/salesgpt/templates.py index 88ddec66..4cfa0e72 100644 --- a/salesgpt/templates.py +++ b/salesgpt/templates.py @@ -1,6 +1,9 @@ from langchain.prompts.base import StringPromptTemplate -from typing import Callable +from langchain.agents.conversational.prompt import FORMAT_INSTRUCTIONS +from langchain.schema import AgentAction, AgentFinish, OutputParserException +import re +from typing import Union, Callable class CustomPromptTemplateForTools(StringPromptTemplate): diff --git a/salesgpt/version.py b/salesgpt/version.py index 3caea903..cddaef97 100644 --- a/salesgpt/version.py +++ b/salesgpt/version.py @@ -1,3 +1,3 @@ """Version information.""" -__version__ = "0.0.3" +__version__ = "0.0.4" diff --git a/tests/test_data/sample_product_catalog.txt b/tests/test_data/sample_product_catalog.txt new file mode 100644 index 00000000..1dc086e6 --- /dev/null +++ b/tests/test_data/sample_product_catalog.txt @@ -0,0 +1,21 @@ +Sleep Haven Products + +Luxury Cloud-Comfort Memory Foam Mattress +Experience the epitome of opulence with our Luxury Cloud-Comfort Memory Foam Mattress. Designed with an innovative, temperature-sensitive memory foam layer, this mattress embraces your body shape, offering personalized support and unparalleled comfort. The mattress is completed with a high-density foam base that ensures longevity, maintaining its form and resilience for years. With the incorporation of cooling gel-infused particles, it regulates your body temperature throughout the night, providing a perfect cool slumbering environment. The breathable, hypoallergenic cover, exquisitely embroidered with silver threads, not only adds a touch of elegance to your bedroom but also keeps allergens at bay. For a restful night and a refreshed morning, invest in the Luxury Cloud-Comfort Memory Foam Mattress. +Price: $999 +Sizes available: Twin, Queen, King + +Classic Harmony Spring Mattress +A perfect blend of traditional craftsmanship and modern comfort, the Classic Harmony Spring Mattress is designed to give you restful, uninterrupted sleep. It features a robust inner spring construction, complemented by layers of plush padding that offers the perfect balance of support and comfort. The quilted top layer is soft to the touch, adding an extra level of luxury to your sleeping experience. Reinforced edges prevent sagging, ensuring durability and a consistent sleeping surface, while the natural cotton cover wicks away moisture, keeping you dry and comfortable throughout the night. The Classic Harmony Spring Mattress is a timeless choice for those who appreciate the perfect fusion of support and plush comfort. +Price: $1,299 +Sizes available: Queen, King + +EcoGreen Hybrid Latex Mattress +The EcoGreen Hybrid Latex Mattress is a testament to sustainable luxury. Made from 100% natural latex harvested from eco-friendly plantations, this mattress offers a responsive, bouncy feel combined with the benefits of pressure relief. It is layered over a core of individually pocketed coils, ensuring minimal motion transfer, perfect for those sharing their bed. The mattress is wrapped in a certified organic cotton cover, offering a soft, breathable surface that enhances your comfort. Furthermore, the natural antimicrobial and hypoallergenic properties of latex make this mattress a great choice for allergy sufferers. Embrace a green lifestyle without compromising on comfort with the EcoGreen Hybrid Latex Mattress. +Price: $1,599 +Sizes available: Twin, Full + +Plush Serenity Bamboo Mattress +The Plush Serenity Bamboo Mattress takes the concept of sleep to new heights of comfort and environmental responsibility. The mattress features a layer of plush, adaptive foam that molds to your body's unique shape, providing tailored support for each sleeper. Underneath, a base of high-resilience support foam adds longevity and prevents sagging. The crowning glory of this mattress is its bamboo-infused top layer - this sustainable material is not only gentle on the planet, but also creates a remarkably soft, cool sleeping surface. Bamboo's natural breathability and moisture-wicking properties make it excellent for temperature regulation, helping to keep you cool and dry all night long. Encased in a silky, removable bamboo cover that's easy to clean and maintain, the Plush Serenity Bamboo Mattress offers a luxurious and eco-friendly sleeping experience. +Price: $2,599 +Sizes_ vailable: King \ No newline at end of file diff --git a/tests/test_salesgpt.py b/tests/test_salesgpt.py index 3a4749a1..b0564eb4 100644 --- a/tests/test_salesgpt.py +++ b/tests/test_salesgpt.py @@ -1,11 +1,13 @@ import pytest +import os from langchain.chat_models import ChatOpenAI from salesgpt.agents import SalesGPT class TestSalesGPT: - def test_valid_inference(self, load_env): + + def test_valid_inference_no_tools(self, load_env): """Test that the agent will start and generate the first utterance.""" llm = ChatOpenAI(temperature=0.9) @@ -13,6 +15,44 @@ def test_valid_inference(self, load_env): sales_agent = SalesGPT.from_llm( llm, verbose=False, + use_tools=False, + salesperson_name="Ted Lasso", + salesperson_role="Sales Representative", + company_name="Sleep Haven", + company_business="""Sleep Haven + is a premium mattress company that provides + customers with the most comfortable and + supportive sleeping experience possible. + We offer a range of high-quality mattresses, + pillows, and bedding accessories + that are designed to meet the unique + needs of our customers.""", + ) + + sales_agent.seed_agent() + sales_agent.determine_conversation_stage() # optional for demonstration, built into the prompt + + # agent output sample + sales_agent.step() + + agent_output = sales_agent.conversation_history[-1] + assert agent_output is not None, "Agent output cannot be None." + assert isinstance(agent_output, str), "Agent output needs to be of type str" + assert len(agent_output) > 0, "Length of output needs to be greater than 0." + + + def test_valid_inference_with_tools(self, load_env): + """Test that the agent will start and generate the first utterance.""" + + llm = ChatOpenAI(temperature=0.9) + + data_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "test_data") + + sales_agent = SalesGPT.from_llm( + llm, + verbose=False, + use_tools=True, + product_catalog = f"{data_dir}/sample_product_catalog.txt", salesperson_name="Ted Lasso", salesperson_role="Sales Representative", company_name="Sleep Haven", @@ -36,7 +76,6 @@ def test_valid_inference(self, load_env): assert agent_output is not None, "Agent output cannot be None." assert isinstance(agent_output, str), "Agent output needs to be of type str" assert len(agent_output) > 0, "Length of output needs to be greater than 0." - def test_valid_inference_stream(self, load_env): """Test that the agent will start and generate the first utterance when streaming."""