Skip to content

Instantly share code, notes, and snippets.

@AI-Guru
Created February 10, 2024 09:36
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save AI-Guru/3f8f842219c3010d6daa678c14de9b7e to your computer and use it in GitHub Desktop.
Save AI-Guru/3f8f842219c3010d6daa678c14de9b7e to your computer and use it in GitHub Desktop.
Summarizing a text with LangChain and Ollama and StableLM 2.
# 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