ExamplesAdvancedConcurrent Agent Processing

Concurrent Processing with Rig

This example demonstrates how to use Rig, a powerful Rust library for building LLM-powered applications, to perform concurrent processing of LLM tasks. This approach significantly improves performance when dealing with multiple LLM queries, making it ideal for batch processing or high-throughput scenarios.

Prerequisites

Before you begin, ensure you have the following installed:

  • Rust (latest stable version)
  • Cargo (Rust’s package manager)

You’ll also need an OpenAI or Cohere API key. If you don’t have one, you can sign up at OpenAI’s website or Cohere’s website

Setup

  1. Create a new Rust project:

    cargo new rig-concurrent-processing
    cd rig-concurrent-processing
  2. Add the following dependencies to your Cargo.toml:

    [dependencies]
    rig-core = "0.1.0"
    tokio = { version = "1.0", features = ["full"] }
  3. Set your OpenAI API key as an environment variable:

    export OPENAI_API_KEY=your_api_key_here

Code Overview

The main components of this example are:

  1. OpenAI client initialization.
  2. Creation of a shared GPT-3.5-turbo model instance.
  3. Spawning of multiple concurrent tasks using Tokio.
  4. Concurrent execution of LLM queries.
  5. Collection and display of results.

Running the Example

  1. Copy the provided code into your src/main.rs file.
  2. Run the example using:
    cargo run

Customization

You can easily modify this example to suit your specific use case:

  • Change the number of concurrent tasks by adjusting the loop range.
  • Modify the prompt to generate different types of content.
  • Experiment with different OpenAI models by changing the model name.

Performance Considerations

  • Be mindful of OpenAI’s rate limits when increasing concurrency.
  • Monitor system resource usage to optimize the number of concurrent tasks.
  • Consider implementing error handling and retry logic for production use.

Troubleshooting

If you encounter any issues:

  • Ensure your OpenAI API key is correctly set.
  • Check that all dependencies are properly installed.
  • Verify that you’re using a compatible Rust version.

For more detailed information, refer to the Rig documentation.