Dynamic Sentiment Analysis
Sometimes you don't just want a simple "prompt => response." Instead, you want the LLM to decide how to proceed. Below is a minimal example that:
- Receives user input via
POST /api/analyze-sentiment
- Calls OpenAI and instructs the LLM to return JSON with a
sentiment
field - Parses that JSON and emits different events depending on whether
sentiment
is"positive"
or"negative"
(or anything else) - Two specialized responders handle each sentiment separately
The Steps
analyzeSentimentApi.step.ts
openAiAnalyzeSentiment.step.ts
handlePositive.step.ts
handleNegative.step.ts
Visual Overview
Here's how the events chain together:

- Analyze Sentiment (API) → emits
openai.analyzeSentimentRequest
- OpenAI Sentiment Analyzer → calls OpenAI, parses JSON →
- If
sentiment: "positive"
→ emitsopenai.positiveSentiment
- Else → emits
openai.negativeSentiment
- If
- Positive Sentiment Responder or Negative Sentiment Responder
Trying It Out
Install Dependencies
Create Project Structure
analyzeSentimentApi.step.ts
openAiAnalyzeSentiment.step.ts
handlePositive.step.ts
handleNegative.step.ts
Set Environment Variables
Run the Project
Test the API
Check your logs - you should see either the [Positive Responder]
or [Negative Responder]
step firing, depending on the LLM's JSON output.
Extend Further
Here are some ways to build upon this example:
- Tweak the system instructions to force certain outputs or include more details
- Create more specialized responders (like "neutralSentiment")
- Integrate a notification step (Slack, database, etc.)
Try it out, see the branching logic in action, and enjoy skipping all the boring boilerplate!
Need help? See our Community Resources for questions, examples, and discussions.