import streamlit as st from utils import * if True: st.markdown(""" ## With ChatGPT #### 1. Set up OpenAI config in your notebook with code snippet below: ```python import openai ## you need to install this with "pip install openai" import os from dotenv import load_dotenv load_dotenv() # read local .env file openai.api_key = os.getenv('') # this assign the key to a variable named "openai.api_key" ``` - you might want to check on your own notebook if you did correctly by "print(openai.api_key)" #### 2. Define a python function to make our life easier later on ```python def get_completion(prompt, model="gpt-3.5-turbo"): messages = [{"role": "user", "content": prompt}] response = openai.ChatCompletion.create(model=model, messages=messages, temperature=0, # this is the degree of randomness of the model's output ) content = response.choices[0].message["content"] # This is the real reply from the ChatGPT ## This is the data we can grab from the API, how many tokens we have used in this conversation token_dict = { 'prompt_tokens':response['usage']['prompt_tokens'], 'completion_tokens':response['usage']['completion_tokens'], 'total_tokens':response['usage']['total_tokens'], } ## This is another important data we will use later on moderation_output = openai.Moderation.create(input=prompt)["results"][0] return content, token_dict, moderation_output ``` - The above function have two input, - prompt is a string input that will pass to ChatGPT - model is another input for us to sellet which ChatGPT model we wish to talk on - The function return three peaces of information, AI's reply, the usage info, and the moderation. - we only focus on the 'content' this week, - we will use the other two in the coming weeks #### 3. Our example text to summarise, a product review in this case """) st.code( ''' prod_review = """ Got this panda plush toy for my daughter's birthday, who loves it and takes it everywhere. \n It's soft and super cute, and its face has a friendly look. It's a bit small for what I paid though. \n I think there might be other options that are bigger for the same price. \n It arrived a day earlier than expected, so I got to play with it myself before I gave it to her. """ ''' , language = 'python') st.write("##") st.markdown(""" #### 4. Now, we add our system requirement to the bot, asking it to follow our instruction specifically """) st.code(''' numberOfWords = '20' # Here we specify the max number of words we want it to reply topics = "weather" # Here we want it to focus on specific topic ## Here is our system instruction, check how we pass our specific requirements to the system one prompt = f""" Your task is to generate a short summary of a given text \n Summarize the text below, delimited by triple backticks, \n in at most {numberOfWords} words, \n and focusing on any aspects that mention {topics}. Review: ```{prod_review}``` """ ''', language = 'python') st.markdown(""" Now if you run code below, you will get the response, if everything were correct so far... ```python response, _, _ = get_completion(prompt) # we use '_' to bypass other outputs print(response) ``` """) st.markdown(""" #### 5. More specific prompt """) st.code(''' numberOfWords = '20' topics = "marry" prompt = f""" Your task is to generate a short summary from text below, delimited by triple backticks, in at most {numberOfWords} words.\n Firstly, extract relevant information and create a list of keywords without response,\n Then, check if {topics} is in your list, if not, just response no relevent topics about {topics} to summarise,\ if it is in your list, focusing on any aspects that mention {topics}, \n Review: ```{prod_review}``` """ ''', language = 'python') st.markdown(""" - The prompt above is a template for us to build our first app next week. - We hope to create an app that can only pass the user input into those pre-defined variables - Therefore, user only need very easy interface to get the desired output. """) st.markdown(""" ## With Hugging Chat """) st.code(''' # we need to install the hugchat library fistly ## in your notebook, copy and execute the following code !pip install hugchat ''') st.markdown(" - Now we can call the Hugging chat API and let it do the same job as ChatGPT") st.code(''' from hugchat.login import Login from hugchat import hugchat sign = Login(email, passwd) ## You need to here are your sign up email and password for Hugging Face ## Just copy these two lines and execute it, don't worry about it too much cookies = sign.login() chatbot = hugchat.ChatBot(cookies=cookies.get_dict()) ## get the response from the Hugging Chat res = chatbot.chat(prompt) print(res) ''', language = 'python') st.markdown(""" - As you might see, Hugging chat can do the job of summarization, \n - But in terms of satisfy specific requirement from us, it has limitation compare to ChatGPT, \n - ChatGPT is more advanced and it can follow human instruction clearly.\n - The advantage of Hugging Chat is that it is free, so you might do as many experiment as you wish, \n without cost you money. - In our app, hugging chat would be a good alternative to ChatGPT for some case. """)