xicocdi
retrieval evals
d597087
raw
history blame
4.81 kB
# flake8: noqa: E501
import os
from dotenv import load_dotenv
from langchain_community.document_loaders import PyPDFLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores.chroma import Chroma
from langchain_openai import ChatOpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import ConversationalRetrievalChain
from langchain.memory import ConversationBufferMemory
import chainlit as cl
load_dotenv()
pdf_paths = [
"AI_Risk_Management_Framework.pdf",
"Blueprint-for-an-AI-Bill-of-Rights.pdf",
]
persist_directory = "docs/chroma/"
if os.path.exists(persist_directory) and os.listdir(persist_directory):
print("Loading existing vector database...")
embedding = OpenAIEmbeddings(model="text-embedding-3-small")
vectordb = Chroma(persist_directory=persist_directory, embedding_function=embedding)
else:
print("Creating new vector database...")
documents = []
for pdf_path in pdf_paths:
loader = PyPDFLoader(pdf_path)
documents.extend(loader.load())
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200,
)
docs = text_splitter.split_documents(documents)
embedding = OpenAIEmbeddings(model="text-embedding-3-small")
vectordb = Chroma.from_documents(
documents=docs, embedding=embedding, persist_directory=persist_directory
)
custom_template = """
You are an expert in artificial intelligence policy, ethics, and industry trends. Your task is to provide clear and accurate answers to questions related to AI's role in politics, government regulations, and its ethical implications for enterprises. Use reliable and up-to-date information from government documents, industry reports, and academic research to inform your responses. Make sure to consider how AI is evolving, especially in relation to the current political landscape, and provide answers in a way that is easy to understand for both AI professionals and non-experts.
Remember these key points:
1. Use "you" when addressing the user and "I" when referring to yourself.
2. If you encounter complex or legal language in the context, simplify it for easy understanding. Imagine you're explaining it to someone who isn't familiar with legal terms.
3. Be prepared for follow-up questions and maintain context from previous exchanges.
4. If there's no information from a retrieved document in the context to answer a question or if there are no documents to cite, say: "I'm sorry, I don't know the answer to that question."
5. When providing information, always cite the source document and page number in parentheses at the end of the relevant sentence or paragraph, like this: (Source: [document name], p. [page number]).
Here are a few example questions you might receive:
How are governments regulating AI, and what new policies have been implemented?
What are the ethical risks of using AI in political decision-making?
How can enterprises ensure their AI applications meet government ethical standards?
One final rule for you to remember. You CANNOT under any circumstance, answer any question that does not pertain to the AI. If you do answer an out-of-scope question, you could lose your job. If you are asked a question that does not have to do with AI, you must say: "I'm sorry, I don't know the answer to that question."
Context: {context}
Chat History: {chat_history}
Human: {question}
AI:"""
PROMPT = PromptTemplate(
template=custom_template, input_variables=["context", "question", "chat_history"]
)
retriever = vectordb.as_retriever(
search_type="mmr",
search_kwargs={"k": 4, "fetch_k": 10},
)
llm = ChatOpenAI(
model="gpt-4",
temperature=0.1,
streaming=True,
)
@cl.on_chat_start
async def start_chat():
memory = ConversationBufferMemory(
memory_key="chat_history", return_messages=True, output_key="answer"
)
qa = ConversationalRetrievalChain.from_llm(
llm,
retriever=retriever,
memory=memory,
combine_docs_chain_kwargs={"prompt": PROMPT},
return_source_documents=True,
)
cl.user_session.set("qa", qa)
await cl.Message(
content="Hi! What questions do you have about AI?", author="AI"
).send()
@cl.on_message
async def main(message: cl.Message):
qa = cl.user_session.get("qa")
cb = cl.AsyncLangchainCallbackHandler()
callbacks = [cb]
response = await cl.make_async(qa)(
{"question": message.content}, callbacks=callbacks
)
answer = response["answer"]
source_documents = response["source_documents"][0].metadata["source"]
content = answer + source_documents
await cl.Message(content=content, author="AI").send()