import os import openai import gradio as gr from langchain.chat_models import ChatOpenAI from langchain.prompts import ChatPromptTemplate from langchain.prompts import ChatPromptTemplate from langchain.output_parsers import ResponseSchema from langchain.output_parsers import StructuredOutputParser os.environ['OPENAI_API_KEY'] = '' openai.api_key = os.environ['OPENAI_API_KEY'] # To control the randomness and creativity of the generated # text by an LLM, use temperature = 0.0 chat = ChatOpenAI(temperature=0.0) chat def get_format_instructions(): gift_schema = ResponseSchema(name="gift", description="Was the item purchased\ as a gift for someone else? \ Answer True if yes,\ False if not or unknown.") delivery_days_schema = ResponseSchema(name="delivery_days", description="How many days\ did it take for the product\ to arrive? If this \ information is not found,\ output -1.") price_value_schema = ResponseSchema(name="price_value", description="Extract any\ sentences about the value or \ price, and output them as a \ comma separated Python list.") response_schemas = [gift_schema, delivery_days_schema, price_value_schema] output_parser = StructuredOutputParser.from_response_schemas(response_schemas) format_instructions = output_parser.get_format_instructions() return format_instructions # To retrieve ChatGPT response in the reqiuired style def response(template_string, user_input, translate_style_parsing_inst): prompt_template = ChatPromptTemplate.from_template(template_string) customer_messages = prompt_template.format_messages( style=translate_style_parsing_inst, text=user_input) # Call the LLM to translate the style or parse the customer message customer_response = chat(customer_messages) return customer_response.content, prompt_template.messages[0].prompt.input_variables # To parse the ChatGPT response into a python dictionary def parser_response(template_string_parse, user_input_parse): format_instructions = get_format_instructions() prompt = ChatPromptTemplate.from_template(template=template_string_parse) messages = prompt.format_messages(text=user_input_parse, format_instructions=format_instructions) response = chat(messages) output_dict = output_parser.parse(response.content) return output_dict, type(output_dict) demo = gr.Blocks() title = """