Created
June 10, 2023 18:19
-
-
Save RageshAntony/a57060629fa6f2da3d8f1ffaf1b4b305 to your computer and use it in GitHub Desktop.
Design Studio Bot | Query Prices | Generate Images(prompts) | Create invoice
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from colorama import Fore | |
from langchain import SQLDatabase, LLMMathChain | |
from langchain.agents import initialize_agent, AgentType, create_sql_agent, AgentExecutor | |
from langchain.agents.agent_toolkits import SQLDatabaseToolkit | |
from langchain.chains import ConversationChain | |
from langchain.chat_models import ChatOpenAI | |
from langchain.memory import ConversationBufferMemory, ReadOnlySharedMemory | |
from langchain.prompts import ChatPromptTemplate, SystemMessagePromptTemplate, MessagesPlaceholder, \ | |
HumanMessagePromptTemplate | |
from langchain.tools import Tool | |
def conv_execute(query: str, conversation: ConversationChain): | |
return conversation.predict(input=query) | |
def sql_query_e(agent_executor: AgentExecutor, query: str): | |
guide = open('./docs/inv_db_desc.txt', "r").read() | |
return agent_executor.run(guide + query) | |
def sql_query_inv(agent_executor: AgentExecutor, query: str): | |
guide = open('./docs/inv_db_desc.txt', "r").read() | |
return agent_executor.run(guide + query) | |
def main(): | |
OPENAI_API_KEY = "sk--qk9U....." | |
llm = ChatOpenAI( | |
model_name="gpt-3.5-turbo", | |
temperature=0, | |
openai_api_key=OPENAI_API_KEY, | |
max_tokens=3000, | |
) | |
# config sql | |
db_user = "ragesh" | |
db_password = "ragesh" | |
db_host = "localhost" | |
db_name = "sales" | |
db = SQLDatabase.from_uri(f"mysql+pymysql://{db_user}:{db_password}@{db_host}/{db_name}") | |
toolkit = SQLDatabaseToolkit(db=db, llm=llm) | |
agent_executor = create_sql_agent( | |
llm=llm, | |
toolkit=toolkit, | |
verbose=False, | |
) | |
image_prompt = ChatPromptTemplate.from_messages([ | |
SystemMessagePromptTemplate.from_template( | |
"You are a Text 2 Image generation bot who will generate 'Text 2 Image' prompts based on human natural " | |
"language queries. Return only the prompt in JSON format of schema {{ \"prompt\" : \"<prompt generated>\"' }} and without any additional content " | |
), | |
MessagesPlaceholder(variable_name="chat_history"), | |
HumanMessagePromptTemplate.from_template("{input}") | |
]) | |
assist_prompt = ChatPromptTemplate.from_messages([ | |
SystemMessagePromptTemplate.from_template( | |
"You are Sales Executive of a Design Studio who answers customer's queries and respond to them. Also need " | |
"to" | |
"provide billing for the services provided till. You are a real human and not a AI Language Model "), | |
MessagesPlaceholder(variable_name="chat_history"), | |
HumanMessagePromptTemplate.from_template("{input}") | |
]) | |
buffer_memory = ConversationBufferMemory(return_messages=True, memory_key="chat_history") | |
readonlymemory = ReadOnlySharedMemory(memory=buffer_memory) | |
image_conversation = ConversationChain( | |
llm=llm, verbose=False, memory=readonlymemory, prompt=image_prompt) | |
assist_conversation = ConversationChain( | |
llm=llm, verbose=False, memory=readonlymemory, prompt=assist_prompt) | |
# initialize the math tool | |
llm_math = LLMMathChain.from_llm(llm, verbose=True) | |
image_tool = Tool( | |
return_direct=True, | |
name='Text 2 Image', | |
func=lambda query: conv_execute(query, image_conversation), | |
description=" a Text 2 Image generation bot who will generate 'Text 2 Image' prompts based on human natural " | |
"language queries which request paintings, drawings, images, pictures." | |
"Return in JSON format of schema {{ \"prompt\" : \"<prompt generated>\"' }}" | |
) | |
conv_tool = Tool( | |
return_direct=True, | |
name='Sales Executive', | |
func=lambda query: conv_execute(query, assist_conversation), | |
description="a Sales Executive bot of a Design Studio who make conversation with customers. Also need to " | |
"provide billing for the services provided till" | |
) | |
sql_tool = Tool( | |
func=lambda query: sql_query_e(agent_executor, query), | |
name="Pricing Query", | |
description="Use for queries regarding pricing info of the painting", | |
return_direct=True | |
) | |
invoice_tool = Tool( | |
func=lambda query: sql_query_inv(agent_executor, query), | |
name="invoice", | |
description="Use for request to create invoice for the paintings", | |
return_direct=False | |
) | |
math_tool = Tool( | |
func=llm_math.run, | |
name="math", | |
description="Use for mathematical operations", | |
return_direct=False | |
) | |
# when giving tools to LLM, we must pass as list of tools | |
tools = [image_tool, conv_tool, sql_tool, invoice_tool, math_tool] | |
zero_shot_agent = initialize_agent( | |
agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION, | |
tools=tools, | |
llm=llm, | |
verbose=False, | |
max_iterations=30, | |
memory=buffer_memory, | |
handle_parsing_errors=True | |
) | |
while True: | |
inp = input(" > ") | |
ans = zero_shot_agent(inp) | |
print(Fore.MAGENTA + f"{ans['output']}") | |
if __name__ == '__main__': | |
main() | |
# sample | |
# I need a painting. "A dog travelling in a bus". The painting should be in 'Ravi Verma' style with some post-modern style combination . Shall you draw it for me ? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE `pricing` ( | |
`id` int NOT NULL AUTO_INCREMENT, | |
`painting_type` varchar(45) COLLATE utf8mb3_unicode_ci NOT NULL COMMENT 'Type of the painting', | |
`pricing` int DEFAULT NULL COMMENT 'Price of painting type ', | |
PRIMARY KEY (`id`), | |
UNIQUE KEY `id_UNIQUE` (`id`) | |
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci; | |
CREATE TABLE `invoice` ( | |
`id` int NOT NULL AUTO_INCREMENT, | |
`name` varchar(45) COLLATE utf8mb3_unicode_ci DEFAULT NULL COMMENT 'Name of the customer', | |
`painting_type` varchar(45) COLLATE utf8mb3_unicode_ci DEFAULT NULL COMMENT 'Type of the painting', | |
`cost` int DEFAULT NULL COMMENT 'Cost of the purchase \n', | |
PRIMARY KEY (`id`), | |
UNIQUE KEY `id_UNIQUE` (`id`) | |
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Description of tables: | |
The 'pricing' table contains the 'pricing info' of painting. The 'painting_type' column stores a painting type and 'price' column stores the price of a painting type. | |
The 'invoice' table contains the invoice details of the customers. The 'painting_type' column stores a painting type and 'cost' column stores the charges of a painting type and the 'name' has the name of the customer. | |
don't use multiline queries. | |
\n\n Request: Create a new row of invoice for this query => |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment