📝 Step 1: Text Generation
Using OpenAI GPT, we can generate human-like text from a simple prompt.
import OpenAI from "openai";
const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
async function generateText() {
const response = await client.chat.completions.create({
model: "gpt-4o-mini",
messages: [{ role: "user", content: "Write a motivational quote about AI" }],
});
console.log(response.choices[0].message);
}🖼️ Step 2: Image Generation
Generate images from text using Stable Diffusion or DALL·E.
from diffusers import StableDiffusionPipeline
import torch
pipe = StableDiffusionPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5",
torch_dtype=torch.float16
).to("cuda")
image = pipe("a cat wearing VR glasses").images[0]
image.save("cat-vr.png")💬 Step 3: Build a Simple Chatbot
With LangChain and Next.js, you can build a chatbot that remembers context.
import { ChatOpenAI } from "langchain/chat_models/openai";
import { ConversationChain } from "langchain/chains";
const model = new ChatOpenAI({ temperature: 0.7 });
const chain = new ConversationChain({ llm: model });
const response = await chain.call({ input: "Hello, who are you?" });
console.log(response);💻 Step 4: Code Generation
Generative AI can help developers write code. Example with OpenAI Codex:
response = client.completions.create( model="code-davinci-002", prompt="Write a Python function to calculate Fibonacci numbers", max_tokens=100 ) print(response.choices[0].text)
⚡ Quick Recap
With a few lines of code, you can generate text, images, conversations, and even code. These hands-on projects show how Generative AI can power real-world applications.