Skip to content

Instantly share code, notes, and snippets.

@Cdaprod
Last active August 2, 2023 04:43
Show Gist options
  • Save Cdaprod/b5fd8cf740c27fe850041b09b6a21fa9 to your computer and use it in GitHub Desktop.
Save Cdaprod/b5fd8cf740c27fe850041b09b6a21fa9 to your computer and use it in GitHub Desktop.
Tutorial on how to use Streamlit, Langchain,and SerpAPI to search the web with AI
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "b3f7dbe3",
"metadata": {},
"source": [
"\n",
"# LangChain Search with Streamlit\n",
"\n",
"In this tutorial, we will guide you through the process of setting up and running a Streamlit app for LangChain Search. The app allows users to input their OpenAI and SERP API keys, enter a search query, and retrieve results using LangChain's capabilities.\n"
]
},
{
"cell_type": "markdown",
"id": "821d332d",
"metadata": {},
"source": [
"\n",
"## Installation of Required Packages\n",
"\n",
"Before we begin, let's ensure we have all the necessary packages installed.\n"
]
},
{
"cell_type": "markdown",
"id": "14c0a9dc",
"metadata": {},
"source": [
"\n",
"## Creating the Streamlit App\n",
"\n",
"We'll define our Streamlit app and save the code to a local file named `app.py`.\n"
]
},
{
"cell_type": "markdown",
"id": "c04340d9",
"metadata": {},
"source": [
"\n",
"## Setting Up LocalTunnel\n",
"\n",
"To make our Streamlit app accessible over the internet, we'll set up LocalTunnel.\n"
]
},
{
"cell_type": "markdown",
"id": "c2827d1e",
"metadata": {},
"source": [
"\n",
"## Running the Streamlit App\n",
"\n",
"Let's run our Streamlit app in the background. It will start serving on the default port, 8501.\n"
]
},
{
"cell_type": "markdown",
"id": "2c2d8496",
"metadata": {},
"source": [
"\n",
"## Exposing the App\n",
"\n",
"Finally, we'll expose our Streamlit app to the internet using LocalTunnel on port 8501.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6121b9f8",
"metadata": {},
"outputs": [],
"source": [
"\n",
"!pip install streamlit\n",
"!pip install langchain\n",
"!pip install openai\n",
"!pip install google-search-results\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ce583d11",
"metadata": {},
"outputs": [],
"source": [
"\n",
"%%writefile app.py\n",
"import streamlit as st\n",
"from langchain.llms.openai import OpenAI\n",
"from langchain.agents import load_tools, initialize_agent\n",
"\n",
"# Streamlit app\n",
"st.title('LangChain Search')\n",
"\n",
"# Get OpenAI API key, SERP API key and search query\n",
"openai_api_key = st.text_input(\"OpenAI API Key\", type=\"password\")\n",
"serpapi_api_key = st.text_input(\"SERP API Key\", type=\"password\")\n",
"search_query = st.text_input(\"Search Query\")\n",
"\n",
"# Check if the 'Search' button is clicked\n",
"if st.button(\"Search\"):\n",
" # Validate inputs\n",
" if not openai_api_key.strip() or not serpapi_api_key.strip() or not search_query.strip():\n",
" st.write(f\"Please provide the missing fields.\")\n",
" else:\n",
" try:\n",
" # Initialize the OpenAI module, load the SerpApi tool, and run the search query using an agent\n",
" llm=OpenAI(temperature=0, openai_api_key=openai_api_key, verbose=True)\n",
" tools = load_tools([\"serpapi\"], llm, serpapi_api_key=serpapi_api_key)\n",
" agent = initialize_agent(tools, llm, agent=\"zero-shot-react-description\", verbose=True)\n",
"\n",
" result = agent.run(search_query)\n",
" \n",
" st.write(result)\n",
" except Exception as e:\n",
" st.write(f\"An error occurred: {e}\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7c9da7f9",
"metadata": {},
"outputs": [],
"source": [
"\n",
"!npm install localtunnel\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "39f00d04",
"metadata": {},
"outputs": [],
"source": [
"\n",
"!streamlit run app.py &>/content/logs.txt &\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f89ec071",
"metadata": {},
"outputs": [],
"source": [
"\n",
"!npx localtunnel --port 8501\n"
]
}
],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment