Spaces:
Sleeping
Sleeping
mit
Browse files- app.py +32 -30
- requirements.txt +1 -1
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
-
|
| 2 |
import chainlit as cl
|
| 3 |
-
|
| 4 |
from dotenv import load_dotenv
|
| 5 |
from langchain_openai import OpenAIEmbeddings
|
| 6 |
from langchain_core.prompts import ChatPromptTemplate
|
|
@@ -11,25 +10,27 @@ from langchain.schema.runnable import RunnablePassthrough
|
|
| 11 |
from langchain_openai import ChatOpenAI
|
| 12 |
from langchain.schema.runnable.config import RunnableConfig
|
| 13 |
from langchain_core.output_parsers import StrOutputParser
|
|
|
|
| 14 |
from langchain_community.document_loaders import UnstructuredPDFLoader
|
| 15 |
|
|
|
|
| 16 |
load_dotenv()
|
| 17 |
|
| 18 |
|
| 19 |
-
RAG_PROMPT = """
|
| 20 |
|
| 21 |
-
CONTEXT:
|
| 22 |
-
{context}
|
| 23 |
|
| 24 |
-
QUERY:
|
| 25 |
-
{question}
|
| 26 |
|
| 27 |
-
You house builder and can only provide your answers from the context.
|
| 28 |
-
You can only provide a response in danish
|
| 29 |
|
| 30 |
-
Don't tell in your response that you are getting it from the context.
|
| 31 |
|
| 32 |
-
"""
|
| 33 |
|
| 34 |
|
| 35 |
text_splitter = RecursiveCharacterTextSplitter(
|
|
@@ -76,37 +77,38 @@ text_splitter = RecursiveCharacterTextSplitter(
|
|
| 76 |
# )
|
| 77 |
|
| 78 |
|
| 79 |
-
loader = UnstructuredPDFLoader("
|
| 80 |
-
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
embedding_model = OpenAIEmbeddings(model="text-embedding-3-small")
|
| 83 |
|
| 84 |
-
# vector_store = Pinecone.from_documents(data, embedding_model, index_name=
|
| 85 |
-
|
| 86 |
-
retriever = vector_store.as_retriever()
|
| 87 |
|
| 88 |
-
rag_prompt = ChatPromptTemplate.from_template(RAG_PROMPT)
|
| 89 |
|
| 90 |
-
model = ChatOpenAI(model="gpt-3.5-turbo")
|
| 91 |
|
| 92 |
@cl.on_chat_start
|
| 93 |
async def main():
|
| 94 |
mecanic_qa_chain = ""
|
| 95 |
-
mecanic_qa_chain = (
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
)
|
| 100 |
|
| 101 |
cl.user_session.set("runnable", mecanic_qa_chain)
|
| 102 |
|
| 103 |
@cl.on_message
|
| 104 |
async def on_message(message: cl.Message):
|
| 105 |
runnable = cl.user_session.get("runnable")
|
| 106 |
-
msg = cl.Message(content="")
|
| 107 |
|
| 108 |
-
async for chunk in runnable.astream(
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
):
|
| 112 |
-
|
|
|
|
|
|
|
| 1 |
import chainlit as cl
|
| 2 |
+
import os
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
from langchain_openai import OpenAIEmbeddings
|
| 5 |
from langchain_core.prompts import ChatPromptTemplate
|
|
|
|
| 10 |
from langchain_openai import ChatOpenAI
|
| 11 |
from langchain.schema.runnable.config import RunnableConfig
|
| 12 |
from langchain_core.output_parsers import StrOutputParser
|
| 13 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 14 |
from langchain_community.document_loaders import UnstructuredPDFLoader
|
| 15 |
|
| 16 |
+
|
| 17 |
load_dotenv()
|
| 18 |
|
| 19 |
|
| 20 |
+
# RAG_PROMPT = """
|
| 21 |
|
| 22 |
+
# CONTEXT:
|
| 23 |
+
# {context}
|
| 24 |
|
| 25 |
+
# QUERY:
|
| 26 |
+
# {question}
|
| 27 |
|
| 28 |
+
# You house builder and can only provide your answers from the context.
|
| 29 |
+
# You can only provide a response in danish
|
| 30 |
|
| 31 |
+
# Don't tell in your response that you are getting it from the context.
|
| 32 |
|
| 33 |
+
# """
|
| 34 |
|
| 35 |
|
| 36 |
text_splitter = RecursiveCharacterTextSplitter(
|
|
|
|
| 77 |
# )
|
| 78 |
|
| 79 |
|
| 80 |
+
loader = UnstructuredPDFLoader("./br_femogfirs.pdf")
|
| 81 |
+
# loader = UnstructuredPDFLoader("./br_syvoghalvfjerds.pdf")br_femogfirs.pdf
|
| 82 |
+
# data = loader.load_and_split(text_splitter)
|
| 83 |
+
data = loader.load()
|
| 84 |
|
| 85 |
+
# embedding_model = OpenAIEmbeddings(model="text-embedding-3-small")
|
| 86 |
|
| 87 |
+
# vector_store = Pinecone.from_documents(data, embedding_model, index_name="bygnings-regl-rag-1")
|
| 88 |
+
# retriever = vector_store.as_retriever()
|
|
|
|
| 89 |
|
| 90 |
+
# rag_prompt = ChatPromptTemplate.from_template(RAG_PROMPT)
|
| 91 |
|
| 92 |
+
# model = ChatOpenAI(model="gpt-3.5-turbo")
|
| 93 |
|
| 94 |
@cl.on_chat_start
|
| 95 |
async def main():
|
| 96 |
mecanic_qa_chain = ""
|
| 97 |
+
# mecanic_qa_chain = (
|
| 98 |
+
# {"context": itemgetter("question") | retriever, "question": itemgetter("question")}
|
| 99 |
+
# | RunnablePassthrough.assign(context=itemgetter("context"))
|
| 100 |
+
# | rag_prompt | model | StrOutputParser()
|
| 101 |
+
# )
|
| 102 |
|
| 103 |
cl.user_session.set("runnable", mecanic_qa_chain)
|
| 104 |
|
| 105 |
@cl.on_message
|
| 106 |
async def on_message(message: cl.Message):
|
| 107 |
runnable = cl.user_session.get("runnable")
|
| 108 |
+
# msg = cl.Message(content="")
|
| 109 |
|
| 110 |
+
# async for chunk in runnable.astream(
|
| 111 |
+
# {"question":message.content},
|
| 112 |
+
# config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]),
|
| 113 |
+
# ):
|
| 114 |
+
# await msg.stream_token(chunk)
|
requirements.txt
CHANGED
|
@@ -13,4 +13,4 @@ pdf2image
|
|
| 13 |
bitsandbytes
|
| 14 |
pillow_heif
|
| 15 |
opencv-python-headless
|
| 16 |
-
|
|
|
|
| 13 |
bitsandbytes
|
| 14 |
pillow_heif
|
| 15 |
opencv-python-headless
|
| 16 |
+
pikepdf
|