A Rust-based API for Vercel Serverless Functions that provides sentiment analysis based on weighted post data.
Returns all available topics for a given category.
Query Parameters:
category(required): The category to filter topics by
Example:
GET /api/topics?category=politics
Response:
[
{
"id": 1,
"name": "2024 Election",
"category": "politics"
},
{
"id": 2,
"name": "Climate Policy",
"category": "politics"
}
]Returns top posts for a topic, sorted by weight * relevancy.
Query Parameters:
topic_id(required): The ID of the topiclimit(optional): Maximum number of posts to return (default: 10)
Example:
GET /api/posts?topic_id=1&limit=5
Response:
[
{
"id": 1,
"topic_id": 1,
"content": "Post content here...",
"weight": 0.9,
"relevancy": 0.85,
"score": 0.765
}
]Returns normalized sentiment probabilities for all outcomes of a topic.
The sentiment is calculated by:
- For each post and outcome:
weight * relevancy * probability - Sum all scores for each outcome
- Normalize probabilities so they sum to 1.0
Query Parameters:
topic_id(required): The ID of the topic
Example:
GET /api/sentiment?topic_id=1
Response:
{
"topic_id": 1,
"topic_name": "2024 Election",
"sentiments": [
{
"outcome_id": 1,
"outcome_name": "Candidate A Wins",
"probability": 0.55
},
{
"outcome_id": 2,
"outcome_name": "Candidate B Wins",
"probability": 0.45
}
]
}Create a .env file with the following variables:
DATABASE_URL=postgres://user:password@host:port/database
The API expects the following PostgreSQL tables:
CREATE TABLE topics (
id BIGSERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
category VARCHAR(255) NOT NULL
);
CREATE TABLE posts (
id BIGSERIAL PRIMARY KEY,
topic_id BIGINT NOT NULL REFERENCES topics(id),
content TEXT NOT NULL,
weight DOUBLE PRECISION NOT NULL,
relevancy DOUBLE PRECISION NOT NULL
);
CREATE TABLE outcomes (
id BIGSERIAL PRIMARY KEY,
topic_id BIGINT NOT NULL REFERENCES topics(id),
name VARCHAR(255) NOT NULL
);
CREATE TABLE post_outcome_probabilities (
id BIGSERIAL PRIMARY KEY,
post_id BIGINT NOT NULL REFERENCES posts(id),
outcome_id BIGINT NOT NULL REFERENCES outcomes(id),
probability DOUBLE PRECISION NOT NULL
);
CREATE INDEX idx_topics_category ON topics(category);
CREATE INDEX idx_posts_topic_id ON posts(topic_id);
CREATE INDEX idx_outcomes_topic_id ON outcomes(topic_id);
CREATE INDEX idx_post_outcome_probabilities_post_id ON post_outcome_probabilities(post_id);- Rust (latest stable)
- PostgreSQL database
-
Install dependencies:
cargo build
-
Set up your
.envfile with database credentials -
Run locally with Vercel CLI:
vercel dev
Deploy to Vercel:
vercelMake sure to set the DATABASE_URL environment variable in your Vercel project settings.
sentiment-api/
├── api/
│ ├── topics.rs # Topics endpoint handler
│ ├── posts.rs # Posts endpoint handler
│ └── sentiment.rs # Sentiment endpoint handler
├── src/
│ └── lib.rs # Shared database models and queries
├── Cargo.toml # Rust dependencies
├── vercel.json # Vercel configuration
└── README.md