This example shows how to use GenSX to implement a self-reflection loop for LLMs, improving the quality of AI-generated outputs by recursively evaluating and refining it. Learn more in the self-reflection docs.
The self-reflection loop is used to create a ImproveTextWithReflection workflow that takes a piece of text as input and iteratively improves it using OpenAI's GPT-4o-mini model. The workflow follows the self-reflection pattern:
- The model evaluates the text and provides feedback on how it can be improved.
- The text is improved based on the feedback.
- Steps 1 and 2 repeat until no further improvements are suggested or a maximum number of iterations is reached.
- The final, improved text is output.
-
Log in to GenSX (if you haven't already):
npx gensx login
-
Install the required dependencies:
pnpm install
-
Set up your environment variables:
export OPENAI_API_KEY=your_api_key_here
To run the workflow in GenSX Cloud:
-
Deploy your workflow:
pnpm run deploy
-
Run the reflection workflow:
gensx run ImproveTextWithReflection --input '{"text": "We are a cutting-edge technology company leveraging bleeding-edge AI solutions..."}'
Once deployed, you can go to the GenSX console to see your workflow, test it, analyze traces, and get code snippets.
You can run the workflow directly using the index.ts file:
pnpm devThis will clean a sample text containing buzzwords and jargon. You can modify the input text in src/index.ts to clean different content, or pass your own text as a command-line argument:
pnpm start -- "Your text here to be improved."This will start a local API server and you can call the workflow APIs via curl or any HTTP client:
curl -X POST http://localhost:1337/workflows/ImproveTextWithReflection \
-H "Content-Type: application/json" \
-d '{
"text": "We are a cutting-edge technology company leveraging bleeding-edge AI solutions..."
}'A swagger UI will also be available at http://localhost:1337/swagger-ui to view the API details and test the workflow.