Skip to content

Commit c8d709f

Browse files
committed
tutorial78
1 parent 8e8dd15 commit c8d709f

7 files changed

Lines changed: 58 additions & 67626 deletions

File tree

tutorial78/api.py

Lines changed: 0 additions & 17 deletions
This file was deleted.

tutorial78/get_info.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

tutorial78/main.py

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,41 @@
1-
from langchain.chains.api.base import APIChain
2-
from langchain_openai import ChatOpenAI
1+
from langchain_openai import OpenAIEmbeddings,ChatOpenAI
2+
from langchain_community.document_loaders import TextLoader
3+
from langchain.text_splitter import CharacterTextSplitter
4+
from langchain.prompts import ChatPromptTemplate
5+
from langchain.schema.runnable import RunnablePassthrough
6+
from langchain.schema.output_parser import StrOutputParser
7+
import weaviate
8+
from langchain_weaviate.vectorstores import WeaviateVectorStore
9+
llm=ChatOpenAI(model="gpt-4o")
10+
loader=TextLoader(r"C:\Users\welcome\OneDrive\Documents\GitHub\LLMtutorial\tutorial78\test.txt")
11+
docs=loader.load()
12+
text_splitter=CharacterTextSplitter(chunk_size=500,chunk_overlap=0)
13+
document=text_splitter.split_documents(docs)
314

4-
llm = ChatOpenAI(model="gpt-4o")
5-
6-
api_docs = """
7-
8-
BASE_URL: http://localhost:8000/
9-
10-
API Documentation:
11-
12-
The API endpoint /get/zip_code/{city} Used to find informatin about the zip code for the given us city. All URL parameters are listed below:
13-
- city: Name of city - Ex: Camuy, Maricao
14-
15-
The API endpoint /get/state_name/{id}} Uesd to find information about the state name for the given state code. All URL parameters are listed below:
16-
- id: 2 letter usa state code. Example: PR,VI
17-
15+
URL = "https://test-2lt4id2i.weaviate.network"
16+
APIKEY = "DnmmkH4txFMUr6UnAg0SCBbOSeuzaLANxhqF"
17+
18+
# Connect to a WCS instance
19+
client = weaviate.connect_to_wcs(
20+
cluster_url=URL,
21+
auth_credentials=weaviate.auth.AuthApiKey(APIKEY))
22+
# vs=WeaviateVectorStore.from_documents(document,embedding=OpenAIEmbeddings(),client=client)
23+
vs=WeaviateVectorStore(client=client,index_name="LangChain_65700351ec2b4baf889198b62eeb6e13",embedding=OpenAIEmbeddings(),text_key="text")
24+
retriever=vs.as_retriever()
25+
template= """You are an assistant for question-answering tasks.
26+
Use the following pieces of retrieved context to answer the question.
27+
If you don't know the answer, just say that you don't know.
28+
Use five sentences minimum and keep the answer concise.
29+
Question: {question}
30+
Context: {context}
31+
Answer:
1832
"""
19-
20-
chain = APIChain.from_llm_and_api_docs(llm, api_docs=api_docs, verbose=True,limit_to_domains=None)
21-
22-
response=chain.run('Can you tell me zip code for about Frederiksted?')
23-
print()
33+
prompt=ChatPromptTemplate.from_template(template)
34+
rag_chain=(
35+
{"context":retriever,"question":RunnablePassthrough()}
36+
|prompt
37+
|llm
38+
|StrOutputParser()
39+
)
40+
response=rag_chain.invoke("write is the moral of this story")
41+
print(response)

tutorial78/test.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
**The Tale of Prince John**
2+
3+
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.
4+
5+
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.
6+
7+
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.
8+
9+
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.
10+
11+
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.
12+
13+
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.
14+
15+
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.
16+
17+
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.
18+
19+
And so, the kingdom of Aldoria thrived, a beacon of hope and harmony, all thanks to the noble heart of Prince John.

tutorial78/tutorial78.pptx

50.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)