-
-
Save AI-Guru/3f8f842219c3010d6daa678c14de9b7e to your computer and use it in GitHub Desktop.
Summarizing a text with LangChain and Ollama and StableLM 2.
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
# Get a Mac with Apple Silicon tech or a PC with NVIDA GPU and Linux. | |
# Install all necessary dependencies. | |
# Install Ollama: https://ollama.com/ | |
# Pull stablelm2: ollama pull stablelm2 | |
# If ollama does not run as a service, run: ollama serve | |
import time, datetime | |
import colorama | |
from langchain_openai import ChatOpenAI | |
from langchain.chains.summarize import load_summarize_chain | |
from langchain_community.document_loaders import WebBaseLoader | |
from langchain.text_splitter import CharacterTextSplitter | |
# Take the current time. | |
start_time = time.time() | |
# Create a connection to the local Ollama server. | |
llm = ChatOpenAI( | |
temperature=0, | |
model_name="stablelm2", | |
openai_api_base="http://localhost:11434/v1", | |
openai_api_key="Not needed for local server", | |
) | |
# Downloading the data from the web. | |
print("Downloading...") | |
url = "https://energy.ec.europa.eu/topics/nuclear-energy/small-modular-reactors_en" | |
loader = WebBaseLoader(url) | |
docs = loader.load() | |
# Split the documents into chunks. | |
print("Splitting...") | |
text_splitter = CharacterTextSplitter.from_tiktoken_encoder( | |
chunk_size=1000, chunk_overlap=0 | |
) | |
split_docs = text_splitter.split_documents(docs) | |
# Summarize the documents. | |
print("Summarizing...") | |
chain = load_summarize_chain(llm, chain_type="refine") | |
summary = chain.invoke(split_docs)["output_text"] | |
# Print the summary. | |
print("Summary: ") | |
print(colorama.Fore.GREEN + summary + colorama.Style.RESET_ALL) | |
# Print the elapsed time. Use timedelta. | |
end_time = time.time() | |
print("Elapsed time: ", datetime.timedelta(seconds=end_time - start_time)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment