|
| 1 | +import streamlit as st |
| 2 | +from streamlit_chat import message |
| 3 | +import streamlit.components.v1 as components |
| 4 | +from dotenv import dotenv_values |
| 5 | +import os |
| 6 | +from langchain.chat_models import ChatOpenAI |
| 7 | +from langchain.llms import OpenAI |
| 8 | +from langchain.chains import ConversationChain, LLMChain |
| 9 | +from langchain.memory import ConversationBufferWindowMemory |
| 10 | +from langchain.prompts.chat import ( |
| 11 | + ChatPromptTemplate, |
| 12 | + SystemMessagePromptTemplate, |
| 13 | + AIMessagePromptTemplate, |
| 14 | + HumanMessagePromptTemplate, |
| 15 | +) |
| 16 | + |
| 17 | +api_keys=dotenv_values() |
| 18 | +os.environ['OPENAI_API_KEY'] = dotenv_values()['openai_api_key'] #set environment variable |
| 19 | + |
| 20 | + |
| 21 | +## generate html/css |
| 22 | +def generate_code(human_input): |
| 23 | + # I have no idea if the Jon Carmack thing makes for better code. YMMV. |
| 24 | + # See https://python.langchain.com/en/latest/modules/models/chat/getting_started.html for chat info |
| 25 | + system_prompt_template = """You are expert coder Jon Carmack. Use the provided design context to create idomatic HTML/CSS code as possible based on the user request. |
| 26 | + Everything must be inline in one file and your response must be directly renderable by the browser.""" |
| 27 | + |
| 28 | + human_prompt_template = "Code the {text}. Ensure it's mobile responsive" |
| 29 | + system_message_prompt = SystemMessagePromptTemplate.from_template(system_prompt_template) |
| 30 | + human_message_prompt = HumanMessagePromptTemplate.from_template(human_prompt_template) |
| 31 | + # delete the gpt-4 model_name to use the default gpt-3.5 turbo for faster results |
| 32 | + gpt_4 = ChatOpenAI(temperature=0, model_name='gpt-4') |
| 33 | + conversation = [system_message_prompt, human_message_prompt] |
| 34 | + chat_prompt = ChatPromptTemplate.from_messages(conversation) |
| 35 | + response = gpt_4(chat_prompt.format_prompt( |
| 36 | + text=human_input).to_messages()) |
| 37 | + return response |
| 38 | + |
| 39 | + |
| 40 | +st.set_page_config( |
| 41 | + page_title="Langchain website builder", |
| 42 | + page_icon=":robot:" |
| 43 | +) |
| 44 | + |
| 45 | +st.header("Langchain website builder") |
| 46 | + |
| 47 | +if 'ai' not in st.session_state: |
| 48 | + st.session_state['ai'] = [] |
| 49 | +if 'human' not in st.session_state: |
| 50 | + st.session_state['human'] = [] |
| 51 | + |
| 52 | +def get_text(): |
| 53 | + input_text = st.text_area('', key='input') |
| 54 | + return input_text |
| 55 | + |
| 56 | + |
| 57 | +user_input=get_text() |
| 58 | +if user_input: |
| 59 | + output = generate_code(user_input).content |
| 60 | + |
| 61 | + st.session_state['human'].append(user_input) |
| 62 | + st.session_state['ai'].append(output) |
| 63 | + components.html(output, height=600, scrolling=True) |
| 64 | + |
| 65 | +if st.session_state['ai']: |
| 66 | + for i in range(len(st.session_state['ai']) -1, -1, -1): |
| 67 | + message(st.session_state['ai'][i], key=str(i)) |
| 68 | + message(st.session_state['human'][i], is_user=True, key=str(i) + '_user') |
| 69 | + |
| 70 | + |
0 commit comments