Skip to content

Instantly share code, notes, and snippets.

@tomshaw
Created January 27, 2025 06:20
Show Gist options
  • Save tomshaw/a537e9a5d3a6f30067a29d5e63c6a055 to your computer and use it in GitHub Desktop.
Save tomshaw/a537e9a5d3a6f30067a29d5e63c6a055 to your computer and use it in GitHub Desktop.
Generate a Business Proposal Word Document Using LangChain and Python-Docx
from langchain.prompts import PromptTemplate
from langchain_groq import ChatGroq
from langchain_ollama import ChatOllama
from docx import Document
# Step 1: Define the Prompt Template
prompt = PromptTemplate(
input_variables=["purpose"],
template="Write an introduction for a {purpose}. Keep it professional and concise. Include a header and footer with contact information, and a call to action.",
)
# Step 2: Initialize the LLM (Groq)
llm = ChatGroq(
model_name="mixtral-8x7b-32768",
temperature=0,
)
# Step 2: Initialize the LLM (Ollama)
llm = ChatOllama(
model = "llama3",
temperature = 0,
num_predict = 256,
)
# Step 3: Chain the Prompt and LLM for Content Generation
llm_chain = prompt | llm
# Step 4: Generate Text Using LangChain
purpose = "business proposal"
response = llm_chain.invoke({"purpose": purpose})
# Extract the text content from the response
generated_text = response.content
# Step 5: Use python-docx to Create a Word Document
doc = Document()
doc.add_heading('Business Proposal', level=1)
# Split the generated text by newline characters and add each part as a paragraph
for paragraph in generated_text.split('\n'):
doc.add_paragraph(paragraph)
# Save the Word document
doc.save("business_proposal.docx")
print("Word document created successfully!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment