Created
March 14, 2024 09:56
-
-
Save janakiramm/8fcfc6c055c09a6f5dc5248b890f0567 to your computer and use it in GitHub Desktop.
Python code to build a content summarization application based on Gemini and LangChain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Install required modules and set the envvar for Gemini API Key | |
#pip install google.generativeai | |
#pip install langchain-google-genai | |
#pip install langchain | |
#pip install langchain_community | |
#pip install jupyter | |
#export GOOGLE_API_KEY="YOUR_GOOGLE_API_KEY" | |
#Import Modules | |
from langchain_google_genai import ChatGoogleGenerativeAI | |
from langchain_google_genai import GoogleGenerativeAIEmbeddings | |
from langchain_community.document_loaders import WebBaseLoader | |
from langchain.chains import StuffDocumentsChain | |
from langchain.chains.llm import LLMChain | |
from langchain.prompts import PromptTemplate | |
#Initialize Model | |
llm = ChatGoogleGenerativeAI(model="gemini-pro") | |
#Load the blog | |
loader = WebBaseLoader("https://thenewstack.io/the-building-blocks-of-llms-vectors-tokens-and-embeddings/") | |
docs = loader.load() | |
#Define the Summarize Chain | |
template = """Write a concise summary of the following: | |
"{text}" | |
CONCISE SUMMARY:""" | |
prompt = PromptTemplate.from_template(template) | |
llm_chain = LLMChain(llm=llm, prompt=prompt) | |
stuff_chain = StuffDocumentsChain(llm_chain=llm_chain, document_variable_name="text") | |
#Invoke Chain | |
response=stuff_chain.invoke(docs) | |
print(response["output_text"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment