Spaces:
Configuration error
Configuration error
Upload PAL.py
Browse filesAdd a way to select the model.
PAL.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from langchain.llms import OpenAI
|
3 |
+
from langchain.chains import PALChain
|
4 |
+
from langchain.chains.llm import LLMChain
|
5 |
+
|
6 |
+
# set page config
|
7 |
+
# Add title and description
|
8 |
+
st.set_page_config(page_title="๐ฆ๐ Program Aided Language model")
|
9 |
+
st.title("๐ฆ๐๐งฎ Program Aided Language Model: Helps with math problems")
|
10 |
+
st.markdown(
|
11 |
+
"""This example was adapted from Data Professor Github [repo](https://github.com/dataprofessor/langchain-quickstart/blob/master/streamlit_app.py)"""
|
12 |
+
)
|
13 |
+
st.markdown(
|
14 |
+
"""Paper: [Program-Aided Language Models for Program Synthesis](https://arxiv.org/pdf/2211.10435.pdf)"""
|
15 |
+
)
|
16 |
+
st.markdown(
|
17 |
+
"""Credit: [Sam Witteven](https://www.youtube.com/playlist?list=PL8motc6AQftk1Bs42EW45kwYbyJ4jOdiZ)"""
|
18 |
+
)
|
19 |
+
|
20 |
+
# sidebar for OpenAI API key & model selection
|
21 |
+
openai_api_key = st.sidebar.text_input("Enter your OpenAI API key", type="password")
|
22 |
+
select_instruct_model = st.sidebar.selectbox(
|
23 |
+
"Select Instruction Model",
|
24 |
+
("text-davinci-003", "gpt-3.5-turbo-instruct"),
|
25 |
+
)
|
26 |
+
|
27 |
+
|
28 |
+
def generate_response(input_text):
|
29 |
+
"""
|
30 |
+
Generates response to input text using PALChain
|
31 |
+
|
32 |
+
Parameters
|
33 |
+
----------
|
34 |
+
input_text : str
|
35 |
+
Input text to generate response for using PALChain
|
36 |
+
|
37 |
+
Returns
|
38 |
+
-------
|
39 |
+
None
|
40 |
+
|
41 |
+
Example
|
42 |
+
-------
|
43 |
+
>>> generate_response("Leah had 32 chocolates and her sister had 42. If they ate 35, how many pieces do they have left in total?")
|
44 |
+
"""
|
45 |
+
llm = OpenAI(
|
46 |
+
temperature=0,
|
47 |
+
openai_api_key=openai_api_key,
|
48 |
+
max_tokens=512,
|
49 |
+
model=select_instruct_model,
|
50 |
+
)
|
51 |
+
pal_chain = PALChain.from_math_prompt(llm, verbose=True)
|
52 |
+
st.markdown(pal_chain.run(input_text))
|
53 |
+
|
54 |
+
|
55 |
+
# Start a new form named "my_form"
|
56 |
+
with st.form("my_form"):
|
57 |
+
# Create a text area for the user to input a math question. The text area is pre-filled with a default question.
|
58 |
+
text = st.text_area(
|
59 |
+
"Enter math question:",
|
60 |
+
"Leah had 32 chocolates and her sister had 42. If they ate 35, how many pieces do they have left in total?",
|
61 |
+
)
|
62 |
+
# Create a submit button for the form. When the button is clicked, the form is submitted and the page reruns from the top.
|
63 |
+
submitted = st.form_submit_button("Submit")
|
64 |
+
# Check if the OpenAI API key is not valid (it should start with "sk-"). If it's not valid, display a warning message.
|
65 |
+
if not openai_api_key.startswith("sk-"):
|
66 |
+
st.warning("Please enter your OpenAI API key!", icon="โ ")
|
67 |
+
# If the form is submitted and the OpenAI API key is valid, generate a response to the user's question.
|
68 |
+
if submitted and openai_api_key.startswith("sk-"):
|
69 |
+
generate_response(text)
|