|
|
|
|
|
|
|
|
|
|
|
import os |
|
import openai |
|
from openai import AsyncOpenAI |
|
import chainlit as cl |
|
|
|
from chainlit.prompt import Prompt, PromptMessage |
|
from chainlit.playground.providers import ChatOpenAI |
|
|
|
|
|
|
|
from getpass import getpass |
|
|
|
openai.api_key = getpass("Please provide your OpenAI Key: ") |
|
os.environ["OPENAI_API_KEY"] = openai.api_key |
|
|
|
|
|
|
|
|
|
from langchain_community.document_loaders import PyMuPDFLoader |
|
|
|
loader = PyMuPDFLoader( |
|
"NVIDIA_report.pdf", |
|
) |
|
|
|
documents = loader.load() |
|
|
|
|
|
from langchain.text_splitter import RecursiveCharacterTextSplitter |
|
|
|
text_splitter = RecursiveCharacterTextSplitter( |
|
chunk_size = 1000, |
|
chunk_overlap = 100 |
|
) |
|
|
|
documents = text_splitter.split_documents(documents) |
|
|
|
|
|
from langchain_openai import OpenAIEmbeddings |
|
|
|
embeddings = OpenAIEmbeddings( |
|
model="text-embedding-3-small" |
|
) |
|
|
|
|
|
from langchain_community.vectorstores import FAISS |
|
|
|
vector_store = FAISS.from_documents(documents, embeddings) |
|
|
|
|
|
retriever = vector_store.as_retriever() |
|
|
|
|
|
from langchain.prompts import ChatPromptTemplate |
|
|
|
template = """Answer the question based only on the following context. If you cannot answer the question with the context, please respond with 'I don't know': |
|
|
|
Context: |
|
{context} |
|
|
|
Question: |
|
{question} |
|
""" |
|
|
|
prompt = ChatPromptTemplate.from_template(template) |
|
|
|
|
|
from operator import itemgetter |
|
|
|
from langchain_openai import ChatOpenAI |
|
from langchain_core.output_parsers import StrOutputParser |
|
from langchain_core.runnables import RunnablePassthrough |
|
|
|
primary_qa_llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0) |
|
|
|
retrieval_augmented_qa_chain = ( |
|
|
|
|
|
|
|
{"context": itemgetter("question") | retriever, "question": itemgetter("question")} |
|
|
|
|
|
| RunnablePassthrough.assign(context=itemgetter("context")) |
|
|
|
|
|
|
|
| {"response": prompt | primary_qa_llm, "context": itemgetter("context")} |
|
) |
|
|
|
|
|
from langchain.retrievers import MultiQueryRetriever |
|
advanced_retriever = MultiQueryRetriever.from_llm(retriever=retriever, llm=primary_qa_llm) |
|
|
|
|
|
from langchain.chains.combine_documents import create_stuff_documents_chain |
|
from langchain import hub |
|
|
|
retrieval_qa_prompt = hub.pull("langchain-ai/retrieval-qa-chat") |
|
document_chain = create_stuff_documents_chain(primary_qa_llm, retrieval_qa_prompt) |
|
|
|
|
|
from langchain.chains import create_retrieval_chain |
|
retrieval_chain = create_retrieval_chain(advanced_retriever, document_chain) |
|
|
|
|
|
user_template = """{input} |
|
Think through your response step by step. |
|
""" |
|
@cl.on_chat_start |
|
async def start_chat(): |
|
settings = { |
|
"model": "gpt-3.5-turbo", |
|
"temperature": 1.0, |
|
"max_tokens": 500, |
|
"top_p": 1, |
|
"frequency_penalty": 0, |
|
"presence_penalty": 0, |
|
} |
|
|
|
cl.user_session.set("settings", settings) |
|
|
|
|
|
@cl.on_message |
|
async def main(message: cl.Message): |
|
settings = cl.user_session.get("settings") |
|
|
|
client = AsyncOpenAI( |
|
api_key=os.environ.get("OPENAI_API_KEY"), |
|
) |
|
print(message.content) |
|
|
|
prompt = Prompt( |
|
|
|
provider="ChatOpenAI", |
|
messages=[ |
|
PromptMessage( |
|
role="system", |
|
template=template, |
|
formatted=template, |
|
), |
|
PromptMessage( |
|
role="user", |
|
template=user_template, |
|
formatted=user_template.format(input=message.content), |
|
), |
|
], |
|
inputs={"input": message.content}, |
|
settings=settings, |
|
) |
|
|
|
print([m.to_openai() for m in prompt.messages]) |
|
|
|
msg = cl.Message(content="") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
result = retrieval_chain.invoke({"input":message.content}) |
|
msg.content = result["answer"] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
msg.prompt = prompt |
|
|
|
|
|
await msg.send() |
|
|
|
|
|
|
|
|