In [ ]:
from langchain.vectorstores.cassandra import Cassandra
from langchain.indexes.vectorstore import VectorStoreIndexWrapper
from langchain.llms import OpenAI
from langchain.embeddings import OpenAIEmbeddings
In [ ]:
from cassandra.cluster import Cluster
from cassandra.auth import PlainTextAuthProvider
In [ ]:
from datasets import load_dataset
In [ ]:
from dotenv import load_dotenv
load_dotenv()
Out[ ]:
True
In [ ]:
import os
In [ ]:
cloud_config= {
'secure_connect_bundle': os.getenv('ASTRA_DB_SECURE_BUNDLE_PATH')
}
auth_provider = PlainTextAuthProvider(os.getenv('ASTRA_DB_CLIENT_ID'), os.getenv('ASTRA_DB_CLIENT_SECRET'))
cluster = Cluster(cloud=cloud_config, auth_provider=auth_provider)
astraSession = cluster.connect()
In [ ]:
llm = OpenAI(openai_api_key=os.getenv('OPENAI_API_KEY'))
In [ ]:
myEmbedding = OpenAIEmbeddings(openai_api_key=os.getenv('OPENAI_API_KEY'))
In [ ]:
myCassandraVStore = Cassandra(
embedding=myEmbedding,
session=astraSession,
keyspace=os.getenv('ASTRA_DB_KEYSPACE'),
table_name="qa_mini_demo",
)
In [ ]:
print("Loading data from huggingface")
myDataset = load_dataset("Biddls/Onion_News", split="train")
headlines = myDataset['text'][:50]
Loading data from huggingface
Found cached dataset text (/home/shpark/.cache/huggingface/datasets/Biddls___text/Biddls--Onion_News-68e4388dcc8b1aec/0.0.0/cb1e9bd71a82ad27976be3b12b407850fe2837d80c22c5e03a28949843a8ace2)
In [ ]:
headlines[0:5]
Out[ ]:
['Relaxed Marie Kondo Now Says She Perfectly Happy Living In Waist-High Sewage #~# LOS ANGELES—Admitting that she’d made some major lifestyle changes since developing her famous KonMari method, a relaxed Marie Kondo told reporters Tuesday that she was now perfectly happy living in waist-high sewage. “The truth is, while I used to be very hard on myself about keeping everything clean, I’m now able to find peace living my life half-submerged in a large, fetid pool of human waste,” said Kondo, who added that while things like tidiness, organization, and minimalism used to spark joy for her, she now felt that same warmth from wading, floating, and swimming in the many gallons of untreated urine and feces that currently filled her home. “It was difficult, but once I had my children, I began to find it impossible to remove, clean, and sanitize the unending stream of excrement that bubbled up out of my toilets and filled my house to the point of collapse. While I used to hate it, I now see that each individual piece of human shit plays a part in creating a relaxed, easygoing atmosphere. And that is beautiful.” At press time, Kondo had publicly apologized to her fans after many admitted they had removed several feet of raw sewage from their own homes and now deeply regretted the decision.', 'U.S. Officials Call For Correct Amount Of Violence #~# WASHINGTON—Addressing the need for swift and moderate change, U.S. officials reportedly called Tuesday for the correct amount of violence. “Like so many of you, I am outraged by the visibility of these senseless acts of hatred and violence,” said President Joe Biden, who urged leaders at all levels of government to work together in order to dial down instances of egregious police misconduct to a more tolerable level. “This kind of inexcusable violence should rise above no more than medium levels. If it makes a headline, that’s too far. We don’t want to read about it, we don’t want to see it on TV, and we don’t want to hear our communities discussing it.” At press time, Biden had signaled that he would be open to less but higher-quality state-sanctioned violence. ', 'Kamala Harris Asks Communications Assistant If She Can Take Them Out For Coffee And Pick Their Brain Sometime #~# WASHINGTON—In an effort to expand her professional network, Vice President Kamala Harris reportedly asked a White House communications assistant Tuesday if she could take them out for coffee and pick their brain sometime. “I’ve always been super interested in communications, so I’d love to hear your perspective on it—for example, I was wondering what is communications, exactly?” said Harris, who noted that she was curious about what the assistant’s day-to-day experience was like and was interested in any other tips they might have for her career. “Is it like social media, or is it more of a cross-hybrid of different communications stuff? I’m sure you’ve had a very cool experience. There aren’t any openings, are there? There’s a Starbucks right around the corner, and I’m buying! Thanks in advance. I understand if you can’t make it, of course. I know you’re busy.” At press time, sources confirmed Harris was awestruck upon learning the assistant had gotten to meet President Joe Biden. ', '25 Arrested In Fake Nursing School Diploma Scheme #~# Federal agents have arrested 25 suspects accused of selling fake nursing degrees to thousands of students who then used the bogus diplomas to take licensing exams in several states, including Florida, New York, New Jersey, and Texas. What do you think?', 'World’s Oldest American Dies At 72 #~# OXNARD, CA—Living longer than any citizen in the nation’s history, Gus Rolnicki, the world’s oldest American, passed away Tuesday at the age of 72. “My grandfather was the last person who could tell you what it was like to watch television in black-and-white, or turn on the radio and hear bands like Creedence Clearwater Revival and the Doors,” grandson Devin Rolnicki, 21, said of the deceased, whose life remarkably spanned both the Vietnam War and the terrorist attacks of 9/11. “These are things the rest of us only know from books, so our country has lost its last living witness to an era—a guy who remembered the first man walking on the moon and even where he was when he heard President Kennedy had been shot. Grandpa Gus lived a rich, full life, but his loss is still very hard for our family, especially because we know he was really looking forward to retiring next year.” Upon Gus Rolnicki’s death, reports confirmed the baton of world’s oldest American had been passed to 55-year-old Arkansas resident Pauletta Bickford.']
In [ ]:
print("\nGenerating embeddings and storing in AstraDB")
myCassandraVStore.add_texts(headlines)
print("Inserted %i headlines.\n" % len(headlines))
Generating embeddings and storing in AstraDB Inserted 50 headlines.
In [ ]:
vectorIndex = VectorStoreIndexWrapper(vectorstore=myCassandraVStore)
In [ ]:
first_question = True
while True:
if first_question:
query_text = input("\nEnter your question (or type 'quit' to exit): ")
first_question = False
else:
query_text = input("\nWhat's your next question (or type 'quit' to exit): ")
if query_text.lower() == "quit":
break
print("QUESTION: \"%s\"" % query_text)
answer = vectorIndex.query(query_text, llm=llm).strip()
print("ANSWER: \"%s\"\n" % answer)
print("DOCUMENTS BY RELEVAMCE:")
for doc, score in myCassandraVStore.similarity_search_with_score(query_text,k=4):
print(" %0.4f \"%s ...\"" % (score, doc.page_content[:60]))
QUESTION: "What are the biggest questions in science?" ANSWER: "I don't know." DOCUMENTS BY RELEVAMCE: 0.8908 "Biologists Torture Amoeba For Information On Where Life Came ..." 0.8908 "Biologists Torture Amoeba For Information On Where Life Came ..." 0.8695 "Study Shows Humans Still Have Genes To Grow Full Coat Of Bod ..." 0.8694 "Study Shows Humans Still Have Genes To Grow Full Coat Of Bod ..." QUESTION: "What should I know about Silicon Valley banks?" ANSWER: "I'm sorry, I don't know." DOCUMENTS BY RELEVAMCE: 0.8721 "Google Employees React To Company Layoffs #~# After Google r ..." 0.8721 "Google Employees React To Company Layoffs #~# After Google r ..." 0.8678 "Kamala Harris Asks Communications Assistant If She Can Take ..." 0.8678 "Kamala Harris Asks Communications Assistant If She Can Take ..." QUESTION: "Are amoebas really our overlords?" ANSWER: "No, amoebas are not our overlords." DOCUMENTS BY RELEVAMCE: 0.9271 "Biologists Torture Amoeba For Information On Where Life Came ..." 0.9270 "Biologists Torture Amoeba For Information On Where Life Came ..." 0.8732 "Zelensky Calls On U.S. To Send Totally Psycho Marine #~# KYI ..." 0.8732 "Zelensky Calls On U.S. To Send Totally Psycho Marine #~# KYI ..."
In [ ]:
In [ ]:
In [ ]: