|
| 1 | +--- |
| 2 | +title: LLM quickstart |
| 3 | +slug: /knowledge-base/tutorials/llm-quickstart |
| 4 | +--- |
| 5 | + |
| 6 | +# LLM quickstart |
| 7 | +## OpenAI, LangChain, and Streamlit in 18 lines of code |
| 8 | + |
| 9 | +In this tutorial, you will build a Streamlit LLM app that can generate text from a user-provided prompt. This Python app will use the LangChain framework and Streamlit. Optionally, you can deploy your app to [Streamlit Community Cloud](https://streamlit.io/cloud) when you're done. |
| 10 | + |
| 11 | +*This tutorial is adapted from a blog post by Chanin Nantesanamat: [LangChain tutorial #1: Build an LLM-powered app in 18 lines of code](https://blog.streamlit.io/langchain-tutorial-1-build-an-llm-powered-app-in-18-lines-of-code/).* |
| 12 | + |
| 13 | +<Cloud src="https://doc-tutorial-llm-18-lines-of-code.streamlit.app/?embed=true" height="600" /> |
| 14 | + |
| 15 | +## Objectives |
| 16 | + |
| 17 | +1. Get an OpenAI key from the end user. |
| 18 | +2. Validate the user's OpenAI key. |
| 19 | +3. Get a text prompt from the user. |
| 20 | +4. Authenticate OpenAI with the user's key. |
| 21 | +5. Send the user's prompt to OpenAI's API. |
| 22 | +6. Get a response and display it. |
| 23 | + |
| 24 | +Bonus: Deploy the app on Streamlit Community Cloud! |
| 25 | + |
| 26 | +## Prerequisites |
| 27 | + |
| 28 | +- Python 3.8+ |
| 29 | +- Streamlit |
| 30 | +- LangChain |
| 31 | +- [OpenAI API key](https://platform.openai.com/account/api-keys?ref=blog.streamlit.io) |
| 32 | + |
| 33 | +## Setup coding environment |
| 34 | + |
| 35 | +In your IDE (integrated coding environment), open the terminal and install the following three Python libraries: |
| 36 | + |
| 37 | +```python |
| 38 | +pip install streamlit openai langchain |
| 39 | +``` |
| 40 | + |
| 41 | +Create a `requirements.txt` file located in the root of your working directory and save these dependencies. This is necessary for deploying the app to the Streamlit Community Cloud later. |
| 42 | + |
| 43 | +```python |
| 44 | +streamlit |
| 45 | +openai |
| 46 | +langchain |
| 47 | +``` |
| 48 | + |
| 49 | +## Building the app |
| 50 | + |
| 51 | +The app is only 18 lines of code: |
| 52 | + |
| 53 | +```python |
| 54 | +import streamlit as st |
| 55 | +from langchain.llms import OpenAI |
| 56 | + |
| 57 | +st.title('🦜🔗 Quickstart App') |
| 58 | + |
| 59 | +openai_api_key = st.sidebar.text_input('OpenAI API Key', type='password') |
| 60 | + |
| 61 | +def generate_response(input_text): |
| 62 | + llm = OpenAI(temperature=0.7, openai_api_key=openai_api_key) |
| 63 | + st.info(llm(input_text)) |
| 64 | + |
| 65 | +with st.form('my_form'): |
| 66 | + text = st.text_area('Enter text:', 'What are the three key pieces of advice for learning how to code?') |
| 67 | + submitted = st.form_submit_button('Submit') |
| 68 | + if not openai_api_key.startswith('sk-'): |
| 69 | + st.warning('Please enter your OpenAI API key!', icon='⚠') |
| 70 | + if submitted and openai_api_key.startswith('sk-'): |
| 71 | + generate_response(text) |
| 72 | +``` |
| 73 | + |
| 74 | +To start, create a new Python file and save it as `streamlit_app.py` in the root of your working directory. |
| 75 | + |
| 76 | +1. Import the necessary Python libraries. |
| 77 | + |
| 78 | + ```python |
| 79 | + import streamlit as st |
| 80 | + from langchain.llms import OpenAI |
| 81 | + ``` |
| 82 | + |
| 83 | +2. Create the app's title using `st.title`. |
| 84 | + |
| 85 | + ```python |
| 86 | + st.title('🦜🔗 Quickstart App') |
| 87 | + ``` |
| 88 | + |
| 89 | +3. Add a text input box for the user to enter their OpenAI API key. |
| 90 | + |
| 91 | + ```python |
| 92 | + openai_api_key = st.sidebar.text_input('OpenAI API Key', type='password') |
| 93 | + ``` |
| 94 | + |
| 95 | +4. Define a function to authenticate to OpenAI API with the user's key, send a prompt, and get an AI-generated response. This function accepts the user's prompt as an argument and displays the AI-generated response in a blue box using `st.info`. |
| 96 | + |
| 97 | + ```python |
| 98 | + def generate_response(input_text): |
| 99 | + llm = OpenAI(temperature=0.7, openai_api_key=openai_api_key) |
| 100 | + st.info(llm(input_text)) |
| 101 | + ``` |
| 102 | + |
| 103 | +5. Finally, use `st.form()` to create a text box (`st.text_area()`) for user input. When the user clicks `Submit`, the `generate-response()` function is called with the user's input as an argument. |
| 104 | + |
| 105 | + ```python |
| 106 | + with st.form('my_form'): |
| 107 | + text = st.text_area('Enter text:', 'What are the three key pieces of advice for learning how to code?') |
| 108 | + submitted = st.form_submit_button('Submit') |
| 109 | + if not openai_api_key.startswith('sk-'): |
| 110 | + st.warning('Please enter your OpenAI API key!', icon='⚠') |
| 111 | + if submitted and openai_api_key.startswith('sk-'): |
| 112 | + generate_response(text) |
| 113 | + ``` |
| 114 | + |
| 115 | +6. Remember to save your file! |
| 116 | +7. Return to your computer's terminal to run the app. |
| 117 | + |
| 118 | + ```bash |
| 119 | + streamlit run streamlit_app.py |
| 120 | + ``` |
| 121 | + |
| 122 | +## Deploying the app |
| 123 | + |
| 124 | +To deploy the app to the Streamlit Cloud, follow these steps: |
| 125 | + |
| 126 | +1. Create a GitHub repository for the app. Your repository should contain two files: |
| 127 | + |
| 128 | + ``` |
| 129 | + your-repository/ |
| 130 | + ├── streamlit_app.py |
| 131 | + └── requirements.txt |
| 132 | + ``` |
| 133 | + |
| 134 | +1. Go to [Streamlit Community Cloud](http://share.streamlit.io), click the `New app` button from your workspace, then specify the repository, branch, and main file path. Optionally, you can customize your app's URL by choosing a custom subdomain. |
| 135 | +2. Click the `Deploy!` button. |
| 136 | + |
| 137 | +Your app will now be deployed to Streamlit Community Cloud and can be accessed from around the world! 🌎 |
| 138 | + |
| 139 | +## Conclusion |
| 140 | + |
| 141 | +Congratulations on building an LLM-powered Streamlit app in 18 lines of code! 🥳 You can use this app to generate text from any prompt that you provide. The app is limited by the capabilities of the OpenAI LLM, but it can still be used to generate some creative and interesting text. |
| 142 | + |
| 143 | +We hope you found this tutorial helpful! Check out [more examples](https://streamlit.io/generative-ai) to see the power of Streamlit and LLM. 💖 |
| 144 | + |
| 145 | +Happy Streamlit-ing! 🎈 |
0 commit comments