Skip to content

Commit

Permalink
tutorial76
Browse files Browse the repository at this point in the history
  • Loading branch information
ronidas39 committed Jun 4, 2024
1 parent 05dd99c commit 908bd8f
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
50 changes: 50 additions & 0 deletions tutorial76/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

from langchain_openai import ChatOpenAI,OpenAIEmbeddings
from langchain.prompts import ChatPromptTemplate
from langchain_elasticsearch import ElasticsearchStore
from langchain_community.document_loaders import TextLoader
from langchain.text_splitter import CharacterTextSplitter
from langchain.schema.runnable import RunnablePassthrough
from langchain.schema.output_parser import StrOutputParser

template="""
You are an assistant for question-answering tasks.
Use the following pieces of retrieved context to answer the question.
If you don't know the answer, just say that you don't know.
Use five sentences minimum and keep the answer concise.
Question: {question}
Context: {context}
Answer:
"""
loader=TextLoader(r"C:\Users\welcome\OneDrive\Documents\GitHub\LLMtutorial\tutorial76\test.txt")
documents=loader.load()
text_splitter=CharacterTextSplitter(chunk_size=500,chunk_overlap=0)
docs=text_splitter.split_documents(documents)
embeddings=OpenAIEmbeddings()

# vector_db=ElasticsearchStore.from_documents(
# docs,
# embedding=embeddings,
# index_name="tutorial76",
# es_cloud_id="tt:dXMtZWFzdC0yLmF3cy5lbGFzdGljLWNsb3VkLmNvbTo0NDMkZjdmOWIwMTBjNTRhNDg4NDk2MTY4OGE0NjM5NTkwZmEkMmQ0MTQ3NDU0NjEwNGY3OWE4Y2E4MWEyNDAwYTQwZDQ=",
# es_api_key="Vnl0SjRZOEJkQ2RfOUxjNndRZ1Q6RHo3bHF5RVVTZGVHajZMU3FTd1lOUQ=="
# )

vector_db=ElasticsearchStore(
embedding=embeddings,
index_name="tutorial76",
es_cloud_id="tt:dXMtZWFzdC0yLmF3cy5lbGFzdGljLWNsb3VkLmNvbTo0NDMkZjdmOWIwMTBjNTRhNDg4NDk2MTY4OGE0NjM5NTkwZmEkMmQ0MTQ3NDU0NjEwNGY3OWE4Y2E4MWEyNDAwYTQwZDQ=",
es_api_key="Vnl0SjRZOEJkQ2RfOUxjNndRZ1Q6RHo3bHF5RVVTZGVHajZMU3FTd1lOUQ=="
)
retriever=vector_db.as_retriever()
prompt=ChatPromptTemplate.from_template(template)
llm=ChatOpenAI(model="gpt-4o",temperature=0)
rag_chain=(
{"context":retriever,"question":RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
query="given some family information on king john"
response=rag_chain.invoke(query)
print(response)
8 changes: 8 additions & 0 deletions tutorial76/prompt.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"""You are an assistant for question-answering tasks.
Use the following pieces of retrieved context to answer the question.
If you don't know the answer, just say that you don't know.
Use five sentences minimum and keep the answer concise.
Question: {question}
Context: {context}
Answer:
"""
4 changes: 4 additions & 0 deletions tutorial76/qsn.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
What did this text about
Who is king john?
give some information on kingdom of Aldori
given some family information on king john
19 changes: 19 additions & 0 deletions tutorial76/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
**The Tale of Prince John**

Once upon a time, in the heart of the ancient kingdom of Aldoria, there lived a noble and kind-hearted prince named John. Aldoria was a land of lush forests, towering mountains, and sparkling rivers, where peace and prosperity reigned under the wise rule of King Edmund and Queen Isabella, John's beloved parents.

Prince John was admired by all for his courage, intelligence, and compassion. From a young age, he was taught the arts of diplomacy, combat, and governance, preparing him for the day he would ascend to the throne. Despite his royal duties, John had a deep love for the natural world and often spent his free time exploring the kingdom’s enchanting landscapes, helping the villagers, and learning their stories.

One fateful day, news arrived that a dark sorcerer named Malakar had risen in the distant mountains, threatening to cast a shadow over Aldoria. Malakar, with his formidable magic and army of enchanted creatures, sought to conquer the kingdom and enslave its people. The threat was imminent, and the once-peaceful realm now faced the dire prospect of war.

Determined to protect his homeland, Prince John embarked on a perilous journey to confront Malakar. Armed with his trusted sword, Valor, and accompanied by his loyal companions – Sir Gareth, a brave knight, and Elara, a skilled archer with magical abilities – John ventured into the treacherous mountains.

The journey was fraught with danger. The trio faced numerous challenges: crossing raging rivers, battling fierce beasts, and navigating through dark, enchanted forests. Along the way, they encountered allies who joined their cause – a wise old wizard named Merlin, a fierce warrior named Kael, and a group of rebels who had been oppressed by Malakar's tyranny.

As they approached Malakar’s fortress, the final battle loomed. Prince John and his companions stormed the sorcerer’s stronghold, fighting valiantly against Malakar’s minions. The climactic showdown between John and Malakar was a fierce clash of might and magic. With the help of his friends and the strength of his convictions, John was able to overcome the dark sorcerer, breaking his spell over the land.

With Malakar defeated, peace was restored to Aldoria. The people hailed Prince John as a hero, celebrating his bravery and leadership. Upon returning to the capital, he was greeted with great joy and gratitude by his parents and the kingdom’s citizens.

Years later, as King John, he ruled Aldoria with wisdom and compassion, just as his parents had taught him. His reign was marked by justice, prosperity, and the continued protection of the natural beauty of the kingdom. The tale of Prince John’s courage and the defeat of Malakar became a cherished legend, inspiring future generations to uphold the values of bravery, kindness, and the enduring fight against darkness.

And so, the kingdom of Aldoria thrived, a beacon of hope and harmony, all thanks to the noble heart of Prince John.
Binary file added tutorial76/tutorial76.pptx
Binary file not shown.

0 comments on commit 908bd8f

Please sign in to comment.