> ## Documentation Index
> Fetch the complete documentation index at: https://runcrate.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# AI Sentiment Analysis API

> Analyze sentiment in customer reviews, social media posts, and survey responses using structured output with DeepSeek-V3.

export const RuncrateStyles = () => {
  if (typeof document !== 'undefined' && !document.getElementById('runcrate-overrides')) {
    const s = document.createElement('style');
    s.id = 'runcrate-overrides';
    s.textContent = `
      /* Match Runcrate's rounding scale (--radius: 0.75rem) */
      .rounded-sm { border-radius: 0.5rem !important; }   /* 8px */
      .rounded-md { border-radius: 0.625rem !important; } /* 10px */
      .rounded-lg { border-radius: 0.75rem !important; }  /* 12px */
      .rounded-l-sm { border-top-left-radius: 0.5rem !important; border-bottom-left-radius: 0.5rem !important; }
      .rounded-r-sm { border-top-right-radius: 0.5rem !important; border-bottom-right-radius: 0.5rem !important; }
      .rounded-l-md { border-top-left-radius: 0.625rem !important; border-bottom-left-radius: 0.625rem !important; }
      .rounded-r-md { border-top-right-radius: 0.625rem !important; border-bottom-right-radius: 0.625rem !important; }
      .rounded-l-lg { border-top-left-radius: 0.75rem !important; border-bottom-left-radius: 0.75rem !important; }
      .rounded-r-lg { border-top-right-radius: 0.75rem !important; border-bottom-right-radius: 0.75rem !important; }

      /* Cards: never pure white in light mode */
      .card { background-color: #fcfcfc !important; border-radius: 0.75rem !important; }
      html.dark .card { background-color: #141414 !important; }

      /* Docs hero box */
      .rc-hero { background-color: #fcfcfc; border: 1px solid #e0e0e0; }
      html.dark .rc-hero { background-color: #141414; border-color: #242424; }
      html.dark .rc-hero h1 { color: #f5f5f5; }

      /* Runcrate scrollbar — thin, transparent track, hide-until-hover thumb */
      ::-webkit-scrollbar { width: 6px; height: 6px; background-color: transparent; }
      ::-webkit-scrollbar-track { background-color: transparent; }
      ::-webkit-scrollbar-thumb { background-color: rgba(155, 155, 155, 0.5); border-radius: 10px; transition: opacity 0.3s ease; opacity: 0; }
      ::-webkit-scrollbar-thumb:hover { background-color: rgba(155, 155, 155, 0.7); }
      *:hover::-webkit-scrollbar-thumb,
      *:focus::-webkit-scrollbar-thumb,
      *:active::-webkit-scrollbar-thumb { opacity: 1; }
      * { scrollbar-width: thin; scrollbar-color: rgba(155, 155, 155, 0.5) transparent; }
    `;
    document.head.appendChild(s);
  }
  return null;
};

<RuncrateStyles />

Classify sentiment in text — customer reviews, social media posts, support tickets — using structured output that returns typed JSON with scores and categories.

***

## Single text analysis

<CodeGroup>
  ```python Python theme={"theme":"github-dark"}
  from runcrate import Runcrate
  import json

  client = Runcrate(api_key="rc_live_YOUR_API_KEY")

  review = "The laptop display is gorgeous. Battery life could be better — barely lasts 5 hours."

  response = client.models.chat_completion(
      model="deepseek-ai/DeepSeek-V3",
      messages=[
          {"role": "system", "content": "Analyze sentiment. Return JSON: {sentiment, score (-1 to 1), topics, summary}"},
          {"role": "user", "content": review},
      ],
      max_tokens=256,
      temperature=0.1,
      response_format={"type": "json_object"},
  )

  result = json.loads(response.choices[0].message.content)
  print(f"Sentiment: {result['sentiment']} ({result['score']})")
  print(f"Topics: {', '.join(result['topics'])}")
  ```

  ```typescript Vercel AI SDK theme={"theme":"github-dark"}
  import { runcrate } from '@runcrate/ai';
  import { generateText, Output } from 'ai';
  import { z } from 'zod';

  const SentimentSchema = z.object({
    sentiment: z.enum(['positive', 'negative', 'neutral', 'mixed']),
    score: z.number().min(-1).max(1),
    topics: z.array(z.string()),
    summary: z.string(),
  });

  const { output } = await generateText({
    model: runcrate('deepseek-ai/DeepSeek-V3'),
    output: Output.object({ schema: SentimentSchema }),
    prompt: `Analyze the sentiment:\n\n${review}`,
  });

  console.log(`${output.sentiment} (${output.score})`);
  ```
