Skip to content

Commit

Permalink
Update langchain usage in tutorial (#1116)
Browse files Browse the repository at this point in the history
* docs: update langchain usage

* Update instructions to match

---------

Co-authored-by: Debbie Matthews <[email protected]>
  • Loading branch information
Undertone0809 and sfc-gh-dmatthews authored Jul 29, 2024
1 parent 15e17f5 commit 358b466
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 35 deletions.
57 changes: 33 additions & 24 deletions content/develop/tutorials/llms/llm-quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Bonus: Deploy the app on Streamlit Community Cloud!
In your IDE (integrated coding environment), open the terminal and install the following three Python libraries:

```python
pip install streamlit openai langchain
pip install streamlit openai langchain langchain-community langchain-openai
```

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.
Expand All @@ -53,23 +53,29 @@ The app is only 18 lines of code:

```python
import streamlit as st
from langchain.llms import OpenAI
from langchain_openai.chat_models import ChatOpenAI

st.title('🦜🔗 Quickstart App')
st.title("🦜🔗 Quickstart App")

openai_api_key = st.sidebar.text_input("OpenAI API Key", type="password")

openai_api_key = st.sidebar.text_input('OpenAI API Key', type='password')

def generate_response(input_text):
llm = OpenAI(temperature=0.7, openai_api_key=openai_api_key)
st.info(llm(input_text))

with st.form('my_form'):
text = st.text_area('Enter text:', 'What are the three key pieces of advice for learning how to code?')
submitted = st.form_submit_button('Submit')
if not openai_api_key.startswith('sk-'):
st.warning('Please enter your OpenAI API key!', icon='')
if submitted and openai_api_key.startswith('sk-'):
model = ChatOpenAI(temperature=0.7, api_key=openai_api_key)
st.info(model.invoke(input_text))


with st.form("my_form"):
text = st.text_area(
"Enter text:",
"What are the three key pieces of advice for learning how to code?",
)
submitted = st.form_submit_button("Submit")
if not openai_api_key.startswith("sk-"):
st.warning("Please enter your OpenAI API key!", icon="")
if submitted and openai_api_key.startswith("sk-"):
generate_response(text)

```

To start, create a new Python file and save it as `streamlit_app.py` in the root of your working directory.
Expand All @@ -78,38 +84,41 @@ To start, create a new Python file and save it as `streamlit_app.py` in the roo

```python
import streamlit as st
from langchain.llms import OpenAI
from langchain_openai.chat_models import ChatOpenAI
```

2. Create the app's title using `st.title`.

```python
st.title('🦜🔗 Quickstart App')
st.title("🦜🔗 Quickstart App")
```

3. Add a text input box for the user to enter their OpenAI API key.

```python
openai_api_key = st.sidebar.text_input('OpenAI API Key', type='password')
openai_api_key = st.sidebar.text_input("OpenAI API Key", type="password")
```

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`.

```python
def generate_response(input_text):
llm = OpenAI(temperature=0.7, openai_api_key=openai_api_key)
st.info(llm(input_text))
model = ChatOpenAI(temperature=0.7, api_key=openai_api_key)
st.info(model.invoke(input_text))
```

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.

```python
with st.form('my_form'):
text = st.text_area('Enter text:', 'What are the three key pieces of advice for learning how to code?')
submitted = st.form_submit_button('Submit')
if not openai_api_key.startswith('sk-'):
st.warning('Please enter your OpenAI API key!', icon='')
if submitted and openai_api_key.startswith('sk-'):
with st.form("my_form"):
text = st.text_area(
"Enter text:",
"What are the three key pieces of advice for learning how to code?",
)
submitted = st.form_submit_button("Submit")
if not openai_api_key.startswith("sk-"):
st.warning("Please enter your OpenAI API key!", icon="")
if submitted and openai_api_key.startswith("sk-"):
generate_response(text)
```

Expand Down
27 changes: 16 additions & 11 deletions python/tutorial-source/llm-18-lines-of-code/streamlit_app.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import streamlit as st
from langchain.llms import OpenAI
from langchain_openai.chat_models import ChatOpenAI

st.title('🦜🔗 Quickstart App')
st.title("🦜🔗 Quickstart App")

openai_api_key = st.sidebar.text_input("OpenAI API Key", type="password")

openai_api_key = st.sidebar.text_input('OpenAI API Key', type='password')

def generate_response(input_text):
llm = OpenAI(temperature=0.7, openai_api_key=openai_api_key)
st.info(llm(input_text))
model = ChatOpenAI(temperature=0.7, api_key=openai_api_key)
st.info(model.invoke(input_text))


with st.form('my_form'):
text = st.text_area('Enter text:', 'What are the three key pieces of advice for learning how to code?')
submitted = st.form_submit_button('Submit')
if not openai_api_key.startswith('sk-'):
st.warning('Please enter your OpenAI API key!', icon='⚠')
if submitted and openai_api_key.startswith('sk-'):
with st.form("my_form"):
text = st.text_area(
"Enter text:",
"What are the three key pieces of advice for learning how to code?",
)
submitted = st.form_submit_button("Submit")
if not openai_api_key.startswith("sk-"):
st.warning("Please enter your OpenAI API key!", icon="⚠")
if submitted and openai_api_key.startswith("sk-"):
generate_response(text)

0 comments on commit 358b466

Please sign in to comment.