Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,7 @@ from langchain.vectorstores import Chroma
|
|
3 |
from langchain.embeddings.openai import OpenAIEmbeddings
|
4 |
from langchain.schema import Document
|
5 |
from langchain.chat_models import ChatOpenAI
|
6 |
-
from langchain.
|
7 |
from PyPDF2 import PdfReader
|
8 |
import os
|
9 |
|
@@ -54,15 +54,15 @@ def rag_from_pdf(question, pdf_file, api_key):
|
|
54 |
# Initialize the LLM
|
55 |
llm = ChatOpenAI(model="gpt-3.5-turbo")
|
56 |
|
57 |
-
# Create the chat
|
58 |
messages = [
|
59 |
-
|
60 |
-
|
61 |
]
|
62 |
|
63 |
# Generate response
|
64 |
response = llm(messages=messages)
|
65 |
-
return response
|
66 |
except Exception as e:
|
67 |
return f"An error occurred: {str(e)}"
|
68 |
|
|
|
3 |
from langchain.embeddings.openai import OpenAIEmbeddings
|
4 |
from langchain.schema import Document
|
5 |
from langchain.chat_models import ChatOpenAI
|
6 |
+
from langchain.schema import SystemMessage, HumanMessage
|
7 |
from PyPDF2 import PdfReader
|
8 |
import os
|
9 |
|
|
|
54 |
# Initialize the LLM
|
55 |
llm = ChatOpenAI(model="gpt-3.5-turbo")
|
56 |
|
57 |
+
# Create the chat messages
|
58 |
messages = [
|
59 |
+
SystemMessage(content="You are a helpful assistant answering questions based on the provided PDF document."),
|
60 |
+
HumanMessage(content=f"Question: {question}\n\nContext: {context}")
|
61 |
]
|
62 |
|
63 |
# Generate response
|
64 |
response = llm(messages=messages)
|
65 |
+
return response.content.strip()
|
66 |
except Exception as e:
|
67 |
return f"An error occurred: {str(e)}"
|
68 |
|