/search_with_langchain

Building a quick conversation-based search demo with langchain.

Primary LanguagePythonApache License 2.0Apache-2.0

Search with Langchain

Build your own conversational search engine using less than 500 lines of code.
Live Demo

Features

  • Built-in support for LLM
  • Built-in support for search engine
  • Customizable pretty UI interface
  • Shareable, cached search results

Setup Search Engine API

There are two default supported search engines: Bing and Google.

Bing Search

To use the Bing Web Search API, please visit this link to obtain your Bing subscription key. You also need to replace the search_with_google function in rag_chain.py with search_with_bing in search_with_lepton.py. Change the service keys accordingly.

Google Search

You have three options for Google Search: you can use the SearchApi Google Search API from SearchApi, Serper Google Search API from Serper, or opt for the Programmable Search Engine provided by Google. I have implemented Programmable Search Engine in rag_chain.py. But you could change it to other ones (see section Bing Search).

Setup LLM and Search Service

Note

I don't get access to powerful GPUs :( so I use the NVIDIA AI Foundation Endpoints. But I recommend you replace it with ollama if you insist to have everthing set up locally. Set up your service keys starting line 23 in rag_chain.py like this:

# ===========================================================
# set service keys
NVAPI_KEY = 'nvapi-xxx'
SEARCH_API_KEY_GOOGLE = "xxx"                                          
SEARCH_ID_GOOGLE = "xxx" # cx parameter   

Running the following commands to set up the environment.

pip install langchain
pip install loguru
pip install --upgrade --quiet langchain-nvidia-ai-endpoints
pip install fastapi
pip install "uvicorn[standard]"

Build

  1. Build web
cd web && npm install && npm run build
  1. Run server
python search_with_langchain.py
  1. Visit your local conversational search engine at http://localhost:8080/ !

Error Handling

  1. prettier/prettier

If you have encounter something like

 Error: Delete ``  prettier/prettier

during build, visit web/.eslintrc.json and add a line to turn prettier/prettier off like this. (That's how I get around this anyway.)

"rules": {
    "unused-imports/no-unused-imports": "error",
    "prettier/prettier": "off"
  }