OpenAI Mock
OpenAI Mock is a tool designed to simulate the behavior of the OpenAI API for testing and development purposes without incurring costs or using actual API tokens.
Table of Contents
Installation
To install and set up the OpenAI Mock project, follow these steps:
-
Clone the Repository
-
Build the Project
Ensure you have Rust installed. Then, build the project using Cargo:
-
Run the Mock Server
By default, the server runs on
http://localhost:8000
. You can customize the port using environment variables or configuration files as needed.
Usage
Example 1: Basic Setup
This example demonstrates how to set up and run the OpenAI Mock server with default settings.
-
Start the Server
-
Send a Request to the Mock Endpoint
You can use
curl
or any HTTP client to interact with the mock API. -
Expected Response
Example 2: Custom Responses
Customize the responses of the mock server to simulate different scenarios.
-
Modify the Handler
Update the
completions_handler
insrc/handlers/completion_handler.rs
to return a custom response based on the input prompt.// Inside the completions_handler function let response = if req.prompt.as_deref == Some else ;
-
Restart the Server
-
Send a Custom Request
-
Expected Custom Response
Example 3: Integrating with Actix-Web
Integrate the OpenAI Mock library into an existing Actix-Web application.
-
Add Dependency
In your project's
Cargo.toml
, addopenai-mock
as a dependency:[] = { = "../openai-mock" } # Adjust the path as necessary = "4" = "1.0"
-
Configure Routes
Update your
src/main.rs
or equivalent to include the mock routes.use ; use configure_completion_routes; async
-
Run Your Application
-
Interact with the Mock API
Send requests to your Actix-Web application as shown in Example 1.
Running Tests
OpenAI Mock includes a suite of tests to ensure its functionality. To run the tests:
Example Test Case
Here's an example test case located in src/tests.rs
that verifies the completions handler.
Adding New Tests
To add new tests:
-
Create a New Test Function
Add a new
#[actix_web::test]
function insrc/tests.rs
or create additional test modules as needed. -
Write Test Logic
Utilize Actix-Web's testing utilities to initialize the service, send requests, and assert responses.
-
Run Tests
Contributing
Contributions are welcome! Please follow these steps to contribute:
-
Fork the Repository
Click the Fork button at the top right corner of the repository page.
-
Clone Your Fork
-
Create a New Branch
-
Make Your Changes
Implement your feature or bug fix.
-
Commit Your Changes
-
Push to Your Fork
-
Create a Pull Request
Navigate to the original repository and click the "Compare & pull request" button to submit your changes for review.
License
This project is licensed under the MIT License.
Contact
For any questions or suggestions, please contact:
- James T. Ray
- Email: raymac@ievolution.com
- GitHub: socrates8300
Thank you for using OpenAI Mock! We hope it aids in your development and testing endeavors.