|
import os |
|
|
|
import streamlit as st |
|
from openai import OpenAI |
|
|
|
|
|
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"]) |
|
|
|
|
|
|
|
def run_prompt_through_openai(system_prompt, user_prompt, model='gpt-3.5-turbo'): |
|
|
|
chat_completion = client.chat.completions.create( |
|
model=model, |
|
messages=[ |
|
{'role': 'system', 'content': system_prompt}, |
|
{'role': 'user', 'content': user_prompt} |
|
], |
|
temperature=0.9, |
|
top_p=1, |
|
) |
|
|
|
return chat_completion.choices[0].message.content |
|
|
|
|
|
|
|
st.title("Single Input Example") |
|
st.write("This is an example for Sinan Ozdemir's [ChatGPT and LLMs in 3 weeks course](https://www.oreilly.com/live-events/large-language-models-and-chatgpt-in-3-weeks/0636920090988/0636920095656/)") |
|
|
|
|
|
system_prompt = "You are a Twitter bot that helps people with their tweets" |
|
|
|
|
|
user_input = st.text_input("Description of a tweet you want", value="I need a tweet about GPT-4") |
|
|
|
user_prompt = '''Input: I need a tweet about GPT-4 |
|
Tweet: "Wow! I just read about GPT-4 and it's amazing! I can't wait to see what it can do! #gpt4 #ai #machinelearning" |
|
Input: Dogs in the summer and avoiding fleas and ticks |
|
Tweet: "I love my dog, but I hate the fleas and ticks that come with him. I'm going to try to avoid them this summer." |
|
Input: San Francisco's Golden Gate Bridge |
|
Tweet: "I love the Golden Gate Bridge. It's a beautiful sight to see. I can't wait to go back to San Francisco." |
|
Input: {user_input} |
|
Tweet:''' |
|
|
|
|
|
if st.button("Run"): |
|
user_prompt = user_prompt.format(user_input=user_input) |
|
|
|
response = run_prompt_through_openai(system_prompt, user_prompt) |
|
|
|
|
|
st.markdown(f"```\n# System Prompt\n---\n{system_prompt}\n```") |
|
st.markdown(f"```\n# User Prompt\n---\n{user_prompt}\n```") |
|
st.markdown(f"```\n# AI Response\n---\n{response}\n```") |
|
|
|
|