from langchain.llms import OpenAI import os import streamlit as st from langchain.chat_models import ChatOpenAI from langchain.prompts.chat import ChatPromptTemplate from langchain.schema import BaseOutputParser chat_llm = ChatOpenAI(temperature=0.5, model='gpt-3.5-turbo') class CommaseperatedOutput(BaseOutputParser): def parse(self, text:str): return text # function to call openai api and get response def get_response(question): template = "You are a naughty assistant who is great at flirting. You have to make a cheesy pickup line by using words given by user." human_template = "{text}" chatprompt = ChatPromptTemplate.from_messages([ ('system', template), ('human', human_template)]) chain = chatprompt|chat_llm|CommaseperatedOutput() try: response = chain.invoke({"text":question}) except: response = "I am running out of money right now. Come back after a decade. I am hoping to be a rich by then." return response # create streamlit app st.set_page_config(page_title="Langchain Application") st.header("Your Flirting Guardian") input = st.text_input("You give me words and I will give you a pickup line: ", key="input") response = get_response(input) submit = st.button("Get a pickup line") if submit: st.subheader("Here is your pickup line") st.write(response)