</CodeGroup>

***

## Batch analysis — customer reviews

```python theme={"theme":"github-dark"}
from runcrate import Runcrate
from concurrent.futures import ThreadPoolExecutor
import json

client = Runcrate(api_key="rc_live_YOUR_API_KEY")

reviews = [
    {"id": 1, "text": "Absolutely love this product. Best purchase this year."},
    {"id": 2, "text": "Arrived broken. Support took 3 weeks to respond."},
    {"id": 3, "text": "It's fine. Does what it says, nothing special."},
    {"id": 4, "text": "Great build quality but the software is buggy."},
]

def analyze(review):
    response = client.models.chat_completion(
        model="deepseek-ai/DeepSeek-V3",
        messages=[
            {"role": "system", "content": "Return JSON: {sentiment, score, topics, summary}"},
            {"role": "user", "content": review["text"]},
        ],
        max_tokens=256, temperature=0.1,
        response_format={"type": "json_object"},
    )
    return {"id": review["id"], **json.loads(response.choices[0].message.content)}

with ThreadPoolExecutor(max_workers=5) as pool:
    results = list(pool.map(analyze, reviews))

avg_score = sum(r["score"] for r in results) / len(results)
print(f"Average score: {avg_score:.2f}")
for r in results:
    print(f"  #{r['id']}: {r['sentiment']} ({r['score']}) — {r['summary']}")
```

***

## Next.js API route

```typescript theme={"theme":"github-dark"}
// app/api/analyze-sentiment/route.ts
import { runcrate } from '@runcrate/ai';
import { generateText, Output } from 'ai';
import { z } from 'zod';

const SentimentResult = z.object({
  sentiment: z.enum(['positive', 'negative', 'neutral', 'mixed']),
  score: z.number().min(-1).max(1),
  topics: z.array(z.string()),
  summary: z.string(),
});

export async function POST(req: Request) {
  const { text } = await req.json();

  const { output } = await generateText({
    model: runcrate('deepseek-ai/DeepSeek-V3'),
    output: Output.object({ schema: SentimentResult }),
    prompt: `Analyze the sentiment of this text:\n\n${text}`,
  });

  return Response.json(output);
}
```

***

## Social media monitoring

```python theme={"theme":"github-dark"}
from runcrate import Runcrate
import json

client = Runcrate(api_key="rc_live_YOUR_API_KEY")

posts = [
    "Just tried @acme's new app — the onboarding is so smooth.",
    "@acme your checkout is broken AGAIN. Third time this month.",
    "The new @acme update fixed everything. Finally works on my phone.",
]

for post in posts:
    response = client.models.chat_completion(
        model="deepseek-ai/DeepSeek-V3",
        messages=[
            {"role": "system", "content": "Return JSON: {sentiment, score, urgency: low|medium|high, action_needed: bool}"},
            {"role": "user", "content": post},
        ],
        max_tokens=128, temperature=0.1,
        response_format={"type": "json_object"},
    )
    r = json.loads(response.choices[0].message.content)
    flag = " [ACTION]" if r.get("action_needed") else ""
    print(f"{r['sentiment']:>8} ({r['score']:+.1f}) | {post[:55]}...{flag}")
```

***

## Tips

* **Temperature 0.1** gives the most consistent classifications.
* **`response_format: json_object`** forces JSON output from the API.
* **Vercel AI SDK `Output.object`** gives typed results with Zod validation.
* **DeepSeek-V3** handles nuanced sentiment — it catches mixed reviews and sarcasm.

## Next steps

* [AI Summarization](/examples/ai-summarization) — combine with sentiment for review digests
* [Build an AI SaaS Backend](/examples/ai-saas-backend) — integrate sentiment into a product
