|
import os |
|
import openai |
|
from dotenv import load_dotenv |
|
load_dotenv() |
|
|
|
print(os.getenv("OPENAI_API_KEY")) |
|
openai.api_key = os.getenv("OPENAI_API_KEY") |
|
|
|
from langchain import PromptTemplate |
|
from langchain.chains import RetrievalQA |
|
|
|
from langchain.embeddings.openai import OpenAIEmbeddings |
|
from langchain.chat_models import ChatOpenAI |
|
from langchain.text_splitter import CharacterTextSplitter |
|
from langchain.text_splitter import MarkdownTextSplitter |
|
from langchain.vectorstores import Chroma |
|
from langchain.document_loaders import TextLoader |
|
|
|
from langchain.agents import Tool |
|
from langchain.memory import ConversationBufferMemory |
|
from langchain.chat_models import ChatOpenAI |
|
from langchain.utilities import SerpAPIWrapper |
|
from langchain.agents import initialize_agent |
|
from langchain.agents import AgentType |
|
|
|
|
|
from langchain.chains.router import MultiRetrievalQAChain |
|
from langchain.llms import OpenAI |
|
|
|
bulevar_restaurant_texts = [ |
|
"Bulevar is open Sunday through Wednesday from 5-9pm, and Thursday through Satruday from 4-10pm", |
|
"Bulevar is located in the Arboretum at 360 and Mopac, next to Eddie V's", |
|
"Bulevar offers tasty Mexican Cuisine, with a laid back style to fine-dining.", |
|
] |
|
bulevar_details_retriever = Chroma.from_texts(bulevar_restaurant_texts, embedding_function= OpenAIEmbeddings()) |
|
|
|
loader = TextLoader('raw_text/food_guide.md') |
|
documents = loader.load() |
|
|
|
text_splitter = MarkdownTextSplitter(chunk_size=750, chunk_overlap=35) |
|
docs = text_splitter.split_documents(documents) |
|
|
|
embeddings = OpenAIEmbeddings() |
|
|
|
docs_retriever = Chroma.from_documents(docs, embeddings) |
|
|
|
query = "Who is Jonny?" |
|
docs = docs_retriever.similarity_search(query) |
|
|
|
print(docs[0].page_content) |
|
|
|
prompt = """Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer. |
|
|
|
Context: {context} |
|
|
|
Question: {question} |
|
Helpful Answer:""" |
|
|
|
prompt_template = PromptTemplate( |
|
template=prompt, input_variables=["context", "question"] |
|
) |
|
qa_chain = RetrievalQA.from_chain_type(llm= ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0.0), |
|
chain_type='stuff', |
|
retriever=docs_retriever.as_retriever(), |
|
|
|
return_source_documents=True, |
|
chain_type_kwargs={"prompt": prompt_template, "verbose": True}, |
|
) |
|
result = qa_chain({"query": query}) |
|
print(result["result"]) |
|
|
|
retriever_infos = [ |
|
{ |
|
"name": "Food Guide", |
|
"description": "Good for answering questions about Bulevar's dinner menu", |
|
"retriever": docs_retriever.as_retriever() |
|
}, |
|
{ |
|
"name": "Bulevar Restaurant Details", |
|
"description": "Good for answering questions about Bulevar's hours, and restaurant details such as its mission, history, and owners.", |
|
"retriever": bulevar_details_retriever |
|
} |
|
] |
|
|
|
chain = MultiRetrievalQAChain.from_retrievers(OpenAI(), retriever_infos, verbose=True) |
|
|
|
print(chain.run("What is Bulevar?")) |
|
print(chain.run("What kinds of meats are offered?")) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|