Advertisement
jarekmor

LangChain OpenAI python automation

Aug 16th, 2023 (edited)
937
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.98 KB | None | 0 0
  1. import os
  2. from dotenv import load_dotenv
  3. load_dotenv()
  4.  
  5. import nest_asyncio     #useful in Windows 10/11/ & WSL
  6. nest_asyncio.apply()
  7.  
  8. from pprint import pprint
  9.  
  10. from docx import Document
  11. from docx.shared import Inches, Pt
  12. from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
  13.  
  14. from langchain.schema import (
  15.     AIMessage,
  16.     HumanMessage,
  17.     SystemMessage
  18. )
  19. from langchain.chat_models import ChatOpenAI
  20. from langchain.document_loaders import AsyncHtmlLoader
  21. from langchain.document_transformers import BeautifulSoupTransformer
  22.  
  23. url = ["https://www.example.com/"]
  24.  
  25. def summarize_weblink(url):    
  26.  
  27.     loader = AsyncHtmlLoader(url)
  28.     docs = loader.load()
  29.  
  30.     bs_transformer = BeautifulSoupTransformer()
  31.     docs_transformed = bs_transformer.transform_documents(docs, tags_to_extract=["div"])
  32.  
  33.     chat = ChatOpenAI()
  34.     messages = [
  35.         SystemMessage(content="You are a helpful assistant that summarize product specification from given input. List top 5 product features. Below the top 5 print price of the product"),
  36.         HumanMessage(content=docs_transformed[0].page_content)]
  37.     summary = chat(messages)
  38.    
  39.     return summary.content
  40.  
  41. def write_to_docx(filename="output.docx", logo_path="logo.png"):
  42.     doc = Document()
  43.    
  44.     section = doc.sections[0]
  45.     header = section.header
  46.     header.paragraphs[0].add_run().add_picture(logo_path, width=Inches(1.5))  # Adjust width as needed
  47.    
  48.     footer = section.footer
  49.     footer.paragraphs[0].text = "Firma XYZ, Miasto, ul. Ulica 7, email: biuro@gmail.com"
  50.    
  51.     for _ in range(6):
  52.         doc.add_paragraph()
  53.    
  54.     oferta_paragraph = doc.add_paragraph()
  55.     oferta_paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
  56.     run = oferta_paragraph.add_run("Oferta handlowa")
  57.     run.font.size = Pt(24)
  58.    
  59.     text = summarize_weblink(url)
  60.    
  61.     doc.add_paragraph(text)
  62.    
  63.     doc.save(filename)
  64.     print(f"Text saved to {filename}")
  65.  
  66. if __name__ == "__main__":
  67.     write_to_docx()
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement