Help needed with code #16185
Closed
prachuspyder
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi I have created a code for a chatbot with Langchain. The code is below.
import PyPDF2
from langchain.llms import OpenAI # Ensure correct import
from langchain.chains import OpenAIModerationChain
import os
def read_pdf(file_path):
try:
with open(file_path, 'rb') as file:
pdf_reader = PyPDF2.PdfReader(file)
text = ''
for page_num in range(pdf_reader.numPages):
page = pdf_reader.getPage(page_num)
text += page.extractText()
return text
except Exception as e:
print(f"Error reading {file_path}: {e}")
return ""
openai_api_key = "xxxxxxxxxxx" # Update with your key
if not openai_api_key:
raise ValueError("API key not found. Set your OpenAI API key as an environment variable.")
Initialize Langchain with OpenAI LLM
langchain_client = OpenAI(api_key=openai_api_key) # Use OpenAI LLM instance
openai_chain = OpenAIModerationChain(llm=langchain_client) # Create OpenAIChain instance
def search_documents(query, documents):
for doc in documents:
if query.lower() in doc.lower():
return doc[:200]
return "No relevant information found."
def maintenance_chatbot(query, documents):
response = openai_chain.run(query=query) # Use
openai_chain.run
for question answeringanswer = search_documents(response, documents)
return answer
pdf_file_paths = [
r"C:\Users\UNME\Downloads\Langchain trial\Honeywell_Forge_DM_EO_Interactive Brochure.pdf",
r"C:\Users\UNME\Downloads\Langchain trial\CB_Ebook_Predictive Maintenance_CRE.pdf",
r"C:\Users\UNME\Downloads\Langchain trial\CB_ESA_BROCHURE_UpdateQ421.pdf",
r"C:\Users\UNME\Downloads\Langchain trial\CB_Global Internet Connectivity Standard.pdf",
r"C:\Users\UNME\Downloads\Langchain trial\CB_Technical Specifications_Energy Optimization.pdf",
r"C:\Users\UNME\Downloads\Langchain trial\CB_Brochure_Predictive Maintenance_CRE.pdf",
r"C:\Users\UNME\Downloads\Langchain trial\CB_Brochure_Predictive Maintenance_Higher Ed.pdf",
r"C:\Users\UNME\Downloads\Langchain trial\CB_Brochure_Predictive Maintenance_Retail.pdf",
r"C:\Users\UNME\Downloads\Langchain trial\CB_Competitive_Intel_EO & Sustainability_H1 2020.pdf",
r"C:\Users\UNME\Downloads\Langchain trial\CB_Ebook_Modernize Your Maintenance With Intelligent Automation_MSFT.pdf",
r"C:\Users\UNME\Downloads\Langchain trial\CB_Brochure_Honeywell Forge_Industrial-Manufacturing.pdf"
]
pdf_texts = [read_pdf(file_path) for file_path in pdf_file_paths if os.path.exists(file_path)]
user_query = "How often should I perform maintenance checks?"
print(maintenance_chatbot(user_query, pdf_texts))
I am getting an error "NameError: name 'get_pydantic_field_names' is not defined"
Could someone let me know what I am doing incorrectly? I have installed langchain-community already.
Thanks in advance
Beta Was this translation helpful? Give feedback.
All reactions