init with claude

This commit is contained in:
songtianlun 2025-07-02 23:49:36 +08:00
parent 07e0c76805
commit ef1af2171d
8 changed files with 1988 additions and 126 deletions

View File

@ -1,33 +1,59 @@
This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next-app`](https://nextjs.org/docs/app/api-reference/cli/create-next-app).
# Anything vs Anything
## Getting Started
A Next.js application that uses AI to compare any two things and display the results in a table format.
First, run the development server:
## Features
- Compare any two items using AI-powered analysis
- Optional detailed descriptions for more context
- Results displayed in a formatted table
- Responsive design with Tailwind CSS
- Powered by Replicate AI API
## Setup
1. Clone the repository and install dependencies:
```bash
npm run dev
# or
yarn dev
# or
pnpm dev
# or
bun dev
npm install
```
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
2. Set up environment variables:
```bash
cp .env.example .env.local
```
You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.
3. Add your Replicate API token to `.env.local`:
```
REPLICATE_API_TOKEN=your_replicate_api_token_here
REPLICATE_MODEL=meta/llama-2-70b-chat:02e509c789964a7ea8736978a43525956ef40397be9033abf9fd2badfe68c9e3
```
This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel.
4. Run the development server:
```bash
npm run dev
```
## Learn More
5. Open [http://localhost:3000](http://localhost:3000) to view the application.
To learn more about Next.js, take a look at the following resources:
## Usage
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
1. Enter two items you want to compare
2. Optionally add detailed descriptions for more context
3. Click "Compare Now" to get AI-powered analysis
4. View the results in a formatted table
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js) - your feedback and contributions are welcome!
## Environment Variables
- `REPLICATE_API_TOKEN`: Your Replicate API token (required)
- `REPLICATE_MODEL`: The Replicate model to use (optional, defaults to Llama 2 70B)
## Tech Stack
- Next.js 15 with App Router
- TypeScript
- Tailwind CSS
- Replicate AI API
- React Markdown for table rendering
## Deploy on Vercel

1650
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -9,19 +9,22 @@
"lint": "next lint"
},
"dependencies": {
"next": "15.3.4",
"react": "^19.0.0",
"react-dom": "^19.0.0",
"next": "15.3.4"
"react-markdown": "^10.1.0",
"remark-gfm": "^4.0.1",
"replicate": "^1.0.1"
},
"devDependencies": {
"typescript": "^5",
"@eslint/eslintrc": "^3",
"@tailwindcss/postcss": "^4",
"@types/node": "^20",
"@types/react": "^19",
"@types/react-dom": "^19",
"@tailwindcss/postcss": "^4",
"tailwindcss": "^4",
"eslint": "^9",
"eslint-config-next": "15.3.4",
"@eslint/eslintrc": "^3"
"tailwindcss": "^4",
"typescript": "^5"
}
}

View File

@ -0,0 +1,55 @@
import { NextRequest, NextResponse } from 'next/server';
import Replicate from 'replicate';
const replicate = new Replicate({
auth: process.env.REPLICATE_API_TOKEN,
});
export async function POST(request: NextRequest) {
try {
const { item1, item2, description1, description2 } = await request.json();
if (!item1 || !item2) {
return NextResponse.json(
{ error: 'Both items are required' },
{ status: 400 }
);
}
const prompt = `Compare ${item1} and ${item2} in a detailed table format. ${
description1 ? `Additional context for ${item1}: ${description1}` : ''
} ${
description2 ? `Additional context for ${item2}: ${description2}` : ''
}
Please provide a comprehensive comparison in a markdown table format with the following structure:
- Create rows for different comparison aspects (features, pros, cons, price range, best use cases, etc.)
- Use clear, objective language
- Include both similarities and differences
- Make the comparison balanced and informative
- Format the response as a proper markdown table
The table should have three columns: Aspect, ${item1}, ${item2}`;
const output = await replicate.run(
process.env.REPLICATE_MODEL || "meta/llama-2-70b-chat:02e509c789964a7ea8736978a43525956ef40397be9033abf9fd2badfe68c9e3",
{
input: {
prompt: prompt,
max_new_tokens: 1000,
temperature: 0.7,
},
}
);
const comparison = Array.isArray(output) ? output.join('') : output;
return NextResponse.json({ comparison });
} catch (error) {
console.error('Error calling Replicate API:', error);
return NextResponse.json(
{ error: 'Failed to generate comparison' },
{ status: 500 }
);
}
}

View File

@ -24,3 +24,19 @@ body {
color: var(--foreground);
font-family: Arial, Helvetica, sans-serif;
}
.prose table {
width: 100%;
margin: 1rem 0;
}
.prose table th,
.prose table td {
border: 1px solid #e5e7eb;
padding: 0.75rem;
text-align: left;
}
.prose table thead {
background-color: #f9fafb;
}

View File

@ -1,103 +1,65 @@
import Image from "next/image";
'use client';
import { useState } from 'react';
import ComparisonForm from '@/components/ComparisonForm';
import ComparisonResults from '@/components/ComparisonResults';
export default function Home() {
return (
<div className="grid grid-rows-[20px_1fr_20px] items-center justify-items-center min-h-screen p-8 pb-20 gap-16 sm:p-20 font-[family-name:var(--font-geist-sans)]">
<main className="flex flex-col gap-[32px] row-start-2 items-center sm:items-start">
<Image
className="dark:invert"
src="/next.svg"
alt="Next.js logo"
width={180}
height={38}
priority
/>
<ol className="list-inside list-decimal text-sm/6 text-center sm:text-left font-[family-name:var(--font-geist-mono)]">
<li className="mb-2 tracking-[-.01em]">
Get started by editing{" "}
<code className="bg-black/[.05] dark:bg-white/[.06] px-1 py-0.5 rounded font-[family-name:var(--font-geist-mono)] font-semibold">
src/app/page.tsx
</code>
.
</li>
<li className="tracking-[-.01em]">
Save and see your changes instantly.
</li>
</ol>
const [comparisonResults, setComparisonResults] = useState<string | null>(null);
const [isLoading, setIsLoading] = useState(false);
<div className="flex gap-4 items-center flex-col sm:flex-row">
<a
className="rounded-full border border-solid border-transparent transition-colors flex items-center justify-center bg-foreground text-background gap-2 hover:bg-[#383838] dark:hover:bg-[#ccc] font-medium text-sm sm:text-base h-10 sm:h-12 px-4 sm:px-5 sm:w-auto"
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
<Image
className="dark:invert"
src="/vercel.svg"
alt="Vercel logomark"
width={20}
height={20}
/>
Deploy now
</a>
<a
className="rounded-full border border-solid border-black/[.08] dark:border-white/[.145] transition-colors flex items-center justify-center hover:bg-[#f2f2f2] dark:hover:bg-[#1a1a1a] hover:border-transparent font-medium text-sm sm:text-base h-10 sm:h-12 px-4 sm:px-5 w-full sm:w-auto md:w-[158px]"
href="https://nextjs.org/docs?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
Read our docs
</a>
const handleComparison = async (item1: string, item2: string, description1: string, description2: string) => {
setIsLoading(true);
try {
const response = await fetch('/api/compare', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
item1,
item2,
description1,
description2,
}),
});
if (!response.ok) {
throw new Error('Failed to get comparison');
}
const data = await response.json();
setComparisonResults(data.comparison);
} catch (error) {
console.error('Error:', error);
setComparisonResults('Error occurred while comparing items.');
} finally {
setIsLoading(false);
}
};
return (
<main className="min-h-screen bg-gradient-to-br from-blue-50 to-indigo-100 py-12">
<div className="max-w-4xl mx-auto px-4">
<div className="text-center mb-12">
<h1 className="text-4xl font-bold text-gray-900 mb-4">
Anything vs Anything
</h1>
<p className="text-xl text-gray-600">
Compare any two things using AI-powered analysis
</p>
</div>
</main>
<footer className="row-start-3 flex gap-[24px] flex-wrap items-center justify-center">
<a
className="flex items-center gap-2 hover:underline hover:underline-offset-4"
href="https://nextjs.org/learn?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
<Image
aria-hidden
src="/file.svg"
alt="File icon"
width={16}
height={16}
/>
Learn
</a>
<a
className="flex items-center gap-2 hover:underline hover:underline-offset-4"
href="https://vercel.com/templates?framework=next.js&utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
<Image
aria-hidden
src="/window.svg"
alt="Window icon"
width={16}
height={16}
/>
Examples
</a>
<a
className="flex items-center gap-2 hover:underline hover:underline-offset-4"
href="https://nextjs.org?utm_source=create-next-app&utm_medium=appdir-template-tw&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer"
>
<Image
aria-hidden
src="/globe.svg"
alt="Globe icon"
width={16}
height={16}
/>
Go to nextjs.org
</a>
</footer>
</div>
<div className="bg-white rounded-xl shadow-lg p-8 mb-8">
<ComparisonForm onSubmit={handleComparison} isLoading={isLoading} />
</div>
{comparisonResults && (
<div className="bg-white rounded-xl shadow-lg p-8">
<ComparisonResults results={comparisonResults} />
</div>
)}
</div>
</main>
);
}

View File

@ -0,0 +1,111 @@
'use client';
import { useState } from 'react';
interface ComparisonFormProps {
onSubmit: (item1: string, item2: string, description1: string, description2: string) => void;
isLoading: boolean;
}
export default function ComparisonForm({ onSubmit, isLoading }: ComparisonFormProps) {
const [item1, setItem1] = useState('');
const [item2, setItem2] = useState('');
const [description1, setDescription1] = useState('');
const [description2, setDescription2] = useState('');
const [showDescriptions, setShowDescriptions] = useState(false);
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
if (item1.trim() && item2.trim()) {
onSubmit(item1.trim(), item2.trim(), description1.trim(), description2.trim());
}
};
return (
<form onSubmit={handleSubmit} className="space-y-6">
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
<div>
<label htmlFor="item1" className="block text-sm font-medium text-gray-700 mb-2">
First Item
</label>
<input
type="text"
id="item1"
value={item1}
onChange={(e) => setItem1(e.target.value)}
placeholder="e.g., iPhone"
className="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
required
/>
</div>
<div>
<label htmlFor="item2" className="block text-sm font-medium text-gray-700 mb-2">
Second Item
</label>
<input
type="text"
id="item2"
value={item2}
onChange={(e) => setItem2(e.target.value)}
placeholder="e.g., Android Phone"
className="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
required
/>
</div>
</div>
<div className="text-center">
<button
type="button"
onClick={() => setShowDescriptions(!showDescriptions)}
className="text-blue-600 hover:text-blue-800 text-sm font-medium"
>
{showDescriptions ? 'Hide' : 'Add'} additional descriptions
</button>
</div>
{showDescriptions && (
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
<div>
<label htmlFor="description1" className="block text-sm font-medium text-gray-700 mb-2">
Description for {item1 || 'First Item'}
</label>
<textarea
id="description1"
value={description1}
onChange={(e) => setDescription1(e.target.value)}
placeholder="Additional details about the first item..."
rows={3}
className="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
/>
</div>
<div>
<label htmlFor="description2" className="block text-sm font-medium text-gray-700 mb-2">
Description for {item2 || 'Second Item'}
</label>
<textarea
id="description2"
value={description2}
onChange={(e) => setDescription2(e.target.value)}
placeholder="Additional details about the second item..."
rows={3}
className="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent"
/>
</div>
</div>
)}
<div className="text-center">
<button
type="submit"
disabled={isLoading || !item1.trim() || !item2.trim()}
className="px-8 py-3 bg-blue-600 text-white font-medium rounded-lg hover:bg-blue-700 focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 disabled:opacity-50 disabled:cursor-not-allowed"
>
{isLoading ? 'Comparing...' : 'Compare Now'}
</button>
</div>
</form>
);
}

View File

@ -0,0 +1,51 @@
'use client';
import ReactMarkdown from 'react-markdown';
import remarkGfm from 'remark-gfm';
interface ComparisonResultsProps {
results: string;
}
export default function ComparisonResults({ results }: ComparisonResultsProps) {
return (
<div>
<h2 className="text-2xl font-bold text-gray-900 mb-6">Comparison Results</h2>
<div className="prose prose-lg max-w-none">
<ReactMarkdown
remarkPlugins={[remarkGfm]}
components={{
table: ({ children }) => (
<div className="overflow-x-auto">
<table className="min-w-full divide-y divide-gray-200 border border-gray-300">
{children}
</table>
</div>
),
thead: ({ children }) => (
<thead className="bg-gray-50">{children}</thead>
),
tbody: ({ children }) => (
<tbody className="bg-white divide-y divide-gray-200">{children}</tbody>
),
tr: ({ children }) => (
<tr className="hover:bg-gray-50">{children}</tr>
),
th: ({ children }) => (
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider border-r border-gray-200 last:border-r-0">
{children}
</th>
),
td: ({ children }) => (
<td className="px-6 py-4 whitespace-pre-wrap text-sm text-gray-900 border-r border-gray-200 last:border-r-0">
{children}
</td>
),
}}
>
{results}
</ReactMarkdown>
</div>
</div>
);
}