|
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) |
3 | 14 |
|
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: |
18 | 32 | """ |
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) |
0 commit comments