On Mac/Linux
`YOUR_OPENAI_API_KEY` or `YOUR_VECTOR_DB_CONFIGURATION` fill in the details specific to your setup.\*\*
YOUR_OPENAI_API_KEY or YOUR_VECTOR_DB_CONFIGURATION fill in the details specific to your setup.**
1. Project Setup
1.1 Create and Activate a Virtual Environment (Optional but Recommended)
# On Mac/Linux
python3 -m venv venv
source venv/bin/activate
# On Windows
python -m venv venv
venv\Scripts\activate
1.2 Install Required Packages
pip install openai langchain pydantic faiss-cpu pinecone-client tiktoken requests
# For PDF parsing (if needed for FAQ ingestion):
pip install pypdf
# For image generation using a third-party API (optional):
# pip install replicate or # pip install diffusers[torch]
1.3 Set Environment Variables
In your terminal or .env file (depending on your workflow), make sure your OpenAI key (and any other keys) are accessible:
export OPENAI_API_KEY="YOUR_OPENAI_API_KEY"
(If you’re using Pinecone or another vector DB, also set those credentials similarly.)
2. Complete main.py with All Steps
Create a file called main.py and paste the entire code below. You can then run python main.py to execute. Modify each section as needed.
import os
import openai
from langchain.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
from langchain.output_parsers import PydanticOutputParser
from pydantic import BaseModel, Field
from typing import List
# If using a vector DB like Pinecone or FAISS, import them here:
# import pinecone
# from langchain.vectorstores import FAISS, Pinecone
# from langchain.embeddings.openai import OpenAIEmbeddings
###############################################################################
# STEP 1: ENVIRONMENT SETUP & BASIC CONFIGURATION
###############################################################################
# Make sure your OpenAI API key is set in environment variables:
OPENAI_API_KEY = os.environ.get("OPENAI_API_KEY")
openai.api_key = OPENAI_API_KEY
# (Optional) Pinecone config or other vector DB config:
# PINECONE_API_KEY = os.environ.get("PINECONE_API_KEY")
# pinecone.init(api_key=PINECONE_API_KEY, environment="YOUR_ENV")
###############################################################################
# STEP 2: USING THE FIVE PRINCIPLES OF PROMPTING – EMAIL EXAMPLE
###############################################################################
def generate_cold_outreach_email(product_name: str, features: List[str]) -> str:
"""
Generates a cold outreach email about a product using the "Five Principles of Prompting."
"""
# 1) Direction & Persona
system_prompt = (
"You are a marketing copywriter for a tech startup. "
"Write a concise email introducing our new product."
)
# 2) Format Constraints
# 3) Provide Examples (in the prompt text)
# 4) Evaluate Quality (We keep it brief, but you could add checks)
# 5) Divide Labor (We do the main drafting in one step here)
features_text = ", ".join(features)
user_prompt = f"""
Write a concise cold outreach email introducing our new product, {product_name}.
Emphasize these features: {features_text}.
Return the email in the following format:
Subject: <Subject line>
Hello <Name>,
<Body Paragraph 1>
<Body Paragraph 2>
Best,
<Signature>
Ensure the email is under 150 words and uses first-person plural.
"""
chat = ChatOpenAI(temperature=0.7)
messages = [
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt},
]
response = chat(messages)
return response.content
###############################################################################
# STEP 3: BUILD A CHAIN-OF-THOUGHT (COT) & REACT AGENT (VENUE BOOKING EXAMPLE)
###############################################################################
def venue_search(location: str, budget: int, date_range: tuple) -> List[dict]:
"""
Placeholder for searching venues.
In a real implementation, you'd call an API or database of venues.
Here we just return a mocked list of venues.
"""
dummy_data = [
{"name": "Midtown Conference Center", "price": 4000, "proximity": 0.5},
{"name": "Downtown Rooftop Space", "price": 6000, "proximity": 0.8},
{"name": "Brooklyn Loft Venue", "price": 3000, "proximity": 1.2},
]
# Filter by budget
return [d for d in dummy_data if d["price"] <= budget]
def book_venue_using_react(location: str, budget: int, date_range: tuple):
"""
Demonstrates a simple Chain-of-Thought -> ReAct approach:
1. Observe user query
2. Think (decide how to handle)
3. Act (call venue_search)
4. Observe result, finalize answer
"""
print("Observation: User needs a venue in", location, "with budget under", budget)
print("Thought: I'll call the venue_search API with the parameters.")
results = venue_search(location=location, budget=budget, date_range=date_range)
# Let's say we only want venues with proximity < 1.0 for convenience
final_options = [venue for venue in results if venue["proximity"] < 1.0]
print("Final Answer: Potential Venues:")
for venue in final_options:
print(f" - {venue['name']} at ${venue['price']} (proximity: {venue['proximity']})")
###############################################################################
# STEP 4: CHAIN LLM CALLS FOR PRODUCT IDEA GENERATION + DOMAIN CHECK
###############################################################################
class ProductIdea(BaseModel):
name: str = Field(..., description="Name of the product idea")
description: str = Field(..., description="Short description of the product")
class ProductIdeas(BaseModel):
ideas: List[ProductIdea] = Field(..., description="List of product ideas")
def generate_product_ideas() -> ProductIdeas:
"""
Generates a list of 5 product ideas in JSON, using LangChain and a Pydantic parser.
"""
parser = PydanticOutputParser(pydantic_object=ProductIdeas)
system_message = "You are an expert startup founder generating new product ideas."
user_message = f"""
Generate 5 product ideas in valid JSON.
{parser.get_format_instructions()}
Each idea must have:
- name (one to two words)
- description (brief explanation)
"""
prompt = ChatPromptTemplate.from_messages([
("system", system_message),
("user", user_message)
])
chat = ChatOpenAI(temperature=0)
model_output = chat.invoke(prompt.format_messages())
parsed_ideas = parser.parse(model_output.content)
return parsed_ideas
def check_domain_availability(domain: str) -> bool:
"""
Hypothetical function to check domain availability.
Replace this with a real API call, e.g., GoDaddy, Namecheap, etc.
"""
# For demonstration, let's say any domain containing "x" is taken.
return "x" not in domain.lower()
def filter_available_domains(product_ideas: ProductIdeas) -> List[ProductIdea]:
available_ideas = []
for idea in product_ideas.ideas:
domain = idea.name.lower().replace(" ", "") + ".com"
if check_domain_availability(domain):
available_ideas.append(idea)
return available_ideas
###############################################################################
# STEP 5: BUILD A PRODUCT FAQ CHATBOT USING VECTOR DATABASE RETRIEVAL
###############################################################################
def ingest_faq_documents(file_path: str):
"""
Example function to chunk a PDF or text file, embed each chunk, and store in a vector DB.
NOTE: Requires your chosen vector DB.
We'll outline how you'd do it with FAISS for local usage, but this is a stub.
"""
# from langchain.document_loaders import PyPDFLoader
# from langchain.text_splitter import RecursiveCharacterTextSplitter
# loader = PyPDFLoader(file_path)
# documents = loader.load()
#
# text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
# docs = text_splitter.split_documents(documents)
#
# embedding_model = OpenAIEmbeddings(model="text-embedding-ada-002")
# db = FAISS.from_documents(docs, embedding_model)
# db.save_local("faq_index")
pass
def faq_query(question: str):
"""
Example function to query your FAQ vector DB and have the LLM provide an answer.
"""
# Load the index:
# db = FAISS.load_local("faq_index", OpenAIEmbeddings(model="text-embedding-ada-002"))
#
# relevant_docs = db.similarity_search(question, k=3)
# relevant_texts = [doc.page_content for doc in relevant_docs]
#
# prompt = f"""
# Here are relevant excerpts from our product FAQ:
# 1) {relevant_texts[0]}
# 2) {relevant_texts[1]}
# 3) {relevant_texts[2]}
#
# User question: "{question}"
# Using these excerpts, answer the question accurately.
# If the answer is not in the excerpts, say you don't have enough information.
# """
#
# response = openai.ChatCompletion.create(
# model="gpt-4",
# messages=[{"role": "user", "content": prompt}]
# )
# print(response.choices[0].message.content)
pass
###############################################################################
# STEP 6: GENERATE PROMOTIONAL IMAGES USING DIFFUSION MODEL TECHNIQUES
###############################################################################
def generate_promotional_image_prompt():
"""
Returns an example prompt for a diffusion model (Stable Diffusion, Midjourney, etc.).
You can copy this prompt directly into your image generation tool.
"""
prompt = (
"(city skyline:1.2) at night, cinematic lighting, (neon:1.4), "
"(rain-soaked streets:1.2), in the style of Blade Runner, ultra-detailed "
"--no text, watermark, frame"
)
return prompt
# If you want to automate calling a local or cloud Stable Diffusion:
# def generate_image_via_api(prompt: str):
# # Example with replicate (pip install replicate)
# # import replicate
# # model = replicate.models.get("stability-ai/stable-diffusion")
# # version = model.versions.get("some-version-id")
# # output_url = version.predict(prompt=prompt)
# # return output_url
# pass
###############################################################################
# STEP 7: CREATE A LONG-FORM BLOG POST USING A MULTI-STEP PIPELINE
###############################################################################
def research_topic(topic: str) -> str:
"""
Asks the LLM to provide bullet points summarizing the latest trends/stats on a topic.
"""
chat = ChatOpenAI(temperature=0)
user_prompt = f"Summarize the latest trends and statistics on {topic}. Return bullet points."
messages = [
{"role": "system", "content": "You are a knowledgeable research assistant."},
{"role": "user", "content": user_prompt},
]
resp = chat(messages)
return resp.content
def generate_expert_questions(topic: str) -> str:
"""
Generates open-ended interview questions about a given topic.
"""
chat = ChatOpenAI(temperature=0)
user_prompt = (
f"Generate 5 open-ended questions to ask a(n) {topic} expert that reveal personal experiences and tips."
)
messages = [
{"role": "system", "content": "You are a skilled journalist."},
{"role": "user", "content": user_prompt},
]
resp = chat(messages)
return resp.content
def create_blog_outline(research_points: str, expert_answers: str) -> str:
"""
Creates a blog post outline based on research points and expert Q&A.
"""
chat = ChatOpenAI(temperature=0)
prompt = (
"You are a content strategist. Based on the following research points and expert answers, "
"create a blog post outline with a title, introduction, 5 sections with subheadings, and a conclusion.\n\n"
f"Research Points:\n{research_points}\n\n"
f"Expert Responses:\n{expert_answers}\n"
)
messages = [{"role": "user", "content": prompt}]
resp = chat(messages)
return resp.content
def write_full_blog_post(outline: str, title: str) -> str:
"""
Writes a cohesive blog post from an outline, ensuring an authoritative yet friendly tone.
"""
chat = ChatOpenAI(temperature=0)
user_prompt = (
f"You are a content writer. Using the provided outline:\n\n{outline}\n\n"
f"Write a cohesive blog post titled '{title}'. "
"Ensure the tone is friendly yet authoritative and aim for ~1,000 words."
)
messages = [{"role": "user", "content": user_prompt}]
resp = chat(messages)
return resp.content
###############################################################################
# STEP 8: INTEGRATE ALL MODULES INTO A UNIFIED PIPELINE (DEMO)
###############################################################################
def main():
print("=== STEP 2: Generate a Cold Outreach Email ===")
email = generate_cold_outreach_email("FocusFlow", ["time tracking", "distraction blocking", "daily progress summaries"])
print("Generated Email:\n", email)
print("\n=== STEP 3: ReAct Agent for Venue Booking (Demo) ===")
book_venue_using_react("New York", 5000, ("2025-03-01", "2025-03-03"))
print("\n=== STEP 4: Generate Product Ideas & Check Domains ===")
ideas = generate_product_ideas()
print("Raw Ideas:\n", ideas)
available = filter_available_domains(ideas)
print("Ideas with Available Domains:\n", available)
print("\n=== STEP 5: FAQ Chatbot (Vector DB) - Placeholder ===")
print("Ingesting FAQ Docs (stub)...")
# ingest_faq_documents("path/to/faq.pdf")
print("Querying FAQ (stub)...")
# faq_query("How do I reset the device's Wi-Fi settings?")
print("\n=== STEP 6: Promotional Image Prompt ===")
image_prompt = generate_promotional_image_prompt()
print("Use this prompt in Midjourney/Stable Diffusion:\n", image_prompt)
print("\n=== STEP 7: Long-Form Blog Post ===")
topic = "Remote Work Best Practices"
research_points = research_topic(topic)
print("Research Points:\n", research_points)
expert_qs = generate_expert_questions(topic)
# In a real flow, you'd ask these questions to an expert and record answers.
# For demo, let's pretend the model also answered them:
expert_answers = "1) Expert Answer 1\n2) Expert Answer 2\n..."
outline = create_blog_outline(research_points, expert_answers)
print("Blog Outline:\n", outline)
blog_post = write_full_blog_post(outline, "Mastering Remote Work: Expert Insights and Data-Driven Strategies")
print("Final Blog Post:\n", blog_post)
if __name__ == "__main__":
main()
Usage
- Fill in the placeholders (e.g., your OpenAI key, Pinecone API key, or any real domain-checking API) inside the code.
- Run the script:
python main.py - Watch the terminal output for each step’s result.
- Adapt or comment out anything you don’t need (e.g., if you’re not doing domain availability or a vector DB, you can skip those parts).
Additional Notes
-
Domain Availability:
In the real world, you’ll integrate with a domain registrar’s API (e.g., GoDaddy, Namecheap). The placeholder functioncheck_domain_availability(domain)just simulates availability. -
Vector Database:
- If you don’t want to set up Pinecone or FAISS, you can skip the entire FAQ ingestion/query steps.
- If using Pinecone, install the pinecone-client and initialize it with your credentials.
- If using FAISS, you can store the index locally.
-
ReAct Agent:
We provided a simple demo. In production, you’d use LangChain’sAgentandToolsmodules for more sophisticated multi-step interactions. -
Image Generation:
- For Midjourney, paste the prompt text into Discord with the Midjourney bot.
- For Stable Diffusion (local or API), call the relevant function to generate the image automatically.
-
Blog Post Flow:
The example pipeline shows how you can orchestrate research → interview questions → outline → final post. In a real scenario, you might:- Generate research bullet points.
- Actually interview a subject-matter expert.
- Feed their real answers back into the outline.
- Use the final text for your blog or marketing site.
Related Documents
SUMMARY
permalink: ai-implementation
Retrieval & Prompts
Retrieval quality depends on two things: **what the extraction prompt produces**, and **how Vector Storage is configured**. Most people start with Vector Storage settings — but the bigger lever is the prompt. A well-structured memory block retrieves accurately even with default settings. A poorly structured one won't retrieve well no matter how much you tune.
App Review Support Guide — Switch2Go
Switch2Go is an **AAC (Augmentative and Alternative Communication)** app designed specifically for users with **Cerebral Visual Impairment (CVI)**. It allows non-verbal or communication-impaired users to compose and speak phrases entirely hands-free using:
RFC-BLite: High-Performance Embedded Document Database for .NET
**Status:** Draft (living document)